2

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:

enter image description here

The result according to my code:

enter image description here

The result I want:

enter image description here

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:

enter image description here

2
  • You add/subtract colours with+= or -=. I believe that you're getting an overflow of integer values (e.g. adding 120 causes to go over 255). Commented Jun 12, 2021 at 10:31
  • @decadenza Thanks for your reply. As per your suggestion, I tried changing the value within the range but nothing happened ! can you check my PART-2 (edited code above). Where I tried with mask overlay for changing color like realistic but nothing happened !! Commented Jun 12, 2021 at 12:08

1 Answer 1

2

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.

enter image description here

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

enter image description here

if you change the values to be [0.3,1,1] it will be yellow.

enter image description here

and it will be red if the value is [0.3,0.1,1]

enter image description here

and it will be green bluish if the value is [1.5,1.0,0.8]

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

@abbs I appreciate your answer as well as suggestions. Can you please elaborate me these values -> [0.5,1,0.5] ? For mixing any color with rgb is it the right approach?
Another thing with this line i got error in my code I hade to change it into ones[np.where((mask_hair>0).all(axis = 2))] = [0.5,1,0.5]
@MSI basically I have factor for (RGB) values. so if I want the color to be green the green value must be the greatest. So if I multiply all R and B by 0.5 and G by one the color will become more green. Hope that answered your question. I would appreciate it if you can vote for my answer if you find my answer helpful.
Yes, it was! just a little inquiry. If I want it to turn in yellow is it possible?
basically the factors are for [B,G,R] so the dominant color shall be the greatest. see the examples I added
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.