题目

This assignment aims to histogram equalization of a gray image with more than 8 bits per pixel. The provided image is called splash.png, which is a 10 bits gray image. As the image is stored in png format, so the image is in mode ‘I’ when it is read into pillow Image object. for images other than those with 8 bit-depth, Image.histogram can not be used to get its histogram (one can verify this, by applying histogram() to the above image), in this case one can only rely on the histogram function provided by numpy. After histogram is obtained for the image, histogram equalization mapping can then be calculated similar way as what is discussed in the class, except that we are now dealing with 10 bit-depth images. Finally, histogram-equalized image can be obtained, the resulting image will be stored as he-splash.png.
So the assignment requires:

  1. read in splash.png
  2. collect its histogram
  3. calculate histogram-equalization mapping
  4. perform histogram equalization to the image
  5. save the resulting image to he-splash.png
    the prototype code is provided, and for turnning-in, implemented code, and the resulting image file should be submitted. Some restrictions:The histogram collected from the image is a strict image histogram.

    代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    import numpy as np
    from PIL import Image
    import sys
    import matplotlib.pyplot as plt
    import scipy.signal as signal

    def ReadImage(file_path):
    img = Image.open(file_path).convert('L') # 转换为灰度图像
    return np.array(img)


    def FreqLaplacian(f):
    # 创建一个拉普拉斯滤波器
    laplacian_kernel = np.array([[0, 1, 0],
    [1, -4, 1],
    [0, 1, 0]])

    # 使用 scipy.signal.convolve2d 进行卷积操作
    g = signal.convolve2d(f, laplacian_kernel, boundary='symm', mode='same')

    return g


    if __name__ == '__main__':
    # 读取图像
    f = ReadImage('./aLenna.bmp')

    # 应用拉普拉斯滤波器
    g = FreqLaplacian(f)

    # 显示结果
    plt.imshow(g, cmap='gray')
    plt.title('Laplacian Filtered Image')
    plt.show()