opencv手势识别.docx
《opencv手势识别.docx》由会员分享,可在线阅读,更多相关《opencv手势识别.docx(11页珍藏版)》请在冰豆网上搜索。
![opencv手势识别.docx](https://file1.bdocx.com/fileroot1/2022-11/25/6d196926-9573-47dc-bc16-1179d6a8561f/6d196926-9573-47dc-bc16-1179d6a8561f1.gif)
opencv手势识别
#ifdef_CH_
#defineWIN32
#error"Thefileneedscvaux,whichisnotwrappedyet.Sorry"
#endif
#ifndef_EiC
#include"cv.h"
#include"cxcore.h"
#include"cvaux.h"
#include"highgui.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#endif
staticCvMemStorage*storage;
//staticdoublecolor;
staticCvSizeimgSize;
staticCvSizetmplSize;
staticCvSeq*contour;
staticCvSeq*biggest;
staticCvCapture*capture=0;
staticintisHandOpen=1;
//staticdoublecolor;
staticCvScalarcolor;
staticIplImage*img;
staticIplImage*tmpImg;
staticIplImage*conv;
staticIplImage*H;
staticIplImage*S;
staticIplImage*V;
staticIplImage*tmpH1;
staticIplImage*tmpS1;
staticIplImage*tmpH2;
staticIplImage*tmpS2;
staticIplImage*tmpH3;
staticIplImage*tmpS3;
staticIplImage*openHandTmpl,*closedHandTmpl;
staticIplImage*openGrayHandTmpl,*closedGrayHandTmpl;
staticIplImage*openscaledTmpl,*closedscaledTmpl;
staticIplImage*openMatchResult,*closedMatchResult;
FILE*output;
voidhandDetect(IplImage*img);
staticintnumImg=10;
char*names[]={"./images/test01.jpg","./images/test02.jpg","./images/test03.jpg","./images/test04.jpg","./images/test05.jpg",
"../images/test06.jpg","./images/test07.jpg","./images/test08.jpg","./images/test09.jpg","./images/test10.jpg"};
char*resultsiamge[]={"result01.jpg","result02.jpg","result03.jpg","result04.jpg","result05.jpg","result06.jpg","result07.jpg",
"result08.jpg","result09.jpg","result10.jpg"};
intmain(intargc,char**argv)
{
inti=0;
charfilename[80];
storage=cvCreateMemStorage(0);
#ifdefDEBUG
cvNamedWindow("Template1",1);
cvNamedWindow("Template2",1);
cvNamedWindow("CompResults1",1);
cvNamedWindow("CompResults2",1);
cvNamedWindow("SkinDetection",1);
#endif
openHandTmpl=cvLoadImage("openHandTmpl.jpg",1);
closedHandTmpl=cvLoadImage("closedHandTmpl.jpg",1);
//ConvertformRGBtoGRAY
tmplSize.width=openHandTmpl->width;
tmplSize.height=openHandTmpl->height;
openGrayHandTmpl=cvCreateImage(tmplSize,IPL_DEPTH_8U,1);
cvCvtColor(openHandTmpl,openGrayHandTmpl,CV_BGR2GRAY);
tmplSize.width=closedHandTmpl->width;
tmplSize.height=closedHandTmpl->height;
closedGrayHandTmpl=cvCreateImage(tmplSize,IPL_DEPTH_8U,1);
cvCvtColor(closedHandTmpl,closedGrayHandTmpl,CV_BGR2GRAY);
cvNamedWindow("source",1);
cvMoveWindow("source",0,0);
cvNamedWindow("Results",1);
cvMoveWindow("Results",400,300);
cvAddSearchPath("\\images\\");
//OpentheOutputFile
output=fopen("results.log","w");
//Colorforcontourdrawing
color=CV_RGB(255,255,255);
if(argc==1||(argc==2&&strlen(argv[1])==1&&isdigit(argv[1][0])))
{
capture=cvCaptureFromCAM(argc==2?
argv[1][0]-'0':
0);
}
elseif(argc==2)
{
capture=cvCaptureFromAVI(argv[1]);
}
if(capture!
=0)
{
for(;;)
{
IplImage*frame;//,*frame_copy;
if(!
cvGrabFrame(capture))
break;
frame=cvRetrieveFrame(capture);
if(!
frame)
break;
imgSize.width=frame->width;
imgSize.height=frame->height;
// frame_copy=cvCloneImage(frame);
handDetect(frame);
if(cvWaitKey(10)>=0)
break;
}
cvReleaseCapture(&capture);
}
else
{
for(i=0;i {
fprintf(output,"Image%i%s************************\n",i+1,names);
strcpy(filename,"result_");
// cvNamedWindow(names,1);
// cvNamedWindow("source",1);
// cvMoveWindow("source",0,0);
img=cvLoadImage(names,1);
cvShowImage("source",img);
cvWaitKey(0);
if(img)
{
imgSize.width=img->width;
imgSize.height=img->height;
handDetect(img);
// strcat(filename,names);
cvSaveImage(resultsiamge,img);
cvReleaseImage(&img);
}
}
cvWaitKey(0);
// for(i=0;i// {
// cvDestroyWindow(names);
// }
}
cvReleaseImage(&tmpImg);
cvReleaseImage(&conv);
cvReleaseImage(&H);
cvReleaseImage(&S);
cvReleaseImage(&V);
cvReleaseImage(&tmpH1);
cvReleaseImage(&tmpH2);
cvReleaseImage(&tmpH3);
cvReleaseImage(&tmpS1);
cvReleaseImage(&tmpS2);
cvReleaseImage(&tmpS3);
cvReleaseImage(&openHandTmpl);
cvReleaseImage(&closedHandTmpl);
cvReleaseImage(&openGrayHandTmpl);
cvReleaseImage(&closedGrayHandTmpl);
cvReleaseImage(&openscaledTmpl);
cvReleaseImage(&closedscaledTmpl);
cvReleaseImage(&openMatchResult);
cvReleaseImage(&closedMatchResult);
cvReleaseMemStorage(&storage);
fclose(output);
// cvDestroyWindow("Result");
#ifdefDEBUG
cvDestroyWindow("Template1");
cvDestroyWindow("Template2");
cvDestroyWindow("CompResults1");
cvDestroyWindow("CompResults2");
cvDestroyWindow("SkinDetection");
#endif
return0;
}
voidhandDetect(IplImage*img)
{
//CvSizetmplSize;
CvSizeresultSize;
IplConvKernel*erosionElement,*dilationElement;
doublecontArea,imgArea,maxRatio=0.0;
CvRectbndRect=cvRect(0,0,0,0);
CvPointpt1,pt2;
floatscaleFactor;
CvScalaraverageValue;
intstartCount=0;
intopenCount=0;
intclosedCount=0;
floatopenCompRatio=0.0;
floatclosedCompRatio=0.0;
floatbestRatio=0.0;
imgArea=imgSize.width*imgSize.height;
// OpenTemplatefiles
// openHandTmpl=cvLoadImage("openHandTmpl.jpg",1);
// closedHandTmpl=cvLoadImage("closedHandTmpl.jpg",1);
// //ConvertformRGBtoGRAY
// tmplSize.width=openHandTmpl->width;
// tmplSize.height=openHandTmpl->height;
//
// openGrayHandTmpl=cvCreateImage(tmplSize,IPL_DEPTH_8U,1);
// cvCvtColor(openHandTmpl,openGrayHandTmpl,CV_BGR2GRAY);
//
// tmplSize.width=closedHandTmpl->width;
// tmplSize.height=closedHandTmpl->height;
//
// closedGrayHandTmpl=cvCreateImage(tmplSize,IPL_DEPTH_8U,1);
// cvCvtColor(closedHandTmpl,closedGrayHandTmpl,CV_BGR2GRAY);
//Generateprocessingimages
tmpImg=cvCloneImage(img);
conv=cvCreateImage(imgSize,IPL_DEPTH_8U,3);
tmpH1=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
tmpS1=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
tmpH2=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
tmpS2=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
tmpH3=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
tmpS3=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
H=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
S=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
V=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
//Fliptheimageifincapturemode
if(capture)
{
cvFlip(conv,conv,0);
}
//ImageSmoothing
//cvSmooth(img,tmpImg,CV_BLUR,3,3);
cvSmooth(img,img,CV_GAUSSIAN,3,3);
//ConverttoHSV
cvCvtColor(tmpImg,conv,CV_BGR2HSV);
//SplittoHSVplanes
cvCvtPixToPlane(conv,H,S,V,0);
//AverageIllumination
averageValue=cvAvg(V,0);
fprintf(output,"IlluminationLevel=%f\n",averageValue.val[0]);
//DetectskintoneHuesandSaturations
//NOTE:
Huevaluesneedtobedoubledforactual
//RedtoOrangeHuewithHighSaturation
//Hue0to28degreeandSat190to200
cvInRangeS(H,cvScalar(0.0,0.0,0,0),cvScalar(14.0,0.0,0,0),tmpH1);
cvInRangeS(S,cvScalar(75.0,0.0,0,0),cvScalar(200.,0.0,0,0),tmpS1);
cvAnd(tmpH1,tmpS1,tmpH1,0);
//RedHuewithLowSaturation
//Hue0to26degreeandSat20to90
cvInRangeS(H,cvScalar(0.0,0.0,0,0),cvScalar(13.0,0.0,0,0),tmpH2);
cvInRangeS(S,cvScalar(20.0,0.0,0,0),cvScalar(90.0,0.0,0,0),tmpS2);
cvAnd(tmpH2,tmpS2,tmpH2,0);
//RedHuetoPinkwithLowSaturation
//Hue340to360degreeandSat15to90
cvInRangeS(H,cvScalar(170.0,0.0,0,0),cvScalar(180.0,0.0,0,0),tmpH3);
cvInRangeS(S,cvScalar(15.0,0.0,0,0),cvScalar(90.,0.0,0,0),tmpS3);
cvAnd(tmpH3,tmpS3,tmpH3,0);
//CombinetheHueandSatdetections
cvOr(tmpH3,tmpH2,tmpH2,0);
cvOr(tmpH1,tmpH2,tmpH1,0);
//DilationandErosion
//StructuringElementGeneration
dilationElement=cvCreateStructuringElementEx(5,5,3,3,CV_SHAPE_RECT,0);
erosionElement=cvCreateStructuringElementEx(5,5,3,3,CV_SHAPE_RECT,0);
//Dilationaddsalayeron,andreturnsthingstothecorrectsize.
cvDilate(tmpH1,tmpH2,dilationElement,1);
//Erosionpeelsalayerofpixelsoff,andmakessmallregionsdisappear
cvErode(tmpH1,tmpH3,erosionElement,1);
//Findthecontoursofall