我正在尝试创建卷积内核,中间将是1.5。不幸的是,我一直在思考如何做到这一点。我正在尝试创建与此类似的内容
Array = [
[0 , 1 , 0]
[1 , 1.5 , 1]
[0 , 1 , 0]
]
由于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()
。像这样的东西
cv2.filter2D()
有关内核构造的更多信息,请参见import cv2
image = cv2.imread('1.png')
result = cv2.filter2D(image, -1, convolution_kernel)
。这是一些常见的内核以及卷积后的结果。使用此输入图像:
Sharpen内核
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内核
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
top_sobel = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])