Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
/*
 * hand_point.cpp
 *
 *  Created on: Jul 12, 2011
 *      Author: Ho Dac Loc
 */
#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; //so ngon tay trong lan detect truoc

int main(int argc, char ** argv)

{

using namespace std;

int end = 0; int key = 0;
int minus2,minus1,present,final = 0;
//CvCapture* capture = cvCaptureFromCAM(1);
//if ( !capture )
//     {
//   	fprintf( stderr, "ERROR: capture is NULL \n" );
//    	getchar();
//    	return -1;
//     }

//while(!end)
//
{
//cvGrabFrame (capture);
IplImage* in0 = cvLoadImage( "C:\\Users\\Ajithesh_404546\\pro desk\\a2.jpg");
//if ( !in0 )
//     {
//       fprintf( stderr, "ERROR: frame is null...\n" );
//       getchar();
//       break;
//     }
cvNamedWindow("init",CV_WINDOW_AUTOSIZE );


		/*==================init phase=====================*/
if (key == 0)
	{
	IplImage* init = cvCloneImage(in0);
	//cvNamedWindow("init",CV_WINDOW_AUTOSIZE );
	cvShowImage("init",init);
	if ( (cvWaitKey(10) & 255) == 10 ) // enter key to escape init and start recognition phase
		{
		key = 1;
		//cvDestroyWindow( "init" );
		}
	}

		/*==================recognition phase=====================*/

if (key == 1)
{

//convert to gray image
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;

	// tach duong bao
	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)
	{
	//tim diem giua cua ban tay
		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; 	//khoang cach giua diem trung tam va 2 lan can
		int step = 16; 	//buoc nhay diem chay tren contour
		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);			//tinh he so cos tai r0
			if (cos0 > 4)
			{
			final = g_nFingers;
			break;
			}
			if (cos0 > 0.5)
				{
				double cos1 = curve (hand_contour, i - step, r); 	// tinh he so cos tai lan can 1
				double cos2 = curve (hand_contour, i + step, r); 	// tinh he so cos tai lan can 2
				double max1 = max_3 (cos0, cos1, cos2);			// tinh max 3 he so goc
				bool equal = IsEqual (max1 , cos0);
				signed int z = zCrossProduct (hand_contour, i, r);	// tinh z tich chap 2 vecto
					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; //up
		}
		else if (cos_angle > 0.7)
		{
			cDirect = 2; //down
		}
		else
		{
			if (y_finger > hand_center.x)
			{
			cDirect = 3; //right
			}
			else
			{
			cDirect = 4; //left
			}
		}
	}
	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);

//fingers quantity stabilizer



//if ( (cvWaitKey(10) & 255) == 27 ) //wait for esc key to stop
//end = 1;
cvWaitKey(0);

cvReleaseMemStorage( &storage );
cvReleaseImage(&in);
cvReleaseImage(&thres2);
cvReleaseImage(&smooth);
	/*cvReleaseImage(&v_plane);
cvReleaseImage(&s_plane);
cvReleaseImage(&h_plane);*/
}


//cvDestroyWindow( "thres2" );


}

cvDestroyWindow( "in" );

//cvReleaseCapture(&capture);
return 0;
}


and the header file tools.h is given below

C++
/*
 * tools.h
 *
 *  Created on: Jul 12, 2011
 *      Author: Ho Dac Loc
 */

#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 );
	// Build the histogram and compute its contents.
	//

	CvHistogram* hist;
	{
	int hist_size[] = { h_bins, s_bins };
	float h_ranges[] = { 0, 180 }; // hue is [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 ); //Compute histogram
	cvNormalizeHist( hist, 20*255 ); //Normalize it
	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 );
	// populate our visualization with little gray squares.
	//
	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; // or some other small number
    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...
Posted
Comments
Jochen Arndt 29-Mar-12 9:44am    
If the code is known as working, quoting it here will not help. You should improve your question with specific information on the crash (error messages).

1 solution

No error was found, except for some places may need to be modified
//////////////////////////////////////////////
1. in func main, the first cvWaitKey, the param 10ms ?
if ( (cvWaitKey(10) & 255) == 10 )

2. pls confirm the path of jpg file, and the size of "a2.jpg" be 640x480
IplImage* in0 = cvLoadImage( "C:\\Users\\Ajithesh_404546\\pro desk\\a2.jpg");
...
IplImage* in = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 1 );

//////////////////////////////////////////////

regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900