Python opencv:1036:错误:(-215)mask.size == src1.size函数binary_op

时间:2016-03-24 20:51:18

标签: python-2.7 opencv

enter image description here您好我正在使用覆盆子pi 3运行下面的python opencv代码并使用USB摄像头。该程序跟踪人脸并在脸上覆盖蒙版图像。我的程序不断崩溃并显示以下错误:屏蔽人脸的图像也不是去除背景颜色而只是留下图像蒙版。希望你能帮忙。

:1036:错误:(-215)mask.size == src1.size in function binary_op

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml')

face_mask = cv2.imread('../images/mask_skull.png')
h_mask, w_mask = face_mask.shape[:2]

if face_cascade.empty():
    raise IOError('Unable to load the face cascade classifier xml file')

cap = cv2.VideoCapture(0)
scaling_factor = 0.5

while True:
  ret, frame = cap.read()
  frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor,      interpolation=cv2.INTER_AREA)
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

  face_rects = face_cascade.detectMultiScale(gray, 1.3, 5)
  for (x,y,w,h) in face_rects:
      if h > 0 and w > 0:
        h, w = int(1.4*h), int(1.0*w)
        y -= 0.1*h
        frame_roi = frame[y:y+h, x:x+w] 
        face_mask_small = cv2.resize(face_mask, (w, h), interpolation=cv2.INTER_AREA)

        gray_mask = cv2.cvtColor(face_mask_small, cv2.COLOR_BGR2GRAY)
        ret, mask = cv2.threshold(gray_mask, 180, 255, cv2.THRESH_BINARY_INV)
        mask_inv = cv2.bitwise_not(mask)
        masked_face = cv2.bitwise_and(face_mask_small, face_mask_small, mask=mask)
        masked_frame = cv2.bitwise_and(frame_roi, frame_roi, mask=mask_inv)
        frame[y:y+h, x:x+w] = cv2.add(masked_face, masked_frame)

cv2.imshow('Face Detector', frame)

c = cv2.waitKey(1)
if c == 27:
    break

cap.release()
cv2.destroyAllWindows()

0 个答案:

没有答案