您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 运动目标跟踪与检测的源代码(CAMSHIFT-算法)
该运行文件在VC6.0环境下编译通过,是一个stand-alone运行程序,不需要OPENCV的DLL库支持。在运行之前,请先连接好USB接口的摄像头。然后可以用鼠标选定欲跟踪目标。#ifdef_CH_#pragmapackageopencv#endif#ifndef_EiC#includecv.h#includehighgui.h#includestdio.h#includectype.h#endifIplImage*image=0,*hsv=0,*hue=0,*mask=0,*backproject=0,*histimg=0;CvHistogram*hist=0;intbackproject_mode=0;intselect_object=0;inttrack_object=0;intshow_hist=1;CvPointorigin;CvRectselection;CvRecttrack_window;CvBox2Dtrack_box;//tracking返回的区域box,带角度CvConnectedComptrack_comp;inthdims=48;//划分HIST的个数,越高越精确floathranges_arr[]={0,180};float*hranges=hranges_arr;intvmin=10,vmax=256,smin=30;voidon_mouse(intevent,intx,inty,intflags){if(!image)return;if(image-origin)y=image-height-y;if(select_object){selection.x=MIN(x,origin.x);selection.y=MIN(y,origin.y);selection.width=selection.x+CV_IABS(x-origin.x);selection.height=selection.y+CV_IABS(y-origin.y);selection.x=MAX(selection.x,0);selection.y=MAX(selection.y,0);selection.width=MIN(selection.width,image-width);selection.height=MIN(selection.height,image-height);selection.width-=selection.x;selection.height-=selection.y;}switch(event){caseCV_EVENT_LBUTTONDOWN:origin=cvPoint(x,y);selection=cvRect(x,y,0,0);select_object=1;break;caseCV_EVENT_LBUTTONUP:select_object=0;if(selection.width0&&selection.height0)track_object=-1;#ifdef_DEBUGprintf(/n#鼠标的选择区域:);printf(/nX=%d,Y=%d,Width=%d,Height=%d,selection.x,selection.y,selection.width,selection.height);#endifbreak;}}CvScalarhsv2rgb(floathue){intrgb[3],p,sector;staticconstintsector_data[][3]={{0,2,1},{1,2,0},{1,0,2},{2,0,1},{2,1,0},{0,1,2}};hue*=0.033333333333333333333333333333333f;sector=cvFloor(hue);p=cvRound(255*(hue-sector));p^=sector&1?255:0;rgb[sector_data[sector][0]]=255;rgb[sector_data[sector][1]]=0;rgb[sector_data[sector][2]]=p;#ifdef_DEBUGprintf(/n#ConvertHSVtoRGB:);printf(/nHUE=%f,hue);printf(/nR=%d,G=%d,B=%d,rgb[0],rgb[1],rgb[2]);#endifreturncvScalar(rgb[2],rgb[1],rgb[0],0);}intmain(intargc,char**argv){CvCapture*capture=0;IplImage*frame=0;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){fprintf(stderr,Couldnotinitializecapturing.../n);return-1;}printf(Hotkeys:/n/tESC-quittheprogram/n/tc-stopthetracking/n/tb-switchto/frombackprojectionview/n/th-show/hideobjecthistogram/nToinitializetracking,selecttheobjectwithmouse/n);//cvNamedWindow(Histogram,1);cvNamedWindow(CamShiftDemo,1);cvSetMouseCallback(CamShiftDemo,on_mouse);//on_mouse自定义事件cvCreateTrackbar(Vmin,CamShiftDemo,&vmin,256,0);cvCreateTrackbar(Vmax,CamShiftDemo,&vmax,256,0);cvCreateTrackbar(Smin,CamShiftDemo,&smin,256,0);for(;;){inti,bin_w,c;frame=cvQueryFrame(capture);if(!frame)break;if(!image){/*allocateallthebuffers*/image=cvCreateImage(cvGetSize(frame),8,3);image-origin=frame-origin;hsv=cvCreateImage(cvGetSize(frame),8,3);hue=cvCreateImage(cvGetSize(frame),8,1);mask=cvCreateImage(cvGetSize(frame),8,1);backproject=cvCreateImage(cvGetSize(frame),8,1);hist=cvCreateHist(1,&hdims,CV_HIST_ARRAY,&hranges,1);//计算直方图histimg=cvCreateImage(cvSize(320,200),8,3);cvZero(histimg);}cvCopy(frame,image,0);cvCvtColor(image,hsv,CV_BGR2HSV);//彩色空间转换BGRtoHSVif(track_object){int_vmin=vmin,_vmax=vmax;cvInRangeS(hsv,cvScalar(0,smin,MIN(_vmin,_vmax),0),cvScalar(180,256,MAX(_vmin,_vmax),0),mask);//得到二值的MASKcvSplit(hsv,hue,0,0,0);//只提取HUE分量if(track_object0){floatmax_val=0.f;cvSetImageROI(hue,selection);//得到选择区域forROIcvSetImageROI(mask,selection);//得到选择区域formaskcvCalcHist(&hue,hist,0,mask);//计算直方图cvGetMinMaxHistValue(hist,0,&max_val,0,0);//只找最大值cvConvertScale(hist-bins,hist-bins,max_val?255./max_val:0.,0);//缩放bin到区间[0,255]cvResetImageROI(hue);//removeROIcvResetImageROI(mask);track_window=selection;track_object=1;cvZero(histimg);bin_w=histimg-width/hdims;//hdims:条的个数,则bin_w为条的宽度//画直方图for(i=0;ihdims;i++){intval=cvRound(cvGetReal1D(hist-bins,i)*histimg-height/255);CvScalarcolor=hsv2rgb(i*180.f/hdims);cvRectangle(histimg,cvPoint(i*bin_w,histimg-height),cvPoint((i+1)*bin_w,histimg-height-val),color,-1,8,0);}}cvCalcBackProject(&hue,backproject,hist);//使用backproject方法cvAnd(backproject,mask,backproject,0);//callingCAMSHIFT算法模块cvCamShift(backproject,track_window,cvTermCriteria(CV_TERMCRIT_EPS|CV_TERMCRIT_ITER,10,1),&track_comp,&track_box);track_window=track_comp.rect;if(backproject_mode)cvCvtColor(backproject,image,CV_GRAY2BGR);//使用backproject灰度图像if(image-origin)track_box.angle=-track_box.angle;cvEllipseBox(image,track_box,CV_RGB(255,0,0),3,CV_AA,0);}if(select_object&&selection.width0&&selection.height0){cvSetImageROI(image,selection);cvXorS(image,cvScalarAll(255),image,0);cvResetImageROI(image);}cvShowImage(CamShiftDemo,image);cvShowImage(Histogram,histimg);c=cvWaitKey(10);if(c==27)break;//exitfromfor-loopswitch(c){case'b':backproject_mode^=1;break;case'c':track_object=0;cvZero(histimg);break;case'h':show_hist^=1;if(!show_hist)cvDestroyWindow(Histogram);elsecvNamedWindow(Histogram,1);break;default:;}}cvReleaseCapture(&capture);cvDestroyWindow(CamShiftDe
本文标题:运动目标跟踪与检测的源代码(CAMSHIFT-算法)
链接地址:https://www.777doc.com/doc-4141131 .html