• [Python图像处理]十.图像的灰度线性变换


    图像灰度上移变换

    该算法将实现图像灰度值的上移,从而提升图像的亮度,由于图像的灰度值位于0到255之间,需要对灰度值进行溢出判断。

    代码如下:

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    
    img = cv2.imread("src.png")
    grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    height, width = grayImage.shape[:2]
    result = np.zeros((height, width), np.uint8)
    # 图像灰度上移变换
    for i in range(height):
        for j in range(width):
            if int(grayImage[i, j] + 50) > 255:
                gray = 255
            else:
                gray = grayImage[i, j] + 50
            result[i, j] = np.uint8(gray)
    cv2.imshow("src", grayImage)
    cv2.imshow("result", result)
    
    if cv2.waitKey() == 27:
        cv2.destroyAllWindows()

    效果如下:

     图像对比度增强变换

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    
    img = cv2.imread("src.png")
    grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    height, width = grayImage.shape[:2]
    result = np.zeros((height, width), np.uint8)
    # 图像灰度上移变换
    for i in range(height):
        for j in range(width):
            if int(grayImage[i, j]*1.5 + 50) > 255:
                gray = 255
            else:
                gray = grayImage[i, j]*1.5 + 50
            result[i, j] = np.uint8(gray)
    cv2.imshow("src", grayImage)
    cv2.imshow("result", result)
    
    if cv2.waitKey() == 27:
        cv2.destroyAllWindows()

    效果如下:

     图像对比度增强减弱

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    
    img = cv2.imread("src.png")
    grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    height, width = grayImage.shape[:2]
    result = np.zeros((height, width), np.uint8)
    # 图像灰度上移变换
    for i in range(height):
        for j in range(width):
            if int(grayImage[i, j]*0.8 + 50) > 255:
                gray = 255
            else:
                gray = grayImage[i, j]*0.8 + 50
            result[i, j] = np.uint8(gray)
    cv2.imshow("src", grayImage)
    cv2.imshow("result", result)
    
    if cv2.waitKey() == 27:
        cv2.destroyAllWindows()

    效果如下:

     图像灰度反色变换

    反色变换又称为线性灰度补变换,它是对原图像的像素值进行反转,即黑色变为白色,白色变为黑色

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    
    img = cv2.imread("src.png")
    grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    height, width = grayImage.shape[:2]
    result = np.zeros((height, width), np.uint8)
    # 图像灰度上移变换
    for i in range(height):
        for j in range(width):
                gray = 255 - int(grayImage[i,j])
                result[i, j] = np.uint8(gray)
    cv2.imshow("src", grayImage)
    cv2.imshow("result", result)
    
    if cv2.waitKey() == 27:
        cv2.destroyAllWindows()

    效果如下:

     图像灰度非线性变换: DB=DAxDA/255

    图像的灰度非线性变换主要包括对数变换、幂次变换、指数变换、分段函数变换,通过非线性关系对图像进行灰度处理,下面主要讲解三种常见类型的灰度非线性变换。

    原始图像的灰度值按照DB=DA*DA/255

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    
    img = cv2.imread("src.png")
    grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    height, width = grayImage.shape[:2]
    result = np.zeros((height, width), np.uint8)
    # 图像灰度上移变换
    for i in range(height):
        for j in range(width):
                gray = int(grayImage[i, j])*int(grayImage[i, j])/255
                result[i, j] = np.uint8(gray)
    cv2.imshow("src", grayImage)
    cv2.imshow("result", result)
    
    if cv2.waitKey() == 27:
        cv2.destroyAllWindows()

    效果如下:

     图像灰度对数变换

    由于对数曲线在像素值较低的区域斜率大,在像素值较高的区域斜率较小,所以图像经过对数变换后,较暗区域的对比度将有所提升。这种变换可用于增强图像的暗部细节,从而用来扩展被压缩的高值图像中的较暗像素。

    对数变换实现了扩展低灰度值而压缩高灰度值的效果,被广泛地应用于频谱图像的显示中。一个典型的应用是傅立叶频谱,其动态范围可能宽达0~106直接显示频谱时,图像显示设备的动态范围往往不能满足要求,从而丢失大量的暗部细节;而在使用对数变换之后,图像的动态范围被合理地非线性压缩,从而可以清晰地显示。在下图中,未经变换的频谱经过对数变换后,增加了低灰度区域的对比度,从而增强暗部的细节。

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    
    def log_plot(c):
        x = np.arange(0, 256, 0.01)
        y = c * np.log(1+x)
        plt.plot(x, y, "r", linewidth=1)
        plt.rcParams["font.sans-serif"] = ["SimHei"]
        plt.title("对数变换函数")
        plt.xlim(0, 255)
        plt.ylim(0, 255)
        plt.show()
    
    # 对数变换
    def log(c, img):
        output = c * np.log(1.0+img)
        output = np.uint8(output)
        return output
    
    img = cv2.imread("src.png")
    log_plot(42)
    result = log(42, img)
    cv2.imshow("src", img)
    cv2.imshow("result", result)
    if cv2.waitKey() == 27:
        cv2.destroyAllWindows()

     图像灰度伽玛变换

    伽玛变换又称为指数变换或幂次变换,另一种常用的灰度非线性变换。

    Db = cXDa^y

    • 当γ>1时,会拉伸图像中灰度级较高的区域,压缩灰度级较低的部分。
    • 当γ<1时,会拉伸图像中灰度级较低的区域,压缩灰度级较高的部分。
    • 当γ=1时,该灰度变换是线性的,此时通过线性方式改变原图像。
    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    
    def gamma_plot(c, v):
        x = np.arange(0, 256, 0.01)
        y = c *x**v
        plt.plot(x, y, "r", linewidth=1)
        plt.rcParams["font.sans-serif"] = ["SimHei"]
        plt.title("对数变换函数")
        plt.xlim([0, 255])
        plt.ylim([0, 255])
        plt.show()
    
    # 对数变换
    def gamma(img, c, v):
        lut = np.zeros(256, dtype=np.float32)
        for i in range(256):
            lut[i] = c*i**v
        # 灰度值的映射
        output = cv2.LUT(img, lut)
        output = np.uint8(output + 0.5)
        return output
    
    img = cv2.imread("src.png")
    gamma_plot(0.0000005, 4)
    result = gamma(img, 0.0000005, 4)
    cv2.imshow("src", img)
    cv2.imshow("result", result)
    if cv2.waitKey() == 27:
        cv2.destroyAllWindows()

  • 相关阅读:
    案例7-1.2 插入排序还是归并排序 (25分)
    自动化运维工具——puppet详解(一)
    centos6.8的安装和配置
    ZooKeeper内部原理
    ZooKeeper安装和配置
    zookeeper入门
    shell中uniq与sort -u 两种去重的对别
    tomcat日志文件 访问IP统计
    Mysql常用命令
    linux一键安装php脚本
  • 原文地址:https://www.cnblogs.com/zhouzetian/p/13320908.html
Copyright © 2020-2023  润新知