1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
43 #define _CV_NORM_L2(a) (float)(icvSqrt32f(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]))
44 #define _CV_NORM_L22(a) (float)(a[0]*a[0] + a[1]*a[1] + a[2]*a[2])
46 /****************************************************************************************\
48 find region where hand is (for gesture recognition)
49 flag = 0 (use left bucket) flag = 1 (use right bucket)
51 \****************************************************************************************/
53 static CvStatus CV_STDCALL
54 icvFindHandRegion( CvPoint3D32f * points, int count,
56 float *line, CvSize2D32f size, int flag,
57 CvPoint3D32f * center,
58 CvMemStorage * storage, CvSeq ** numbers )
61 /* IppmVect32f sub, cros; */
67 int nbins = 20, i, l, i_point, left, right;
68 int *bin_counts = 0; // pointer to the point's counter in the bickets
69 int low_count; // low threshold
71 CvPoint *tmp_number = 0, *pt;
72 float value, vmin, vmax, vl, bsize, vc;
73 float hand_length, hand_length2, hand_left, hand_right;
74 float threshold, threshold2;
80 hand_length = size.width;
81 hand_length2 = hand_length / 2;
83 threshold = (float) (size.height * 3 / 5.);
84 threshold2 = threshold * threshold;
86 /* low_count = count/nbins; */
87 low_count = (int) (count / 60.);
89 assert( points != NULL && line != NULL );
90 if( points == NULL || line == NULL )
91 return CV_NULLPTR_ERR;
95 return CV_BADFLAG_ERR;
97 assert( flag == 0 || flag == 1 );
98 if( flag != 0 && flag != 1 )
99 return CV_BADFLAG_ERR;
102 sub = icvCreateVector_32f( 3 );
103 cros = icvCreateVector_32f( 3 );
104 if( sub == NULL || cros == NULL )
105 return CV_OUTOFMEM_ERR;
107 /* alloc memory for the point's projections on the line */
108 vv = (float *) cvAlloc( count * sizeof( float ));
111 return CV_OUTOFMEM_ERR;
113 /* alloc memory for the point's counter in the bickets */
114 bin_counts = (int *) cvAlloc( nbins * sizeof( int ));
116 if( bin_counts == NULL )
118 status = CV_OUTOFMEM_ERR;
121 memset( bin_counts, 0, nbins * sizeof( int ));
123 cvStartReadSeq( indexs, &reader, 0 );
125 /* alloc memory for the temporale point's numbers */
126 tmp_number = (CvPoint *) cvAlloc( count * sizeof( CvPoint ));
127 if( tmp_number == NULL )
129 status = CV_OUTOFMEM_ERR;
133 /* find min and max point's projection on the line */
137 for( i = 0; i < count; i++ )
140 icvSubVector_32f ((IppmVect32f )&points[i], (IppmVect32f )&line[3], sub, 3);
142 icvCrossProduct2L_32f ((IppmVect32f )&line[0], sub, cros);
145 sub[0] = points[i].x - line[3];
146 sub[1] = points[i].y - line[4];
147 sub[2] = points[i].z - line[5];
148 a[0] = sub[0] * line[1] - sub[1] * line[0];
149 a[1] = sub[1] * line[2] - sub[2] * line[1];
150 a[2] = sub[2] * line[0] - sub[0] * line[2];
152 /* if(IPPI_NORM_L22 ( cros ) < threshold2) */
153 if( _CV_NORM_L22( a ) < threshold2 )
155 value = (float)icvDotProduct_32f( sub, &line[0], 3 );
163 pt = (CvPoint*)cvGetSeqElem( indexs, i );
164 tmp_number[i_point] = *pt;
169 /* compute the length of one bucket */
173 /* compute the number of points in each bucket */
174 for( i = 0; i < i_point; i++ )
176 l = cvRound( (vv[i] - vmin) / bsize );
180 *numbers = cvCreateSeq( CV_SEQ_POINT_SET, sizeof( CvSeq ), sizeof( CvPoint ), storage );
181 assert( numbers != 0 );
182 if( numbers == NULL )
184 status = CV_OUTOFMEM_ERR;
188 cvStartAppendToSeq( *numbers, &writer );
192 /* find the leftmost bucket */
193 for( l = 0; l < nbins; l++ )
195 if( bin_counts[l] > low_count )
200 /* compute center point of the left hand */
201 hand_left = vmin + left * bsize;
202 vc = hand_left + hand_length2;
203 hand_right = hand_left + hand_length;
207 /* find the rightmost bucket */
208 for( l = nbins - 1; l >= 0; l-- )
210 if( bin_counts[l] > low_count )
215 /* compute center point of the right hand */
216 hand_right = vmax - (nbins - right - 1) * bsize;
217 vc = hand_right - hand_length2;
218 hand_left = hand_right - hand_length;
221 icvScaleVector_32f( &line[0], sub, 3, vc );
222 icvAddVector_32f( &line[3], sub, (float *) center, 3 );
224 /* select hand's points and calculate mean value */
226 //ss.x = ss.y = ss.z = 0;
227 for( l = 0; l < i_point; l++ )
229 if( vv[l] >= hand_left && vv[l] <= hand_right )
231 CV_WRITE_SEQ_ELEM( tmp_number[l], writer );
236 cvEndWriteSeq( &writer );
239 if( tmp_number != NULL )
240 cvFree( &tmp_number );
241 if( bin_counts != NULL )
242 cvFree( &bin_counts );
245 if( sub != NULL ) icvDeleteVector (sub);
246 if( cros != NULL ) icvDeleteVector (cros);
253 //////////////////////////////////////////////////////////////////////////////////////////
254 //////////////////////////////////////////////////////////////////////////////////////////
255 //////////////////////////////////////////////////////////////////////////////////////////
258 #define _CV_NORM_L31(a) (float)(icvSqrt32f(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]))
259 #define _CV_NORM_L32(a) (float)(a[0]*a[0] + a[1]*a[1] + a[2]*a[2])
261 /****************************************************************************************\
263 find region where hand is (for gesture recognition)
264 flag = 0 (use left bucket) flag = 1 (use right bucket)
266 \****************************************************************************************/
268 static CvStatus CV_STDCALL
269 icvFindHandRegionA( CvPoint3D32f * points, int count,
271 float *line, CvSize2D32f size, int jc,
272 CvPoint3D32f * center,
273 CvMemStorage * storage, CvSeq ** numbers )
276 /* IppmVect32f sub, cros; */
278 float eps = (float) 0.01;
283 float gor[3] = { 1, 0, 0 };
284 float ver[3] = { 0, 1, 0 };
286 int nbins = 20, i, l, i_point, left, right, jmin, jmax, jl;
288 int *bin_counts = 0; // pointer to the point's counter in the bickets
290 // int *bin_countsj = 0; // pointer to the index's counter in the bickets
291 int low_count; // low threshold
293 CvPoint *tmp_number = 0, *pt;
294 float value, vmin, vmax, vl, bsize, bsizej, vc, vcl, vcr;
296 float hand_length, hand_length2, hand_left, hand_right;
297 float threshold, threshold2;
304 hand_length = size.width;
305 hand_length2 = hand_length / 2;
307 threshold = (float) (size.height * 3 / 5.);
308 threshold2 = threshold * threshold;
310 /* low_count = count/nbins; */
311 low_count = (int) (count / 60.);
313 assert( points != NULL && line != NULL );
314 if( points == NULL || line == NULL )
315 return CV_NULLPTR_ERR;
319 return CV_BADFLAG_ERR;
322 sub = icvCreateVector_32f( 3 );
323 cros = icvCreateVector_32f( 3 );
324 if( sub == NULL || cros == NULL )
325 return CV_OUTOFMEM_ERR;
327 /* alloc memory for the point's projections on the line */
328 vv = (float *) cvAlloc( count * sizeof( float ));
331 return CV_OUTOFMEM_ERR;
333 /* alloc memory for the point's counter in the bickets */
334 bin_counts = (int *) cvAlloc( nbins * sizeof( int ));
336 if( bin_counts == NULL )
338 status = CV_OUTOFMEM_ERR;
341 memset( bin_counts, 0, nbins * sizeof( int ));
343 /* alloc memory for the point's counter in the bickets */
344 // bin_countsj = (int*) icvAlloc(nbins*sizeof(int));
345 // if(bin_countsj == NULL) {status = CV_OUTOFMEM_ERR; goto M_END;}
346 // memset(bin_countsj,0,nbins*sizeof(int));
348 cvStartReadSeq( indexs, &reader, 0 );
350 /* alloc memory for the temporale point's numbers */
351 tmp_number = (CvPoint *) cvAlloc( count * sizeof( CvPoint ));
352 if( tmp_number == NULL )
354 status = CV_OUTOFMEM_ERR;
358 /* find min and max point's projection on the line */
364 for( i = 0; i < count; i++ )
367 icvSubVector_32f ((IppmVect32f )&points[i], (IppmVect32f )&line[3], sub, 3);
369 icvCrossProduct2L_32f ((IppmVect32f )&line[0], sub, cros);
372 sub[0] = points[i].x - line[3];
373 sub[1] = points[i].y - line[4];
374 sub[2] = points[i].z - line[5];
376 // if(fabs(sub[0])<eps||fabs(sub[1])<eps||fabs(sub[2])<eps) continue;
378 a[0] = sub[0] * line[1] - sub[1] * line[0];
379 a[1] = sub[1] * line[2] - sub[2] * line[1];
380 a[2] = sub[2] * line[0] - sub[0] * line[2];
382 v_gor = icvDotProduct_32f( gor, &line[0], 3 );
383 v_ver = icvDotProduct_32f( ver, &line[0], 3 );
391 /* if(IPPI_NORM_L22 ( cros ) < threshold2) */
393 if(fabs(a[0])<eps && fabs(a[1])<eps && fabs(a[2])<eps)
395 icvDotProduct_32f( sub, &line[0], 3, &value);
396 if(value > vmax) vmax = value;
397 if(value < vmin) vmin = value;
401 pt = (CvPoint* )icvGetSeqElem ( indexs, i, 0);
403 if(pt->x > jmax) jmax = pt->x;
404 if(pt->x < jmin) jmin = pt->x;
406 tmp_number[i_point] = *pt;
412 if( _CV_NORM_L32( a ) < threshold2 )
414 value = (float)icvDotProduct_32f( sub, &line[0], 3 );
422 pt = (CvPoint*)cvGetSeqElem( indexs, i );
440 tmp_number[i_point] = *pt;
446 /* compute the length of one bucket along the line */
449 /* examining on the arm's existence */
459 /* compute the number of points in each bucket along the line */
460 for( i = 0; i < i_point; i++ )
462 l = cvRound( (vv[i] - vmin) / bsize );
466 /* compute the length of one bucket along the X axe */
475 bsizej = (float) (jl / (nbins + 0.));
477 /* compute the number of points in each bucket along the X axe */
478 // for(i=0;i<i_point;i++)
480 // l = cvRound((tmp_number[i].x - jmin)/bsizej);
487 /* find the leftmost and the rightmost buckets */
488 for( l = 0; l < nbins; l++ )
490 if( bin_counts[l] > low_count && left == -1 )
492 else if( bin_counts[l] > low_count && left >= 0 )
497 /* compute center point of the left hand */
498 if( left == -1 && right == -1 )
505 hand_left = vmin + left * bsize;
506 j_left = (int) (jmin + left * bsizej);
508 vcl = hand_left + hand_length2;
510 /* compute center point of the right hand */
511 hand_right = vmax - (nbins - right - 1) * bsize;
512 vcr = hand_right - hand_length2;
514 j_right = (int) (jmax - (nbins - right - 1) * bsizej);
516 j_left = abs( j_left - jc );
517 j_right = abs( j_right - jc );
519 if( j_left <= j_right )
521 hand_right = hand_left + hand_length;
526 hand_left = hand_right - hand_length;
530 icvScaleVector_32f( &line[0], sub, 3, vc );
531 icvAddVector_32f( &line[3], sub, (float *) center, 3 );
533 /* select hand's points and calculate mean value */
534 *numbers = cvCreateSeq( CV_SEQ_POINT_SET, sizeof( CvSeq ), sizeof( CvPoint ), storage );
535 assert( *numbers != 0 );
536 if( *numbers == NULL )
538 status = CV_OUTOFMEM_ERR;
542 cvStartAppendToSeq( *numbers, &writer );
544 for( l = 0; l < i_point; l++ )
546 if( vv[l] >= hand_left && vv[l] <= hand_right )
548 CV_WRITE_SEQ_ELEM( tmp_number[l], writer );
553 cvEndWriteSeq( &writer );
556 if( tmp_number != NULL )
557 cvFree( &tmp_number );
558 // if(bin_countsj != NULL) cvFree( &bin_countsj );
559 if( bin_counts != NULL )
560 cvFree( &bin_counts );
565 if( sub != NULL ) icvDeleteVector (sub);
566 if( cros != NULL ) icvDeleteVector (cros);
572 /*F///////////////////////////////////////////////////////////////////////////////////////
573 // Name: cvFindHandRegion
574 // Purpose: finds hand region in range image data
577 // points - pointer to the input point's set.
578 // count - the number of the input points.
579 // indexs - pointer to the input sequence of the point's indexes
580 // line - pointer to the 3D-line
581 // size - size of the hand in meters
582 // flag - hand direction's flag (0 - left, -1 - right,
583 // otherwise j-index of the initial image center)
584 // center - pointer to the output hand center
585 // storage - pointer to the memory storage
586 // numbers - pointer to the output sequence of the point's indexes inside
592 cvFindHandRegion( CvPoint3D32f * points, int count,
594 float *line, CvSize2D32f size, int flag,
595 CvPoint3D32f * center, CvMemStorage * storage, CvSeq ** numbers )
597 if(flag == 0 || flag == -1)
599 IPPI_CALL( icvFindHandRegion( points, count, indexs, line, size, -flag,
600 center, storage, numbers ));
603 IPPI_CALL( icvFindHandRegionA( points, count, indexs, line, size, flag,
604 center, storage, numbers ));
607 /*F///////////////////////////////////////////////////////////////////////////////////////
608 // Name: cvFindHandRegionA
609 // Purpose: finds hand region in range image data
612 // points - pointer to the input point's set.
613 // count - the number of the input points.
614 // indexs - pointer to the input sequence of the point's indexes
615 // line - pointer to the 3D-line
616 // size - size of the hand in meters
617 // jc - j-index of the initial image center
618 // center - pointer to the output hand center
619 // storage - pointer to the memory storage
620 // numbers - pointer to the output sequence of the point's indexes inside
626 cvFindHandRegionA( CvPoint3D32f * points, int count,
628 float *line, CvSize2D32f size, int jc,
629 CvPoint3D32f * center, CvMemStorage * storage, CvSeq ** numbers )
631 IPPI_CALL( icvFindHandRegionA( points, count, indexs, line, size, jc,
632 center, storage, numbers ));