#include "StdAfx.h"
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
const int MAX_CORNERS = 50;
int main( int argc, char** argv )
{
IplImage*curr=NULL;
IplImage*diff=NULL;
IplImage*dst=NULL;
IplImage*pyr=NULL;
IplImage* eig_image=NULL;
IplImage* tmp_image=NULL;
IplImage**buf = 0;
CvCapture* pCapture = NULL;
cvNamedWindow("video",1);
double fps = 20;
CvSize size = cvSize(
(int)cvGetCaptureProperty( pCapture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty( pCapture, CV_CAP_PROP_FRAME_HEIGHT)
);
int first=0,last=1;
int i=0,idx1=last,idx2;
int N=2;
int nFrmNum=0;
int win_size=10;
int corner_count = MAX_CORNERS;
CvPoint2D32f* corner_idx1 = new CvPoint2D32f[ MAX_CORNERS ];
pCapture=cvCaptureFromFile("d://测试/input.avi");
CvVideoWriter*writer=cvCreateVideoWriter("d:\\测试\\video.avi",CV_FOURCC('X','V','I','D'),fps,size);
while(curr = cvQueryFrame( pCapture ))
{
nFrmNum++;
if(nFrmNum == 1)
{
if( !curr )
break;
if( !dst )
{
dst = cvCreateImage( cvSize(curr->width,curr->height), 8, 3 );
cvZero( dst );
dst->origin = curr->origin;
}
if( buf == 0 ) {
buf = (IplImage**)malloc(N*sizeof(buf[0]));
memset( buf, 0, N*sizeof(buf[0]));
}
for( i = 0; i < N; i++ ) {
cvReleaseImage( &buf[i] );
buf[i] = cvCreateImage( cvSize(curr->width,curr->height), IPL_DEPTH_8U, 1 );
cvZero( buf[i] );
}
IplImage*pyr=cvCreateImage(cvSize((curr->width&-2)/2,(curr->height&-2)/2),8,1);
eig_image = cvCreateImage( cvSize(curr->width,curr->height), IPL_DEPTH_32F, 1 );
tmp_image = cvCreateImage( cvSize(curr->width,curr->height), IPL_DEPTH_32F, 1 );
dst=cvCreateImage(cvSize(curr->width,curr->height),IPL_DEPTH_8U,3);
cvCvtColor(curr,buf[first],CV_BGR2GRAY);
}
else
{
cvCvtColor(curr, buf[last], CV_BGR2GRAY);
idx2 = (last + 1) % N; // index of (last - (N-1))th frame
last = idx2;
cvZero( dst );
cvMerge( buf[idx2], 0, 0, 0, dst );
cvGoodFeaturesToTrack(
buf[idx1],
eig_image,
tmp_image,
corner_idx1,
&corner_count,
0.01,
5.0,
0,
3,
0,
0.04
);
cvFindCornerSubPix(
buf[idx1],
corner_idx1,
corner_count,
cvSize(win_size,win_size),
cvSize(-1,-1),
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,10,0.3)
);
char features_found[ MAX_CORNERS ];
float feature_errors[ MAX_CORNERS ];
CvSize pyr_sz = cvSize( buf[idx1]->width+8, buf[idx2]->height/3 );
IplImage* image1 = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 );
IplImage* image2 = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 );
CvPoint2D32f* corner_idx2 = new CvPoint2D32f[ MAX_CORNERS ];
cvCalcOpticalFlowPyrLK(
buf[idx1],
buf[idx2],
image1,
image2,
corner_idx1,
corner_idx2,
corner_count,
cvSize( win_size,win_size ),
5,
features_found,
feature_errors,
cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, .3 ),
0
);
for( int i=0; i<corner_count; i++ ) {
if( features_found[i]==0|| feature_errors[i]>550 ) {
// printf("Error is %f/n",feature_errors[i]);
continue;
}
// printf("Got it/n");
CvPoint p0 = cvPoint(
cvRound( corner_idx1[i].x ),
cvRound( corner_idx1[i].y )
);
CvPoint p1 = cvPoint(
cvRound( corner_idx2[i].x ),
cvRound( corner_idx2[i].y )
);
if(corner_idx1[i].y=corner_idx2[i].y)
{
cvLine(dst, p0, p1, CV_RGB(255,0,0),2 );
}
}
cvShowImage("video", dst);
cvWriteFrame(writer,dst);
if( cvWaitKey(2) >= 0 )
break;
}
}
cvDestroyWindow("video");
cvReleaseVideoWriter(&writer);
cvReleaseCapture(&pCapture);
return 0;
}
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
const int MAX_CORNERS = 50;
int main( int argc, char** argv )
{
IplImage*curr=NULL;
IplImage*diff=NULL;
IplImage*dst=NULL;
IplImage*pyr=NULL;
IplImage* eig_image=NULL;
IplImage* tmp_image=NULL;
IplImage**buf = 0;
CvCapture* pCapture = NULL;
cvNamedWindow("video",1);
double fps = 20;
CvSize size = cvSize(
(int)cvGetCaptureProperty( pCapture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty( pCapture, CV_CAP_PROP_FRAME_HEIGHT)
);
int first=0,last=1;
int i=0,idx1=last,idx2;
int N=2;
int nFrmNum=0;
int win_size=10;
int corner_count = MAX_CORNERS;
CvPoint2D32f* corner_idx1 = new CvPoint2D32f[ MAX_CORNERS ];
pCapture=cvCaptureFromFile("d://测试/input.avi");
CvVideoWriter*writer=cvCreateVideoWriter("d:\\测试\\video.avi",CV_FOURCC('X','V','I','D'),fps,size);
while(curr = cvQueryFrame( pCapture ))
{
nFrmNum++;
if(nFrmNum == 1)
{
if( !curr )
break;
if( !dst )
{
dst = cvCreateImage( cvSize(curr->width,curr->height), 8, 3 );
cvZero( dst );
dst->origin = curr->origin;
}
if( buf == 0 ) {
buf = (IplImage**)malloc(N*sizeof(buf[0]));
memset( buf, 0, N*sizeof(buf[0]));
}
for( i = 0; i < N; i++ ) {
cvReleaseImage( &buf[i] );
buf[i] = cvCreateImage( cvSize(curr->width,curr->height), IPL_DEPTH_8U, 1 );
cvZero( buf[i] );
}
IplImage*pyr=cvCreateImage(cvSize((curr->width&-2)/2,(curr->height&-2)/2),8,1);
eig_image = cvCreateImage( cvSize(curr->width,curr->height), IPL_DEPTH_32F, 1 );
tmp_image = cvCreateImage( cvSize(curr->width,curr->height), IPL_DEPTH_32F, 1 );
dst=cvCreateImage(cvSize(curr->width,curr->height),IPL_DEPTH_8U,3);
cvCvtColor(curr,buf[first],CV_BGR2GRAY);
}
else
{
cvCvtColor(curr, buf[last], CV_BGR2GRAY);
idx2 = (last + 1) % N; // index of (last - (N-1))th frame
last = idx2;
cvZero( dst );
cvMerge( buf[idx2], 0, 0, 0, dst );
cvGoodFeaturesToTrack(
buf[idx1],
eig_image,
tmp_image,
corner_idx1,
&corner_count,
0.01,
5.0,
0,
3,
0,
0.04
);
cvFindCornerSubPix(
buf[idx1],
corner_idx1,
corner_count,
cvSize(win_size,win_size),
cvSize(-1,-1),
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,10,0.3)
);
char features_found[ MAX_CORNERS ];
float feature_errors[ MAX_CORNERS ];
CvSize pyr_sz = cvSize( buf[idx1]->width+8, buf[idx2]->height/3 );
IplImage* image1 = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 );
IplImage* image2 = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 );
CvPoint2D32f* corner_idx2 = new CvPoint2D32f[ MAX_CORNERS ];
cvCalcOpticalFlowPyrLK(
buf[idx1],
buf[idx2],
image1,
image2,
corner_idx1,
corner_idx2,
corner_count,
cvSize( win_size,win_size ),
5,
features_found,
feature_errors,
cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, .3 ),
0
);
for( int i=0; i<corner_count; i++ ) {
if( features_found[i]==0|| feature_errors[i]>550 ) {
// printf("Error is %f/n",feature_errors[i]);
continue;
}
// printf("Got it/n");
CvPoint p0 = cvPoint(
cvRound( corner_idx1[i].x ),
cvRound( corner_idx1[i].y )
);
CvPoint p1 = cvPoint(
cvRound( corner_idx2[i].x ),
cvRound( corner_idx2[i].y )
);
if(corner_idx1[i].y=corner_idx2[i].y)
{
cvLine(dst, p0, p1, CV_RGB(255,0,0),2 );
}
}
cvShowImage("video", dst);
cvWriteFrame(writer,dst);
if( cvWaitKey(2) >= 0 )
break;
}
}
cvDestroyWindow("video");
cvReleaseVideoWriter(&writer);
cvReleaseCapture(&pCapture);
return 0;
}