Search code examples
pythonmachine-learningimage-processingcomputer-visionscikit-image

Image Reconstruction (Erosion and Dilation) both gives black image as output


I am trying to remove the black spots from a face of this image using the erosion methods.

I have implemented:

img = skimage.io.imread('blemish.jpeg')
img = skimage.color.rgb2gray(img)
img_inten = skimage.exposure.rescale_intensity(img,in_range=(50,100))

diliation_seed = img_inten.copy()
diliation_seed[1:-1,1:-1] = img_inten.min()
mask = img_inten

eroded_img = skimage.morphology.reconstruction(diliation_seed,mask,method='dilation')
matplotlib.pyplot.imshow(eroded_img,cmap='gray')

My output is always a black image in both the cases. What is going wrong here?


Solution

  • rgb2gray is outputting an image as a matrix of floats, with values in [0;1]

    So the rescale_intensity is just outputting a matrix of 0, since you ask for values between 50 and 100 and there is none in the gray img.

    you can fix it like this :

    import skimage
    from skimage import data, exposure, img_as_float
    from skimage.morphology import reconstruction
    import matplotlib.pyplot as plt
    
    img = skimage.io.imread('blemish.jpeg')
    gray_img = 255*skimage.color.rgb2gray(img) # multiply by 255 to get back in the [0;255] range
    img_inten = exposure.rescale_intensity(gray_img,in_range=(50,100))
    
    diliation_seed = img_inten.copy()
    diliation_seed[1:-1,1:-1] = img_inten.min()
    mask = img_inten
    
    eroded_img = reconstruction(diliation_seed,mask,method='dilation')
    plt.imshow(eroded_img,cmap='gray')
    plt.show()