本文介绍了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:

我的代码如下:

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或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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:31