OpenCV基于稠密光流實現(xiàn)視頻跟蹤詳解
1、概述
案例:基于稠密光流的視頻跟蹤
API介紹:
calcOpticalFlowFarneback( InputArray prev, InputArray next, InputOutputArray flow,
double pyr_scale, int levels, int winsize,
int iterations, int poly_n, double poly_sigma,
int flags );
- prev:前一幀單通道CV_8UC1圖像
- next:當(dāng)前幀單通道CV_8UC1圖像
- flow:輸出的光流數(shù)據(jù)
- pyr_scale:金字塔上下兩層的尺度關(guān)系
- levels:金字塔層數(shù)
- winsize:窗口大小
- iterations:迭代次數(shù)
- poly_n:像素領(lǐng)域大小,一般是5、7
- poly_sigma:高斯標(biāo)準(zhǔn)差一般是1~1.5
- flags:計算方法:主要包括OPTFLOW_USE_INITIAL_FLOW和OPTFLOW_FARNEBACK_GAUSSIAN
實現(xiàn)步驟:
1.實例化VideoCapture
2.使用其open方法打開視頻文件
3.獲取視頻第一幀并得到其灰度圖(因為稠密光流輸入只支持單通道8位)
4.while(true)循環(huán)讀取視頻幀
5.將當(dāng)前幀灰度化
6.執(zhí)行稠密光流函數(shù),并輸出光流數(shù)據(jù)
7.將光流數(shù)據(jù)繪制出來
8.顯示光流數(shù)據(jù)
2、代碼示例
(ps:界面中的按鈕元素使用到了Qt)
HF_Object_Tracking::HF_Object_Tracking(QWidget *parent)
: MyGraphicsView{parent}
{
this->setWindowTitle("稠密光流對象跟蹤");
QPushButton *btn = new QPushButton(this);
btn->setText("選擇視頻");
connect(btn,&QPushButton::clicked,[=](){
choiceVideo();
});
}
void HF_Object_Tracking::choiceVideo(){
path = QFileDialog::getOpenFileName(this,"請選擇視頻","/Users/yangwei/Downloads/",tr("Image Files(*.mp4 *.avi)"));
qDebug()<<"視頻路徑:"<<path;
hfObjectTracking(path.toStdString().c_str());
}
void HF_Object_Tracking::hfObjectTracking(const char* filePath){
VideoCapture capture;
capture.open(filePath);
if(!capture.isOpened()){
qDebug()<<"視頻路徑為空";
return;
}
Mat frame,gray;
Mat prev_frame ,prev_gray;
Mat flowResult,flowData;
capture.read(frame);//讀取第一幀數(shù)據(jù)
//轉(zhuǎn)灰度圖
cvtColor(frame,prev_gray,COLOR_BGR2GRAY);//將frame轉(zhuǎn)灰度圖賦值給前一幀
while(capture.read(frame)){
cvtColor(frame,gray,COLOR_BGR2GRAY);
if(!prev_gray.empty()){
//稠密光流跟蹤
calcOpticalFlowFarneback(prev_gray,gray,flowData, 0.5, 3, 15, 3, 5, 1.2, 0);
cvtColor(prev_gray, flowResult, COLOR_GRAY2BGR);
for (int row = 0; row < flowResult.rows; row++) {
for (int col = 0; col < flowResult.cols; col++) {
const Point2f fxy = flowData.at<Point2f>(row, col);
if (fxy.x > 1 || fxy.y > 1) {
line(flowResult, Point(col, row), Point(cvRound(col + fxy.x), cvRound(row + fxy.y)), Scalar(0, 255, 0), 2, 8, 0);
circle(flowResult, Point(col, row), 2, Scalar(0, 0, 255), -1);
}
}
}
imshow("flow", flowResult);
imshow("input", frame);
}
// imshow("frame",frame);
int key = waitKey(1);
if(key==27){
break;
}
}
}3、圖像演示

到此這篇關(guān)于OpenCV基于稠密光流實現(xiàn)視頻跟蹤詳解的文章就介紹到這了,更多相關(guān)OpenCV視頻跟蹤內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Clion下載安裝使用的詳細(xì)教程(Win+MinGW)
這篇文章主要介紹了Clion下載安裝使用教程(Win+MinGW),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
C語言數(shù)據(jù)結(jié)構(gòu)與算法之鏈表(一)
鏈表是線性表的鏈?zhǔn)酱鎯Ψ绞?。鏈表的?nèi)存是不連續(xù)的,前一個元素存儲地址的下一個地址中存儲的不一定是下一個元素。小編今天就將帶大家深入了解一下鏈表,快來學(xué)習(xí)吧2021-12-12

