import numpy as np
from skimage import morphology
import cv2 as cv
img = cv.imread('images/TEST.jpg', cv.IMREAD_GRAYSCALE)
img_not = cv.bitwise_not(img)
se = np.ones((50,50), np.uint8)
opened = morphology.opening(img, se)
wth_my= img_not - opened
wth=morphology.white_tophat(img_not, se)
cv.imwrite('images/TEST_Opened.jpg', opened)
cv.imwrite('images/TEST_WTH_MY.jpg', wth_my)
cv.imwrite('images/TEST_WTH.jpg', wth)
cv.waitKey(0)
cv.destroyAllWindows()
Results are quite different (see screenshots). Please advice what's wrong in my code.
CodePudding user response:
As you said, the top-hat filter is I - opening(I)
. You wrote:
opened = morphology.opening(img, se)
wth_my= img_not - opened
Note how one line uses img
, and the other uses img_not
. You need to apply the opening to img_not
as well, so that it is the same image that the two parts of the operation work on.