灰度图的非线性对数变换,其中c为尺度比较常数,可以通过改变c来得到不同的图形效果。
代码如下:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
# 非线性对数变换
# 传入参数 img_gray为灰度图,c为尺度比较常数
def nonlinear_logarithm_transform(img_gray, c):
result = c * np.log(1.0 + img_gray)
# uint8是专门用于存储各种图像的(包括RGB,灰度图像等),范围是从0–255
# 这里要转换成unit8,否则会报错
result = np.uint8(result)
return result
# 绘制对数图像
def nonlinear_logarithm_function(c):
x = np.arange(0, 255)
y = c * np.log(1.0 + x)
plt.title("nonlinear_logarithm")
# # 设置x轴的作图范围
# plt.xlim(0, 255)
# # 设置y轴的作图范围
# plt.ylim(0, 255)
# 表达的是x轴的刻度内容的范围
plt.xticks((0, 255))
# 表达的是y轴的刻度内容的范围
plt.yticks((0, 255))
plt.text(125, 1560, 'c='+str(c))
plt.plot(x, y)
plt.show()
if __name__ == '__main__':
img = cv.imread("./image/fengjing.jpg")
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 此阈值可以改变,c叫做尺度比较常数
# 通过调整c,可以改变图像
c = 300
res = nonlinear_logarithm_transform(img_gray, c)
nonlinear_logarithm_function(c)
cv.imshow("Origin", img_gray)
cv.imshow("nonlinear_logarithm_transform", res)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果:
图像的效果有了明显变化