训练数据需要对样本做增强处理,所以需要知道如何改编图片亮度以及对比度,本来以为OpenCV有自带的函数改变亮度以及对比度,结果发现木有,桑心
Python、C++改变图片亮度以及对比度方法都在官方文档中Changing the contrast and brightness of an image!
图片亮度以及对比度
f ( x ) = a ∗ g ( x ) + b f(x) = a*g(x) + b f(x)=a∗g(x)+b
其中 g ( x ) g(x) g(x)为原图像素点,而 f ( x ) f(x) f(x)为改变后的图片像素点, a a a和 b b b分别为调节图片的对比度以及亮度
from __future__ import print_function
import cv2 as cv
import numpy as np
image = cv.imread("demo.jpg")
new_image = np.zeros(image.shape, image.dtype)
alpha = 1.0 # contrast range [1.0-3.0]
beta = 0 # brightness range [0-100]
for y in range(image.shape[0]):
for x in range(image.shape[1]):
for c in range(image.shape[2]):
# np.clip是将值限定在某个范围内(0,255)
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(0)