【问题标题】:Why doesn't cv2 dilate actually affect my image?为什么 cv2 dilate 实际上不会影响我的形象?
【发布时间】:2012-06-30 02:23:57
【问题描述】:

所以,我正在使用 python 和 opencv2 生成一个二进制(嗯,真正的灰度,8 位,用作二进制)图像,将少量多边形写入图像,然后使用内核扩展图像。但是,无论我使用什么内核,我的源映像和目标映像总是以相同的方式结束。有什么想法吗?

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!!")

我得到的只是EPIC FAIL

谢谢!

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    所以,问题出在内核和映像的创建上。我相信 openCV 期望 'uint8' 作为内核和图像的数据类型。在这种特殊情况下,我使用dtype='int' 创建内核,默认为'int64'。此外,我将图像创建为'int8',而不是'uint8'。不知何故,这并没有触发异常,而是导致扩张以一种令人惊讶的方式失败。

    将上面两行改为

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

    修复了问题,现在我得到了EUREKA!万岁!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      相关资源
      最近更新 更多