由于 OpenCV 使用 Numpy 来显示图像,因此您可以简单地使用 Numpy 创建卷积核。
import numpy as np
convolution_kernel = np.array([[0, 1, 0],
[1, 1.5, 1],
[0, 1, 0]])
这是内核。注意类型是<class 'numpy.ndarray'>
[[0. 1. 0. ]
[1. 1.5 1. ]
[0. 1. 0. ]]
要将内核与图像进行卷积,您可以使用cv2.filter2D()。像这样的东西
import cv2
image = cv2.imread('1.png')
result = cv2.filter2D(image, -1, convolution_kernel)
有关内核构建的更多信息,请参见this。以下是一些常见的内核以及卷积后的结果。使用此输入图像:
锐化内核
sharpen = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])
拉普拉斯核
laplacian = np.array([[0, 1, 0],
[1, -4, 1],
[0, 1, 0]])
压花内核
emboss = np.array([[-2, -1, 0],
[-1, 1, 1],
[0, 1, 2]])
概述内核
outline = np.array([[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]])
底部索贝尔
bottom_sobel = np.array([[-1, -2, -1],
[0, 0, 0],
[1, 2, 1]])
左索贝尔
left_sobel = np.array([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
右索贝尔
right_sobel = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
顶级索贝尔
top_sobel = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])