Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Opening/Closing shifts the positions of the pixels

I'm currently using morphology transformations on binary images with OpenCV 2.4

I just noticed that using the built-in functions of OpenCV, all my pixels' positions are shifted right and down by one (i.e. the pixel previously located at (i,j) is now located at (i+1, j+1))

import cv2
import numpy as np
from skimage.morphology import opening

image = cv2.imread('input.png', 0)
kernel = np.ones((16,16), np.uint8)

opening_opencv = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)

opening_skimage = opening(image, kernel)

cv2.imwrite('opening_opencv.png', opening_opencv)
cv2.imwrite('opening_skimage.png', opening_skimage)

Input :

input

Output :

opening_opencv

As I didn't understand why, I just tied the same operation using skimage, and it doesn't make this "gap" during the morphological transformation.

Ouput :

opening_skimage

Any idea about this issue ?

Thanks !

like image 714
Julian Avatar asked Jun 09 '15 07:06

Julian


People also ask

What is erosion and dilation in OpenCV?

Erosion and Dilation are morphological image processing operations. OpenCV morphological image processing is a procedure for modifying the geometric structure in the image. In morphism, we find the shape and size or structure of an object.

What is opening in OpenCV?

Opening. An opening is an erosion followed by a dilation. Performing an opening operation allows us to remove small blobs from an image: first an erosion is applied to remove the small blobs, then a dilation is applied to regrow the size of the original object.

What does CV dilate do?

As the kernel B is scanned over the image, we compute the maximal pixel value overlapped by B and replace the image pixel in the anchor point position with that maximal value. As you can deduce, this maximizing operation causes bright regions within an image to "grow" (therefore the name dilation).


1 Answers

It is the way you commented, but the exact inverse :)

Structuring elements with even size lead to shifts, there is no middle pixel. With an odd size, you get a middle pixel and (n-1)/2 pixels at each size.

Other way of saying it is that a SE with odd sizes is symmetric and even size is asymmetric.

like image 96
Fábio Dias Avatar answered Sep 30 '22 13:09

Fábio Dias