⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 handlib.cpp

📁 自已用C++写的手势辨识,可分出四个不同的手势。
💻 CPP
字号:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
//#include "cv.h"
//#include "highgui.h"
#include "handLib.h"
#include <iostream>

#define K	1.2  // 1.2~2.7


 void orientationHistogram( IplImage *grayImage, int *hist){
 		IplImage *ampArray = cvCreateImage( cvGetSize( grayImage), IPL_DEPTH_32F, 1);
 		IplImage *angleArray = cvCreateImage( cvGetSize( grayImage), IPL_DEPTH_32F, 1);
		
 	
 			unsigned char *grayData	= (unsigned char*) grayImage->imageData;
 			float *ampData = (float*) ampArray->imageData;
 			float *angleData = (float*) angleArray->imageData;
 			int step = grayImage->widthStep;
			int fStep = ampArray->widthStep/sizeof(float);
 			int i,j,count=0;
 			float dx,dy;
 			float amount=0,average=0;
 			cvZero(ampArray);
 			cvZero(angleArray);
			
 		for( i=0;i<grayImage->height-1;i++){
 				for( j=0;j<grayImage->width-1;j++){
 						dx = (float)(grayData[ i*step + j] - grayData[ i*step +j+1]);
 						dy =(float)( grayData[ i*step + j] - grayData[(i+1)*step +j]);
						
						ampData[ i*fStep + j] = sqrt(dx*dx+dy*dy);
						
						if( ampData[ i*fStep +j] > 0 ){
							amount += ampData[ i*fStep + j]; // calculatin total sum
							count++;
						}
						float temp = (float)atan2( dx, dy);						
						if( temp >0)
							angleData[ i*fStep +j] = temp;
						else
							angleData[ i*fStep +j] = (temp+2*CV_PI);		
							
							
 					}
 			}

  
 			
 			average = amount / count;
 			
 			int contrastTh = average * K;
 			int index;
 			for( i=0;i<ampArray->height;i++){
 					for( j=0;j<ampArray->width;j++){
 						if(	ampData[ i*step +j ] > contrastTh ){
 								index =(int) (angleData[ i*fStep +j]*(180/CV_PI))/10;
 								hist[index]++;
 								
 							}
 						}
 				}
// blur histogram 

	int H[5]={1,4,6,4,1};
	
	int *Y;
	Y=conv(hist,H,36,5);
	
	for(i=0;i<36;i++){
		hist[i] = (int)(Y[i]/16.0);
	}


		
	cvReleaseImage( &ampArray);
	cvReleaseImage( &angleArray);
 	delete Y;
 	}


 void graphImage(IplImage *showImage,int *hist){
 		cvZero(showImage);
 		CvPoint centerPoint = cvPoint( showImage->width/2, showImage->height/2);
 		cvLine( showImage, cvPoint(0,centerPoint.y),cvPoint(centerPoint.x*2,centerPoint.y),CV_RGB(255,0,0),2); // draw x-axis
 		cvLine( showImage, cvPoint(centerPoint.x,0),cvPoint(centerPoint.x,centerPoint.y*2),CV_RGB(255,0,0),2);// draw y-axis
 		
 		int width = showImage->width;
 		int height = showImage->height;
 		int i;
 		int bin =36;
 		float x_dir,y_dir;
 		float amount=0,percent=0;
 		for( i=0;i<bin;i++){
	 			amount += hist[i];	
 			}
 		CvPoint p1,p2,firstPoint;
 		for(i=0;i<bin;i++){
 				x_dir = cos(i*CV_PI*2/bin+CV_PI/2);
 				y_dir = sin(i*CV_PI*2/bin+CV_PI/2);
 				
 				
 				//printf("x_dir=%f  y_dir=%f \n",x_dir,y_dir);
 				percent = (hist[i]/amount)*5;
 				if( i==0 ){
 				p1 = cvPoint( (int)(x_dir*percent*width+centerPoint.x), (int)(y_dir*percent*height+centerPoint.y));
 				firstPoint =p1;
 			}else{
 					p2 = cvPoint( (int)(x_dir*percent*width+centerPoint.x), (int)(y_dir*percent*height+centerPoint.y));
 					 cvLine( showImage, p1,p2,CV_RGB(255,255,0),2);// draw 
					p1=p2;
 				}
 				
 			if( i==(bin-1) ){
 					cvLine( showImage,p1,firstPoint,CV_RGB(255,255,0),2);// draw 

 				}
 				
 				
 			}
 		
 	}



  
 
int*  conv(int *X,int *H,int X_len,int H_len){

	int Y_len = X_len+H_len-1;
	int *Y = new int[Y_len];
	for(int i=0;i<Y_len;i++)
		Y[i]=0;
	int n,k;
	int sum;
	for( n=0;n<Y_len;n++){
				sum=0;
		for( k=0;k<H_len;k++){
				if( ((n-k)>=0) && ((n-k)<X_len) )
						sum+=H[k]*X[n-k];
			}
			Y[n]=sum;
		//	printf("%d  \n",sum);
			
	}

	return Y;
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -