文章目录
- 1.滤波核
- 2.代码
- 3. 效果分析
- 示例1.
- 示例2.
图像锐化和图像平滑相对应,前者用于增强细节表现,后者一般用于降噪
在图像锐化时,往往会 1. 放大 噪声,2. 引入aritfact, 3. 振铃效应 等负面效果
因此需要分析相关锐化方法的效果和副作用,避免图像失真。
这里只是介绍了比较基础的三个滤波核的表现,附上代码和图像效果。
1.滤波核
- 滤波核1, sharpen
np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]]) - 滤波核2, edge_enhance
np.array([[-1,-1,-1,-1,-1],
[-1,2,2,2,-1],
[-1,2,8,2,-1],
[-2,2,2,2,-1],
[-1,-1,-1,-1,-1]])/8.0 - 滤波核3,excessive
np.array([[1,1,1], [1,-7,1], [1,1,1]])
2.代码
from pathlib import Path
import cv2
import numpy as np
import sys
from tqdm import tqdm
def sharpen(path):
#reading the image passed thorugh the command line
img = cv2.imread(path)
#generating the kernels
kernel_sharpen = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
#process and output the image
output = cv2.filter2D(img, -1, kernel_sharpen)
return output
def excessive(path):
#reading the image
img = cv2.imread(path)
#generating the kernels
kernel_sharpen = np.array([[1,1,1], [1,-7,1], [1,1,1]])
#process and output the image
output = cv2.filter2D(img, -1, kernel_sharpen)
return output
def edge_enhance(path):
#reading the image
img = cv2.imread(path)
#generating the kernels
kernel_sharpen = np.array([[-1,-1,-1,-1,-1],
[-1,2,2,2,-1],
[-1,2,8,2,-1],
[-2,2,2,2,-1],
[-1,-1,-1,-1,-1]])/8.0
#process and output the image
output = cv2.filter2D(img, -1, kernel_sharpen)
return output
def is_image_file(filename):
return any(filename.endswith(extension) for extension in [
'.png', '.tif', '.jpg', '.jpeg', '.bmp', '.pgm', '.PNG'
])
if __name__ == "__main__":
input_list = [
str(f) for f in Path(r'D:\superresolution\sharpen\cubic').iterdir() if is_image_file(f.name)
]
print(len(input_list))
for input_file in tqdm(input_list):
# filename = r'D:\superresolution\sharpen\cubic\10_bicubic.png'
filename = input_file
out1 = sharpen(filename)
out2 = excessive(filename)
out3 = edge_enhance(filename)
cv2.imwrite(filename[:-4] + '_shaprpen.png', out1)
cv2.imwrite(filename[:-4] + '_shaprpen_excessive.png', out2)
cv2.imwrite(filename[:-4] + '_shaprpen_edge.png', out3)
3. 效果分析
示例1.
四个图分别是
原图, sharpen
enhance edge, excessive
对字体的锐化, sharpen的效果最好
enhance edge也有增强效果,但是背景颜色发生了变化
excessive 变化较大。
示例2.
sharpen会有较多artifact,锐化太强
edge_enhance 效果较好,但是略微的亮度色彩偏差需要想办法避免