0

I want to Motion tracking of feature points and dense optical flow for video "animal.avi" I wrote this code.

import cv2
import numpy as np
from google.colab.patches import cv2_imshow
cap = cv2.VideoCapture("animal.avi")

ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[...,1] = 255



while(1):
ret, frame2 = cap.read()
next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)

flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0)

mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
hsv[...,0] = ang*180/np.pi/2
hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)

cv2_imshow(rgb)
k = cv2.waitKey(30) & 0xff
if k == 27:
    break
elif k == ord('s'):
    cv2.imwrite('opticalfb.png',frame2)
    cv2.imwrite('opticalhsv.png',rgb)
prvs = next

cap.release()
cv2.destroyAllWindows()

But I get an error. error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

How can I solve this error? Thanks.

stefano
  • 1
  • 1
  • it's always the same reason. the file could not be read. **please** use the search function on the error message **before** asking. – Christoph Rackwitz Nov 01 '21 at 20:43
  • Does this answer your question? [OpenCV VideoCapture and error: (-215:Assertion failed) !\_src.empty() in function 'cv::cvtColor'](https://stackoverflow.com/questions/54121013/opencv-videocapture-and-error-215assertion-failed-src-empty-in-function) – Christoph Rackwitz Nov 01 '21 at 20:45
  • Are you sure about the source filepaths? Can you share images and/or `animal.avi`? – Ceopee Nov 03 '21 at 04:57

0 Answers0