我正在尝试使用蒙版和OpenCV的bitwise_and从图像中减去背景。但是,出现以下错误:
错误:C:\ ci \ opencv_1512684736357 \ work \ modules \ core \ src \ arithm.cpp:241:错误:(-215)(mtype == 0 || mtype == 1)&& _mask.sameSize(* psrc1)在函数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)
Run Code Online (Sandbox Code Playgroud)
关于StackOverflow的一个问题(OpenCV Python错误:错误:(-215)(mtype == CV_8U || mtype == CV_8S)&& _mask.sameSize(* psrc1)在函数cv :: binary_op中)解决了相同的错误形状可能不匹配的问题。但是,同时检查我的面具和图像的形状,在我看来它们是否匹配,从而产生:
mask.shape
OUT: (100, 83)
img.shape
OUT: (100, 83, 3)
Run Code Online (Sandbox Code Playgroud)
我正在使用Python v3和OpenCV v2
问题不在于形状不匹配...在断言的第一部分中失败了:
mtype == 0 || mtype == 1
它表示掩码(mtype
)的类型应为0或1,即分别为CV_8U
和CV_8S
。
您正在使用:
mask = mask.astype('int')
Run Code Online (Sandbox Code Playgroud)
这意味着CV_32S
枚举值中键入4或。
解:
您可以使用np.uint8
或np.int8
假设您已完成import numpy as np
归档时间: |
|
查看次数: |
8018 次 |
最近记录: |