如何使用python-opencv调整图像的颜色?

时间:2019-07-17 07:14:52

标签: python colors computer-vision opencv3.0

我想做的就是调整图像,例如调整彩色电视机的色彩平衡。假定比率在(0,1)之间,其中0表示将图像调整为黑白,而1表示使用原始图像的颜色。我怎么用python-opencv做到这一点?

1 个答案:

答案 0 :(得分:1)

import numpy as np
from matplotlib.pyplot import imread
import matplotlib.pyplot as plt
import cv2


def color_balance(image, balance):
    image2 = np.zeros(image.shape)
    image2[:,:,0] = ((1 + 2*balance)*image[:,:,0] + (1 - balance)*image[:,:,1] + (1 - balance)*image[:,:,2])/3
    image2[:,:,1] = ((1 + 2*balance)*image[:,:,1] + (1 - balance)*image[:,:,0] + (1 - balance)*image[:,:,2])/3
    image2[:,:,2] = ((1 + 2*balance)*image[:,:,2] + (1 - balance)*image[:,:,0] + (1 - balance)*image[:,:,1])/3
    image2 = image2/255
    return image2


image = cv2.imread('./test.jpeg')
image = np.array(image).astype(int)
cv2.imshow("image", color_balance(image, 1))
cv2.waitKey(0)
cv2.imshow("image", color_balance(image, 0.5))
cv2.waitKey(0)
cv2.imshow("image", color_balance(image, 0))
cv2.waitKey(0)

color_balance函数中,如果balance = 1的每个像素的值将保持不变,如果balance = 0,则每个像素的值将是所有3个通道的平均值。