OpenCV Open / Close shifts pixel positions
I am currently using morphological transforms on binary images with OpenCV 2.4
I just noticed that using the built-in functions of OpenCV, all my pixel positions are offset to the right and down by one (i.e. the pixel previously located at (i, j) is now 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)
Entrance:
Output:
Since I didn’t understand why, I just bound the same operation using skimage and doesn’t do that "space" during morphological transformation.
Output:
Are there any ideas on this issue?
Thank!
source to share
This is how you commented, but the exact opposite :)
Structuring elements with an even size leads to offsets, there is no middle pixel. With an odd size, you end up with an average pixel and (n-1) / 2 pixels at each size.
Another way to say that odd-sized SE is symmetrical and even the size is asymmetrical.
source to share