简体   繁体   English

为什么cv2扩张实际上不影响我的形象?

[英]Why doesn't cv2 dilate actually affect my image?

So, I'm generating a binary (well, really gray scale, 8bit, used as binary) image with python and opencv2, writing a small number of polygons to the image, and then dilating the image using a kernel. 因此,我使用python和opencv2生成二进制(很好,真正的灰度,8位,用作二进制)图像,向图像写入少量多边形,然后使用内核扩展图像。 However, my source and destination image always end up the same, no matter what kernel I use. 但是,无论我使用什么内核,我的源和目标映像总是一样。 Any thoughts? 有什么想法吗?

from matplotlib import pyplot
import numpy as np
import cv2

binary_image = np.zeros(image.shape,dtype='int8')
for rect in list_of_rectangles: 
    cv2.fillConvexPoly(binary_image, np.array(rect), 255)
kernel = np.ones((11,11),'int')
dilated = cv2.dilate(binary_image,kernel)
if np.array_equal(dilated, binary_image):
    print("EPIC FAIL!!")
else:
    print("eureka!!")

All I get is EPIC FAIL ! 我得到的只是EPIC FAIL

Thanks! 谢谢!

So, it turns out the problem was in the creation of both the kernel and the image. 因此,事实证明问题在于内核和图像的创建。 I believe that openCV expects 'uint8' as a data type for both the kernel and the image. 我相信openCV期望'uint8'作为内核和图像的数据类型。 In this particular case, I created the kernel with dtype='int' , which defaults to 'int64' . 在这种特殊情况下,我用dtype='int'创建了内核,默认为'int64' Additionally, I created the image as 'int8' , not 'uint8' . 另外,我创建的图像为'int8' ,而不是'uint8' Somehow this did not trigger an exception, but caused the dilation to fail in a surprising fashion. 不知何故,这并未引发异常,但导致扩张以令人惊讶的方式失败。

Changing the above two lines to 将上面两行改为

binary_image = np.zeros(image.shape,dtype='uint8')

kernel = np.ones((11,11),'uint8')

Fixed the problem, and now I get EUREKA ! 解决了这个问题,现在我得到了EUREKA Hooray! 万岁!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM