最小最大值图像直方图拉伸

3
我有一个问题,需要将图像的直方图拟合到特定范围以获得更好的对比度。首先,我使用的数据从输入图像+直方图到输出图像+直方图再到我想要的输出+直方图:
输入图像:My input image 输入图像的直方图:Histogram of input image 输出图像:My output image 输出图像的直方图:The histogram from my output image 期望的输出(看起来更加平滑):I want it to look like this (looks a lot smoother) 期望输出图像的直方图:Histogram of the output image that I want 程序中的方法描述只是说“将黑色映射到最小强度,白色映射到最大强度”。以下是执行映射的代码:
for y in range(0, h):
    for x in range(0, w):
        image[y,x] = (((image[y,x] - smallest) / diff)  * 65535)

在这段代码中,smallest表示原始图像中最小的强度值,diff表示最大强度和最小强度之间的差异。

为了最终得到更加平滑的直方图,我需要做些什么?

谢谢您的帮助!

2个回答

1
你可以尝试使用scikit-image exposure模块的直方图均衡化(通常比对比度/直方图拉伸产生更好的结果):
from skimage.io import imread
from skimage.exposure import equalize_hist
import matplotlib.pylab as plt
image = imread('../lc.png')
image = image / np.max(image)
plt.figure(figsize=(15,10))
plt.subplot(221)
plt.imshow(image, cmap='gray')
plt.title('original image')
plt.subplot(222)
plt.hist(image.ravel(), normed=True)
plt.title('histogram')
image = equalize_hist(image)
plt.subplot(223)
plt.imshow(image, cmap='gray')
plt.title('contrast-enhanced image')
plt.subplot(224)
plt.hist(image.ravel(), normed=True)
plt.title('histogram')
plt.show()

enter image description here


1
如果image是整数类型,那么(image[y,x] - smallest) / diff)将是一个较小的整数--这个操作会向下取整结果,有效地量化输入的灰度级别。
为了防止这种情况发生,预先计算乘数如下:
scale = 65535 / diff
image[y,x] = (image[y,x] - smallest) * scale

或者,在计算映射之前将像素值转换为浮点数,然后再将其转换回整数以存储回image数组中。


@PhilippMarquardt:这很难让人相信。你能分享一下你具体尝试了什么吗?你重新加载过图片吗? - Cris Luengo
抱歉,我再次查看了一下并找到了错误。您的修复程序做得很好。我使用image = cv2.imread(im, 0)读取图像,这将图像转换为8位并进行了一些奇怪的操作。我将其更改为image = cv2.imread(im, -1),现在它可以正常工作了。谢谢! - Philipp Marquardt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接