python - 如何创建 Python 卷积核?

标签 python numpy opencv image-processing computer-vision

我正在尝试创建一个卷积核,中间将是 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() .像这样

import cv2

image = cv2.imread('1.png')
result = cv2.filter2D(image, -1, convolution_kernel)

有关内核构建的更多信息,请查看 this .下面是一些常见的内核和卷积后的结果。使用此输入图像:

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]])

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]])

enter image description here

顶级索贝尔

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

enter image description here

关于python - 如何创建 Python 卷积核?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58383477/

相关文章:

python - 计算汽车 OpenCV + Python 问题

python - nginx+gunicorn 400错误

python - Scrapy不会选择嵌入元素

python - 在返回向量的函数上使用 Numpy Vectorize

python - 如果我们使用管道,我怎样才能得到树解释器的树贡献?

python - 复制 numpy 数组中的行元素

python - 如何在 Django 中添加带有通用外键的 unique_together

python - 将两个 DataFrame 合并为 block

java - OpenCV4Android 默认的 Mat 图像类型是什么?

Python Opencv Ubuntu 不创建 Windows