使用OpenCV在视频上绘制拖尾线的步骤如下:
import cv2
import numpy as np
video = cv2.VideoCapture('video.mp4')
canvas = np.zeros((480, 640, 3), dtype=np.uint8)
line_color = (0, 255, 0) # 绿色
line_thickness = 2
while True:
ret, frame = video.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if previous_frame is None:
previous_frame = gray
continue
frame_delta = cv2.absdiff(previous_frame, gray)
thresh = cv2.threshold(frame_delta, 30, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
if cv2.contourArea(contour) < 500:
continue
(x, y, w, h) = cv2.boundingRect(contour)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
canvas = cv2.addWeighted(canvas, 0.9, 0)
canvas = cv2.line(canvas, (x, y), (x + w, y + h), line_color, line_thickness)
output = cv2.add(frame, canvas)
cv2.imshow("Video", output)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()
这样,就可以使用OpenCV在视频上绘制拖尾线了。
推荐的腾讯云相关产品:无
参考链接:
领取专属 10元无门槛券
手把手带您无忧上云