TypeError: Expected Ptr<cv::UMat> for argument 'src2'

import cv2
import numpy as np

cap = cv2.VideoCapture('people.mp4')

ret,frame1=cap.read()
ret,frame2=cap.read()
print(frame1.shape)
print(frame2.shape)

#cv2.imshow("frame2",frame2)

while cap.isOpened():
    diff = cv2.absdiff(frame1, frame2)
    gray = cv2.cvtColor(diff,cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray,(5,5),0)
    _,thres=cv2.threshold(blur,20,255,cv2.THRESH_BINARY)
    dilated=cv2.dilate(thres,None,iterations=3)
    contours,_=cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    for contour in contours:
        (x, y, w, h) = cv2.boundingRect(contour)
        if cv2.contourArea(contour) < 1000:
            continue
        cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 255, 0), 2)
        cv2.putText(frame1, "Status: {}".format('Movement'), (10, 20), cv2.FONT_HERSHEY_SIMPLEX,   1, (0, 0, 255), 3)
    #cv2.drawContours(frame1,contours,-1,(0,255,0),2)
    image = cv2.resize(frame1, (1280,720))
    cv2.imshow("feed", frame1)
    
    frame1=frame2
    frame2=cap.read()
    if cv2.waitKey(40)==27:

        break

cv2.destroyAllWindows()
cap.release()

This is code but getting

diff = cv2.absdiff(frame1, frame2)

TypeError: Expected Ptrcv::UMat for argument ‘src2’

opencv version ? could you add the complete stacktrace (error), please ? thank you.

also, please explain, what you are trying to achieve here. what is it for ?

Thank you.My openCV version is 4.5.1.I am trying to detect motion and track them
Comple stacktree

please print out array shapes before applying absdiff

(and please NEVER post images of code or errors ANYWHERE, thank you)

((you also should not spam the bug tracker with problems in your code, please close that))

you ABSOLUTELY HAVE TO CHECK if the images you get from the camera are valid.
failing to do so will throw exeptions from the next operations
(typical python, noob problem, they never do this)

every movie file will come to an end, and VideoCapture will deliver empty images then.
(you have to break out of the loop, if you find one)

Sorry I am new to forums…The array sizes are same
(1080, 1920)
(1080, 1920) when i printed them

but not for the last frame of the movie

again, please add checks, and maybe use a more stable logic, like:

cap = cv2.VideoCapture('people.mp4')

gray1, gray2 = None,None
while cap.isOpened():
    ret, frame = cap.read()
    if not ret: # !!
        break 
    gray1 = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    if gray2: # skip 1st iteration
         diff = cv2.absdiff(gray1, gray2)
         # blur, threshold, dilate, contours, etc
    gray2 = gray1
    

oh, and btw, 4.5.2 will throw a much clearer error msg:

cv2.error: OpenCV(4.5.2-pre) :-1: error: (-5:Bad argument) in function 'absdiff'
> Overload resolution failed:
>  - src2 is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'src2'

I resolved it sir.I kept frame1 and frame2 in while loop so no error is raised Thank u for your effort

1 Like