python - OpenCV 错误 : bitwise_and throws error that mask and image are not same size

标签 python opencv image-processing

我正在尝试在 python (3.6.5) 中使用 openCV (3.3.1) 将我制作的蒙版应用于图像以提取所有皮肤。我正在循环查看一张照片并检查窗口并使用两个预制的 sklearm GMM 对它们进行分类。如果窗口是皮肤,我将蒙版的那个区域更改为 True (255),否则将其保留为 0。

我已经初始化了 numpy 数组以在循环之前将掩码保存为与图像相同的尺寸,但是 openCV 一直说图像和掩码没有相同的尺寸(输出和错误消息如下)。我在网站上看到过其他类似的问题,但没有一个解决方案对我有用。

这是我的代码:

# convert the image to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
delta = 6
# create an empty np array to make the mask
#mask = np.zeros((img.shape[0], img.shape[1], 1))
mask = np.zeros(img.shape[:2])
# loop through image and classify each window
for i in range(0,hsv.shape[0],delta):
    for j in range(0,hsv.shape[1],delta):
        # get a copy of the window
        arr = np.copy(hsv[i:i+delta,j:j+delta,0])
        # create a normalized hue histogram for the window
        if arr.sum() > 0:
            arr = np.histogram(np.ravel(arr/arr.sum()), bins=100, range=(0,1))
        else:
            arr = np.histogram(np.ravel(arr), bins=100, range=(0,1))
        # take the histogram and reshape it
        arr = arr[0].reshape(1,-1)
        # get the probabilities that the window is skin or not skin
        skin = skin_gmm.predict_proba(arr)
        not_skin = background_gmm.predict_proba(arr)
        if skin > not_skin:
            # becasue the window is more likely skin than not skin
            # we fill that window of the mask with ones
            mask[i:i+delta,j:j+delta].fill(255)
# apply the mask to the original image to extract the skin
print(mask.shape)
print(img.shape)
masked_img = cv2.bitwise_and(img, img, mask = mask)

输出是:

(2816, 2112)
(2816, 2112, 3)
OpenCV Error: Assertion failed ((mtype == 0 || mtype == 1) && 
_mask.sameSize(*psrc1)) in cv::binary_op, file C:\ci\opencv_1512688052760
\work\modules\core\src\arithm.cpp, line 241
Traceback (most recent call last):
File "skindetector_hist.py", line 183, in <module>
main()
File "skindetector_hist.py", line 173, in main
skin = classifier_mask(img, skin_gmm, background_gmm)
File "skindetector_hist.py", line 63, in classifier_mask
masked_img = cv2.bitwise_and(img, img, mask = mask)
cv2.error: C:\ci\opencv_1512688052760\work\modules\core\src
\arithm.cpp:241: error: (-215) (mtype == 0 || mtype == 1) && 
_mask.sameSize(*psrc1) in function cv::binary_op

如您在输出中所见,图像和蒙版具有相同的宽度和高度。我也试过使蒙版的深度为 1(第 5 行),但这没有帮助。感谢您的帮助!

最佳答案

不仅提示面具的大小。它提示面具的类型。错误:

OpenCV Error: Assertion failed ((mtype == 0 || mtype == 1) && _mask.sameSize(*psrc1))

表示掩码的类型或大小(在您的情况下是相等的)不相同。在documentation我们看到:

mask – optional operation mask, 8-bit single channel array, that specifies elements of the output array to be changed.

这与要求类型 0 (CV_8U) 或 1 (CV_8S) 的错误一致。

另外,即使没有说明,img 也不应该是 float 的,因为它不会给出预期的结果(可能它无论如何都会这样做)。

解决方案可能足以更改:

mask = np.zeros(img.shape[:2])

mask = np.zeros(img.shape[:2], dtype=np.uint8)

一个小测试显示您将获得什么类型:

np.zeros((10,10)).dtype

给你 dtype('float64') 这意味着加倍而不是 8 位

关于python - OpenCV 错误 : bitwise_and throws error that mask and image are not same size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50957151/

相关文章:

c++ - OpenCV 访问 MAT 对象中的 RGB 值

c++ - 将像素值写入空白矩阵

python - 使用 Python 在图像验证码中删除行

python - Lightroom 导出后,PIL 和 pyexiv2 中缺少图像标题标签

python - 使用 pulp cbc 求解器时,我可以设置约束的优先级吗?

c++ - Visual Studio 2010 - 创建便捷静态库 OpenCv

java - java平台下如何使用opencv计算HSV直方图?

python - 使用 Python 发送 Outlook 电子邮件的方法差异

python - Python 中的循环模式

python - 最 'Pythonic'处理重载的方法