OpenCV遮罩图像-错误:(-215)(mtype == 0 || mtype == 1)&&函数cv :: binary_op中的_mask.sameSize(* psrc1) [英] OpenCV Masking Image - error: (-215) (mtype == 0 || mtype == 1) && _mask.sameSize(*psrc1) in function cv::binary_op

查看:649
本文介绍了OpenCV遮罩图像-错误:(-215)(mtype == 0 || mtype == 1)&&函数cv :: binary_op中的_mask.sameSize(* psrc1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用蒙版和OpenCV的bitwise_and从图像中减去背景.但是,出现以下错误:

I am trying to subtract the background from an image using a mask and OpenCV's bitwise_and. However, I get the following error:

错误: C:\ ci \ opencv_1512684736357 \ work \ modules \ core \ src \ arithm.cpp:241: 错误:(-215)(mtype == 0 || mtype == 1)&& _mask.sameSize(* psrc1)在 函数cv :: binary_op

error: C:\ci\opencv_1512684736357\work\modules\core\src\arithm.cpp:241: error: (-215) (mtype == 0 || mtype == 1) && _mask.sameSize(*psrc1) in function cv::binary_op

我的代码如下:

mask = get_mask() #function that returns a mask (boolean)

#conversion of the mask
mask = mask.astype('int')
mask[mask == 0] = 255
mask[mask == 1] = 0

fg_masked = cv2.bitwise_and(img, img, mask=mask)

这里是关于StackOverflow的一个问题(

A question here on StackOverflow (OpenCV Python Error: error: (-215) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function cv::binary_op) that addresses the same error indicates an issue with a potential shape mismatch. However, checking both the shape of my mask and my image it appears to me that they match, yielding:

mask.shape
OUT: (100, 83)

img.shape
OUT: (100, 83, 3)

我正在使用Python v3和OpenCV v2

I am using Python v3 and OpenCV v2

推荐答案

问题不是形状不匹配...在断言的第一部分中失败了:

The problem is not a shape mismatch... it is failing in the first part of the assert:

mtype == 0 || mtype == 1

mtype == 0 || mtype == 1

它表示掩码的类型(mtype)应该为0或1,即分别为CV_8UCV_8S.

It says that the type of the mask (mtype) should be either 0 or 1, i.e. CV_8U and CV_8S respectively.

您正在使用:

mask = mask.astype('int')

这意味着在枚举值中键入CV_32S或4.

That means type CV_32S or 4 in the enum value.

解决方案:

假设已经完成import numpy as np

这篇关于OpenCV遮罩图像-错误:(-215)(mtype == 0 || mtype == 1)&&函数cv :: binary_op中的_mask.sameSize(* psrc1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆