[英]My erode() function doesn't produce same output as openCV's erode function
So i've used a border for the pixels that are 'outside' the image so the kernel can use the border's pixels to get a minimum. 因此,我对图像“外面”的像素使用了边框,以便内核可以使用边框的像素获得最小值。 I've no idea why my function would produce a different output to that of the inbuilt function - I've inspected both outputs carefully (writing to an image and zooming in) and looking very carefully I can't notice a difference in pixels! 我不知道为什么我的函数会产生与内置函数不同的输出-我已经仔细检查了两个输出(写入图像并放大),并且非常仔细地看我看不到像素的差异!
import cv2 import numpy as np 导入cv2导入numpy作为np
img = cv2.imread('lena.png',cv2.IMREAD_GRAYSCALE)
reflect = cv2.copyMakeBorder(img,3,3,3,3,cv2.BORDER_REFLECT)
cv2.imshow('img', reflect)
cv2.waitKey(0)
imgEroded = np.zeros((512+6,512+6,3), np.uint8)
for i in range(0,reflect.shape[0]):
for j in range(0,reflect.shape[1]):
n = np.matrix(reflect[i-3:i+2, j-3:j+2])
if n.size > 0:
imgEroded[i][j] = n.min()
imgEroded = imgEroded[3:512+3, 3:512+3]
kernel = np.ones((5,5),np.uint8)
erode = cv2.erode(img,kernel)
# print erode.shape[:2]
cv2.imshow('erosionCorrect',erode)
cv2.imshow('erosion',imgEroded)
cv2.imwrite('myOutput.png',imgEroded)
cv2.imwrite('correct.png',erode)
print(np.array_equal(erode, imgEroded))
# cv2.imshow('real',erode)
cv2.waitKey(0)
Your code is fine except two small issues: 您的代码很好,除了两个小问题:
First, imgEroded
is a three-channel image while erode
is a single channel image (you read img
as grayscale), so just simply create a single-channel imgEroded
. 首先, imgEroded
是一个三通道图像,而erode
是一个单通道图像(您将img
阅读为灰度图像),因此只需简单地创建一个单通道imgEroded
。
Second, you're one pixel off in your indexing for your n
matrix. 其次,您的n
矩阵索引减少了一个像素。 If you index an array like arr[i-3:i+2]
this means starting at index i-3
and going up to i+2
, which means you're indexing i-3, i-2, i-1, i, i+1
. 如果您为类似arr[i-3:i+2]
的数组建立索引,则意味着从索引i-3
开始, 一直到 i+2
,这意味着您正在索引i-3, i-2, i-1, i, i+1
。 This has size five, which is correct, but the center isn't i
, it's i-1
. 它的大小为5,是正确的,但中心不是i
,而是i-1
。 So you should be indexing i-2:i+3
. 因此,您应该索引i-2:i+3
。 Same with j
of course. 当然与j
相同。 This also means you actually only need to copyMakeBorder()
two pixels wide and not three, but that's not really a big deal. 这也意味着您实际上只需要将copyMakeBorder()
宽为两个像素而不是三个像素,但这并不是什么大问题。
With these two changes: 有了这两个更改:
imgEroded = np.zeros((512+6,512+6), np.uint8)
and 和
n = np.matrix(reflect[i-2:i+3, j-2:j+3])
on the corresponding lines, print(np.array_equal(erode, imgEroded))
prints True
. 在相应的行上, print(np.array_equal(erode, imgEroded))
打印True
。
FYI to help debug images that look similar, I find printing the numpy image array to be super helpful; 仅供参考,以帮助调试外观相似的图像,我发现打印numpy图像数组超级有帮助; that's how I spotted both errors. 这就是我发现这两个错误的方式。 The 1-ch vs 3-ch is immediately obvious, but then after correcting that I simply noticed that the second matrix was basically the same but shifted a pixel in each direction. 1通道与3通道是显而易见的,但是在校正之后,我只是注意到第二个矩阵基本相同,但是在每个方向上都移动了一个像素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.