简体   繁体   中英

Recolor image to other base color and keeping the gradients

I want to recolor an image to another base color but thereby keeping the gradients. For example in this picture the base color is green.

在此处输入图片说明

and I want to change this to base color Blue for example (RGB: 0,119,153). I used the following python code:

import numpy as np
import cv2
dg = [0,59,10]
mg = [0,91,16]
lg = [0,106,18]
db = [92,71,0]
mb = [153,119,0]
lb = [204,163,93]

im = cv2.imread("C:/Temp/recolor_source.png")
im[np.where((im == dg).all(axis=2))] = db
im[np.where((im == mg).all(axis=2))] = mb
im[np.where((im == lg).all(axis=2))] = lb
cv2.imwrite("C:/Temp/recolor_result.png", im)

which gives the following result:

在此处输入图片说明

There is still quite some green around the white text and in the grayed-out button in the resulting image. What is the right approach to convert to another base color and keeping all the gradients?

This is the answer as suggested by Mark Setchell.

import cv2

im = cv2.imread("C:/Temp/recolor_source.png")
hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
hchannel = hsv[:, :, 0]
hchannel = 40 + hchannel
hsv[:, :, 0] = hchannel
rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

cv2.imwrite("C:/Temp/recolor_result.png", rgb)

Resulting in: 在此处输入图片说明

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