[英]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.首先从主图像中分割头发并试图使其成为一个真实的改变 HSV 值的头发,但根据我的代码,结果不是我正在寻找的准确的 output。 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 result according to my code:根据我的代码的结果:
PART- 2第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 mask_clr -->绿色掩码
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 .首先,我使用此代码获取掩码https://docs.opencv.org/3.4/da/d97/tutorial_threshold_inRange.ZFC35FDC70D5FC69D269883A822C7A53 。 This code uses inRange function.此代码使用范围 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.如果您将值更改为 [0.3,1,1],它将是黄色的。
and it will be red if the value is [0.3,0.1,1]如果值为 [0.3,0.1,1],它将是红色的
and it will be green bluish if the value is [1.5,1.0,0.8]如果值为 [1.5,1.0,0.8],它将是绿色的蓝色
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.