Lucas Kanade Method in Real Time Problem

This is my code.

#include <cv.h>
#include <highgui.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <cxcore.h>
#include <iostream>
#include <vector>
const int MAX_CORNERS = 500;
using namespace std;

int main(int argc, char** argv)
{
CvCapture *capture = cvCaptureFromCAM(0);
IplImage *frame = 0;
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH,320);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT,240);

const char* file = "ABC.jpg";

const char* object_filename = argc == 3 ? argv[1] : "object.jpg"; // What we are looking for
const char* object_filename_2 = argc == 3 ? argv[1] : "object_2.jpg";


int Count_Down = 100;
int Count_Down_1 = 100;
int key = 0;

cout <<"Calibration For The First Time. " << endl;


while(Count_Down>0) // need to initialize camera first
{

frame=cvQueryFrame(capture);
cvRectangle(frame,cvPoint(90,50),cvPoint(210,170),CV_RGB(255,0,0),2,0,0);
cvSetImageROI(frame, cvRect(100,60,100,100));
cvSaveImage(file,frame);
cvSaveImage(object_filename,frame);
Count_Down--;
cvResetImageROI(frame);
cvShowImage("Calibration",frame);
cvWaitKey(10);
printf("\nCalibrating object in middle.. Time Left: %d",Count_Down);

}
cout << endl;
cout << endl;
cvDestroyWindow("Calibration");

cout <<"Calibration For The Second Time. " << endl;

while(Count_Down_1>0) // need to initialize camera first
{

frame=cvQueryFrame(capture);
cvRectangle(frame,cvPoint(90,50),cvPoint(210,170),CV_RGB(255,0,0),2,0,0);
cvSetImageROI(frame, cvRect(100,60,100,100));
cvSaveImage(file,frame);
cvSaveImage(object_filename_2,frame);
Count_Down_1--;
cvResetImageROI(frame);
cvShowImage("Calibration",frame);
cvWaitKey(10);
printf("\nCalibrating object in middle.. Time Left: %d",Count_Down_1);
}

cvDestroyWindow("Calibration");

IplImage* imgA = cvLoadImage("object.jpg" , CV_LOAD_IMAGE_GRAYSCALE);
IplImage* imgB = cvLoadImage("object_2.jpg" , CV_LOAD_IMAGE_GRAYSCALE);

CvSize img_sz = cvGetSize (imgA);
int win_size = 10;


IplImage* eig_image = cvCreateImage (img_sz , IPL_DEPTH_32F,1);
IplImage* tmp_image = cvCreateImage (img_sz , IPL_DEPTH_32F,1);

int corner_count = MAX_CORNERS;
CvPoint2D32f* cornersA = new CvPoint2D32f [MAX_CORNERS];

cvGoodFeaturesToTrack(
imgA,
eig_image,
tmp_image,
cornersA,
&corner_count,
0.01,
5.0,
0,
3,
0,
0.04
);

cvFindCornerSubPix(
imgA,
cornersA,
corner_count,
cvSize(win_size,win_size),
cvSize(-1,-1),
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03)
);
// LK Algorithm
char features_found[MAX_CORNERS];
float feature_errors[MAX_CORNERS];

CvSize pyr_sz = cvSize (imgA ->width , imgB->height);
IplImage*pyrA = cvCreateImage( pyr_sz, IPL_DEPTH_32F,1);
IplImage*pyrB = cvCreateImage( pyr_sz, IPL_DEPTH_32F,1);
CvPoint2D32f* cornersB = new CvPoint2D32f[MAX_CORNERS];

cvCalcOpticalFlowPyrLK (
imgA,
imgB,
pyrA,
pyrB,
cornersA,
cornersB,
corner_count,
cvSize (win_size , win_size),
5,
features_found,
feature_errors,
cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS,20,.3),
0
);


while(1) // Load the Scene
{
frame=cvQueryFrame(capture);
cvNamedWindow("Webcam", 0);


for (int i = 0 ; i<corner_count;i++)
{
cout << i << endl;
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 (cornersA[i].x) ,cvRound(cornersA[i].y) );
CvPoint p1 = cvPoint(cvRound (cornersB[i].x), cvRound(cornersB[i].y) );
cvLine(frame , p0 , p1 , CV_RGB(255,0,0),2);
}

cvShowImage("LK Method" , frame);
cvShowImage("Webcam", frame);
key = cvWaitKey(10);
if(key == 'a') break;




}



}

How come it only print red dot or sometime red line over it? It doesn't track at all? Could anyone point out where is my error?
Last edited on
http://www.cplusplus.com/forum/articles/1295/
(See "when asking about code".)
Topic archived. No new replies allowed.