创建python卷积内核

问题描述 投票:1回答:1

我正在尝试创建卷积内核,中间将是1.5。不幸的是,我一直在思考如何做到这一点。我正在尝试创建与此类似的内容

Array = [
        [0 , 1 , 0]
        [1 , 1.5 , 1]
        [0 , 1 , 0]
]
python numpy opencv convolution cv2
1个回答
1
投票

由于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) 。这是一些常见的内核以及卷积后的结果。使用此输入图像:

this

Sharpen内核

enter image description here

sharpen = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])

拉普拉斯内核

enter image description here

laplacian = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])

Emboss内核

enter image description here

emboss = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]])

概述内核

enter image description here

outline = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])

底部底部

enter image description here

bottom_sobel = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])

左苏贝尔

enter image description here

left_sobel = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])

右索贝尔

enter image description here

right_sobel = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])

Top sobel

enter image description here

top_sobel = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])

© www.soinside.com 2019 - 2024. All rights reserved.