OpenCV-Python系列·第十一集

自定义卷积核,如3×3、5×5、7×7、9×9、11×11

Tip:卷积核需归一化。

# -*- coding: utf-8 -*-  """  Created on Sat Aug 25 14:35:33 2018    @author: Miracle  """    import cv2  import numpy as np  #加载图像  image = cv2.imread('../data/lena.jpg')  #自定义卷积核  kernel_sharpen_1 = np.array([          [-1,-1,-1],          [-1,9,-1],          [-1,-1,-1]])  kernel_sharpen_2 = np.array([          [1,1,1],          [1,-7,1],          [1,1,1]])  kernel_sharpen_3 = np.array([          [-1,-1,-1,-1,-1],          [-1,2,2,2,-1],          [-1,2,8,2,-1],          [-1,2,2,2,-1],          [-1,-1,-1,-1,-1]])/8.0  #卷积  output_1 = cv2.filter2D(image,-1,kernel_sharpen_1)  output_2 = cv2.filter2D(image,-1,kernel_sharpen_2)  output_3 = cv2.filter2D(image,-1,kernel_sharpen_3)  #显示锐化效果  cv2.imshow('Original Image',image)  cv2.imshow('sharpen_1 Image',output_1)  cv2.imshow('sharpen_2 Image',output_2)  cv2.imshow('sharpen_3 Image',output_3)  #停顿  if cv2.waitKey(0) & 0xFF == 27:      cv2.destroyAllWindows()