hand detection 3d hand

Hello I have a hand in c + + code detection.
But I want him detecterd my hand and he will display a 3d model of my hand.
And he also sees when I move a finger.
So can anyone help me with?
This is my hand detection cpp code.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#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>
#include "tools_camera.h"
#include <fstream>

int g_nFingers = 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(0);
if ( !capture )
     {
   	fprintf( stderr, "ERROR: capture is NULL \n" );
    	getchar();
    	return -1;
     }

while(!end)

{
cvGrabFrame (capture); 
IplImage* in0 = cvRetrieveFrame (capture);
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 nFingers = 0; 

	// tach 3 plane, tao ảnh gray
	/*IplImage* hsv = cvCreateImage( cvGetSize(in), 8, 3 );
	cvCvtColor( in, hsv, CV_BGR2HSV );
	IplImage* h_plane = cvCreateImage( cvGetSize(in), 8, 1 );
	IplImage* s_plane = cvCreateImage( cvGetSize(in), 8, 1 );
	IplImage* v_plane = cvCreateImage( cvGetSize(in), 8, 1 );
	IplImage* planes[] = { h_plane, s_plane };
	cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );*/
	
	

	// tach duong bao
	IplImage* smooth = doSmoothGaussian(in, 31,31);
	IplImage* thres2 = doThresBinary(smooth,30);
	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));
	if (hand_contour)
	{
	cvDrawContours( in, hand_contour, CV_RGB(255,0,0), CV_RGB(0,255,0), 1, 2, 8, cvPoint(0,0));
	
		int i,h=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
		signed int z = zCrossProduct (hand_contour, i, r);		//tinh z tich chap 2 vecto 

			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);
						cvCircle( in0,n0 , 8, CV_RGB(0,255,0), 3, 8, 0 );
						}
				}
		
		minus2=minus1;
		minus1=present;
		present=nFingers;

		if (minus2 == nFingers)
		final = nFingers;
		else
		final = minus2;
		}

	}



//cvNamedWindow("in",CV_WINDOW_AUTOSIZE );
cvShowImage("init",in0);

//cvNamedWindow("v-plane",CV_WINDOW_AUTOSIZE );
//cvShowImage("v-plane",v_plane);

//cvNamedWindow("thres2",CV_WINDOW_AUTOSIZE );
// cvShowImage("thres2",thres2);

//cout << hand_contour->total << endl;

//fingers quantity stabilizer


if (g_nFingers != final )
{
g_nFingers = final;
cout << g_nFingers << " fingers" << endl;
}

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


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;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
And my header:
#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* 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* 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));
}

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 );
	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);

}

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 );
		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));
}


I have windows
Thanks
Last edited on
I have 4 windows in my room,
you are welcome,,


I don't even know what library you are using ,,,
What does this code do ?
Detecting a hand with a Camera ?

Anyway I can't read your code, too many function I don't know of

But try not to replace the STD name,
like max, it's defined inside <algorithm>
just include them, no need to rewrite something so common

I'm not being much of help, aren't I ?

Please use code tag to post code

I use opencv libary.
This code detects a hand and view circels on the hand.
Can any help me?
Any
No-one's going to read a wall of code like that if you don't use code tags.
I have set it into code tags.

Can anyone help my
You haven't even told us what the problem is. Okay we know what you want to do...but where is your code going wrong
Topic archived. No new replies allowed.