NumPy中的灰度图像直方图均衡化

NumPy中的灰度图像直方图均衡化

在本文中,我们将介绍NumPy中的灰度图像直方图均衡化方法。灰度图像直方图均衡化是一种用于增强图像对比度的常用技术,该技术通过对图像像素值的重新分配,使图像具有更广泛的亮度范围。在本文中,我们将对灰度图像直方图均衡化的原理、方法和示例进行详细介绍。

阅读更多:Numpy 教程

灰度图像直方图均衡化的原理和方法

灰度图像直方图均衡化的原理非常简单。在灰度图像中,每个像素的像素值代表其亮度。每个像素值的频率可以通过计算灰度图像的直方图得到。直方图显示了每个像素值的频率,也就是说,直方图告诉我们每种亮度有多少像素。但这个统计信息并没有在像素值之间进行均衡。直方图均衡化的主要思想是将直方图的亮度分布均匀地分散在整个像素值范围内,从而增强图像对比度。作为计算机图形学中最常用的技术之一,灰度图像直方图均衡化可以用以下数学公式表示:

    def histeq(image, n_bins=256):
        pixel_counts, _ = np.histogram(image, bins=n_bins, range=(0, n_bins))
        pixel_counts = pixel_counts / float(np.sum(pixel_counts))
        cs = np.cumsum(pixel_counts)
        pixel_values = np.arange(n_bins)
        cs = (n_bins - 1) * cs
        rounded = np.round(cs)
        uni_values = np.unique(rounded)
        new_pixel_values = np.zeros_like(pixel_counts)
        for i, v in enumerate(rounded):
            new_pixel_values[v] = new_pixel_values[v] + pixel_counts[i]
        return np.interp(image.flatten(), pixel_values, rounded), rounded

上述代码实现了直接使用NumPy的histogram()函数计算图像的直方图,然后使用cumsum()和interp()函数实现直方图均衡化。函数histeq()输入图像和箱数n_bins,则返回均衡化后的图像和均衡化的映射表。

灰度图像直方图均衡化的示例

在本节中,我们将使用Python和NumPy将直方图均衡化应用于灰度图像并显示结果。 首先,我们需要安装NumPy模块并导入NumPy库。我们将使用一个名为“test.jpg”的图像来演示。该图像是库中的一张典型的猫的图片。

    import numpy as np
    from PIL import Image
    import matplotlib.pyplot as plt

    def histeq(image, n_bins=256):
        pixel_counts, _ = np.histogram(image, bins=n_bins, range=(0, n_bins))
        pixel_counts = pixel_counts / float(np.sum(pixel_counts))
        cs = np.cumsum(pixel_counts)
        pixel_values = np.arange(n_bins)
        cs = (n_bins - 1) * cs
        rounded = np.round(cs)
        uni_values = np.unique(rounded)
        new_pixel_values = np.zeros_like(pixel_counts)
        for i, v in enumerate(rounded):
            new_pixel_values[v] = new_pixel_values[v] + pixel_counts[i]
        return np.interp(image.flatten(), pixel_values, rounded), rounded

    img = Image.open("test.jpg").convert('L')
    img_arr = np.array(img)
    img_eq, mapping = histeq(img_arr)
    img_eq = img_eq.reshape(img_arr.shape)
    hist_eq, _ = np.histogram(img_eq, bins=256, range=(0, 256))
    mapping = mapping / 255.0

    fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(10, 8))
    axs[0, 0].imshow(img, cmap="gray")
    axs[0, 0].set_title("Original Image")
    axs[0, 0].axis("off")
    axs[0, 1].bar(np.arange(256), np.histogram(img_arr, bins=256, range=(0, 256))[0], width=1, color="gray")
    axs[0, 1].set_title("Histogram of Original Image")
    axs[0, 1].set_xlabel("Pixel Values")
    axs[0, 1].set_ylabel("Counts")
    axs[1, 0].imshow(img_eq, cmap="gray")
    axs[1, 0].set_title("Equalized Image")
    axs[1, 0].axis("off")
    axs[1, 1].bar(np.arange(256), hist_eq, width=1, color="gray")
    axs[1, 1].plot(mapping, 'r')
    axs[1, 1].set_title("Histogram of Equalized Image")
    axs[1, 1].set_xlabel("Pixel Values")
    axs[1, 1].set_ylabel("Counts")
    fig.tight_layout()
    plt.show()

上述代码实现了灰度图像直方图均衡化,并在四个子图中显示了原始图像、原始图像的直方图、均衡化后的图像和均衡化后的图像的直方图。结果显示出了直方图均衡化的效果,可以明显看到图像对比度的增强。

总结

灰度图像直方图均衡化是NumPy中常用的图像处理方法之一。本文介绍了灰度图像直方图均衡化的原理和方法,并提供了一个使用Python和NumPy实现灰度图像直方图均衡化的示例。通过灰度图像直方图均衡化,图像的对比度可以得到显著增强,从而使图像更清晰,更易于分析和处理。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程