CV2 morphology open does not work as expected

426 Views Asked by At

I have a problem with OpenCV where the function morphology opening is not working as expected. As you can see in image below, the morphology opening of the image cause the thresholded pattern to move down and right.

Do you have some idea why this moving of the pattern occurs?

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread("originalImg.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret,thresh1 = cv2.threshold(img,0,255,cv2.THRESH_TRIANGLE)
print("Calculated threshold", ret)

f, axarr = plt.subplots(1,5)
kernel = np.ones((2,2),np.uint8)
thresh = thresh/255
opened = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

axarr[0].imshow(img,cmap='gray')
axarr[0].set_title('original image')
axarr[1].imshow(thresh1,cmap='gray')
axarr[1].set_title('threshold triangle')
axarr[2].imshow(opened,cmap='gray')
axarr[2].set_title('morphology open')
plt.show()

![https://i.stack.imgur.com/0t5m1.jpg](https://i.stack.imgur.com/0t5m1.jpg)

Generated images: https://i.stack.imgur.com/0t5m1.jpg.

0

There are 0 best solutions below