在图像锐化时,往往会 1. 放大 噪声,2. 引入aritfact, 3. 振铃效应 等负面效果
因此需要分析相关锐化方法的效果和副作用,避免图像失真。
这里只是介绍了比较基础的三个滤波核的表现,附上代码和图像效果。
from pathlib import Pathimport cv2
import numpy as np
import sysfrom tqdm import tqdmdef sharpen(path):#reading the image passed thorugh the command lineimg = cv2.imread(path)#generating the kernelskernel_sharpen = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])#process and output the imageoutput = cv2.filter2D(img, -1, kernel_sharpen)return output
def excessive(path):#reading the imageimg = cv2.imread(path)#generating the kernelskernel_sharpen = np.array([[1,1,1], [1,-7,1], [1,1,1]])#process and output the imageoutput = cv2.filter2D(img, -1, kernel_sharpen)return output
def edge_enhance(path):#reading the imageimg = cv2.imread(path)#generating the kernelskernel_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 imageoutput = cv2.filter2D(img, -1, kernel_sharpen)return outputdef 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_fileout1 = 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)
四个图分别是
原图, sharpen
enhance edge, excessive
对字体的锐化, sharpen的效果最好
enhance edge也有增强效果,但是背景颜色发生了变化
excessive 变化较大。
sharpen会有较多artifact,锐化太强
edge_enhance 效果较好,但是略微的亮度色彩偏差需要想办法避免