七、skimage对比度与亮度调整

目录

1、gamma调整

2、log对数调整

3、判断图像对比度是否偏低

4、调整强度

(1)强度调节

(2)另一种uint8转float方法


图像亮度与对比度的调整,是放在skimage包的exposure模块里面

1、gamma调整

原理:I=Ig

对原图像的像素,进行幂运算,得到新的像素值。公式中的g就是gamma值。

如果gamma>1, 新图像比原图像暗

如果gamma<1,新图像比原图像亮

函数格式为:skimage.exposure.adjust_gamma(imagegamma=1)

gamma参数默认为1,原像不发生变化 。

from skimage import data, exposure, img_as_float
import matplotlib.pyplot as plt
image = img_as_float(data.moon())
gam1= exposure.adjust_gamma(image, 2)   #调暗
gam2= exposure.adjust_gamma(image, 0.5)  #调亮
plt.figure('adjust_gamma',figsize=(8,8))

plt.subplot(131)
plt.title('origin image')
plt.imshow(image,plt.cm.gray)
plt.axis('off')

plt.subplot(132)
plt.title('gamma=2')
plt.imshow(gam1,plt.cm.gray)
plt.axis('off')

plt.subplot(133)
plt.title('gamma=0.5')
plt.imshow(gam2,plt.cm.gray)
plt.axis('off')

plt.show()

 

2、log对数调整

这个刚好和gamma相反

原理:I=log(I)

from skimage import data, exposure, img_as_float
import matplotlib.pyplot as plt
image = img_as_float(data.moon())
gam1= exposure.adjust_log(image)   #对数调整
plt.figure('adjust_gamma',figsize=(8,8))

plt.subplot(121)
plt.title('origin image')
plt.imshow(image,plt.cm.gray)
plt.axis('off')

plt.subplot(122)
plt.title('log')
plt.imshow(gam1,plt.cm.gray)
plt.axis('off')

plt.show()

 

3、判断图像对比度是否偏低

函数:is_low_contrast(img)

返回一个bool型值

from skimage import data, exposure
image =data.moon()
result=exposure.is_low_contrast(image)
print(result)

输出为False

4、调整强度

(1)强度调节

函数:skimage.exposure.rescale_intensity(imagein_range='image'out_range='dtype')

in_range 表示输入图片的强度范围,默认为'image', 表示用图像的最大/最小像素值作为范围

out_range 表示输出图片的强度范围,默认为'dype', 表示用图像的类型的最大/最小值作为范围

默认情况下,输入图片的[min,max]范围被拉伸到[dtype.min, dtype.max],如果dtype=uint8, 那么dtype.min=0, dtype.max=255

import numpy as np
from skimage import exposure
image = np.array([51, 102, 153], dtype=np.uint8)
mat=exposure.rescale_intensity(image)
print(mat)

输出为[  0 127 255]

即像素最小值由51变为0,最大值由153变为255,整体进行了拉伸,但是数据类型没有变,还是uint8

(2)另一种uint8转float方法

a、前面我们讲过,可以通过img_as_float()函数将unit8类型转换为float型,实际上还有更简单的方法,就是乘以1.0

import numpy as np
image = np.array([51, 102, 153], dtype=np.uint8)
print(image*1.0)

即由[51,102,153]变成了[  51.  102.  153.]

b、而float类型的范围是[0,1],因此对float进行rescale_intensity 调整后,范围变为[0,1],而不是[0,255]

import numpy as np
from skimage import exposure
image = np.array([51, 102, 153], dtype=np.uint8)
tmp=image*1.0
mat=exposure.rescale_intensity(tmp)
print(mat)

结果为[ 0.   0.5  1. ]

c、如果原始像素值不想被拉伸,只是等比例缩小,就使用in_range参数,如:

import numpy as np
from skimage import exposure
image = np.array([51, 102, 153], dtype=np.uint8)
tmp=image*1.0
mat=exposure.rescale_intensity(tmp,in_range=(0,255))
print(mat)

输出为:[ 0.2  0.4  0.6],即原像素值除以255

d、如果参数in_range的[main,max]范围要比原始像素值的范围[min,max] 大或者小,那就进行裁剪,如:

mat=exposure.rescale_intensity(tmp,in_range=(0,102))
print(mat)

输出[ 0.5  1.   1. ],即原像素值除以102,超出1的变为1

e、如果一个数组里面有负数,现在想调整到正数,就使用out_range参数。如:

import numpy as np
from skimage import exposure
image = np.array([-10, 0, 10], dtype=np.int8)
mat=exposure.rescale_intensity(image, out_range=(0, 127))
print(mat)

输出[  0  63 127]

### 图像对比度概念 图像对比度是指图像中不同区域之间的亮度差异程度。高对比度意味着亮区非常明亮,暗区则显得较暗;而在低对比度情况下,这种区别变得模糊不清。对于计算机视觉应用而言,良好的对比度有助于特征提取和模式识别。 ### 调整图像对比度的方法 #### 方法一:直方图均衡化 通过改变图像的灰度分布来增强局部细节,具体来说就是对图像中像素个数多的灰度级进行扩展,而对图像中像素个数少的灰度进行压缩,从而扩展像素的取值范围,提高对比度和灰度色调的变化,使图像更加清晰[^2]。 ```python from skimage import exposure, data import matplotlib.pyplot as plt img = data.moon() plt.figure(figsize=(8, 4)) # 原始图片 plt.subplot(121) plt.imshow(img, cmap='gray') plt.title('Original') # 应用直方图均衡化的图片 equalized_img = exposure.equalize_hist(img) plt.subplot(122) plt.imshow(equalized_img, cmap='gray') plt.title('Equalized Histogram') plt.show() ``` #### 方法二:伽马校正 (Gamma Correction) 伽玛矫正是一种非线性的操作方式,可以用来调整图像的整体明暗效果以及改善其对比度特性。该技术基于人类视觉系统的感知特点设计而成,在某些特定场合下能够取得更好的显示质量。 ```python import numpy as np import cv2 import matplotlib.pyplot as plt def adjust_gamma(image, gamma=1.0): invGamma = 1.0 / gamma table = np.array([((i / 255.0) ** invGamma) * 255 for i in range(256)]).astype("uint8") return cv2.LUT(image, table) image = cv2.imread('example.jpg', flags=cv2.IMREAD_GRAYSCALE) gamma_corrected_image = adjust_gamma(image=image, gamma=2.2) fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5)) ax = axes.ravel() ax[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) ax[0].set_title('Original Image') ax[1].imshow(cv2.cvtColor(gamma_corrected_image, cv2.COLOR_BGR2RGB)) ax[1].set_title('Gamma Corrected Image') plt.tight_layout() plt.show() ``` #### 方法三:CLAHE(Contrast Limited Adaptive Histogram Equalization) 自适应直方图均衡化算法针对整个图像进行了改进,它会将大尺寸图像划分为若干个小块并分别对其进行直方图均衡化处理。为了防止过度放大噪声的影响,还加入了对比度限制机制。这种方法特别适合于那些存在较大光照变化或者复杂背景的任务场景。 ```python clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) clipped_equalized_image = clahe.apply(image) fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5)) ax = axes.ravel() ax[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) ax[0].set_title('Original Image') ax[1].imshow(cv2.cvtColor(clipped_equalized_image, cv2.COLOR_BGR2RGB)) ax[1].set_title('CLAHE Applied Image') plt.tight_layout() plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值