错误类型:cv2.error: OpenCV(3.4.1) D:\Build\OpenCV\opencv-3.4.1\modules\core\src\arithm.cpp:241: error: (-215) (mtype == 0 || mtype == 1) && _mask.sameSize(*psrc1) in function cv::binary_op
完整代码如下:
import numpy as np
import argparse
import cv2
image=cv2.imread("../image/lihao/hand.jpg")
rectangle = np.zeros(image.shape[0:2],dtype='uint8')
cv2.rectangle(rectangle, (25, 25), (275, 275), 255, -1)
cv2.imshow("Rectangle", rectangle)
skin=cv2.bitwise_and(image,image,mask=rectangle)
cv2.imshow("Rectangle", skin)
cv2.waitKey(0)
这时候掩膜rectangle和原图image都是uint8 uint8类型,如果没有红色高亮部分的代码,则image是uint8,
而掩膜rectangle是float64,这时候在执行bitwise_and(image,image,mask=rectangle)就会报错
若掩膜已经为float32,则通过rectangle=np.uint8(rectangle)来转换矩阵数据类型,然后在执行位运算