Why doesnt cv2 dilate actually affect my image?

2024/9/8 9:15:14

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 cv2binary_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!

Thanks!

Answer

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.

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! Hooray!

https://en.xdnf.cn/q/73268.html

Related Q&A

How to plot text clusters?

I have started to learn clustering with Python and sklearn library. I have wrote a simple code for clustering text data. My goal is to find groups / clusters of similar sentences. I have tried to plot…

Selenium - Unresponsive Script Error (Firefox)

This question has been asked before, but the answer given does not seem to work for me. The problem is, when opening a page using Selenium, I get numerous "Unresponsive Script" pop ups, refe…

Fail to validate URL in Facebook webhook subscription with python flask on the back end and ssl

Im trying to start using new messenger platform from FB. So i have server with name (i.e.) www.mysite.com I got a valid SSL certificate for that domain and apache is setup correctly - all good.I have …

What is a proper way to test SQLAlchemy code that throw IntegrityError?

I have read this Q&A, and already try to catch exception on my code that raise an IntegrityError exception, this way :self.assertRaises(IntegrityError, db.session.commit())But somehow my unit test …

What are the different options for social authentication on Appengine - how do they compare?

[This question is intended as a means to both capture my findings and sanity check them - Ill put up my answer toute suite and see what other answers and comments appear.]I spent a little time trying t…

Is there any way to get source code inside context manager as string?

Source code of function can be received with inspect.getsourcelines(func) function. Is there any way to do same for context manager?with test():print(123)# How to get "print(123)" as line he…

Create temporary file in Python that will be deleted automatically after sometime

Is it possible to create temporary files in python that will be deleted after some time? I checked tempfile library which generates temporary files and directories.tempfile.TemporaryFile : This functi…

Python GTK+ Canvas

Im currently learning GTK+ via PyGobject and need something like a canvas. I already searched the docs and found two widgets that seem likely to do the job: GtkDrawingArea and GtkLayout. I need a few b…

Python, can someone guess the type of a file only by its base64 encoding?

Lets say I have the following:image_data = """iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="""This i…

Extract only body text from arXiv articles formatted as .tex

My dataset is composed of arXiv astrophysics articles as .tex files, and I need to extract only text from the article body, not from any other part of the article (e.g. tables, figures, abstract, title…