代码及内容展示和分析:
1.1选用c*log(x,base)来处理lena的每一个像素,并取不同的参数和底数
import cv2 as cv
import numpy as np
import math
Lena = cv.imread(‘lena.jpeg’,0)
q = lambda x,a,b:a*math.log(1+x,b)
bases = [2,math.exp(1),10]
params = [10,20,30]
lena_all = [[np.uint8([[q(b,param,base)for b in a]for a in lena])for
param in params]for base in bases]
[[cv.imshow(f’{bases[a]}-{params[b]}’,lena_all[a][b])for b in range
(len(lena_all[0]))]for a in range(len(lena_all))]
[[cv.imwrite(f’{bases[a]}-{params[b]}.jpeg’,lena_all[a][b])for b in
range(len(lena_all[0]))]for a in range(len(lena_all))]
if cv.waitKey() == ord(‘A’):
cv.destroyAllWindows()
(1)当底数为2,参数选择[10,20,30]时,效果图如下:
Fig.1-1 Fig.1-2 Fig.1-3
(2)当底数为自然对数e,参数选择[10,20,30]时,效果图如下:
Fig.1-4 Fig.1-5 Fig.1-6
(3)当底数为10,参数选择[10,20,30]时,效果图如下:
分析:使用对数处理图像时,图像会变暗,使用修正参数可使图像亮度增加。
1.2选用x^a的形式处理lena的每一个像素
import cv2 as cv
import numpy as np
Lena = cv.imread(‘lena.jpeg’,0)
q = lambda x,a:pow(x,a)
power = [0.4,0.6,0.8,1.1,1.6,2.1]
lena_1 = lena/255
lena_all = [np.float32([[q(b,power)for b in a]for a in lena_1])for
power in powers]
lena_re_255 = [np.uint8(a*255)for a in lena_all]
[cv.imshow(f’{a+1}’,lena_re_255[a])for a in range(len(lena_re_255))]
[cv.imwrite(f’{a+1}.jpeg’,lena_re_255[a])for a in range(len
(lena_re_255))]
If cv.waitKey() == ord(‘A’):
cv.destroyAllWindows()
(1)当幂次选定[0.4,0.6,0.8]小于1的幂次时:
Fig.1-10 Fig.1-11 Fig.1-12
(2)当幂次选定[1.1,1.6,2.1]大于1的幂次时:
Fig.1-13 Fig.1-14 Fig.1-15
分析:当幂次小于1时,越小则图像的亮度越高;当幂次大于1时,越大则图像的亮度越低。
2.lena直方图及其均衡化
import cv 2 as cv
import matplotlib.pyplot as plt
lena = cv.imread(‘lena.jpeg’,0)
lena_equ = cv.equalizeHist(lena)
cv.imshow(‘1’,lena)
cv.imshow(‘2’,lena_equ)
cv.imwrite(‘lena_equ.jpeg’,lena_equ)
plt.figure(‘原始直方图:‘)
plt.hist(lena.ravel(),256)
plt.figure(‘均衡后直方图:‘)
plt.hist(lena_equ.ravel(),256)
plt.show()
if cv.waitKey() == ord(‘A’):
cv.destroyAllWindows()
(1)Lena原始图和均衡后的lena图
Fig.2-1 Fig.2-2
(2)Lena的直方图和均衡后的直方图
Fig.2-3
Fig.2-4
分析:均衡后的lena图的亮度要比原始图稍微暗一点;均衡后的直方图灰度分布更加均匀一点。