幂律变换的基本形式为:,其中
和
是常数
有时考虑到偏移量,上式也写为。然而,偏移量是一般显示标定问题,因而作为一个结果,通常在上式中忽略不计。
与对数变换情况类似,部分值得幂律曲线将较窄范围的暗色值,映射位较宽的目标输出值,相反,对于输入高灰度级值时也成立。
>1的值所生成的曲线和
<1所生成的曲线的效果完全相反,当c=
=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,参数分别等于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()