Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't cv2 dilate actually affect my image?

Tags:

python

opencv

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

All I get is EPIC FAIL!

Thanks!

like image 571
DaveA Avatar asked Jun 30 '12 02:06

DaveA


People also ask

What does cv2 dilate do?

Dilation is usually performed after the image is eroded using another morphological transformation operator called Erosion. This process helps in removing the white noise from the image. We can dilate an image in OpenCV using the cv2.

Is cv2 and OpenCV same?

cv2 (old interface in old OpenCV versions was named as cv ) is the name that OpenCV developers chose when they created the binding generators. This is kept as the import name to be consistent with different kind of tutorials around the internet.

How does cv2 erode work?

cv2. erode() method is used to perform erosion on the image. The basic idea of erosion is just like soil erosion only, it erodes away the boundaries of foreground object (Always try to keep foreground in white). It is normally performed on binary images.

What is cv2 in OpenCV?

cv2 is the module import name for opencv-python, "Unofficial pre-built CPU-only OpenCV packages for Python".


1 Answers

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!

like image 65
DaveA Avatar answered Sep 20 '22 12:09

DaveA