给定一个图像,我尝试使用 (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 中没有这样的功能 - 任何接口(interface)或核心 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。
关于python - opencv 中使用 4D 内核进行图像卷积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52448804/