OpenCV Opening/Closing shifts the positions of the pixels

5.8k views Asked by At

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 !

1

There are 1 answers

1
Fábio Dias On BEST ANSWER

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.