I downloaded the below code from google code and the overview says the code is working fine...but when i run the program it crashes...
this is a program to detect the fingertip...
the code is given below...someone pls tell me why the code isn't working
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fstream>
#include <cv.h>
#include <cvaux.h>
#include <cxcore.h>
#include <highgui.h>
#include <cxmisc.h>
#include "tools.h"
int g_nFingers, g_direct = 0;
int main(int argc, char ** argv)
{
using namespace std;
int end = 0; int key = 0;
int minus2,minus1,present,final = 0;
{
IplImage* in0 = cvLoadImage( "C:\\Users\\Ajithesh_404546\\pro desk\\a2.jpg");
cvNamedWindow("init",CV_WINDOW_AUTOSIZE );
if (key == 0)
{
IplImage* init = cvCloneImage(in0);
cvShowImage("init",init);
if ( (cvWaitKey(10) & 255) == 10 ) {
key = 1;
}
}
if (key == 1)
{
IplImage* in = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 1 );
cvCvtColor( in0, in, CV_RGB2GRAY );
int y_finger = 0;
int cDirect = 0;
CvPoint fVector,hand_center;
CvPoint horizontal=cvPoint(0,10);
int nFingers = 0;
IplImage* equal = doEqualHist(in);
IplImage* smooth = doSmoothGaussian(equal, 41,41);
IplImage* thres2 = doThresBinary(smooth,190);
IplImage* contour = cvCloneImage(thres2);
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* hand_contour = 0;
cvFindContours (contour,storage,&hand_contour,sizeof(CvContour),CV_RETR_TREE, CV_CHAIN_APPROX_NONE,cvPoint(0,0));
cvShowImage("contour",contour);
if (hand_contour)
{
CvRect hand_box= cvBoundingRect (hand_contour,0);
hand_center.x = cvRound(hand_box.x + hand_box.width*0.5);
hand_center.y = cvRound(hand_box.y + hand_box.height*0.5);
int i=0;
int r = 100; int step = 16; for( i = 0; i < (hand_contour ? hand_contour->total : 0); i+= step )
{
CvPoint* r0 = (CvPoint*)cvGetSeqElem( hand_contour, i );
CvPoint n0 = cvPoint (r0->x,r0->y);
double cos0 = curve (hand_contour, i, r); if (cos0 > 4)
{
final = g_nFingers;
break;
}
if (cos0 > 0.5)
{
double cos1 = curve (hand_contour, i - step, r); double cos2 = curve (hand_contour, i + step, r); double max1 = max_3 (cos0, cos1, cos2); bool equal = IsEqual (max1 , cos0);
signed int z = zCrossProduct (hand_contour, i, r); if (equal == 1 && z<0)
{
nFingers += 1;
CvPoint* r0 = (CvPoint*)cvGetSeqElem( hand_contour, i );
CvPoint n0 = cvPoint (r0->x,r0->y);
y_finger = r0->x;
fVector = cvPoint(n0.x - hand_center.x, n0.y - hand_center.y);
cvCircle( in0,n0 , 8, CV_RGB(0,255,0), 3, 8, 0 );
cvLine (in0, n0, hand_center,cvScalarAll(255), 1,8 );
}
}
minus2=minus1;
minus1=present;
present=nFingers;
if (minus2 == nFingers)
final = nFingers;
else
final = minus2;
}
}
if (final == 1)
{
double cos_angle = cos_vector(fVector, horizontal);
if (cos_angle < -0.7)
{
cDirect = 1; }
else if (cos_angle > 0.7)
{
cDirect = 2; }
else
{
if (y_finger > hand_center.x)
{
cDirect = 3; }
else
{
cDirect = 4; }
}
}
cvWaitKey(0);
if (g_direct != cDirect )
{cvWaitKey(0);
g_direct = cDirect;
switch (g_direct)
{cvWaitKey(0);
case 0: cout << " "; break;
case 3: cout << "right" << endl; break;
case 4: cout << "left" << endl; break;
case 1: cout << "up" << endl; break;
case 2: cout << "down" << endl; break;
}
}
cvShowImage("init",in0);
cvWaitKey(0);
cvNamedWindow("thres2",CV_WINDOW_AUTOSIZE );
cvShowImage("thres2",thres2);
cvWaitKey(0);
cvWaitKey(0);
cvReleaseMemStorage( &storage );
cvReleaseImage(&in);
cvReleaseImage(&thres2);
cvReleaseImage(&smooth);
}
}
cvDestroyWindow( "in" );
return 0;
}
and the header file tools.h is given below
#include <stdio.h>
#include <stdlib.h>
#include "cv.h"
#include "cvaux.h"
#include "cxcore.h"
#include "highgui.h"
#include "cxmisc.h"
#include "ml.h"
#include <math.h>
#define PI 3.1415926535898
double rads(double degs)
{
return (PI/180 * degs);
}
CvHistogram* doCalHistHS(IplImage* src,int h_bins , int s_bins )
{
IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 );
cvCvtColor( src, hsv, CV_BGR2HSV );
IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 );
IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 );
IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 );
IplImage* planes[] = { h_plane, s_plane };
cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );
CvHistogram* hist;
{
int hist_size[] = { h_bins, s_bins };
float h_ranges[] = { 0, 180 }; float s_ranges[] = { 0, 255 };
float* ranges[] = { h_ranges, s_ranges };
hist = cvCreateHist(
2,
hist_size,
CV_HIST_ARRAY,
ranges,
1
);
}
cvCalcHist( planes, hist, 0, 0 ); cvNormalizeHist( hist, 20*255 ); return (hist);
}
IplImage* doDrawHist (CvHistogram* hist,int h_bins,int s_bins )
{
int scale = 10;
IplImage* hist_img = cvCreateImage(
cvSize( h_bins * scale, s_bins * scale ),
8,
3
);
cvZero( hist_img );
float max_value = 0;
int h = 0;
int s = 0;
cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
for( h = 0; h < h_bins; h++ ) {
for( s = 0; s < s_bins; s++ ) {
float bin_val = cvQueryHistValue_2D( hist, h, s );
int intensity = cvRound( bin_val * 255 / max_value );
cvRectangle(
hist_img,
cvPoint( h*scale, s*scale ),
cvPoint( (h+1)*scale - 1, (s+1)*scale - 1),
CV_RGB(intensity,intensity,intensity),
CV_FILLED
);
}
}
return (hist_img);
}
IplImage* doSmoothGaussian(IplImage* in,int par1, int par2)
{
IplImage* out = cvCreateImage(cvGetSize( in ),in->depth,in->nChannels);
cvSmooth( in, out, CV_GAUSSIAN, par1, par2 );
return (out);
}
IplImage* doEqualHist(IplImage* in)
{
IplImage* out = cvCreateImage(cvGetSize( in ),in->depth,in->nChannels);
cvEqualizeHist(in, out);
return (out);
}
IplImage* doSmoothMedian(IplImage* in,int par1, int par2)
{
IplImage* out = cvCreateImage(cvGetSize( in ),in->depth,in->nChannels);
cvSmooth( in, out, CV_BLUR , par1, par2 );
return (out);
}
IplImage* doThresBinary (IplImage* in, int thres)
{
IplImage* out = cvCreateImage(cvGetSize( in ),in->depth,in->nChannels);
cvThreshold( in, out, thres, 255, CV_THRESH_BINARY);
return (out);
}
IplImage* doAdapThres (IplImage* in)
{
IplImage* out = cvCreateImage(cvGetSize( in ),in->depth,in->nChannels);
cvAdaptiveThreshold(in, out, 255, CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY, 71, 15);
return (out);
}
IplImage* doDilate (IplImage* in)
{
IplImage* out = cvCreateImage(cvGetSize( in ),in->depth,in->nChannels);
cvDilate( in, out, NULL,1);
return (out);
}
double cos_goc (CvPoint* pt1, CvPoint* pt2, CvPoint* pt3, CvPoint* pt4)
{
double vx1=pt1->x-pt2->x;
double vy1=pt1->y-pt2->y;
double vx2=pt3->x-pt4->x;
double vy2=pt3->y-pt4->y;
return (vx1*vx2 + vy1*vy2)/sqrt((vx1*vx1 + vy1*vy1)*(vx2*vx2 + vy2*vy2));
}
double cos_vector(CvPoint vt1, CvPoint vt2)
{
double vx1=vt1.x;
double vy1=vt1.y;
double vx2=vt2.x;
double vy2=vt2.y;
return (vx1*vx2 + vy1*vy2)/sqrt((vx1*vx1 + vy1*vy1)*(vx2*vx2 + vy2*vy2));
}
signed int zCrossProduct(CvSeq* contour, int pt, int r)
{
CvPoint* r0 = (CvPoint*)cvGetSeqElem( contour, pt );
CvPoint* r1 = (CvPoint*)cvGetSeqElem( contour, pt + r );
CvPoint* r2 = (CvPoint*)cvGetSeqElem( contour, pt - r );
if((r0)&&(r1)&&(r2))
{
double vx1=r0->x-r1->x;
double vy1=r0->y-r1->y;
double vx2=r0->x-r2->x;
double vy2=r0->y-r2->y;
return (vx1*vy2 - vx2*vy1);
}
else
return 10000;
}
double max (double a, double b)
{
if (a>b)
return a;
else
return b;
}
double max_3 (double a, double b, double c)
{
double m1=max (a,b);
double m2=max (m1,c);
return m2;
}
bool IsEqual(double dX, double dY)
{
const double dEpsilon = 0.000001; return fabs(dX - dY) <= dEpsilon * fabs(dX);
}
double curve (CvSeq* contour, int pt, int r)
{
CvPoint* r0 = (CvPoint*)cvGetSeqElem( contour, pt );
CvPoint* r1 = (CvPoint*)cvGetSeqElem( contour, pt + r );
CvPoint* r2 = (CvPoint*)cvGetSeqElem( contour, pt - r );
if((r0)&&(r1)&&(r2))
{
double vx1=r0->x-r1->x;
double vy1=r0->y-r1->y;
double vx2=r0->x-r2->x;
double vy2=r0->y-r2->y;
return (vx1*vx2 + vy1*vy2)/sqrt((vx1*vx1 + vy1*vy1)*(vx2*vx2 + vy2*vy2));
}
else
return 5;
}
someone pls tell what the problem is..the program gets compiled but it crashes...