我正在尝试运行此代码。一开始它运行正常,但卡在中间并出现以下错误:
(-215:断言失败)函数'cv::cvtColor'中的!_src.empty()
我试过更改视频和路径名,但没有用。谁能解释为什么我不断收到此错误。
这是代码:-
import cv2
import numpy as np
cap = cv2.VideoCapture("E:\project vid\lena_jump.avi")
_, first_frame = cap.read()
first_gray = cv2.cvtColor(first_frame, cv2.COLOR_BGR2GRAY)
first_gray = cv2.GaussianBlur(first_gray, (5, 5), 0)
while True:
_, frame = cap.read()
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray_frame = cv2.GaussianBlur(gray_frame, (5, 5), 0)
difference = cv2.absdiff(first_gray, gray_frame)
_, difference = cv2.threshold(difference, 25, 255, cv2.THRESH_BINARY)
cv2.imshow("First frame", first_frame)
cv2.imshow("Frame", frame)
cv2.imshow("difference", difference)
key = cv2.waitKey(30)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()