I'm trying to replace color of an object in an image. Suppose if I want to replace the color in the below image object to the color in the below image object
I change this color using CAD tool, I want to achieve this using python code I'm replacing HUE color of an Image as below code
import cv2
import numpy as np
# read image
img = cv2.imread(r"C:\Research\Replace_colors\Bright\Test\b2\img_1_2_pid_13_group_18_P50_A174.png")
# convert img to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h = hsv[:,:,0]
s = hsv[:,:,1]
v = hsv[:,:,2]
# shift the hue
# cv2 will clip automatically to avoid color wrap-around
huechange = 10 # 0 is no change; 0<=huechange<=180
hnew = cv2.add(h, huechange)
# combine new hue with s and v
hsvnew = cv2.merge([hnew,s,v])
# convert from HSV to BGR
result = cv2.cvtColor(hsvnew, cv2.COLOR_HSV2BGR)
# save result
cv2.imwrite('result.png', result)
I was able to get color close to my required color but not exact color as I wanted
How can I change HUE value so I can replace color exactly ??
Any help or suggestion on this will be very helpful
CodePudding user response:
Are you sure you are using the right value for huechange and you don't need to change the value channel? When I use a color picker on the large flat part to the top right I get the following HSV values:
Original: (51, 100%, 56.1%)
Modified: (70, 100%, 56.1%)
Desired: (60, 100%, 28.2%)
