数字图像处理 -灰度变换 之 gamma变换(gamma transformation)

本文通过Matlab代码演示了不同Gamma值下的图像灰度级变化。当Gamma小于1时,图像变得更亮;反之则更暗。实验中使用了一系列Gamma值,包括0.1、0.2、0.4、0.6、0.8、1、2.5和5,并展示了相应的图像处理效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Reference: https://blog.youkuaiyun.com/zhoufan900428/article/details/12709361 

The gamma transformation is different from log transformation. Its code is similar to that of the last blog. Its figure is as follows: 

Figure :


gamma transformation  Matlab code:

f = imread('2.jpg');  
I=rgb2gray(f);
f = mat2gray(I);

gamma1 = 0.1;
g_0_1 = f.^gamma1; 
gamma2 = 0.2;
g_0_2 = f.^gamma2; 
gamma3 = 0.4;
g_0_4 = f.^gamma3;
gamma4 = 0.6;
g_0_6 = f.^gamma4;
gamma5 = 0.8;
g_0_8 = f.^gamma5; 
gamma6 = 1;
g_1 = f.^gamma6;  
gamma7 = 2.5;
g_2_5 = f.^gamma7; 
gamma8 = 5;
g_5 = f.^gamma8; 
  
figure();  
subplot(3,3,1);  
imshow(f,[0 1]);  
xlabel('a).Original Image');  

subplot(3,3,2);  
imshow(g_0_1,[0 1]);  
xlabel('b).\gamma =0.1'); 

subplot(3,3,3);  
imshow(g_0_2,[0 1]);  
xlabel('c).\gamma =0.2'); 

subplot(3,3,4);  
imshow(g_0_4,[0 1]);  
xlabel('d).\gamma=0.4'); 

subplot(3,3,5);  
imshow(g_0_6,[0 1]);  
xlabel('e).\gamma=0.6'); 

subplot(3,3,6);  
imshow(g_0_8,[0 1]);  
xlabel('f).\gamma=0.8');  
  
subplot(3,3,7);  
imshow(g_1,[0 1]);  
xlabel('g).\gamma=1 i.e. original image');  
  
subplot(3,3,8);  
imshow(g_2_5,[0 1]);  
xlabel('h).\gamma=2.5'); 

subplot(3,3,9);  
imshow(g_5,[0 1]);  
xlabel('i).\gamma=5'); 

Figure :


we can see that if gamma <1, the gray level becomes brighter, otherwise, it is darker. It is obvious to know if we connect this figure with the last figure, namely, the gamma image.  

I make some improvements on the basis of the original blog. https://blog.youkuaiyun.com/zhoufan900428/article/details/12709361 Thanks again.

Due to level constraint, please comment if you ave any questions about this blog. I would greatly appreciate it if you kindly give me some feedback.
^_^


  

### 数字图像处理灰度变换的代码实现 灰度变换是一种常见的数字图像处理技术,用于调整图像的对比度或亮度。以下是几种典型的灰度变换方法及其对应的 Python 实现。 #### 1. 灰度线性变换 灰度线性变换通过简单的线性函数 \( g(x, y) = a \cdot f(x, y) + b \),其中 \( a \) 和 \( b \) 是常数参数,可以用来放大或缩小图像的灰度范围[^1]。 ```python import numpy as np import cv2 def linear_transformation(image, alpha=1.0, beta=0): """ 对输入图像进行线性灰度变换。 参数: image (numpy.ndarray): 输入灰度图像。 alpha (float): 放大系数,默认为1.0。 beta (int): 增加偏移量,默认为0。 返回: numpy.ndarray: 变换后的图像。 """ transformed_image = np.clip(alpha * image.astype(float) + beta, 0, 255).astype(np.uint8) return transformed_image # 示例调用 image = cv2.imread('input.jpg', cv2.IMREAD_GRAYSCALE) result = linear_transformation(image, alpha=1.5, beta=-50) cv2.imwrite('linear_transformed.jpg', result) ``` --- #### 2. 图像二值化 图像二值化是将灰度图像转换为黑白两色图像的过程。通常会设定一个阈值 \( T \),使得像素值大于 \( T \) 的部分设为白色(255),其余部分设为黑色(0)。 ```python def binary_thresholding(image, threshold=127): """ 对输入图像进行二值化处理。 参数: image (numpy.ndarray): 输入灰度图像。 threshold (int): 阈值,默认为127。 返回: numpy.ndarray: 二值化的图像。 """ _, binary_image = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY) return binary_image # 示例调用 binary_result = binary_thresholding(image, threshold=150) cv2.imwrite('binary_image.jpg', binary_result) ``` --- #### 3. 负象变换 负象变换通过对每个像素取反操作 \( g(x, y) = 255 - f(x, y) \),使原始图像的颜色反转,从而得到一种特殊的视觉效果。 ```python def negative_transformation(image): """ 对输入图像进行负象变换。 参数: image (numpy.ndarray): 输入灰度图像。 返回: numpy.ndarray: 负象变换后的图像。 """ negated_image = 255 - image return negated_image # 示例调用 negated_result = negative_transformation(image) cv2.imwrite('negative_image.jpg', negated_result) ``` --- #### 4. 灰度非线性变换 灰度非线性变换可以通过幂律变换或其他非线性映射方式完成。例如伽马校正 \( g(x, y) = c \cdot f(x, y)^{\gamma} \)。 ```python def gamma_correction(image, gamma=1.0, c=1.0): """ 对输入图像进行伽马矫正。 参数: image (numpy.ndarray): 输入灰度图像。 gamma (float): 幂指数,默认为1.0。 c (float): 缩放因子,默认为1.0。 返回: numpy.ndarray: 伽马矫正后的图像。 """ normalized_image = image / 255.0 corrected_image = ((normalized_image ** gamma) * c) * 255 return corrected_image.astype(np.uint8) # 示例调用 gamma_corrected = gamma_correction(image, gamma=2.2, c=1.0) cv2.imwrite('gamma_corrected.jpg', gamma_corrected) ``` --- 以上代码实现了四种主要的灰度变换方法:线性变换、二值化、负象变换以及伽马矫正。这些方法可以根据具体需求灵活应用到实际项目中。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值