基本的灰度變換函數——冪律(伽馬)變換

冪律變換的基本形式爲:s=cr^{\gamma },其中c\gamma是常數

有時考慮到偏移量,上式也寫爲s=c(r+\varepsilon )^{\gamma }。然而,偏移量是一般顯示標定問題,因而作爲一個結果,通常在上式中忽略不計。

與對數變換情況類似,部分\gamma值得冪律曲線將較窄範圍的暗色值,映射位較寬的目標輸出值,相反,對於輸入高灰度級值時也成立。

\gamma>1的值所生成的曲線和\gamma<1所生成的曲線的效果完全相反,當c=\gamma=1時簡化爲了恆等變換。

下面使用Python實現冪律變換:

使用的圖片數據爲:

 導入要使用的第三方庫:

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

 讀取圖片數據,並可視化:

img = Image.open('小亮點.jpg')
plt.axis('off')
plt.imshow(img)
plt.show()

 圖像數據轉換爲numpy數組:

img_data = np.array(img)

 定義冪律變換函數:

def power_law_rollovers(img, func, c, b):
    img_data = np.array(img)
    a = np.shape(img_data)
    new_img = []
    for i in range(a[0]):
        new_row = []
        for j in range(a[1]):
            data = list(img_data[i][j])
            new_data = func(data, c, b)
            new_row.append(np.array(new_data))
        new_img.append(np.array(new_row))
    return new_img
def power_law(data, c, b):
    new_data = []
    for k in data:
        a = int(c*(k**b))
        new_data.append(a)
    return new_data

 結果:

共生成4張圖片,參數c=1,參數\gamma分別等於0.8,0.6,0.4,0.3

new_img1 = power_law_rollovers(img, power_law, 1, 0.8)
new_img2 = power_law_rollovers(img, power_law, 1, 0.6)
new_img3 = power_law_rollovers(img, power_law, 1, 0.4)
new_img4 = power_law_rollovers(img, power_law, 1, 0.3)

new_img1 = np.array(new_img1)
new_img2 = np.array(new_img2)
new_img3 = np.array(new_img3)
new_img4 = np.array(new_img4)

new_img1 = Image.fromarray(new_img1.astype('uint8')).convert('RGB')
new_img2 = Image.fromarray(new_img2.astype('uint8')).convert('RGB')
new_img3 = Image.fromarray(new_img3.astype('uint8')).convert('RGB')
new_img4 = Image.fromarray(new_img4.astype('uint8')).convert('RGB')

plt.figure(figsize=(25,60))
plt.subplot(221)
plt.axis('off')
gray1 = new_img1.convert('L')
plt.imshow(gray1, cmap='gray')
plt.subplot(222)
plt.axis('off')
gray2 = new_img2.convert('L')
plt.imshow(gray2, cmap='gray')
plt.subplot(223)
plt.axis('off')
gray3 = new_img3.convert('L')
plt.imshow(gray3, cmap='gray')
plt.subplot(224)
plt.axis('off')
gray4 = new_img4.convert('L')
plt.imshow(gray4, cmap='gray')
plt.show()

 

 

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