伽马变换原理及示例

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()
    

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

  • 14
    点赞
  • 127
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
输入捕获是一种测量外部信号频率的常用方法。其原理是通过捕获外部信号的边沿(上升沿或下降沿)来计算信号的周期,从而得到频率。 以下是一个示例,演示如何使用输入捕获功能来测量信号的频率: 1. 配置定时器和输入捕获引脚: - 配置一个定时器来作为计数器,并设置其时钟源和工作模式。 - 选择一个GPIO引脚,并将其配置为输入模式,连接到外部信号源。 2. 配置输入捕获功能: - 设置输入捕获模式为边沿捕获,选择上升沿或下降沿。 - 配置触发条件,例如设置捕获事件在每个边沿发生时都触发。 3. 中断处理程序: - 在捕获事件发生时,中断处理程序会被触发。 - 在中断处理程序中,读取定时器的计数器值,并计算两个连续捕获事件之间的时间差。 - 根据时间差计算频率,例如通过计算周期的倒数得到频率。 示例伪代码如下: ```c // 配置定时器和输入捕获引脚 configure_timer(); configure_input_capture_pin(); // 配置输入捕获功能 configure_input_capture(); // 定义中断处理程序 void input_capture_interrupt_handler() { // 读取定时器计数器值 uint32_t capture_value = read_timer_capture_value(); // 计算时间差 uint32_t time_difference = calculate_time_difference(capture_value); // 计算频率 float frequency = calculate_frequency(time_difference); // 打印频率值或进行其他处理 print_frequency(frequency); } // 主程序 int main() { // 启用中断 enable_interrupts(); while (1) { // 等待中断发生 } } ``` 需要根据具体的微控制器和编程环境进行相应的配置和函数调用。请参考相关的技术手册和编程指南以获取更详细的信息和示例代码。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值