logo资料库

opencv调整图像亮度对比度的示例代码.pdf

第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
资料共4页,全文预览结束
opencv调整图像亮度对比度的示例代码 调整图像亮度对比度的示例代码 本文通过实例代码给大家介绍了opencv调整图像亮度对比度,代码简单易懂,非常不错,具有一定的参考借鉴价 值,需要的朋友可以参考下 图像处理 图像处理 图像变换就是找到一个函数,把原始图像矩阵经过函数处理后,转换为目标图像矩阵.   可以分为两种方式,即像素级别的变换和区域级别的变换 Point operators (pixel transforms) Neighborhood (area-based) operators 像素级别的变换就相当于\(p_{after}(i,j) = f(p_{before}(i,j))\),即变换后的每个像素值都与变换前的同位置的像素值有个函数映射关系. 对比度和亮度改变 对比度和亮度改变 线性变换  线性变换   最常用的是线性变换.即\(g(i,j) = \alpha \cdot f(i,j) + \beta\) f(i,j)是原像素值,g(i,j)是变换后的像素值. \(\alpha\)调整对比度,\(\beta\)调整亮度.有时也称之为gain和bias参数. 对比度是什么?不就是"亮和暗的区别"吗?也就是像素值的大小的区别.那我乘以一个alpha系数,当alpha很大的时候就是放大 了这种亮度值的差异,也就是提高了对比度,当alpha很小时,也就是缩小了亮度的差异,也就是缩小了对比度. beta就更好理解了,直接在像素的亮度值上加上一个数,正数就是提高亮度,负数降低亮度. 看一下下面代码的示例: from __future__ import print_function from builtins import input import cv2 as cv import numpy as np import argparse # Read image given by user parser = argparse.ArgumentParser(description='Code for Changing the contrast and brightness of an image! tutorial.') parser.add_argument('--input', help='Path to input image.', default='lena.jpg') args = parser.parse_args() image = cv.imread(cv.samples.findFile(args.input)) if image is None: print('Could not open or find the image: ', args.input) exit(0) new_image = np.zeros(image.shape, image.dtype) alpha = 1.0 # Simple contrast control beta = 0 # Simple brightness control # Initialize values print(' Basic Linear Transforms ') print('-------------------------') try: alpha = float(input('* Enter the alpha value [1.0-3.0]: ')) beta = int(input('* Enter the beta value [0-100]: ')) except ValueError: print('Error, not a number') # Do the operation new_image(i,j) = alpha*image(i,j) + beta # Instead of these 'for' loops we could have used simply: # new_image = cv.convertScaleAbs(image, alpha=alpha, beta=beta) # but we wanted to show you how to access the pixels :) for y in range(image.shape[0]): for x in range(image.shape[1]): for c in range(image.shape[2]): new_image[y,x,c] = np.clip(alpha*image[y,x,c] + beta, 0, 255) cv.imshow('Original Image', image) cv.imshow('New Image', new_image) # Wait until user press some key cv.waitKey() 提示module 'cv2' has no attribute 'samples'的话要先安装pip install opencv-python==4.0.0.21. 执行:python change_brightness_contrast.py --input ./lights.jpeg
上图是alpha=2,beta=20的一个效果图. 非线性变换 线性变换有个问题,如上图,α=1.3 and β=40,提高原图亮度的同时,导致云几乎看不见了.如果要看见云的话,建筑的亮度又不够. 这个时候就引入了非线性变换. 称之为Gamma correction \(O = \left( \frac{I}{255} \right)^{\gamma} \times 255\) 与线性变换不同,对不同的原始亮度值,其改变强度是不同的,是非线性的.
在 γ<1的时候,会提高图片亮度.>1时,降低亮度. γ=0.4的变换效果图如上.可以看到云层及建筑变亮的同时还保持了对比度让图像依然清晰. 如果查看不同变换下的灰度直方图的话可以看到.中间是原图的灰度直方图,可以看到低亮度值的像素点很多. 左边是做了线性变换的,整体直方图产生了右移,并且在255处出现峰值.因为每个像素点都增加了亮度嘛.导致了白云和蓝天过于 明亮无法区分. 而右边做了gamma校正的图像亮度分布比较均匀,即使得低亮度值的部分得以加强,又不至于过度曝光使得白云无法区分. 实现Gamma correction的代码如下. lookUpTable = np.empty((1,256), np.uint8)
for i in range(256): lookUpTable[0,i] = np.clip(pow(i / 255.0, gamma) * 255.0, 0, 255) res = cv.LUT(img_original, lookUpTable) 其中cv.LUT就是个变换函数.从lookUpTable里找到变换关系,生成新的图像矩 阵.https://docs.opencv.org/master/d2/de8/group__core__array.html 参考:https://docs.opencv.org/master/d3/dc1/tutorial_basic_linear_transform.html 总结总结 以上所述是小编给大家介绍的opencv调整图像亮度对比度的示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留 言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持! 如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
分享到:
收藏