Reputation: 125
Trying to recolor the hair using a mask. Firstly segmented hair from the main image & trying to make it a realistic one changing HSV value but according to my code the result is not the accurate output that i am looking for. Any solution?
img = cv2.imread('model-demo.jpg')
img = cv2.resize(img, (256, 256))
_, mask_hair = cv2.threshold(mask_hair, thresh=210, maxval=255, type=cv2.THRESH_BINARY)
#cutting specific portion of hair from image
cut_mask = np.invert(mask_hair)
res = np.where(cut_mask, 0, img)
#HSV value changing for new color
hsv = cv2.cvtColor(res, cv2.COLOR_BGR2HSV)
hsv[:,:,0] +=120
hsv[:,:,1] +=60
hsv[:,:,2] -=20
hsv = cv2.cvtColor(res, cv2.COLOR_HSV2RGB)
plt.imshow(hsv)
The image I have:
The result according to my code:
The result I want:
PART- 2
Also tried to multiply the image mask with color but end the end it doesn't even matter...
img = cv2.resize(cv2.imread('model-demo.jpg'),(256,256))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
mask = cv2.resize(cv2.imread('./mask.jpg'),(256,256))
mask_clr = np.zeros([256,256,3],dtype=np.uint8)
mask_clr[np.where((mask_hair==255).all(axis = 2))] = [0, 255, 0]
imgMultiply = cv2.multiply(img,mask_clr)
mask_clr --> green mask:
Upvotes: 2
Views: 1679
Reputation: 515
I have done a simple program for your project
first I used this code to get a mask https://docs.opencv.org/3.4/da/d97/tutorial_threshold_inRange.html . This code uses inRange function.
after that I used the following code to get the results
a = np.where(frame_threshold > 0)
ones = np.ones_like(img)
ones[a] = [0.5,1,0.5]
r = img*ones
The results I got
if you change the values to be [0.3,1,1] it will be yellow.
and it will be red if the value is [0.3,0.1,1]
and it will be green bluish if the value is [1.5,1.0,0.8]
Upvotes: 2