replace mask with original image opencv Python

徘徊边缘 提交于 2019-12-12 04:45:27

问题


I am trying to replace objects which I found using a mask with the original images pixels. I have a mask that shows black where the object is not detected and white if detected. I am then using the image in a where statement

image[np.where((image2 == [255,255,255].any(axis = 2)) 

I am stuck here and I have no idea how to change found white values to what the original image is (to use alongside other masks). I have tried image.shape and this did not work.

Thanks.


回答1:


Make a copy of the mask and then draw the original image over the white pixels of the mask from the white pixel coordinates. You can also check mask == 255 to compare element-wise. You don't need np.where because you can index arrays via the boolean mask created by mask == 255.

out = mask.copy()
out[mask == 255] = original_image[mask == 255]



回答2:


You can use bitwise operations. Try this:

replaced_image = cv2.bitwise_and(original_image,original_image,mask = your_mask)

Visit https://docs.opencv.org/3.3.0/d0/d86/tutorial_py_image_arithmetics.html to learn more about bitwise operations



来源:https://stackoverflow.com/questions/47062216/replace-mask-with-original-image-opencv-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!