Search code examples
image-processingscikit-imagemahotas

Why after histogram equalization (scikit image) and Otsu mahotas method in some images get out big white squares?


I used histogram equalization and adaptation for erase illumination from the grayscale images:

import scipy
import numpy as np
import pymorph as pm
import mahotas as mh
from skimage import morphology
from skimage import io
from matplotlib import pyplot as plt
from skimage import data, img_as_float
from skimage import exposure

mhgray = io.imread(path)
mhgray = mhgray[:,:,0]

#thresh = mh.otsu(binimg)
#gray =( binimg< thresh)
img = color.rgb2gray(mhgray)   
#img = mhgray #binimg

#from skimage import exposure
#print dir(exposure)

# Contrast stretching
p2 = np.percentile(img, 2)
p98 = np.percentile(img, 98)
#img_rescale = exposure.rescale_intensity(img, in_range=(p2, p98))
img_rescale = exposure.rescale_intensity(img, out_range=(0, 255))

# Equalization
img_eq = exposure.equalize_hist(img)

# Adaptive Equalization
img_adapteq = exposure.equalize_adapthist(img, clip_limit=0.03)

but after the histogram equalization, i use otsu method:

thresh = mh.otsu(binimg) 
gray =( binimg< thresh)

the thresh value for the next example is: 16329

Source image:

enter image description here

After histogram equalization and adaptation:

source image

After Otsu method:

Image after Otsu

The image before Otsu is an array of uint16, after Otsu is a numpy array of bool.

In stackoverflow suggested me to use histogram equalization to avoid illumination problems.

It is for the grey background? How can i fix it?


Solution

  • Adding a dilation command to the above example:

    import numpy as np
    import pylab as plt
    from skimage import io, color, filter, exposure, morphology
    
    
    img = color.rgb2gray(io.imread('7AEJTuA.jpg'))
    
    threshold = filter.threshold_otsu(img)
    
    img_bw = img < threshold
    
    img_bw_thick = morphology.dilation(img_bw, morphology.disk(6))
    
    plt.gray()
    f, (ax0, ax1) = plt.subplots(1, 2)
    ax0.imshow(img)
    ax1.imshow(img_bw_thick)
    plt.show()
    

    I see the following image:

    enter image description here