1

我使用直方图均衡和自适应来消除灰度图像中的照明:

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)

但在直方图均衡之后,我使用 otsu 方法:

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

下一个示例的阈值是:16329

源图像:

在此处输入图像描述

直方图均衡和自适应后:

源图像

大津法后:

大津之后的图片

Otsu 之前的图像是一个 uint16 数组,Otsu 之后是一个 numpy 布尔数组。

stackoverflow中建议我使用直方图均衡来避免照明问题。

是灰色背景吗?我该如何解决?

4

2 回答 2

1

在上面的例子中添加一个膨胀命令:

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()

我看到下图:

在此处输入图像描述

于 2014-02-02T15:21:10.513 回答
0

有什么理由不使用 skimage 的内置 Otsu 功能?

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()

你也可以看看skimage.filter.rank.otsu作为替代...

于 2014-02-01T16:48:46.207 回答