在 opencv 中使用 4D 内核进行图像卷积
Image convolution with a 4D Kernel in opencv
给定一张图像,我正在尝试应用具有 (3 x 3 x 3 x 64) 内核的卷积:
cv2.filter2D(img, -1, np.random.rand(3,3,3,64))
给出:
error: /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/filterengine.hpp:363: error: (-215) anchor.inside(Rect(0, 0, ksize.width, ksize.height)) in function normalizeAnchor
事实上在文档中它说:
kernel – convolution kernel (or rather a correlation kernel), a single-channel floating point matrix; if you want to apply different kernels to different channels, split the image into separate color planes using split() and process them individually.
是否有任何其他 opencv 函数可以对 > 2D 内核进行卷积?还是我必须执行两个应用 filter2d 的 for 循环?
OpenCV 函数cv2.filter2D(),顾名思义,假定一个 2D img 和一个 2D 内核。不仅如此,您还必须使用循环。例如,以下运行没有错误,
import cv2
import numpy as np
# read an rgb image
img = cv2.imread('fig1.png')
# filter the first channel (blue)
out0 = cv2.filter2D( img[:,:,0], -1, np.random.rand(3,3))
请参阅 cv2.filter2D()
中的文档
OpenCV 中没有这样的功能 - 任何接口和核心 C++ 库本身都没有。如果你想做 4D 卷积,你要么必须使用 cv2.filter2D
循环你的 4D 内核的 2D 子矩阵,你自己手动编写,或者使用其他支持它的东西,比如深度学习包,或者 SciPy.
我可以建议的最简单的解决方案是使用 SciPy 的 scipy.signal.convolve
,它执行 N 维卷积:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve.html,而无需您自己编写或修改一个。请记住,图像和内核都需要具有相同的维数,因此预计您的图像也是 4D。
给定一张图像,我正在尝试应用具有 (3 x 3 x 3 x 64) 内核的卷积:
cv2.filter2D(img, -1, np.random.rand(3,3,3,64))
给出:
error: /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/filterengine.hpp:363: error: (-215) anchor.inside(Rect(0, 0, ksize.width, ksize.height)) in function normalizeAnchor
事实上在文档中它说:
kernel – convolution kernel (or rather a correlation kernel), a single-channel floating point matrix; if you want to apply different kernels to different channels, split the image into separate color planes using split() and process them individually.
是否有任何其他 opencv 函数可以对 > 2D 内核进行卷积?还是我必须执行两个应用 filter2d 的 for 循环?
OpenCV 函数cv2.filter2D(),顾名思义,假定一个 2D img 和一个 2D 内核。不仅如此,您还必须使用循环。例如,以下运行没有错误,
import cv2
import numpy as np
# read an rgb image
img = cv2.imread('fig1.png')
# filter the first channel (blue)
out0 = cv2.filter2D( img[:,:,0], -1, np.random.rand(3,3))
请参阅 cv2.filter2D()
中的文档OpenCV 中没有这样的功能 - 任何接口和核心 C++ 库本身都没有。如果你想做 4D 卷积,你要么必须使用 cv2.filter2D
循环你的 4D 内核的 2D 子矩阵,你自己手动编写,或者使用其他支持它的东西,比如深度学习包,或者 SciPy.
我可以建议的最简单的解决方案是使用 SciPy 的 scipy.signal.convolve
,它执行 N 维卷积:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve.html,而无需您自己编写或修改一个。请记住,图像和内核都需要具有相同的维数,因此预计您的图像也是 4D。