作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
/** * HandVu - a library for computer vision-based hand gesture * recognition. * Copyright (C) 2004 Mathias Kolsch, matz@cs.ucsb.edu * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * $Id: hv_OpenCV.cpp,v 1.15 2006/01/03 21:44:15 matz Exp $ **/ /********************************* 2009.3.15 gnuhpc@gmail.com Show How to get the hand state using the socket in a thread. Copyright (C) GPL ***********************************/ #ifdef WIN32 #include <windows.h> #endif #include <stdio.h> #include <unistd.h> #include <signal.h> #include <stdlib.h> #include <sys/time.h> #include <errno.h> #include <pthread.h> #include <sys/socket.h> #include <sys/types.h> #include <arpa/inet.h> #include <string.h> #include <netinet/in.h> #include <cv.h> #include <highgui.h> #include <ctype.h> #include "HandVu.h" #define BILLION 1000000000L #define BUFFSIZE 256 IplImage *capture_image = 0; IplImage *display_image = 0; IplImage *routing_image = 0; bool async_processing = false; int num_async_bufs = 30; IplImage *m_async_image = 0; int m_async_bufID = -1; bool sync_display = true; CvPoint origin; int select_object = 0; int sel_area_left=0, sel_area_top=0, sel_area_right=0, sel_area_bottom=0; bool correct_distortion = false; hvState state; int c; void* thread(void *arg) { int socketfd; int len; struct sockaddr_in address; int result; char buffer[256]; int received = 0; if(!(socketfd = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))) { perror("Error when creating socket!/n"); pthread_exit(NULL); } memset(&address,0,sizeof(address)); address.sin_family = AF_INET; address.sin_addr.s_addr= inet_addr("127.0.0.1"); address.sin_port = htons(1394); if( connect(socketfd,(struct sockaddr *)&address,sizeof(address))<0) { perror("Error when connecting socket!/n"); pthread_exit(NULL); } while(1) { int bytes=0; if( (bytes = recv(socketfd,buffer,BUFFSIZE-1,0))<1 ) { perror("Error When Reading the socket!/n"); pthread_exit(NULL); } buffer[bytes]='/0'; printf("%s/n",buffer); if( c==27 ) { break; pthread_exit(NULL); } } } static void myhandler(int s) { cvZero(routing_image); } void OnMouse( int event, int x, int y, int /*flags*/, void* /*params*/ ) { if( !capture_image ) return; if( capture_image->origin ) y = capture_image->height - y; if( select_object ) { sel_area_left = MIN(x,origin.x); sel_area_top = MIN(y,origin.y); sel_area_right = sel_area_left + CV_IABS(x - origin.x); sel_area_bottom = sel_area_top + CV_IABS(y - origin.y); sel_area_left = MAX( sel_area_left, 0 ); sel_area_top = MAX( sel_area_top, 0 ); sel_area_right = MIN( sel_area_right, capture_image->width ); sel_area_bottom = MIN( sel_area_bottom, capture_image->height ); if( sel_area_right-sel_area_left > 0 && sel_area_bottom-sel_area_top> 0 ) hvSetDetectionArea(sel_area_left, sel_area_top, sel_area_right, sel_area_bottom); } switch( event ) { case CV_EVENT_LBUTTONDOWN: origin = cvPoint(x,y); sel_area_left = sel_area_right = x; sel_area_top = sel_area_bottom = y; select_object = 1; break; case CV_EVENT_LBUTTONUP: select_object = 0; break; } } void showFrame(IplImage* img, hvAction action, hvState state) { int underline; CvFont font; CvSize textsize; char text[256]; cvInitFont(&font,CV_FONT_HERSHEY_PLAIN,1.0,1.0,0.0,1,8); if (action==HV_DROP_FRAME) { // HandVu recommends dropping the frame entirely // printf("HandVuFilter: dropping frame/n"); return; } else if (action==HV_SKIP_FRAME) { // HandVu recommends displaying the frame, but not doing any further // processing on it - keep going // printf("HandVuFilter: supposed to skip frame/n"); } else if (action==HV_PROCESS_FRAME) { // full processing was done and is recommended for following steps; // keep going //printf("HandVuFilter: processed frame/n"); } else { assert(0); // unknown action } sprintf(text,"x=%f,y=%f",100.0*state.center_xpos,100.0*state.center_ypos); cvGetTextSize( text, &font, &textsize, &underline ); cvPutText(img,text,cvPoint(img->width/2,img->height-10), &font,CV_RGB(0,255,0)); cvShowImage( "HandVu", img ); cvCircle(routing_image,cvPoint(300.0*state.center_xpos,300.0*state.center_ypos),1,CV_RGB(255,0,0),-1,CV_AA,0); cvShowImage("Routing",routing_image); } int main( int argc, char** argv ) { int t=123;//For testing int retv; int flag1=1; pthread_t id1; CvCapture* capture = 0; struct itimerval value; struct sigaction act; act.sa_handler = myhandler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGPROF,&act,NULL); value.it_interval.tv_sec=1; value.it_interval.tv_usec=0; value.it_value = value.it_interval; if((setitimer(ITIMER_PROF,&value,NULL)==-1)) { perror("Set timer Error!/n"); return -1; } if (argc<2) { printf("you need to specify a conductor file as first argument/n"); printf("for example: ../config/default.conductor/n"); return -1; } string conductor_fname(argv[1]);//声明配置参数的对象 printf("will load conductor from file:/n%s/n", conductor_fname.c_str());//屏显提示 /*是否设定特定的摄像头,并初始化摄像头 */ if( argc == 2 || argc == 3) { int num = 0; if (argc==3) { num = atoi(argv[2]); } capture = cvCaptureFromCAM( num ); if (!capture) { capture = cvCaptureFromAVI( argv[2] ); } } if( !capture ) { fprintf(stderr,"Could not initialize capturing through OpenCV./n"); return -1; } /* 屏显提示 */ printf( "Hot keys: /n" "/tESC - quit the program/n" "/tr - restart the tracking/n" "/t0-3 - set the overlay (verbosity) level/n" "use the mouse to select the initial detection area/n" ); routing_image = cvCreateImage(cvSize(300,300),IPL_DEPTH_8U,3 ); //设定采集图像大小 int p = 0; // according to docs, these calls don't work in OpenCV beta 4 yet p = cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 640); p = cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 480); //获取一帧 capture_image = cvQueryFrame( capture ); if ( !capture_image ) { fprintf(stderr,"Could not retrieve image through OpenCV./n"); return -1; } /* allocate all the buffers */ CvSize size = cvGetSize(capture_image); hvInitialize(size.width, size.height);//初始化要分析的图像大小 hvLoadConductor(conductor_fname);//装载参数 hvStartRecognition();//开始识别 hvSetOverlayLevel(2);//设置识别的覆盖区级别 /* 设置鼠标事件的回调参数 */ cvSetMouseCallback( "HandVu", OnMouse ); /* 设置窗口 */ int success1 = cvNamedWindow( "HandVu", 1 ); int success2 = cvNamedWindow( "Routing", 1 ); cvResizeWindow("Routing",300,300); cvMoveWindow("Routing",1000,0); fprintf(stderr, "initialized highgui/n"); if ((success1!=1)&&(success2!=1)) { printf("can't open window - did you compile OpenCV with highgui support?"); return -1; } hvStartGestureServer(1394,10); retv = pthread_create(&id1,NULL,thread,NULL); if( retv ) { perror("Error when creating a thread!/n"); return -1; } retv = pthread_detach(id1); if( retv ) { perror("Error when deteching a thread!/n"); return -1; } for (;;) { // synchronous processing in HandVu // ------- main library call --------- hvAction action = HV_INVALID_ACTION; action = hvProcessFrame(capture_image); // ------- hvGetState(0, state); showFrame(capture_image, action,state); c = cvWaitKey(10); if( c == 27 || c == 'q' ) break; switch( c ) { case 'r': hvStopRecognition(); hvStartRecognition(); break; case '0': hvSetOverlayLevel(0); break; case '1': hvSetOverlayLevel(1); break; case '2': hvSetOverlayLevel(2); break; case '3': hvSetOverlayLevel(3); break; case 'u': if (hvCanCorrectDistortion()) { correct_distortion = !correct_distortion; hvCorrectDistortion(correct_distortion); } break; default: ; } // capture next image capture_image = cvQueryFrame( capture ); if ( !capture_image ) { fprintf(stderr,"Could not retrieve image through OpenCV./n"); break; } } cvReleaseCapture( &capture ); cvDestroyWindow("HandVu"); cvDestroyWindow("Routing"); return 0; }