opencv3/C++實(shí)現(xiàn)光流點(diǎn)追蹤
光流金字塔
calcOpticalFlowPyrLK()函數(shù)參數(shù)說(shuō)明:
void calcOpticalFlowPyrLK( InputArray prevImg, //第一個(gè)8位輸入圖像或者通過(guò) buildOpticalFlowPyramid()建立的金字塔 InputArray nextImg,//第二個(gè)輸入圖像或者和prevImg相同尺寸和類(lèi)型的金字塔 InputArray prevPts, //二維點(diǎn)向量存儲(chǔ)找到的光流;點(diǎn)坐標(biāo)必須是單精度浮點(diǎn)數(shù) InputOutputArray nextPts,//輸出二維點(diǎn)向量(用單精度浮點(diǎn)坐標(biāo))包括第二幅圖像中計(jì)算的輸入特征的新點(diǎn)位置;當(dāng)OPTFLOW_USE_INITIAL_FLOW 標(biāo)志通過(guò),向量必須有和輸入一樣的尺寸。 OutputArray status, //輸出狀態(tài)向量(無(wú)符號(hào)char);如果相應(yīng)的流特征被發(fā)現(xiàn),向量的每個(gè)元素被設(shè)置為1,否則,被置為0. OutputArray err,//輸出錯(cuò)誤向量;向量的每個(gè)元素被設(shè)為相應(yīng)特征的一個(gè)錯(cuò)誤,誤差測(cè)量的類(lèi)型可以在flags參數(shù)中設(shè)置;如果流不被發(fā)現(xiàn)然后錯(cuò)誤未被定義(使用status(狀態(tài))參數(shù)找到此情形)。 Size winSize = Size(21,21), //在每個(gè)金字塔水平搜尋窗口的尺寸。 int maxLevel = 3,//最大金字塔層數(shù); 如果設(shè)置為0,則不使用金字塔(單層),如果設(shè)置為1,則使用兩個(gè)層次,依此類(lèi)推; 如果將金字塔傳遞給輸入,則算法將使用與金字塔一樣多的級(jí)別,但不超過(guò)maxLevel。 TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01),//指定迭代搜索算法的終止標(biāo)準(zhǔn)(指定的最大迭代次數(shù)criteria.maxCount或搜索窗口移動(dòng)小于criteria.epsilon) int flags = 0, //操作標(biāo)志 double minEigThreshold = 1e-4 //計(jì)算光流方程的2×2標(biāo)準(zhǔn)矩陣的最小特征值除以窗口中的像素?cái)?shù)量;如果這個(gè)值小于minEigThreshold,那么一個(gè)相應(yīng)的特征被過(guò)濾出來(lái),且它的光流不被處理,所以它允許去除壞點(diǎn)提升性能。 );
#include<opencv2/opencv.hpp>
using namespace cv;
//光流跟蹤
Mat frame, gray, pr_frame, pr_gray;
std::vector<Point2f> inPoints;
std::vector<Point2f> fpts[2];
void trackFeature();
int main()
{
VideoCapture capture;
capture.open(0);
if(!capture.isOpened())
{
printf("can not open the camear......\n");
return -1;
}
namedWindow("input", CV_WINDOW_AUTOSIZE);
namedWindow("output", CV_WINDOW_AUTOSIZE);
while (capture.read(frame))
{
cvtColor(frame, gray, COLOR_BGR2GRAY);
if (fpts[0].size() < 40)
{
imshow("input", frame);
std::vector<Point2f> features;
//角點(diǎn)檢測(cè)
goodFeaturesToTrack(gray, features, 300, 0.01, 10);
fpts[0].insert(fpts[0].end(), features.begin(), features.end());
inPoints.insert(inPoints.end(), features.begin(), features.end());
}
else
printf("object tracking......\n");
if (pr_gray.empty())
gray.copyTo(pr_gray);
trackFeature();
for (int i = 0; i < fpts[0].size(); i++)
circle(frame, fpts[0][i], 2, Scalar(0,255,0),2,8,0);
gray.copyTo(pr_gray);
frame.copyTo(pr_frame);
imshow("output", frame);
waitKey(1);
}
waitKey(0);
capture.release();
return 0;
}
void trackFeature()
{
std::vector<uchar> status;
std::vector<float> errors;
//計(jì)算稀疏特征集的光流
calcOpticalFlowPyrLK(pr_gray, gray, fpts[0], fpts[1], status, errors);
int k = 0;
for (int i = 0; i < fpts[1].size(); i++)
{
double dist = abs(fpts[0][i].x-fpts[1][i].x) + abs(fpts[0][i].y-fpts[1][i].y);
if (dist > 2 && status[i])
{
inPoints[k] = inPoints[i];
fpts[1][k++] = fpts[1][i];
}
}
inPoints.resize(k);
fpts[1].resize(k);
//繪制光流軌跡
RNG rng(0);
for (int i = 0; i < fpts[0].size(); i++)
{
Scalar color = Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255));
line(frame, inPoints[i], fpts[1][i], color,2);
circle(frame, fpts[1][i], 2, Scalar(0,255,255),2);
}
std::swap(fpts[1], fpts[0]);
}


以上這篇opencv3/C++實(shí)現(xiàn)光流點(diǎn)追蹤就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Opencv光流運(yùn)動(dòng)物體追蹤詳解
- python+opencv實(shí)現(xiàn)動(dòng)態(tài)物體追蹤
- 使用OpenCV實(shí)現(xiàn)檢測(cè)和追蹤車(chē)輛
- 如何用OpenCV -python3實(shí)現(xiàn)視頻物體追蹤
- opencv+arduino實(shí)現(xiàn)物體點(diǎn)追蹤效果
- Python+OpenCV實(shí)現(xiàn)實(shí)時(shí)眼動(dòng)追蹤的示例代碼
- OpenCV3.0+Python3.6實(shí)現(xiàn)特定顏色的物體追蹤
- 淺析Python+OpenCV使用攝像頭追蹤人臉面部血液變化實(shí)現(xiàn)脈搏評(píng)估
- OpenCV 顏色追蹤的示例代碼
- 圖文詳解OpenCV中光流以及視頻特征點(diǎn)追蹤
相關(guān)文章
零基礎(chǔ)學(xué)習(xí)C/C++需要注意的地方
這篇文章主要介紹了零基礎(chǔ)學(xué)習(xí)C/C++需要注意的地方,文中講解非常細(xì)致,供大家參考和學(xué)習(xí),想要學(xué)習(xí)C/C++的可以閱讀此文2020-06-06
C++ OpenCV實(shí)戰(zhàn)之文檔照片轉(zhuǎn)換成掃描文件
這篇文章主要為大家介紹一個(gè)C++?OpenCV的實(shí)戰(zhàn)——文檔照片轉(zhuǎn)換成掃描文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09
C語(yǔ)言使用順序表實(shí)現(xiàn)電話(huà)本功能
C/C++字符串與數(shù)字互轉(zhuǎn)的實(shí)現(xiàn)
淺談C語(yǔ)言中的sizeof()和strlen()的區(qū)別
C語(yǔ)言實(shí)現(xiàn)數(shù)組棧的代碼示例
C語(yǔ)言scanf語(yǔ)句吃掉回車(chē)或者空格問(wèn)題及解決
C語(yǔ)言實(shí)現(xiàn)職工工資管理系統(tǒng)的示例代碼
Qt實(shí)現(xiàn)TCP網(wǎng)絡(luò)編程

