简体   繁体   中英

How could I adjust color of an image with python-opencv?

What I would like to do is to adjust the images like adjust the color balance of a color TV. Suppose the ratio is between (0, 1), where 0 means to adjust the image to black and white while 1 means to use the color of the original image. How could I do that with python-opencv?

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)

in the color_balance function, if balance = 1 the value of each pixel will remain unchanged and if balance = 0 the value of each pixel will be the average of all 3 channels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM