python图像处理:图像灰度非线性变换

本文介绍了图像处理中三种常见的灰度非线性变换:平方变换、对数变换和伽马变换。通过示例代码展示了如何使用Python的numpy库对图像进行这些变换,以改变图像的亮度和对比度。平方变换用于降低亮度并增强亮部对比度;对数变换则在增强暗部对比度的同时提高亮度;伽马变换则能增强亮部对比度,适用于阳光下的照片处理。

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

图像灰度的非线性变换

平方变换

将灰度进行平方再除以255,用于

  1. 降低图像亮度
  2. 增强亮部对比度
  3. 减缓暗部对比度

对比度可以看作灰度的斜率
在这里插入图片描述

对数变换

在这里插入图片描述
将灰度取对数再乘个常数,可以

  1. 提高图像亮度
  2. 增强暗部对比度
  3. 减缓亮部对比度

Q:为什么要加1?
A:为了确保结果是正数

比如拍了夜景图片,就可以用这个处理。

实验中我取c=46,log底数为e
因为np.log默认底数就是e, 255/np.log(256)=45.9。

在这里插入图片描述

伽马变换

在这里插入图片描述
将灰度幂一下再乘个常数,一般c都取得很小

  1. 降低图像亮度
  2. 增强亮部对比度
  3. 减缓暗部对比度

如果你再阳光下拍了个照,就可以用这个处理
在这里插入图片描述

样例

在这里插入图片描述

代码

import matplotlib
import matplotlib.pyplot as plt
import numpy as np 
import cv2 
matplotlib.rcParams['font.family'] = 'simHei'
matplotlib.rcParams['axes.unicode_minus'] = False
%matplotlib qt5
img = cv2.imread('666.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
rows,cols = img_gray.shape

img1 = np.ones((rows,cols),dtype = np.uint8)
img2 = np.ones((rows,cols),dtype = np.uint8)
img3 = np.ones((rows,cols),dtype = np.uint8)

#DB = (DA^2)/255
for i in range(rows):
    for j in range(cols):
        img1[i,j] = pow(img_gray[i,j],2)/255


#DB = 46*ln(DA)
for i in range(rows):
    for j in range(cols):
        img2[i,j] = np.uint8(np.log(img_gray[i,j]+1)*46)

#DB = 0.0000006031*DA^4
for i in range(rows):
    for j in range(cols):
        img3[i,j] = np.uint8(6.031e-8*pow(img_gray[i,j],4))

plt.subplot(321)
plt.title('原图')
plt.imshow(img_gray,'gray',vmin=0,vmax=255)
plt.subplot(322)
plt.imshow(img1,'gray',vmin=0,vmax=255)
plt.title('平方变换')
plt.subplot(323)
plt.title('原图')
plt.imshow(img_gray,'gray',vmin=0,vmax=255)
plt.subplot(324)
plt.title('对数变换')
plt.imshow(img2,'gray',vmin=0,vmax=255)
plt.subplot(325)
plt.title('原图')
plt.imshow(img_gray,'gray',vmin=0,vmax=255)
plt.subplot(326)
plt.title('伽马变换')
plt.imshow(img3,'gray',vmin=0,vmax=255)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值