OpenCV错误:cv :: binary_op

时间:2017-11-07 19:18:37

标签: python python-3.x opencv

我已经看到这个问题被问到了,然而,答案并没有帮助解决我的问题,所以希望有人可以帮助我。我正在构建一个自适应皮肤检测器,因此我使用数据集来训练不同肤色的分类器。然后,我想使用我的网络摄像头来检测图像中的皮肤。我将预测的蒙版应用于相机框时出现以下错误:

OpenCV Error: Assertion failed ((mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1)) in cv::binary_op, file ..\..\..\modules\core\src\arithm.cpp, line 241
Traceback (most recent call last):
  File "skinDetectionBetter.py", line 80, in <module>
    skin = cv2.bitwise_and(gray, gray, mask=imgLabels)
cv2.error: ..\..\..\modules\core\src\arithm.cpp:241: error: (-215) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function cv::binary_op

我已经尝试调整我的蒙版imgLabels的大小以使用3个颜色通道(例如(480,640,3)),然后尝试调整大小以使用1个颜色通道(例如(480,640,1)),但是,我继续得到这个错误。据我所见,imgLabels和框架的形状均为(480,640,3)。这是我的代码片段。

camera = cv2.VideoCapture(0)

while True:
    # reads in the current frame
    # .read() returns True if frame read correctly, and False otherwise
    ret, frame = camera.read()

    if ret:
        # reshape the frame to follow format of training data (rows*col, 3)
        data = np.reshape(frame, (frame.shape[0] * frame.shape[1], 3))
        predictedLabels = classifier.predict(data)   # output:  [2 2 2 1 1 2 2 1 ...]  and shape: (480,640,3)
        predictedMask = (-(predictedLabels - 1) + 1) * 255

        # the AND operator applies the skinMask to the image
        imgLabels = np.resize(predictedMask, (frame.shape[0], frame.shape[1], 3))            # ERROR HAPPENS WITH BELOW LINE
        skin = cv2.bitwise_and(frame, frame, mask=imgLabels)

        # show the skin in the image along with the mask, show images side-by-side
        cv2.imshow("images", np.hstack([frame, skin]))

0 个答案:

没有答案
相关问题