Extraction part of image that matches to a mask in OpenCV

爷,独闯天下 提交于 2019-12-10 08:11:16

问题


In a OpenCV application with Python, i have a mask and an RGB image, i want to extract part of image that matches to mask but i dont know how.

for example this is a mask:

and i want to do like this:

i do this:

temp = cv2.bitwise_and(img ,img, mask=feature_map)

but it gives me and error:

cv2.error: /Users/mee/opencv/modules/core/src/arithm.cpp:1589: error: (-215) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function binary_op

回答1:


You can do something like:

mask = cv2.imread('mask.png',0)
im = cv2.imread('guy.png')
mask_inv =  255 - mask;
final_im = mask_inv*im



回答2:


You should convert your mask as being object mask pixels value is 255 and background pixels value is 0. After that you can apply and operation or multiply operation for each color channel of RGB image with new mask .



来源:https://stackoverflow.com/questions/34466597/extraction-part-of-image-that-matches-to-a-mask-in-opencv

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