为什么cv2膨胀实际上不会影响我的形象? [英] Why doesn't cv2 dilate actually affect my image?

查看:142
本文介绍了为什么cv2膨胀实际上不会影响我的形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用python和opencv2生成二进制(很好,真正的灰度级,8位,用作二进制)图像,向该图像写入少量多边形,然后使用内核对图像进行扩展.但是,无论我使用什么内核,我的源映像和目标映像始终总是相同的.有什么想法吗?

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

我得到的只是EPIC FAIL

谢谢!

推荐答案

因此,事实证明问题出在内核和映像的创建上.我相信openCV希望'uint8'作为内核和映像的数据类型.在这种情况下,我使用dtype='int'创建了内核,默认为'int64'.另外,我将图像创建为'int8',而不是'uint8'.不知何故,这并没有引发异常,但以令人惊讶的方式导致了扩张失败.

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. In this particular case, I created the kernel with dtype='int', which defaults to 'int64'. Additionally, I created the image as 'int8', not 'uint8'. Somehow this did not trigger an exception, but caused the dilation to fail in a surprising fashion.

将以上两行更改为

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

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

解决了问题,现在我得到了EUREKA!哇!

Fixed the problem, and now I get EUREKA! Hooray!

这篇关于为什么cv2膨胀实际上不会影响我的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆