我想使用OpenCV执行灰度扩展。这似乎很容易,但是我没有做到。因此,我想知道是否可以使用OpenCV做到这一点?
为了检查结果,我创建了一个MWE比较OpenCV和SciPy。Scipy似乎提供了预期的结果,而OpenCV没有。不幸的是,由于其他限制,我必须使用OpenCV而不是Scipy并进行灰度扩展。从MWE看来,可以进行二进制膨胀。
MWE:
import cv2
import scipy
import scipy.ndimage
import numpy as np
print('start test')
image=np.array( [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 25, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]] )
Kernel=np.array([[0, 1, 0],
[1, 1, 1],
[0, 1, 5]])
print('input')
print(image)
image=image.astype('uint8')
Kernel=Kernel.astype('uint8')
output=cv2.dilate(image, Kernel)
print('OpenCV')
print(output)
Output2=scipy.ndimage.grey_dilation(image, structure=Kernel)
print('Scipy')
print(Output2)
print('end test')
结果:
start test
input
[[ 0 0 0 0 0]
[ 0 0 0 0 0]
[ 0 0 25 0 0]
[ 0 0 0 0 0]
[ 0 0 0 0 0]]
OpenCV
[[ 0 0 0 0 0]
[ 0 25 25 0 0]
[ 0 25 25 25 0]
[ 0 0 25 0 0]
[ 0 0 0 0 0]]
Scipy
[[ 5 5 5 5 5]
[ 5 25 26 25 5]
[ 5 26 26 26 5]
[ 5 25 26 30 5]
[ 5 5 5 5 5]]
end test
因此,有一种简单的方法(或选项)对OpenCV进行灰度扩展,并获得与SciPy相同的结果?
OpenCV使用的平面结构元素:
[[1, 1, 1],
[0, 1, 1]]
Scipy使用的非平面结构元素:
[[1, 1, 1],
[0, 1, 5]]