www.diy567.com累了,去上面休息一下下,QQ空间,美文,非主流,网络日记,搞笑短信,祝福短信,热门短信,有意思啊
//---------------------------------------------------------------------
//
// File : main.cpp
// Project : klt tracker using CV library
// Date : Nov 2007
// Author : tulip toppo + abhishek rawat
// Description : KLT tracker (we have used open CV functions)
//---------------------------------------------------------------------
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <stdio.h>
#include <ctype.h>
#endif
IplImage *image = 0, *grey = 0, *prev_grey = 0, *pyramid = 0, *prev_pyramid = 0, *swap_temp;
int win_size = 10;
const int MAX_COUNT = 500;
CvPoint2D32f* points[2] = {0,0}, *swap_points;
char* status = 0;
int count = 0;
int need_to_init = 0;
int flags = 0;
int add_remove_pt = 0;
float optical_flow_error[2][400];
float optical_flow_errorP[2][400];
float errX[400];
float errY[400];
CvPoint pt;
static int flagg=0;
void on_mouse( int event, int x, int y, int flags, void* param )
{
if( !image )
return;
if( image->origin )
y = image->height - y;
if( event == CV_EVENT_LBUTTONDOWN )
{
pt = cvPoint(x,y);
add_remove_pt = 1;
flagg=1;
}
}
int main( int argc, char** argv )
{
FILE *ptr;
ptr=fopen("dataerr.dat","w+");
CvCapture* capture = 0;
int counter1=0;
IplImage* image2 = 0;
float sumX=0;
float sumY=0;
float err_X;
float err_Y;
int XX=0;
int YY=0;
CvPoint ipt1;
int tempxx1=0;
int tempyy1=0;
int tempxx2=0;
int tempyy2=0;
char *imgFmt="pgm";
char str1[100];
/* Initailize the error array */
for(int kk=0;kk<=400;kk++)
{
optical_flow_error[0][kk]=0;
optical_flow_errorP[0][kk]=0;
optical_flow_error[1][kk]=0;
optical_flow_errorP[1][kk]=0;
}
//capturing frame from video
capture = cvCaptureFromAVI("soccer_track.mpeg");
cvNamedWindow( "KLT-Tracking Group_R", 0 );
cvSetMouseCallback( "KLT-Tracking Group_R", on_mouse, 0 );
if(add_remove_pt==1)
{
flagg=1;
}
for(;;)
{
IplImage* frame = 0;
int i, k, c;
//creating file name
sprintf(str1,"%d.%s",counter1,imgFmt);
err_X=0;
err_Y=0;
sumX=0;
sumY=0;
//decompressing the grab images
frame = cvQueryFrame( capture );
if( !frame )
break;
if( !image )
//The first frame:to allocation some memories,and do somen initialization work
{
// allocate all the image buffers
image = cvCreateImage( cvGetSize(frame), 8, 3 );
image->origin = frame->origin;
grey = cvCreateImage( cvGetSize(frame), 8, 1 );//make it grey
prev_grey = cvCreateImage( cvGetSize(frame), 8, 1 );//the previous frame in grey mode
pyramid = cvCreateImage( cvGetSize(frame), 8, 1 );//pyramid frame
prev_pyramid = cvCreateImage( cvGetSize(frame), 8, 1 );//previous pyramid frame
/* Define two pointers */
points[0] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));
points[1] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));
status = (char*)cvAlloc(MAX_COUNT);
flags = 0;
}
cvCopy( frame, image, 0 );//frame->image
//converting the image into gray scale for further computation
cvCvtColor( image, grey, CV_BGR2GRAY );
if( need_to_init )
{
IplImage* eig = cvCreateImage( cvGetSize(grey), 32, 1 );
IplImage* temp = cvCreateImage( cvGetSize(grey), 32, 1 );
double quality = 0.01;
double min_distance = 10;
//using good features to track
count = MAX_COUNT;
cvGoodFeaturesToTrack( grey, eig, temp, points[1], &count,
quality, min_distance, 0, 3, 0, 0.04 );
cvFindCornerSubPix( grey, points[1], count,
cvSize(win_size,win_size), cvSize(-1,-1),
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));
cvReleaseImage( &eig );
cvReleaseImage( &temp );
add_remove_pt = 0;
}
else if( count > 0 )
{
//using pyramidal optical flow method
cvCalcOpticalFlowPyrLK(
prev_grey, grey,
prev_pyramid, pyramid,
points[0], points[1],
count, cvSize(win_size,win_size),
5, status,0,
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03), flags );
flags |= CV_LKFLOW_PYR_A_READY|CV_LKFLOW_PYR_B_READY;
for( i = k = 0; i < count; i++ )
{
/* When need to add or remove the point */
if( add_remove_pt )
{
double dx = pt.x - points[1][i].x;
double dy = pt.y - points[1][i].y;
/* Calulate the distance between the point you select and the point tracked
if they are far from less than 5,stop the add or move action
*/
if( dx*dx + dy*dy <= 25 )
{
add_remove_pt = 0;
continue;
}
}
if( !status[i] )//if the point is not tracked correctly,pass!
continue;
points[1][k++] = points[1][i];
ipt1=cvPointFrom32f(points[1][i]);//get a point
//calculating error here,initalize the error array
optical_flow_error[0][i]=ipt1.x;
optical_flow_error[1][i]=ipt1.y;
}
//taking average error for moving the window
for(int zz=0; zz<=count;zz++)
{
errX[zz]=optical_flow_error[0][zz]- optical_flow_errorP[0][zz];
errY[zz]=optical_flow_error[1][zz]- optical_flow_errorP[1][zz];
sumX=sumX+errX[zz];
sumY=sumY+errY[zz];
optical_flow_errorP[0][zz]=optical_flow_error[0][zz];
optical_flow_errorP[1][zz]=optical_flow_error[1][zz];
}
fprintf(ptr,"%d\n",count);
err_X=sumX/count;
err_Y=sumY/count;
if(flagg==1)
{
int static startonce=0;
if(startonce==0)
{
tempxx1=pt.x-20;
tempyy1=pt.y-20;
tempxx2=pt.x+20;
tempyy2=pt.y+20;
XX=pt.x;
YY=pt.y;
startonce=1;
}
if(err_X<3)
{
tempxx1=tempxx1+err_X;
tempyy1=tempyy1+err_Y;
tempxx2=tempxx2+err_X;
tempyy2=tempyy2+err_Y;
XX=XX+err_X;
YY=YY+err_Y;
fprintf(ptr,"%f %f\n",err_X,err_Y);
}
printf("\n%f",err_X);
//moving window
cvRectangle(image, cvPoint(tempxx1,tempyy1), cvPoint(tempxx2,tempyy2), cvScalar(255,0,0), 1);
cvCircle(image, cvPoint(XX,YY), 3, cvScalar(0,0,255), 1);
}
count = k;
}
if( add_remove_pt && count < MAX_COUNT )
{
points[1][count++] = cvPointTo32f(pt);
cvFindCornerSubPix( grey, points[1] + count - 1, 1,
cvSize(win_size,win_size), cvSize(-1,-1),
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));
add_remove_pt = 0;
}
CV_SWAP( prev_grey, grey, swap_temp );
CV_SWAP( prev_pyramid, pyramid, swap_temp );
CV_SWAP( points[0], points[1], swap_points );
need_to_init = 0;
//writing image file to the file
//if(!cvSaveImage(str1,image)) printf("Could not save: %s\n",str1);
//storing in a video also
cvShowImage( "KLT-Tracking Group_R", image );
c = cvWaitKey(100);
if( (char)c == 27 )
break;
switch( (char) c )
{
case 's':
need_to_init = 1;
}
counter1++;
}
cvReleaseCapture( &capture );
cvDestroyWindow("KLT-Tracking Group_R");
fcloseall();
return 0;
}
分享到:
相关推荐
Fuzzy Logic模糊逻辑边缘提取,LK光流算法动作跟踪程序,LK算法特征点运动跟踪,轮廓检测与重绘,眼部识别算法实现,基于轮廓寻找的视频流运动检测 打包下载.有需要的自己下.
opencv基于LK光流的跟踪算法demo,vs2010直接打开
基于LK光流法实现跟踪关键点C++源码+项目说明.zip基于LK光流法实现跟踪关键点C++源码+项目说明.zip基于LK光流法实现跟踪关键点C++源码+项目说明.zip基于LK光流法实现跟踪关键点C++源码+项目说明.zip基于LK光流法实现...
基于LK光流算法的C语言实现,代码测试部分基于opencv,代码工程基于Qt5
LK稀疏光流Demo可以在场景中自动找出关键点并自动跟踪,也可以使用鼠标在指定位置添加关键点,此程序是很好的学习光流的示例。本程序基于OpenCV。
python opencv sift 手动区域光流跟踪
OpenCV运动目标跟踪光流法工程,调试通过-OpticalFlow OpenCV
经典的opencv环境下光流算法的实现,希望对您的学习有所帮助!
C++开发基于LK光流法代替ORB-SLAM2中的特征点提取匹配法来跟踪关键点源码+使用说明.zip C++开发基于LK光流法代替ORB-SLAM2中的特征点提取匹配法来跟踪关键点源码+使用说明.zip C++开发基于LK光流法代替ORB-SLAM2中的...
基于opencv的光流算法实现以及光流图显示,代码工程基于Qt5
基于opencv 实现的LK跟踪算法,可以跟踪摄像头视频或是视频文件,效果很好。
美国斯坦福stavens 07年编译的lk光流算法,附有ppt讲解
用opencv读取视频帧,解压并返回这一帧 然后进行光流法处理
《OpenCV教程——基础篇》刘瑞祯 实例源代码。有注释,project,范例代码齐全。
光流算法的假设条件是亮度恒定,就是同一点随着时间的变化,其亮度不会发生改变,用于得到光流法基本方程;基于opencv实现,可实现最小障碍检测光流算法。
基于vs2010和opencv环境的光流算法
赵建峰光流简介.ppt: ppt file for introduction 赵建峰光流简介.pdf: you can read this file if you can't open ppt(I am so sorry about this) myprog: my programmer, you can see the detail in this directory
光流金字塔 calcOpticalFlowPyrLK()函数参数说明: void calcOpticalFlowPyrLK( InputArray prevImg, //第一个8位输入图像或者通过 buildOpticalFlowPyramid()建立的金字塔 InputArray nextImg,//第二个输入图像...
OpenCV中的光流算法实现:C++与MATLAB代码下载.zip
基于KLT光流法实现对视频中的运动目标进行跟踪,包含《learning opencv3》源码实现及《opencv3 cookbook》的简化实现版本。