OPENCV中的基于ROI的KLT光学跟踪器
ROI-based KLT optical tracker in opencv
如何在lkdemo.pp(klt optical flow tracker openCV示例)源代码中添加基于ROI的选择?我想在第一个帧和轨道特征点中选择ROI中选择的ROI。
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
static void help()
{
// print a welcome message, and the OpenCV version
cout << "nThis is a demo of Lukas-Kanade optical flow lkdemo(),n"
"Using OpenCV version " << CV_VERSION << endl;
}
Point2f point;
bool addRemovePt = false;
static void onMouse( int event, int x, int y, int , void* )
{
if( event == CV_EVENT_LBUTTONDOWN )
{
point = Point2f((float)x, (float)y);
addRemovePt = true;
}
}
int main( int argc, char** argv )
{
help();
VideoCapture cap(CV_CAP_ANY);
TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
Size subPixWinSize(10,10), winSize(61,61);
const int MAX_COUNT = 500;
bool needToInit = false;
bool nightMode = false;
//if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
//cap.open(argc == 2 ? argv[1][0] - '0' : 0);
//else if( argc == 2 )
//cap.open(argv[1]);
if( !cap.isOpened() )
{
cout << "Could not initialize capturing...n";
return 0;
}
namedWindow( "LK Demo", 1 );
setMouseCallback( "LK Demo", onMouse, 0 );
Mat gray, prevGray, image;
vector<Point2f> points[2];
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() )
break;
frame.copyTo(image);
cvtColor(image, gray, COLOR_RGB2GRAY);
if( nightMode )
image = Scalar::all(0);
if( needToInit )
{
// automatic initialization
goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
addRemovePt = false;
}
else if( !points[0].empty() )
{
vector<uchar> status;
vector<float> err;
if(prevGray.empty())
gray.copyTo(prevGray);
calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,10, termcrit, 0, 0.001);
size_t i, k;
for( i = k = 0; i < points[1].size(); i++ )
{
if( addRemovePt )
{
if( norm(point - points[1][i]) <= 5 )
{
addRemovePt = false;
continue;
}
}
if( !status[i] )
continue;
points[1][k++] = points[1][i];
circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
}
points[1].resize(k);
}
if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
{
vector<Point2f> tmp;
tmp.push_back(point);
cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
points[1].push_back(tmp[0]);
addRemovePt = false;
}
needToInit = false;
imshow("LK Demo", image);
char c = (char)waitKey(10);
if( c == 27 )
break;
switch( c )
{
case 'r':
needToInit = true;
break;
case 'c':
points[0].clear();
points[1].clear();
break;
case 'n':
nightMode = !nightMode;
break;
}
std::swap(points[1], points[0]);
cv::swap(prevGray, gray);
}
return 0;
}
这是我在这些情况下使用的:
void SelectNewTemplate(int event, int posx, int posy, int flags, void* userdata)
{
if( event == EVENT_MBUTTONDOWN )
{
waitKey();
}
if( event == CV_EVENT_LBUTTONDOWN )
{
x1pt = posx;
y1pt = posy;
}
if( event == CV_EVENT_LBUTTONUP )
{
x2pt = posx;
y2pt = posy;
Rect newTemp(x1pt, y1pt, (x2pt - x1pt), (y2pt - y1pt));
Mat imgROI = frame(newTemp);
}
}
用法:使用中间鼠标按钮暂停视频,然后左clic,拖动并放开,按任何键继续前进。
之后,您可以在新的ROI映像上计算您的功能:imgROI
。
希望有帮助,托马斯
相关文章:
- 光线跟踪器灯光反射错误
- 人脸跟踪arduino代码的优化
- 跟踪滚动条上的鼠标事件
- 如何使用新运算符跟踪在循环中创建的 QLabel
- C++:跟踪类对象
- 用于解析 win64 堆栈跟踪的命令行客户端(可以访问符号服务器)
- 跟踪日志中的T.11803()是什么意思?
- 我能做些什么来跟踪矢量元素?
- 两个进程可以通过跟踪附加到同一个 PID 吗?
- 提升堆栈跟踪不显示函数名称和行号
- cygwin_exception::open_stackdumpfile:将堆栈跟踪转储到 class4.exe.sta
- 如何按指针查看堆栈跟踪
- WinDbg 不显示某些小型转储文件的完整堆栈跟踪
- C++ MFC 跟踪事件调用
- 是否可以/希望创建不可复制的共享指针模拟(以启用weak_ptr跟踪/借用类型语义)?
- 使用 Dijkstra 算法跟踪两个节点之间的最短路径
- 发布代码的 gdb 堆栈跟踪可读性如何影响 x64?
- MATLAB:跟踪imufilter对象中的状态变化
- mingw32-make 使用"MinGW Makefiles"生成器跟踪 CMAKE 无法将可执行文件链接到对象库
- OPENCV中的基于ROI的KLT光学跟踪器