Python 图像处理OpenCV:灰度图的非线性对数变换(笔记)

灰度图的非线性对数变换,其中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()

运行结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
图像的效果有了明显变化

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值