----------------------------------------------------------------------------
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
IplImage *eig_image = NULL, *temp_image = NULL;
const double pi = 3.14159265358979323846;
double square(int a)
{
return a * a;
}
int main(void)
{
IplImage* input_video = cvLoadImage( "C:\\Users\\abc\\Desktop\\IMG_0039.jpg" );
CvSize frame_size;
frame_size.height = input_video->height;
frame_size.width = input_video->width;
IplImage* frame1 = cvCreateImage(frame_size, IPL_DEPTH_8U, 1 );
cvConvertImage(input_video, frame1, CV_CVTIMG_FLIP);
CvPoint2D32f frame1_features[400];
CvPoint2D32f frame2_features[400];
int number_of_features=400;
cvGoodFeaturesToTrack(frame1, eig_image, temp_image, frame1_features, &number_of_features, .01, .01, NULL);
char optical_flow_found_feature[400];
float optical_flow_feature_error[400];
CvTermCriteria optical_flow_termination_criteria = cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.3 );
IplImage* input_video1 = cvLoadImage( "C:\\Users\\abc\\Desktop\\IMG_0040.jpg" );
IplImage* frame2 = cvCreateImage(frame_size, IPL_DEPTH_8U, 1 );
cvConvertImage(input_video1, frame2, CV_CVTIMG_FLIP);
IplImage *pyramid1 = cvCreateImage( frame_size, IPL_DEPTH_8U, 1 );
IplImage *pyramid2 = cvCreateImage( frame_size, IPL_DEPTH_8U, 1 );
CvSize optical_flow_window = cvSize(3,3);
cvCalcOpticalFlowPyrLK(frame1, frame2, pyramid1, pyramid2, frame1_features,
frame2_features, number_of_features, optical_flow_window, 5,
optical_flow_found_feature, optical_flow_feature_error,
optical_flow_termination_criteria, 0 );
for(int i = 0; i < number_of_features; i++)
{
if ( optical_flow_found_feature[i] == 0 ) continue;
int line_thickness = 10;
CvScalar line_color = CV_RGB(255,0,0);
CvPoint p,q;
p.x = (int) frame1_features[i].x;
p.y = (int) frame1_features[i].y;
q.x = (int) frame2_features[i].x;
q.y = (int) frame2_features[i].y;
double angle = atan2( (double) p.y - q.y, (double) p.x - q.x );
double hypotenuse = sqrt( square(p.y - q.y) + square(p.x - q.x) );
q.x = (int) (p.x - 3 * hypotenuse * cos(angle));
q.y = (int) (p.y - 3 * hypotenuse * sin(angle));
cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
p.x = (int) (q.x + 9 * cos(angle + pi / 4));
p.y = (int) (q.y + 9 * sin(angle + pi / 4));
cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
p.x = (int) (q.x + 9 * cos(angle - pi / 4));
p.y = (int) (q.y + 9 * sin(angle - pi / 4));
cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
}
cvNamedWindow( "Optical Flow", 0 );
cvShowImage("Optical Flow", frame1);
int p[3]; p[0] = CV_IMWRITE_JPEG_QUALITY; p[1] = 100; p[2] = 0; cvSaveImage("out2.jpg", frame1, p);
cvNamedWindow( "Optical Flow1", 0 );
cvShowImage("Optical Flow1", frame2);
cvWaitKey(0);
}
------------------------------------------------------------------------------
No comments:
Post a Comment