简体   繁体   中英

Bitwise_and mask error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'

Hello I tried to use bitwise_and from opencv with a camera but I recieved this error:

imgMask = cv2.bitwise_and(frame,frame,mask=mask) cv2.error: OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:230: error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'

import cv2
import numpy as np
captura = cv2.VideoCapture(0)

mask = np.zeros((480,640),dtype=np.uint8)
mask = cv2.circle(mask,(320,240),125,(255),-1)
#mask=cv2.bitwise_not(mask)
cv2.imshow('mask',mask)
while (captura.isOpened()):
  ret,frame = captura.read()
  frame = cv2.resize(frame,(480,640))
  
  if ret == True:
    imgMask = cv2.bitwise_and(frame,frame,mask=mask)
    cv2.imshow('video',imgMask)
    if cv2.waitKey(1) & 0xFF == ord('s'):
      break
  else: break
captura.release()
cv2.destroyAllWindows()

I think check your frame and mask shapes first. If they are same you can try this;

imgMask = cv2.bitwise_and(frame,frame,mask=mask[:,:,2])

in stead of

imgMask = cv2.bitwise_and(frame,frame,mask=mask)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM