資源簡(jiǎn)介
基于opencv 實(shí)現(xiàn)的LK跟蹤算法,可以跟蹤攝像頭視頻或是視頻文件,效果很好。
代碼片段和文件信息
/*?Demo?of?modified?Lucas-Kanade?optical?flow?algorithm.
???See?the?printf?below?*/
#ifdef?_CH_
#pragma?package?
#endif
#ifndef?_EiC
#include?“cv.h“
#include?“highgui.h“
#include?
#include?
#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]?=?{00}?*swap_points;
char*?status?=?0;
int?count?=?0;
int?need_to_init?=?0;
int?night_mode?=?0;
int?flags?=?0;
int?add_remove_pt?=?0;
CvPoint?pt;
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(xy);
????????add_remove_pt?=?1;
????}
}
int?main(?int?argc?char**?argv?)
{
????CvCapture*?capture?=?0;
????
????capture?=?cvCaptureFromAVI(?argv[1]?);
????if(?!capture?)
????{
????????fprintf(stderr“Could?not?initialize?capturing...\n“);
????????return?-1;
????}
????printf(?“\tESC?-?quit?the?program\n“
????????????“\tr?-?auto-initialize?tracking\n“
????????????“\tc?-?delete?all?the?points\n“
????????????“\tn?-?switch?the?\“night\“?mode?on/off\n“
????????????“To?add/remove?a?feature?point?click?it\n“?);
????cvNamedWindow(?“LkDemo“?0?);
????cvSetMouseCallback(?“LkDemo“?on_mouse?0?);
????for(;;)
????{
????????IplImage*?frame?=?0;
????????int?i?k?c;
????????frame?=?cvQueryframe(?capture?);
????????if(?!frame?)
????????????break;
????????if(?!image?)
????????{
????????????/*?allocate?all?the?buffers?*/
????????????image?=?cvCreateImage(?cvGetSize(frame)?8?3?);
????????????image->origin?=?frame->origin;
????????????grey?=?cvCreateImage(?cvGetSize(frame)?8?1?);
????????????prev_grey?=?cvCreateImage(?cvGetSize(frame)?8?1?);
????????????pyramid?=?cvCreateImage(?cvGetSize(frame)?8?1?);
????????????prev_pyramid?=?cvCreateImage(?cvGetSize(frame)?8?1?);
????????????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?);
????????cvCvtColor(?image?grey?CV_BGR2GRAY?);
????????if(?night_mode?)
????????????cvZero(?image?);
????????
????????if(?need_to_init?)
????????{
????????????/*?automatic?initialization?*/
????????????IplImage*?eig?=?cvCreateImage(?cvGetSize(grey)?32?1?);
????????????IplImage*?temp?=?cvCreateImage(?cvGetSize(grey)?32?1?);
????????????double?quality?=?0.01;
????????????double?min_distance?=?10;
????????????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
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???19246080??2010-11-24?15:48??LKDemo\Debug\liu.avi
?????文件????2262016??2007-07-08?17:35??LKDemo\Debug\lkdemo.bsc
?????文件?????196675??2007-08-21?11:04??LKDemo\Debug\lkdemo.exe
?????文件?????220544??2007-08-21?11:04??LKDemo\Debug\lkdemo.ilk
?????文件??????25457??2007-08-21?11:04??LKDemo\Debug\lkdemo.obj
?????文件????3103468??2007-08-21?11:04??LKDemo\Debug\lkdemo.pch
?????文件?????476160??2007-08-21?11:04??LKDemo\Debug\lkdemo.pdb
?????文件??????????0??2007-07-08?17:35??LKDemo\Debug\lkdemo.sbr
?????文件??????50176??2010-12-29?11:01??LKDemo\Debug\vc60.idb
?????文件??????77824??2007-08-21?11:04??LKDemo\Debug\vc60.pdb
?????文件?????432700??2007-02-24?15:18??LKDemo\Debug\video1.avi
?????文件???????5362??2010-12-29?11:01??LKDemo\lkdemo.c
?????文件???????3484??2007-06-29?12:07??LKDemo\lkdemo.dsp
?????文件????????520??2007-06-29?12:07??LKDemo\lkdemo.dsw
?????文件??????58368??2010-12-29?13:13??LKDemo\lkdemo.ncb
?????文件?????349184??2010-12-29?13:13??LKDemo\lkdemo.opt
?????文件????????246??2010-12-02?20:11??LKDemo\lkdemo.plg
?????文件?????432700??2007-02-24?15:18??LKDemo\video1.avi
?????文件????????136??2009-12-25?15:01??LKDemo\說(shuō)明.txt
?????目錄??????????0??2010-12-29?09:57??LKDemo\Debug
?????目錄??????????0??2010-12-29?13:13??LKDemo
-----------?---------??----------?-----??----
?????????????26941100????????????????????21
評(píng)論
共有 條評(píng)論