伽马变换原理及示例

您所在的位置:网站首页 场景gamma设置 伽马变换原理及示例

伽马变换原理及示例

2023-10-03 09:16| 来源: 网络整理| 查看: 265

1.伽马变换的应用场景。

在图像处理中,常常利用伽马变换来对过曝或者曝光不足(过暗)的灰度图利用伽马变换进行对比度调节。具体年来讲:就是通过非线性变换,让图像中较暗的区域的灰度值得到增强,图像中灰度值过大的区域的灰度值得到降低。经过伽马变换,图像整体的细节表现会得到增强。

2、伽马变换的原理。

数学公式如下: 在这里插入图片描述 其中,r为灰度图像的输入值(原来的灰度值),取值范围为[0,1]。s为经过伽马变换后的灰度输出值。c为灰度缩放系数,通常取1。γ为伽马因子大小。控制了整个变换的缩放程度。

3.伽马变换示意图。 # -*- coding: utf-8 -*- """ Created on Tue Apr 21 19:26:40 2020 @author: Administrator """ import numpy as np import matplotlib.pyplot as plt #必须添加以下两行,因为matplotlib中无法显示中文字符。 from pylab import * mpl.rcParams['font.sans-serif']=['SimHei'] a=np.arange(0,1,1e-4) b=list(a)*11 matrix=np.array(b).reshape(11,10000) gamma=[0.1,0.2,0.3,0.5,0.7,1.0,1.5,2,3.5,7.5,15] #伽马变换 for i in range(11): matrix[i,:]=np.power(matrix[i,:],gamma[i]) colors=["lightgreen","green","greenyellow","lightblue","blue","darkblue","yellow",\ "orange","pink", "red", "gray"] plt.figure(figsize=(13,13),dpi=100) plt.subplot(111) for j in range(11): y=matrix[j,:] x=a.copy() label_=gamma[j] #设置线条形状、颜色以及标签。 plt.plot(a, y, color=colors[j], linewidth=2.5, linestyle="-") label=["γ = 0.1","γ = 0.2","γ = 0.3","γ = 0.5","γ = 0.7","γ = 1.0","γ = 1.5","γ = 2.0",\ "γ = 3.5","γ = 7.5","γ = 15"] plt.title("gama transformation",fontsize='35') plt.xlabel("灰度输入值(相对值:R=I(i,j)/255)",fontsize='35') plt.ylabel('灰度输出值(相对值S)',fontsize='35') #添加图例。 plt.legend(label) #后面的plt.xlim以及plt.xticks函数的作用主要是覆盖掉原本的真实数据。 #使坐标轴的数值分布按照自定义的值来排列。 plt.xlim(0,1)#设置横轴的上下限 plt.xticks(np.linspace(0,1,11),size=20)#设置图像上横轴的区间分段。 plt.ylim(0,1) plt.yticks(np.linspace(0,1,11),size=20) plt.savefig('Gamma Translation.jpg') plt.show()

生成的伽马变换示意图如下: 在这里插入图片描述 其中,I(i,j)表示图像的第i行j列的灰度值。有图像可以得出:当γ>1时,将会减小原图像的灰度值,当γ<1时,将会增大原图像的灰度值。

4.伽马变换的源代码实现及示例。

原图及其灰度直方图如下:

def draw_gray_hist(source_path): #draw the grey histogram of one three channels picture source_image=cv2.imread(source_path) gray_img=cv2.cvtColor(source_image,cv2.COLOR_BGR2GRAY) hist_total,hist_edges=np.histogram(gray_img[:,:].ravel(),bins=256,range=(0,256)) plt.plot(0.5*(hist_edges[1:]+hist_edges[:-1]),hist_total,label='grey',color='gray') plt.show() draw_gray_hist('1.jpg')

原图如下: 在这里插入图片描述 统计结果如下: 在这里插入图片描述 现在取γ值分别为:[0.1,0.5,1.5,3.0,5.0]时,进行伽马变换,并对伽马变换后的结果进行直方图进行统计。具体如下:

# -*- coding: utf-8 -*- """ Created on Wed Apr 22 19:48:08 2020 @author: Administrator """ import cv2 import numpy as np import matplotlib.pyplot as plt class Gammatranslation(): def __init__(self): pass def gammatraslation(self,s_c_img,gamma): gamma_result=[np.power(i/255.0,gamma) *255.0 for i in range(0,256)] gamma_result=np.round(gamma_result).astype(np.uint8)#将运算结果转换为图像灰度值类型。 return cv2.LUT(s_c_img,gamma_result)#使用查找表完成gamma变换。 #绘制灰度直方图 def drawhist(self,img,tname): hist,bin_edges=np.histogram(img.ravel(),bins=256,range=(0,256)) plt.subplot(111) plt.plot(0.5*(bin_edges[:-1]+bin_edges[1:]),hist,color="blue") plt.xlim(0,300)#设置横轴的上下限 plt.xticks(np.linspace(0,300,7),size=10)#设置图像上横轴的区间分段。 plt.title(tname) plt.savefig(f'{gama_ls[i]}.jpg') filename='1.jpg' img=cv2.imread(filename) g_source=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) gama_ls=[0.1,0.5,1.5,3.0,5.0] g_translation=Gammatranslation() for i in range(5): gama_result=g_translation.gammatraslation(g_source,gama_ls[i]) plt.subplot(111) plt.imshow(gama_result) plt.title('The gammatranslation result') plt.savefig(f'γ = {gama_ls[i]}.jpg') plt.show() g_translation.drawhist(gama_result,f"The result of γ = {gama_ls[i]}") plt.show()

结果如下: 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3