【千律】OpenCV基础:图像的线性变换和非线性变换

狐沐说

关注

阅读 67

2022-03-20

环境:Python3.8 和 OpenCV

内容:图像的线性变换和非线性变换

逐像素运算就是对图像中的全部像素点的亮度值,通过函数关系转换为新的亮度值。

线性变换     : y = a * x + b
非线性变换 : y = a + ln(x + 1) / (b * lnc)
gamma变换: y = c * x^(g)

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt


# 封装图片显示函数
def image_show(image):
    if image.ndim == 2:
        plt.imshow(image, cmap='gray')
    else:
        image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
        plt.imshow(image)
    plt.show()


if __name__ == '__main__':

    # 读取灰度图像
    img_lenna = cv.imread('lenna.png', 0)

    # 线性变换
    alpha = 2             # 斜率参数
    beta = 20             # 偏置参数
    img_linear = cv.convertScaleAbs(img_lenna, alpha=alpha, beta=beta)
    image_show(img_linear)

    # 非线性变换
    para1 = 10            # 非线性参数1
    para2 = 0.1           # 非线性参数2

    img_nonlinear = para1 + np.log(np.float32(img_lenna) + 1) / para2
    img_nonlinear = np.clip(img_nonlinear, 0, 255)
    image_show(img_nonlinear)

    # 非线性变换(gamma变换)
    gamma = 1.2           # 指数参数
    img_nonlinear = np.power(img_lenna, gamma)
    img_nonlinear = np.clip(img_nonlinear, 0, 255)
    image_show(img_nonlinear)

精彩评论(0)

0 0 举报