OpenCV Opening/Closing shifts the positions of the pixels
Asked Answered
K

1

6

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 !

Kimbell answered 9/6, 2015 at 7:58 Comment(1)
Ok I may have an answer... In case of a non-even size of my structural element (i.e. 15x15), there is actually no integer coordinates for its center. OpenCV might chose (7,7) and as a result, the opening transformation leads to a shift... But here, it's 16x16 ... so the center is always (8,8)..!Kimbell
G
4

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.

Gorgonzola answered 18/9, 2015 at 13:7 Comment(1)
Yep exactly, I was wrong. However, OpenCV could handle the problem, as scikit-image is able to do it :)Kimbell

© 2022 - 2024 — McMap. All rights reserved.