简体   繁体   中英

How to recolor image object using mask?

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

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]

在此处输入图像描述

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