OpenCV掩蔽图像 - 错误:(-215)(mtype == 0 || mtype == 1)&&函数cv :: binary_op中的_mask.sameSize(* psrc1)

时间:2018-04-22 19:56:16

标签: python opencv

我试图使用掩码和OpenCV的bitwise_and从图像中减去背景。但是,我收到以下错误:

  

错误:   C:\ CI \ opencv_1512684736357 \工作\模块\芯\ SRC \ arithm.cpp:241:   错误:(-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(OpenCV Python Error: error: (-215) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function cv::binary_op)上的一个解决同一错误的问题表明潜在的形状不匹配问题。但是,检查我的面具和我的图像的形状在我看来它们匹配,产生:

mask.shape
OUT: (100, 83)

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

我正在使用Python v3和OpenCV v2

1 个答案:

答案 0 :(得分:3)

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

  

mtype == 0 || mtype == 1

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

您正在使用:

mask = mask.astype('int')

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

解决方案:

假设您已完成np.uint8

,则可以使用np.int8import numpy as np
相关问题