01Dense Optical Flow in OpenCV (Python) 光流法 视频
Posted CV-杨帆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了01Dense Optical Flow in OpenCV (Python) 光流法 视频相关的知识,希望对你有一定的参考价值。
b站操作视频:https://www.bilibili.com/video/BV14V4y1V7mT/
Dense Optical Flow in OpenCV
import numpy as np
import cv2
cap = cv2.VideoCapture('./1.mp4')
#获取第一帧
ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
#遍历每一行的第1列
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)
#print(flow.shape)
print(flow)
#笛卡尔坐标转换为极坐标,获得极轴和极角
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('frame2',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()
python demo.py
参考
Optical Flow in OpenCV (C++/Python):https://learnopencv.com/optical-flow-in-opencv/
Optical Flow:https://docs.opencv.org/4.x/d4/dee/tutorial_optical_flow.html
python opencv入门 光流法(41):https://blog.csdn.net/tengfei461807914/article/details/80978947
以上是关于01Dense Optical Flow in OpenCV (Python) 光流法 视频的主要内容,如果未能解决你的问题,请参考以下文章