基本的灰度变换函数——幂律(伽马)变换

幂律变换的基本形式为: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()

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫猫虫(——)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值