%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%¶¥²ãº¯Êýcamshift%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%AdamKukucka%ZachClay%MarceloMolina%CSE486Project3function[trackmovprobmovcenters]=camshift%ÓÃcamshiftËã·¨¸ú×ÙÔ˶¯ÎïÌ壬¸ú×ÙÄ¿±êÐëÊÖ¶¯Ñ¡¶¨%******************************************************************%initializevariables%******************************************************************rmin=0;%minrowvalueforsearchwindowrmax=0;%maxrowvalueforsearchwindowcmin=0;%mincolvalueforsearchwindowcmax=0;%maxcolvalueforsearchwindownumofframes=0;%numberofframesintheavithreshold=1;%thresholdforconvergencecenterold=[00];%forconvergence...previouscenterofwindowcenternew=[00];%forconvergence...newcenterofwindow%******************************************************************%Precode...loadmovieandselectinitialframe%******************************************************************%promptuserforavifilename%user_entry=input('Pleaseenteranavifilename:','s');%loadtheavifile...handleisM%´ÓµçÄÔÀï¶ÁÈ¡Ò»¸öaviÊÓƵÎļþM=aviread('G:\test.avi');%getnumberofframes%»ñÈ¡¸ÃÊÓƵÎļþµÄ×ÜÖ¡Êýnumberofframes[dontneednumberofframes]=size(M);%initializematrixtoholdcentercoordinates%´´½¨¾ØÕóimagecenters£¬ÓÃÓÚ´æ·Åÿһ֡µÄͼÏñÖÊÐÄ×ø±êimagecenters=zeros(numberofframes,2);%extractthefirstframefromtheavi%°ÑÊÓƵÎļþµÄµÚһ֡ת»¯³ÉͼÏñImage1Frame1=M(1,1);Image1=frame2im(Frame1);%%%**********images(:,:,numberofframes)=G(:,:);%getsearchwindowforfirstframe%µ÷ÓÃ×Ô¶¨ÒåµÄÄ¿±êÑ¡¶¨º¯Êýselect£¨£©£¬µÃµ½Ä¿±ê¾ØÐÎ×óÉϽÇÓÒϽÇ×ø±ê£¨È¡ÕûµÄ£©[cmin,cmax,rmin,rmax]=select(Image1);cmin=round(cmin);cmax=round(cmax);rmin=round(rmin);rmax=round(rmax);%¼ÆËãÄ¿±êµÄ³ß´çwsize£º³¤Óë¸ßwsize(1)=abs(rmax-rmin);wsize(2)=abs(cmax-cmin);%createhistogram%translatetohsv%°ÑԴͼÏñת³Éhsv¸ñʽ£¬È¡³öÐèÒªµÄhͨµÀ£¬¸³¸øhuenormhsvimage=rgb2hsv(Image1);%pulloutthehhuenorm=hsvimage(:,:,1);%scaleto0to255%°ÑhͨµÀµÄÊý¾Ý´óС´Ó0-1µ÷Ϊ0-255£¬²¢×ª»¯³Éuint8¸ñʽhue=huenorm*255;%setunittypehue=uint8(hue);%GettingHistogramofImage:histogram=zeros(256);%¼ÆËãÄ¿±ê¾ØÐÎÄÚµÄÉ«µ÷Ö±·½Í¼histogramfori=rmin:rmaxforj=cmin:cmaxindex=uint8(hue(i,j)+1);%countnumberofeachpixelhistogram(index)=histogram(index)+1;endend%******************************************************************%Algorithmfrompdf%******************************************************************%н¨Á½¸öaviÊÓƵÎļþaviobj1=avifile('example3.avi');aviobj2=avifile('example4.avi');%foreachframe%¶ÔaviÊÓƵÎļþµÄÇ°200Ö¡½øÐÐcamshift¸ú×Ùfori=1:200%½«Ã¿Ò»Ö¡×ª³ÉͼÏñ½øÐд¦Àídisp('Processingframe');disp(i);Frame=M(1,i);I=frame2im(Frame);%ͼÏñת³Éhsv¸ñʽ£¬²¢³éÈ¡ÆäÖеÄhͨµÀ£¬´æÓÚhuenorm%translatetohsvhsvimage=rgb2hsv(I);%pulloutthehhuenorm=hsvimage(:,:,1);%0-1µÄdoubleת³É0-255µÄuint8¸ñʽ%scaleto0to255hue=huenorm*255;%setunittypehue=uint8(hue);%»ñÈ¡Õûһ֡ͼÏñµÄ·Ö±æÂÊ£ºrows*cols[rowscols]=size(hue);%chooseinitialsearchwindow%thesearchwindowis(cmin,rmin)to(cmax,rmax)%createaprobabilitymapprobmap=zeros(rows,cols);forr=1:rowsforc=1:colsif(hue(r,c)~=0)%histogramÊÇ×Ô¶¨Òå¾ØÕ󣬶ÔӦλÖôæ·ÅÁËÕâµãµÄÑÕÉ«³öÏֵĸöÊý£¬Êµ¼ÊÉϾÍÊÇ·´ÏòͶӰͼback_project£¨prob£©%Ϊʲôhue==0ʱ²»¿¼ÂÇÄØ£¿ÕâÑù²»¾Í¶ªÊ§ÁËhue==0ÕâÖÖÑÕÉ«ÁËÂð£¿%ÒòΪprobmap³õʼ»¯¾ÍÊÇ0£¬Ã»±ØÒªÖØи³0Â𣿲»ÊÇ£¬ÊÇÒòΪ¾ØÕ󣨣©Àï²»ÄܵÈÓÚ0probmap(r,c)=histogram(hue(r,c));endendend%½«Ö±·½Í¼µÄ·´ÏòͶӰͼ¹éÒ»»¯µ½256£¨ÕâÑù×öÊǵ±Ä¿±ê½Ï´ó£¨Èç100*100£©Ê±£¬¿ÉÄÜÓÐЩ¾ØÕóÔªËØ»áºÜ´ó£¬Õâô´¦ÀíÄÜ·½±ãºóÃæµÄ¼ÆË㣩probmap=probmap/max(max(probmap));%ÓÃÁËÁ½´Îmax()ÊÇÒòΪ¾ØÕóÊǶþάµÄprobmap=probmap*255;count=0;%°Ñ´ý¼ì²âµÄÒ»Ö¡µÄÄ¿±êÖÐÐĶ¨ÔÚԵ㣨0,0£©rowcenter=0;%anynumberjustsoitrunsthroughatleasttwicecolcenter=0;rowcenterold=30;colcenterold=30;%MeanShiftfor15iterationsoruntilconvergence(thecenterdoesnt%change)%Ä¿±êÖÐÐÄÎó²î¡Ü2ʱ£¬»òÕß¼ì²â´ÎÊý³¬¹ý15´Îʱ£¬½éÉÜÑ»·while(((abs(rowcenter-rowcenterold)2)&&(abs(colcenter-colcenterold)2))||(count15))%forj=1:5%disp('meanshift');%disp(j);%ÏÈÀ©´ó´ý¼ì²â¾ØÐΣ¬ÕâʱËÑË÷´°Îª15*15£¬È»ºóµ÷ÓÃmeanshiftËã·¨µÃµ½Ä¿±êÖÐÐÄ£¬Ôٵõ½ÔÀ´´óСµÄÄ¿±ê¾ØÐÎrmin=rmin-7;%increasewindowsizeandcheckforcenterrmax=rmax+7;%Ôö´óËÑË÷´°µÄ´óСΪ15*15cmin=cmin-7;cmax=cmax+7;rowcenterold=rowcenter;%saveoldcenterforconvergencecheckcolcenterold=colcenter;%µ÷ÓÃmeanshiftº¯Êý»ñµÃÖÊÐÄ×ø±ê[rowcentercolcenterM00]=meanshift(I,rmin,rmax,cmin,cmax,probmap);%givenimage(I),searchwindow(rminrmaxcmincmax)%returnsnewcenter(colcenter,rowcenter)forwindowand%zerothmoment(Moo)%redeterminewindowaroundnewcenterrmin=round(rowcenter-wsize(1)/2);rmax=round(rowcenter+wsize(1)/2);cmin=round(colcenter-wsize(2)/2);cmax=round(colcenter+wsize(2)/2);wsize(1)=abs(rmax-rmin);wsize(2)=abs(cmax-cmin);count=count+1;end%markcenteronimage%saveimage%°ÑRGB¸ñʽµÄͼÏñIת»»³É»Ò¶ÈͼÏñtrackimG=.2989*I(:,:,1)+.5870*I(:,:,2)+.1140*I(:,:,3);trackim=G;%makeboxofcurrentsearchwindowonsavedimage%°Ñtrackim¾ØÐεÄÄ¿±ê±ß¿ò¶¼¸³ÖµÎª255£¬¼´±ê³ö¸ú×Ùµ½µÄ´°¿ÚµÄ°×É«±ß¿òforr=rmin:rmaxtrackim(r,cmin)=255;trackim(r,cmax)=255;endforc=cmin:cmaxtrackim(rmin,c)=255;trackim(rmax,c)=255;end%ÔÚн¨µÄÁ½¸öÊÓƵÎļþaviobj1£¬aviobj2Àï²åÈëÖ¡£¬Ò»¸öÊÇ»³öÁË·½¿òµÄÊÓƵÎļþ£¬ÁíÒ»¸öÊÇ·´ÏòͶӰͼµÄÊÓƵÎļþaviobj1=addframe(aviobj1,trackim);aviobj2=addframe