python opencv的函数cv2.LUT(src, lut, dst=None)的具体使用(LUT:查找表)

1 查看cv2.LUT(src, lut, dst=None)函数的使用

LUT是look-up table查找表的意思,cv2.LUT(src, lut, dst=None)的作用是对输入的src执行查找表lut转换

1.1 LUT函数的python接口cv2.LUT()的参数说明

1、打印输出cv2.LUT()文档的使用

print(cv2.LUT.__doc__)

>>> print(cv2.LUT.__doc__)
LUT(src, lut[, dst]) -> dst
.   @brief Performs a look-up table transform of an array.
.   
.   The function LUT fills the output array with values from the look-up table. Indices of the entries
.   are taken from the input array. That is, the function processes each element of src as follows:
.   \f[\texttt{dst} (I)  \leftarrow \texttt{lut(src(I) + d)}\f]
.   where
.   \f[d =  \fork{0}{if \(\texttt{src}\) has depth \(\texttt{CV_8U}\)}{128}{if \(\texttt{src}\) has depth \(\texttt{CV_8S}\)}\f]
.   @param src input array of 8-bit elements.
.   @param lut look-up table of 256 elements; in case of multi-channel input array, the table should
.   either have a single channel (in this case the same table is used for all channels) or the same
.   number of channels as in the input array.
.   @param dst output array of the same size and number of channels as src, and the same depth as lut.
.   @sa  convertScaleAbs, Mat::convertTo
>>> 

2、 cv2.LUT(src, lut, dst=None)参数说明

cv2. LUT(src, lut, dst=None)有三个参数,分别为:

  • src:输入数据array,类型为8位整型(np.uin8)
  • lut查找表,如果输入src是多通道的,例如是BGR三通到的图像,而查表是单通道的,则此时B、G、R三个通道使用的是同一个查找表
  • dst=None:输出数组,大小和通道数与src相同,而深度depth与lut相同

函数LUT查找表中的值填充输出数组,LUT函数对src中的每个元素的处理如下:

dst ( I ) ← lut(src(I)   +   d) \texttt{dst} (I) \leftarrow \texttt{lut(src(I) + d)} dst(I)lut(src(I) + d)

其中d的取值为:

d = { 0 if src has depth  CV_8U 128 if src has depth  CV_8S d= \begin{cases} 0& \text{if src has depth } \texttt{CV\_8U}\\ 128& \text{if src has depth } \texttt{CV\_8S} \end{cases} d={0128if src has depth CV_8Uif src has depth CV_8S

CV_8U和CV_8S的含义数据类型的宏定义):

  • CV_8U:8位无符号整数,取值范围(0,255)
  • CV_8S :8位有符号整数,取值范围(-128,127)

1.2 LUT函数在C++中的定义

void cv::LUT定义在core/src/lut.cpp,源码是C++的代码,我看不太懂,感兴趣的可以自己去阅读

2 cv2. LUT(src, lut, dst=None)实例

2.1 使用cv.LUT增加亮度

__Author__ = "Shliang"
__Email__ = "shliang0603@gmail.com"

import numpy as np
import cv2

def adjust_brightness(img_path, brightness_factor):

    img = cv2.imread(img_path)

    # clip(0, 255)会把处理后的像素值的大小,现在在[0, 255]范围内,如果有值大于255则取255,如果有值小于0则取值0
    table = np.array([i * brightness_factor for i in range (0,256)]).clip(0,255).astype('uint8')

    # 单通道img
    if img.shape[2] == 1:
        return cv2.LUT(img, table)[:,:,np.newaxis]
    # 多通道img
    else:
        result = cv2.LUT(img, table)
        # 左边原图、右边增加亮度后的图
        imgs_hstack = np.hstack((img, result))
        cv2.imwrite("adjust_brightness_result.png", imgs_hstack)
        cv2.imshow("result", imgs_hstack)
        cv2.waitKey(0)
        return result


if __name__ == '__main__':
    adjust_brightness("./img.png", brightness_factor=1.5)

下面是结果:左边是原图,右边是增加亮度后的图

在这里插入图片描述

2.2 正常增加亮度

  • alpha=1:图片的对比度不发生变化

  • alpah>1:时对比度增强,图像看起来更加清晰;

  • alpah<1:时对比度减弱,图像看起来变模糊;

  • beta>0:值越大,图像的亮度增加的越强烈

import numpy as np
import cv2


# 参考: https://blog.csdn.net/qq_33840601/article/details/90400375
def Contrast_and_Brightness(alpha, beta, img_path):
    img = cv2.imread(img_path)
    blank = np.zeros(img.shape, img.dtype)
    #
    # dst = alpha * img + (1-alpha) * blank + beta
    dst = cv2.addWeighted(img, alpha, blank, 1 - alpha, beta)
    imgs_hstack = np.hstack((img, dst))
    while True:
        cv2.imwrite("add_brightness_result.png", imgs_hstack)
        cv2.imshow("result", imgs_hstack)
        if cv2.waitKey(1) == ord("q"):
            break
    cv2.destroyAllWindows()

    return dst


if __name__ == '__main__':
    Contrast_and_Brightness(1, 100, "./img.png")

这种方式是把图片中的每个像素值都加上beta,因此,图片也是均匀的增加亮度,就像蒙上一层膜一样
在这里插入图片描述

参考:https://blog.csdn.net/Dontla/article/details/103963085
参考 # opencv官方文档对LUT API的解释
参考:https://vimsky.com/examples/detail/python-attribute-cv2.LUT.html # LUT的使用案例

欢迎大家关注笔者,你的关注是我持续更博的最大动力


原创文章,转载告知,盗版必究

微信:suihailiang0816
QQ:931762054
wx公众号:仰望星空的小随
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:白松林 返回首页