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

灰度图的非线性伽马变换,其中c为尺度比较常数,gamma为伽马函数的阈值,可以通过改变c和gamma来得到不同的图像效果。

代码如下:

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
# 非线性伽马变换
# 传入参数img_gray为灰度图,c为尺度比较常数,gamma为伽马函数的阈值
def nonlinear_gamma_transform(img_gray, c, gamma):
    # power(x, y) 函数,计算x的y次方。
    print(img_gray)
    print(float(np.max(img_gray)))
    # 这里类似于做一个归一化的处理
    print(img_gray/float(np.max(img_gray)))
    result = c * np.power(img_gray/float(np.max(img_gray)), gamma) * 255.0
    # uint8是专门用于存储各种图像的(包括RGB,灰度图像等),范围是从0–255
    # 这里要转换成unit8
    result = np.uint8(result)
    return result
# 绘制伽马图像
def nonlinear_gamma_function(c, gamma):
    x = np.arange(0, 255)
    # power(x, y) 函数,计算x的y次方。
    y = c * np.power(x, gamma)
    plt.title("nonlinear_gamma")
    plt.plot(x, y)
    plt.show()
if __name__ == '__main__':
    img = cv.imread("./image/fengjing.jpg")
    img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    c = 1
    gamma_a = 0.6
    gamma_b = 3
    res_b = nonlinear_gamma_transform(img_gray, c, gamma_b)
    res_a = nonlinear_gamma_transform(img_gray, c, gamma_a)
    nonlinear_gamma_function(c, gamma_b)
    cv.imshow("Origin", img)
    cv.imshow("Gamma_b", res_b)
    cv.imshow("Gamma_a", res_a)
    cv.waitKey(0)
    cv.destroyAllWindows()

运行结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

图像的效果有了明显变化

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值