數字圖像處理——灰度級分層

灰度級分層的兩種基本形式如下:

一:將感興趣的範圍內的灰度值顯示爲一個值,而其他灰度值顯示爲另一個值

二:將感興趣的範圍內的灰度值變亮或變暗,而其他灰度值保持不變

Python實現過程如下:

使用的圖像資源爲:

 定義新圖片生成函數和分層函數:

def logarithm_transformation(img, func):
    img_data = np.array(img)
    a = np.shape(img_data)
    new_img = [[] for _ in range(a[0])]
    for i in range(a[0]):
        for j in range(a[1]):
            data = img_data[i][j]
            new_data = func(data)
            new_img[i].append(new_data)
    return new_img

 第一種分層方式,所有灰度值大於150的灰度,全部設爲255,其他的灰度設爲0:

def power_law1(data):
    new_data = 0
    if data >= 150:
        new_data = 255
    else:
        new_data = 0
    return new_data

 第二種分層方式,所有灰度值大於150的灰度,全部設爲255,其他的灰度值不變:

def power_law2(data):
    new_data = []
    if data >= 150:
        new_data = 255
    else:
        new_data = data
    return new_data

 函數調用和可視化如下:

方法一:

img_new = logarithm_transformation(img, power_law1)

plt.figure(figsize=(60,107))
plt.subplot(121)
plt.axis('off')
plt.imshow(img_new, cmap='gray')

plt.subplot(122)
plt.axis('off')
plt.imshow(img, cmap = 'gray')

plt.show()

 左側爲結果,右側爲原圖像:

 

 方法二:

img_new = logarithm_transformation(img, power_law2)

plt.subplot(121)
plt.axis('off')
plt.imshow(img_new, cmap='gray')

plt.subplot(122)
plt.axis('off')
plt.imshow(img, cmap = 'gray')

plt.show()

 左側爲結果,右側爲原圖像:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章