Home > database >  Why skimage white_tophat is different from manually achieved top hat?
Why skimage white_tophat is different from manually achieved top hat?

Time:12-03

I am trying to remove gradient background from image using morphology top hat operation. for this purpose I use skimage morphology library (opening, whiet_tophat) functions. By itself white tophat means = initial image - opened image. In my code I am comparing the results of skimage wht function result to manually obtained wth.
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.

Initial Image Morphology Opening Manual WTH Library WTH

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.

  • Related