OpenCV基于稠密光流實現(xiàn)視頻跟蹤詳解
更新時間:2023年02月20日 08:49:05 作者:音視頻開發(fā)老舅
這篇文章主要為大家詳細介紹了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:當前幀單通道CV_8UC1圖像
- flow:輸出的光流數據
- pyr_scale:金字塔上下兩層的尺度關系
- levels:金字塔層數
- winsize:窗口大小
- iterations:迭代次數
- poly_n:像素領域大小,一般是5、7
- poly_sigma:高斯標準差一般是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.將當前幀灰度化
6.執(zhí)行稠密光流函數,并輸出光流數據
7.將光流數據繪制出來
8.顯示光流數據
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);//讀取第一幀數據
//轉灰度圖
cvtColor(frame,prev_gray,COLOR_BGR2GRAY);//將frame轉灰度圖賦值給前一幀
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、圖像演示

到此這篇關于OpenCV基于稠密光流實現(xiàn)視頻跟蹤詳解的文章就介紹到這了,更多相關OpenCV視頻跟蹤內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

