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

StackOverflow https://stackoverflow.com/questions/21466829

Question

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?

Was it helpful?

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

OTHER TIPS

Any reason you do not use skimage's builtin Otsu function?

import numpy as np
import pylab as plt
from skimage import io, color, filter, exposure


img = color.rgb2gray(io.imread('7AEJTuA.jpg'))
img_rescale = exposure.rescale_intensity(img, out_range=(0, 1))

threshold = filter.threshold_otsu(img_rescale)

plt.gray()
plt.imshow(img_rescale < threshold)
plt.show()

You may also have a look at skimage.filter.rank.otsu as an alternative...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top