VidSegm.cpp

00001 
00008 #include <iostream>
00009 #include <string>
00010 #include<fstream>
00011 
00012 #include "opencv/cv.h"
00013 #include "opencv/cxcore.h"
00014 #include "opencv/highgui.h"
00015 
00016 
00017 #include "options.h"
00018 #include "utils/libutils.h"
00019 #include "video/libvideo.h"
00020 #include "stats/libstats.h"
00021 #include "dytex/libdytex.h"
00022 #include"platInd/platInd.hpp"
00023 
00024 
00025 using namespace cv;
00026 using namespace std;
00027 
00068 VidSegm::VidSegm(int winxy,int winz,int stepxy,int stepz,PatchOptions::norm_type nt,double bkv,int K,int n,double reg,int nf,int bkvf):popt(winxy,winz,stepxy,stepz,nt,bkv,bkvf)
00069         ,dtm(DytexOptions(n,winxy*winxy,CovMatrix::COV_IID,CovMatrix::COV_DIAG,nt==PatchOptions::NORM_NONE?DytexOptions::NONZERO_YMEAN:DytexOptions::ZERO_YMEAN))
00070 {
00071         this->K=K;
00072         this->regV=reg;
00073         this->n=n;
00074         nfrm=nf;
00075 }
00076 
00093 void VidSegm::learnDTM(string vpath,bool dosegm)
00094 {
00095         //load training video
00096         Mat img=MatVid::loaddat(vpath,"t");     
00097 
00098         //Get the required video frames
00099         if(nfrm!=0)
00100         {
00101                 img=MatVid::subvid(img,Range(0,nfrm),Range::all(),Range::all());
00102         }
00103                 
00104         cout<<"Training video size: "<<img.size[0]<<" "<<img.size[1]<<" "<<img.size[2]<<endl;
00105         
00106         //Get patches
00107         PatchBatchExtractor pbe(img, popt);
00108         cout<<"Found Patches: "<<pbe.patches.size()<<endl;
00109 
00110         //Learn the mixture
00111         DytexRegOptions ropt(CovMatrix::COV_REG_MIN,regV,CovMatrix::COV_REG_MIN,regV,CovMatrix::COV_REG_MIN,regV);
00112         EMOptions emopt(K,ropt,1e-4,EMOptions::COMPACT);        
00113         
00114         clock_t s_time=clock();
00115         dtm.learnWithSplitting(pbe.patches,emopt);
00116         clock_t e_time=clock()-s_time;
00117         cout<<"Total Time Taken :"<<(double)e_time/(double)CLOCKS_PER_SEC<<endl;
00118         
00119         if(dosegm)
00120         {
00121                 //Do the initial segmentation   
00122                 Mat smask=pbe.segm_mask(dtm.classes);   
00123                 smask=maxvotefilt(smask,5,5,5,K);
00124                 colorMask(img,smask,initSegm,1);
00125         }
00126 
00127 }
00128 
00159 void VidSegm::segmentVideoSequence(std::vector<string> paths,int stepxy,int stepz,int filtxy,int filtz,string vidPath2)
00160 {
00161         //segment all videos in the sequence
00162         for(int i=0;i<paths.size();i++)
00163         {
00164                 cout<<"Segmenting: "<<paths[i]<<endl;
00165                 Mat segMask;
00166                 Mat segVideo;
00167 
00168                 //first video in the sequence, special case
00169                 if(i==0)
00170                 {
00171                         Mat postFrames;
00172                         //is there any next video in sequence
00173                         if(paths.size()>1)
00174                         {
00175                                 Mat vid2=MatVid::loaddat(paths[i+1],"t");                               
00176                                 int vsize=vid2.size[0];
00177                                 Mat tempM=MatVid::subvid(vid2,Range(0,popt.win.z-stepz),Range::all(),Range::all());                     
00178                                 tempM.copyTo(postFrames);
00179                                 vid2.release();
00180                         }
00181                         cout<<"PostFrames :"<<postFrames.size[0]<<endl;
00182                         Mat vid1=MatVid::loaddat(paths[i],"t");                 
00183                         cout<<"Vid1-Frames"<<vid1.size[0]<<endl;
00184                         Mat newVid;
00185                         if(paths.size()>1)
00186                         {
00187                                 newVid=MatVid::create(postFrames.size[0]+vid1.size[0],vid1.size[1],vid1.size[2],vid1.type());
00188                         }
00189                         else
00190                         {
00191                                 newVid=MatVid::create(vid1.size[0],vid1.size[1],vid1.size[2],vid1.type());
00192                         }
00193                         cout<<"neVid-Frames"<<newVid.size[0]<<endl;
00194 
00195                         Mat t1=MatVid::subvid(newVid,Range(0,vid1.size[0]),Range::all(),Range::all());
00196                         vid1.copyTo(t1);
00197 
00198                         if(paths.size()>1)
00199                         {
00200                                 Mat t2=MatVid::subvid(newVid,Range(vid1.size[0],newVid.size[0]),Range::all(),Range::all());
00201                                 postFrames.copyTo(t2);
00202                         }
00203 
00204                         int vid1size=vid1.size[0];
00205                         vid1.release();
00206 
00207                         Mat segMask2,segVideo2;
00208                         segVideo2=segmentVideo(newVid,stepxy,stepz,filtxy,filtz,segMask2);
00209                         (MatVid::subvid(segMask2,Range(0,vid1size),Range::all(),Range::all())).copyTo(segMask);
00210                         (MatVid::subvid(segVideo2,Range(0,vid1size),Range::all(),Range::all())).copyTo(segVideo);               
00211 
00212                 }
00213                 else if(i==(paths.size()-1)) //last video special case
00214                 {
00215                         Mat vid1=MatVid::loaddat(paths[i-1],"t");                               
00216                         int vsize=vid1.size[0];
00217                         cout<<"Vid1-Frames"<<vid1.size[0]<<endl;
00218                         Mat tempM=MatVid::subvid(vid1,Range( (vsize-popt.win.z)+stepz,vsize),Range::all(),Range::all());
00219                         Mat preFrames;
00220                         tempM.copyTo(preFrames);
00221                         vid1.release();
00222                         cout<<"Pre-Frames"<<preFrames.size[0]<<endl;                    
00223 
00224                         Mat vid2=MatVid::loaddat(paths[i],"t"); 
00225                         cout<<"Vid2-Frames"<<vid2.size[0]<<endl;
00226 
00227                         Mat newVid=MatVid::create(preFrames.size[0]+vid2.size[0],vid2.size[1],vid2.size[2],vid1.type());
00228                         cout<<"neVid-Frames"<<newVid.size[0]<<endl;
00229 
00230                         Mat t1=MatVid::subvid(newVid,Range(0,preFrames.size[0]),Range::all(),Range::all());
00231                         preFrames.copyTo(t1);
00232                         Mat t2=MatVid::subvid(newVid,Range(preFrames.size[0],newVid.size[0]),Range::all(),Range::all());
00233                         vid2.copyTo(t2);
00234                                                 
00235                         vid2.release();
00236                         Mat segMask2,segVideo2;
00237                         segVideo2=segmentVideo(newVid,stepxy,stepz,filtxy,filtz,segMask2);
00238                                                 
00239                         (MatVid::subvid(segMask2,Range(preFrames.size[0],segMask2.size[0]),Range::all(),Range::all())).copyTo(segMask);
00240                         (MatVid::subvid(segVideo2,Range(preFrames.size[0],segVideo2.size[0]),Range::all(),Range::all())).copyTo(segVideo);
00241                         
00242                 }
00243                 else //rest of the videos
00244                 {
00245                         //PRe
00246                         Mat vid1=MatVid::loaddat(paths[i-1],"t");                               
00247                         int vsize=vid1.size[0];
00248                         cout<<"Vid1-Frames"<<vid1.size[0]<<endl;
00249                         Mat tempM=MatVid::subvid(vid1,Range( (vsize-popt.win.z)+stepz,vsize),Range::all(),Range::all());
00250                         Mat preFrames;
00251                         tempM.copyTo(preFrames);
00252                         vid1.release();
00253                         cout<<"Pre-Frames"<<preFrames.size[0]<<endl;
00254 
00255                         //Post
00256                         Mat vid3=MatVid::loaddat(paths[i+1],"t");                               
00257                         int vsize3=vid3.size[0];
00258                         cout<<"Vid3-Frames"<<vid3.size[0]<<endl;                        
00259                         Mat tempM3=MatVid::subvid(vid3,Range(0,popt.win.z-stepz),Range::all(),Range::all());
00260                         Mat postFrames;
00261                         tempM3.copyTo(postFrames);                      
00262                         vid3.release();
00263                         cout<<"Post-Frames"<<postFrames.size[0]<<endl;
00264 
00265                         
00266                         Mat vid2=MatVid::loaddat(paths[i],"t"); 
00267                         cout<<"Vid2-Frames"<<vid2.size[0]<<endl;
00268 
00269                         Mat newVid=MatVid::create(preFrames.size[0]+vid2.size[0]+postFrames.size[0],vid2.size[1],vid2.size[2],vid1.type());
00270                         cout<<"neVid-Frames"<<newVid.size[0]<<endl;
00271 
00272                         Mat t1=MatVid::subvid(newVid,Range(0,preFrames.size[0]),Range::all(),Range::all());
00273                         preFrames.copyTo(t1);
00274                         Mat t2=MatVid::subvid(newVid,Range(preFrames.size[0],newVid.size[0]-postFrames.size[0]),Range::all(),Range::all());
00275                         vid2.copyTo(t2);
00276                         Mat t3=MatVid::subvid(newVid,Range(newVid.size[0]-postFrames.size[0],newVid.size[0]),Range::all(),Range::all());
00277                         postFrames.copyTo(t3);
00278                         
00279                         vid2.release();
00280                         Mat segMask2,segVideo2;
00281                         segVideo2=segmentVideo(newVid,stepxy,stepz,filtxy,filtz,segMask2);
00282                                         
00283                         (MatVid::subvid(segMask2,Range(preFrames.size[0],segMask2.size[0]-postFrames.size[0]),Range::all(),Range::all())).copyTo(segMask);
00284                         (MatVid::subvid(segVideo2,Range(preFrames.size[0],segVideo2.size[0]-postFrames.size[0]),Range::all(),Range::all())).copyTo(segVideo);
00285                 }
00286 
00287                 //save things for current video segmentation in the output place
00288                 char carr[100];
00289                 sprintf(carr,"%d",i);
00290                 string vidid(carr);
00291                 string dirpath=vidPath2 + vidid + string("_segm");
00292                 PlatInd::makedir(dirpath);
00293 
00294                 string fpath=dirpath+string("/")+string("vid") + vidid+ string("_segm_");
00295                 Mat smask=segMask;      
00296                 
00297                 if(smask.dims==2)
00298                 {
00299                         Mat img=smask;
00300 
00301                         char buffer [100];
00302                         sprintf(buffer,"%03d",0);
00303 
00304                         string filename=fpath+buffer+".png";
00305 
00306                         Mat frame=Mat::zeros(img.rows,img.cols,CV_8UC1);
00307                         for(int j=0;j<img.rows;j++)
00308                                 for(int k=0;k<img.cols;k++)
00309                                 {
00310                                         if(img.at<unsigned char>(j,k)==1)
00311                                                 frame.at<unsigned char>(j,k)=127;
00312                                         else if(img.at<unsigned char>(j,k)==2)
00313                                                 frame.at<unsigned char>(j,k)=255;
00314                                         else
00315                                                 frame.at<unsigned char>(j,k)=0;
00316                                 }
00317                                 imwrite(filename,frame);
00318                 }
00319                 else
00320                 {               
00321                         for(int m=0;m<smask.size[0];m++)
00322                         {
00323                                 Mat img=MatVid::frame(smask,m);
00324 
00325                                 char buffer [100];
00326                                 sprintf(buffer,"%03d",m);
00327 
00328                                 string filename=fpath+buffer+".png";
00329 
00330                                 Mat frame=Mat::zeros(img.rows,img.cols,CV_8UC1);
00331                                 for(int j=0;j<img.rows;j++)
00332                                         for(int k=0;k<img.cols;k++)
00333                                         {
00334                                                 if(img.at<unsigned char>(j,k)==1)
00335                                                         frame.at<unsigned char>(j,k)=127;
00336                                                 else if(img.at<unsigned char>(j,k)==2)
00337                                                         frame.at<unsigned char>(j,k)=255;
00338                                                 else
00339                                                         frame.at<unsigned char>(j,k)=0;
00340                                         }
00341                                         imwrite(filename,frame);
00342 
00343                         }
00344                 }
00345                 //save avi and dat for windows pc
00346                 #ifdef _WIN32
00347                         string avipath=vidPath2 + vidid + string(".avi");
00348                         string datpath=vidPath2 + vidid + string(".dat");
00349                         MatVid::saveavi(avipath,segVideo);
00350                         MatVid::savedat(datpath,segVideo,"unsigned_1"); 
00351                 #endif
00352         }
00353 }
00354 
00355 
00389 Mat VidSegm::segmentVideo(Mat vid,int stepxy,int stepz,int filtxy,int filtz, Mat &osmask)
00390 {
00391         //turn on segmentation timer for current video
00392         timerOn();
00393 
00394         //reset current video segmentation results
00395         vector<Mat>          patches;           
00396         allclasses.clear();
00397         allx.clear();       
00398         ally.clear();       
00399         allz.clear();           
00400         vidsize = Point3i(vid.size[2], vid.size[1], vid.size[0]);
00401         locall.clear();     
00402         locall_mask.clear();
00403         
00404         
00405         //segmented video
00406         Mat segm;
00407         
00408         //update patch option steps for segmentation
00409         PatchOptions npopt(popt);
00410         npopt.step=Point3i(stepxy,stepxy,stepz);
00411         popt=npopt;
00412 
00413         //initialize Kalman Cashe for the training mixture
00414         dtm.setupKFB(popt.win.z);
00415 
00416         //option for box segmenttaion; set to all video here
00417         Range box_z = Range::all();
00418         Range box_y = Range::all();
00419         Range box_x = Range::all();
00420                      
00421         // internal bounding box size and offsets
00422         Point3i vbox_size, vbox_off;
00423         // subvideo from internal bounding box
00424         Mat     boxvid;
00425 
00426         // check bounding box: 
00427         if ((box_z == Range::all()) && (box_y == Range::all()) && (box_z == Range::all())) 
00428         {
00429                 // using full video
00430                 vbox_size = Point3i(vid.size[2], vid.size[1], vid.size[0]);
00431                 vbox_off  = Point3i(0, 0, 0);
00432 
00433                 boxvid = vid;
00434         } 
00435         else 
00436         {
00437                 // using sub-video
00438                 Range   vbox_z, vbox_x, vbox_y;
00439                 // adjust box to align with patch locations
00440                 if (box_z == Range::all()) 
00441                 {
00442                         vbox_off.z  = 0;
00443                         vbox_size.z = vid.size[0];
00444                         vbox_z      = Range::all();
00445                 } 
00446                 else 
00447                 {
00448                         vbox_off.z  = (box_z.start % popt.step.z == 0 ? box_z.start : ((box_z.start/popt.step.z)+1)*popt.step.z);
00449                         vbox_size.z = box_z.end - vbox_off.z;
00450                         vbox_z      = Range(vbox_off.z, MIN(box_z.end+popt.win.z, vid.size[0]) );
00451                 }
00452 
00453                 if (box_y == Range::all()) 
00454                 {
00455                         vbox_off.y  = 0;
00456                         vbox_size.y = vid.size[1];
00457                         vbox_y      = Range::all();
00458                 } 
00459                 else 
00460                 {
00461                         vbox_off.y  = (box_y.start % popt.step.y == 0 ? box_y.start : ((box_y.start/popt.step.y)+1)*popt.step.y);
00462                         vbox_size.y = box_y.end - vbox_off.y;
00463                         vbox_y      = Range(vbox_off.y, MIN(box_y.end+popt.win.y, vid.size[1]) );
00464                 }
00465        
00466                 if (box_x == Range::all()) 
00467                 {
00468                         vbox_off.x  = 0;
00469                         vbox_size.x = vid.size[2];
00470                         vbox_x      = Range::all();
00471                 } 
00472                 else 
00473                 {
00474                         vbox_off.x  = (box_x.start % popt.step.x == 0 ? box_x.start : ((box_x.start/popt.step.x)+1)*popt.step.x);
00475                         vbox_size.x = box_x.end - vbox_off.x;
00476                         vbox_x      = Range(vbox_off.x, MIN(box_x.end+popt.win.x, vid.size[2]) );
00477                 }    
00478 
00479                 boxvid = MatVid::subvid(vid, vbox_z, vbox_y, vbox_x);   
00480         }
00481         
00482         // create the patch extractor
00483         PatchExtractor pe(popt, boxvid.size[1], boxvid.size[2]);  
00484         // copy some info
00485         coff = pe.coff;
00486         allx = pe.allx;
00487         ally = pe.ally;
00488         int xsize = allx.size();
00489         int ysize = ally.size();
00490 
00491         // figure out possible z
00492         int zsize = (boxvid.size[0]-popt.win.z) / popt.step.z + 1;
00493         allz.reserve(zsize);
00494         for (int z=0; z<=boxvid.size[0]-popt.win.z; z+=popt.step.z) 
00495         {
00496                 allz.push_back(z);
00497         }
00498         // offset allx, ally, allz
00499         for (unsigned int i=0; i<allx.size(); i++)
00500                 allx[i] += vbox_off.x;
00501         for (unsigned int i=0; i<ally.size(); i++)
00502                 ally[i] += vbox_off.y;
00503         for (unsigned int i=0; i<allz.size(); i++)
00504                 allz[i] += vbox_off.z;
00505 
00506   
00507         // loop through frames, segmentation in parts to save memory
00508         int frame=0;
00509         Mat clip;  
00510         while (frame < boxvid.size[0]) 
00511         {
00512                 // get next clip to add
00513                 int clipz = (frame == 0 ? popt.win.z : popt.step.z);
00514 
00515                 if (frame+clipz > boxvid.size[0]) 
00516                 {
00517                         break;
00518                 }
00519                 clip = MatVid::subvid(boxvid, Range(frame, frame+clipz), Range::all(), Range::all());
00520                 // add frames
00521                 if (!pe.addFrames(clip)) 
00522                 {
00523                         // we should not get here
00524                         CV_Error(-1, "something wrong!");
00525 
00526                 } 
00527                 else 
00528                 {
00529                         // now add to our patch vector
00530                         const vector<Mat> &mypat = pe.getPatches();                 
00531                         for (unsigned int i=0; i<mypat.size(); i++) 
00532                         {
00533                                 // create a copy of the patch
00534                                 if(pe.locyx_mask[i]==true) //only valid patches
00535                                 {                       
00536                                         Mat p;
00537                                         mypat[i].copyTo(p);
00538                                         patches.push_back(p);                                   
00539                                 }
00540                                 //save all locations
00541                                 locall.push_back(Point3i(pe.locyx[i].x + vbox_off.x,pe.locyx[i].y + vbox_off.y, pe.locz       + vbox_off.z));
00542                                 locall_mask.push_back(pe.locyx_mask[i]);                  
00543                         }
00544                         cout<<"Found Patches "<<patches.size()<<"/"<<mypat.size()<<endl;
00545                         //Keep classes and discard patches
00546                         std::vector<int> tmpclasses;
00547                         dtm.getClasses(patches,tmpclasses);
00548                         for(int cc=0;cc<patches.size();cc++)
00549                         {
00550                                 allclasses.push_back(tmpclasses[cc]);
00551                         }
00552                         patches.clear();
00553                 }
00554                 frame += clipz;
00555         }
00556         //Get the segmentation mask
00557         osmask=segm_mask();
00558         //filter the mask
00559         osmask=maxvotefilt(osmask,filtxy,filtxy,filtz,K);
00560         //Get color segmentation
00561         colorMask(vid,osmask,segm,1); 
00562 
00563         int ttime=timerOff();
00564         cout<<"segmentation time for cur video is: "<<ttime<<endl;
00565         return segm;
00566 }
00567 
00568 
00569 
00586 Mat VidSegm::segm_mask()
00587 {
00588         //mask to return
00589         Mat smask;
00590         
00591         //fill in implicit background class      
00592         std::vector<int> classes;               
00593         int index=0;
00594         for(int i=0;i<locall_mask.size();i++)
00595         {
00596                 if(locall_mask[i]==true)
00597                 {
00598                         classes.push_back(allclasses[index]);
00599                         index++;
00600                 }
00601                 else
00602                 {
00603                         classes.push_back(0);
00604                 }
00605         }
00606         
00607         // get locations
00608         std::vector<Point3i> loc;
00609         for(int i=0;i<locall.size();i++)
00610         {
00611                 loc.push_back(locall[i]+coff);
00612         }
00613         Point3i winsize=popt.win;
00614         Point3i step=popt.step;
00615 
00616         //check for single z-layer, and force mask to be 2d.
00617         if(allz.size()==1)
00618         {
00619                 for(int i=0;i<loc.size();i++)
00620                         loc[i].z=0;
00621 
00622                 winsize.z=1;
00623                 step.z=1;
00624                 vidsize.z=1;
00625         }
00626 
00627         //total classes
00628         int numclasses=*max_element(classes.begin(),classes.end());
00629 
00630         //create blank mask and fill with zeros
00631         smask=MatVid::create(vidsize.z,vidsize.y,vidsize.x,CV_8UC1);
00632         smask=Scalar(0);
00633 
00634         //initialzie some sizes
00635         int zsize=step.z;
00636         Point3i rwin;
00637         rwin.x=winsize.x/2;
00638         rwin.y=winsize.y/2;
00639         rwin.z=zsize/2;
00640         Point2i box_y(-rwin.x,winsize.x-1-rwin.x);
00641         Point2i box_x(-rwin.y,winsize.y-1-rwin.y);
00642         Point2i box_z(-rwin.z,zsize-1-rwin.z);
00643         int cy=rwin.x+1;
00644         int cx=rwin.y+1;
00645 
00646         //the structuring element
00647         Mat se(winsize.x,winsize.y,CV_8UC1);
00648         se=Scalar(0);
00649 
00650         //the distance matrix
00651         Mat sed(winsize.x,winsize.y,OPT_MAT_TYPE);
00652         sed=Scalar(0);
00653 
00654         std::vector<double> ssc;
00655         for(int i=0;i<sed.rows;i++)
00656         {
00657                 for(int j=0;j<sed.cols;j++)
00658                 {
00659                         sed.at<OPT_F_TYPE>(i,j)=sqrt(  pow((double)(i+1-cy),2) + pow((double)(j+1-cx),2) );
00660                         ssc.push_back(sed.at<OPT_F_TYPE>(i,j));
00661                 }
00662         }
00663                 
00664         //growing schedule
00665         sort(ssc.begin(),ssc.end());
00666         vector<double>::iterator it=unique(ssc.begin(),ssc.end());
00667         ssc.resize( it - ssc.begin() );
00668 
00669         //offsets
00670         std::vector<int> locoffs;
00671         for(int i=0;i<loc.size();i++)
00672         {
00673                 int temp=loc[i].y-rwin.x+vidsize.y*(loc[i].x-rwin.y)+vidsize.y*vidsize.x*(loc[i].z-rwin.z);
00674                 locoffs.push_back(temp);
00675         }
00676                 
00677         for(int seri=0;seri<ssc.size();seri++)
00678         {
00679                 cout<<"segm_mask: nn fill "<<seri<<"/"<<ssc.size()<<endl;
00680                 //build the structure element
00681                         
00682                 for(int i=0;i<sed.rows;i++)
00683                 {
00684                         for(int j=0;j<sed.cols;j++)
00685                         {
00686                                 if(sed.at<OPT_F_TYPE>(i,j)==ssc[seri])
00687                                         se.at<uchar>(i,j)=1;
00688                                 else
00689                                         se.at<uchar>(i,j)=0;
00690                         }
00691                 }
00692 
00693                                 
00694                 Mat se3=MatVid::repeat(se,zsize);
00695                 int s1=se3.size[0];
00696                 int s2=se3.size[1];
00697                 int s3=se3.size[2];
00698                 std::vector<double> se3i;
00699                 for(int kk=0;kk<s1;kk++)
00700                 {
00701                         for(int jj=0;jj<s3;jj++)
00702                         {
00703                                 for(int ii=0;ii<s2;ii++)
00704                                 {
00705                                         if(se3.at<uchar>(kk,ii,jj)==1)
00706                                         {
00707                                                 double temp=ii+1+jj*vidsize.y+kk*vidsize.y*vidsize.x;
00708                                                 se3i.push_back(temp);
00709                                         }
00710                                 }
00711                         }
00712                 }
00713                 for(int i=0;i<classes.size();i++)
00714                 {
00715                         int c=classes[i];
00716                         int off=locoffs[i];
00717 
00718                         //find zero elements in smask that are active in se3
00719                         std::vector<int> tempi;
00720                         for(int j=0;j<se3i.size();j++)
00721                         {
00722                                 int temp=se3i[j]+off-1;
00723                                 int fNo=temp/(smask.size[1]*smask.size[2]);
00724                                 temp=temp-fNo*(smask.size[1]*smask.size[2]);
00725                                 int colNo=temp/smask.size[1];
00726                                 int rowNo=temp%smask.size[1];
00727                                 if(smask.at<uchar>(fNo,rowNo,colNo)==0)
00728                                         tempi.push_back(j);
00729                         }
00730                                         
00731                         //if found any zero elements, 
00732                         for(int j=0;j<tempi.size();j++)
00733                         {
00734                                 int temp=se3i[tempi[j]]+off-1;
00735                                 int fNo=temp/(smask.size[1]*smask.size[2]);
00736                                 temp=temp-fNo*(smask.size[1]*smask.size[2]);
00737                                 int colNo1=temp/smask.size[1];
00738                                 int rowNo1=temp%smask.size[1];
00739                                 // fill location
00740                                 if( (c==1) || (c==2) )
00741                                         c=c;
00742                                 smask.at<uchar>(fNo,rowNo1,colNo1)=c;                                                   
00743                         }
00744 
00745                 }
00746                         
00747         }
00748                                 
00749         //now fill in the borders if they are missing   
00750         double minx=*min_element(allx.begin(),allx.end())+coff.y+box_x.x;
00751         double maxx=*max_element(allx.begin(),allx.end())+coff.y+box_x.y+2;
00752         double miny=*min_element(ally.begin(),ally.end())+coff.x+box_y.x;
00753         double maxy=*max_element(ally.begin(),ally.end())+coff.x+box_y.y+2;
00754 
00755         // fill in left edge
00756         if(1<=minx)
00757         {               
00758                 for(int i=0;i<smask.size[0];i++)
00759                 {
00760                         for(int j=0;j<smask.size[1];j++)
00761                         {
00762                                 for(int k=0;k<minx;k++)
00763                                 {
00764                                         smask.at<uchar>(i,j,k)=smask.at<uchar>(i,j,minx);
00765                                 }
00766                         }
00767                 }
00768 
00769         }
00770 
00771         //fill in right edge
00772         if(maxx<=vidsize.x)
00773         {               
00774                 for(int i=0;i<smask.size[0];i++)
00775                 {
00776                         for(int j=0;j<smask.size[1];j++)
00777                         {
00778                                 for(int k=maxx-1;k<vidsize.x;k++)
00779                                 {
00780                                         smask.at<uchar>(i,j,k)=smask.at<uchar>(i,j,maxx-2);
00781                                 }
00782                         }
00783                 }
00784 
00785         }
00786 
00787         
00788         // fill in top edge
00789         if(1<=miny)
00790         {               
00791                 for(int i=0;i<smask.size[0];i++)
00792                 {
00793                         for(int j=0;j<miny;j++)
00794                         {
00795                                 for(int k=0;k<smask.size[2];k++)
00796                                 {
00797                                         smask.at<uchar>(i,j,k)=smask.at<uchar>(i,miny,k);
00798                                 }
00799                         }
00800                 }
00801         }
00802 
00803         // fill in bottom edge
00804         if(maxy<=vidsize.y)
00805         {               
00806                 for(int i=0;i<smask.size[0];i++)
00807                 {
00808                         for(int j=maxy-1;j<vidsize.y;j++)
00809                         {
00810                                 for(int k=0;k<smask.size[2];k++)
00811                                 {
00812                                         smask.at<uchar>(i,j,k)=smask.at<uchar>(i,maxy-2,k);
00813                                 }
00814                         }
00815                 }
00816 
00817         }
00818 
00819         if(vidsize.z>1)
00820         {
00821                 double minz=*min_element(allz.begin(),allz.end())+coff.z+box_z.x;
00822                 double maxz=*max_element(allz.begin(),allz.end())+coff.z+box_z.y+2;
00823 
00824                 if(1<=minz)
00825                 {
00826                         //fill in first frames
00827                         for(int i=0;i<minz;i++)
00828                         {
00829                                 for(int j=0;j<smask.size[1];j++)
00830                                 {
00831                                         for(int k=0;k<smask.size[2];k++)
00832                                         {
00833                                                 smask.at<uchar>(i,j,k)=smask.at<uchar>(minz,j,k);
00834                                         }
00835                                 }
00836                         }
00837                 }
00838 
00839                 if(maxz<=vidsize.z)
00840                 {
00841                         //fill in last frames
00842                         for(int i=maxz-1;i<vidsize.z;i++)
00843                         {
00844                                 for(int j=0;j<smask.size[1];j++)
00845                                 {
00846                                         for(int k=0;k<smask.size[2];k++)
00847                                         {
00848                                                 smask.at<uchar>(i,j,k)=smask.at<uchar>(maxz-2,j,k);
00849                                         }
00850                                 }
00851                         }
00852                 }
00853         }
00854 
00855         //single frame mask; make it 2d
00856         if(smask.size[0]==1)
00857         {
00858                 Mat tempM;
00859                 MatVid::frame(smask,0).copyTo(tempM);
00860                 return tempM;
00861         }
00862         else
00863         {
00864                 return smask;
00865         }
00866 }
00867 
00868 
00897 Mat VidSegm::maxvotefilt(Mat I,int sx,int sy,int sz,int numclass)
00898 {
00899         //in case of 2d video, make it 2d
00900         if(I.dims==2)
00901         {
00902                 Mat tmp=MatVid::create(1,I.rows,I.cols,CV_8UC1);
00903                 Mat tmp2=MatVid::frame(tmp,0);
00904                 I.copyTo(tmp2);
00905                 I=tmp;
00906         }
00907 
00908         //initializing filtered mask
00909         Mat J=MatVid::create(I.size[0],I.size[1],I.size[2],CV_8UC1);;
00910 
00911         //filling some sizes
00912         int rs1=sx/2;
00913         int rs2=sy/2;
00914         int rs3=sz/2;
00915         Point2i box_y(-rs1,-rs1+sx-1);
00916         Point2i box_x(-rs2,-rs2+sy-1);
00917         Point2i box_z(0,0);
00918         if(sz>0)
00919         {
00920                 box_z.x=-rs3;
00921                 box_z.y=-rs3+sz-1;
00922         }
00923 
00924         //memory for each class
00925         std::vector<Mat> count;
00926         for(int i=0;i<numclass+1;i++)
00927         {       
00928                 Mat tmp=MatVid::create(I.size[0],I.size[1],I.size[2],CV_8UC1);
00929                 tmp.setTo(0);
00930                 count.push_back(tmp);
00931         }
00932 
00933         //applying maxvote
00934         for(int c=0;c<=numclass;c++)
00935         {
00936                 Mat foo=MatVid::create(I.size[0],I.size[1],I.size[2],CV_8UC1);          
00937                 for(int i=0;i<I.size[0];i++)
00938                 {
00939                         for(int j=0;j<I.size[1];j++)
00940                         {
00941                                 for(int k=0;k<I.size[2];k++)
00942                                 {
00943                                         if(I.at<unsigned char>(i,j,k)==c)
00944                                         {
00945                                                 foo.at<unsigned char>(i,j,k)=1;
00946                                         }
00947                                         else
00948                                         {
00949                                                 foo.at<unsigned char>(i,j,k)=0;
00950                                         }
00951                                 }
00952                         }
00953                 }               
00954                 for(int z=box_z.x;z<=box_z.y;z++)
00955                 {
00956                         for(int y=box_y.x;y<=box_y.y;y++)
00957                         {
00958                                 for(int x=box_x.x;x<=box_x.y;x++)
00959                                 {
00960                                         Point yind((y+1)>1?(y+1):1, (y+I.size[1])<(I.size[1])?(y+I.size[1]):(I.size[1]));
00961                                         Point xind((x+1)>1?(x+1):1, (x+I.size[2])<(I.size[2])?(x+I.size[2]):(I.size[2]));
00962                                         Point zind((z+1)>1?(z+1):1, (z+I.size[0])<(I.size[0])?(z+I.size[0]):(I.size[0]));
00963 
00964                                         Point yind2((-y+1)>1?(-y+1):1, (-y+I.size[1])<(I.size[1])?(-y+I.size[1]):(I.size[1]));
00965                                         Point xind2((-x+1)>1?(-x+1):1, (-x+I.size[2])<(I.size[2])?(-x+I.size[2]):(I.size[2]));
00966                                         Point zind2((-z+1)>1?(-z+1):1, (-z+I.size[0])<(I.size[0])?(-z+I.size[0]):(I.size[0]));
00967 
00968                                         std::vector<int> xxind,yyind,zzind,xxind2,yyind2,zzind2;
00969 
00970                                         for(int i=xind.x;i<=xind.y;i++)
00971                                                 xxind.push_back(i);
00972 
00973                                         for(int i=yind.x;i<=yind.y;i++)
00974                                                 yyind.push_back(i);
00975 
00976                                         for(int i=zind.x;i<=zind.y;i++)
00977                                                 zzind.push_back(i);
00978 
00979                                         for(int i=xind2.x;i<=xind2.y;i++)
00980                                                 xxind2.push_back(i);
00981 
00982                                         for(int i=yind2.x;i<=yind2.y;i++)
00983                                                 yyind2.push_back(i);
00984 
00985                                         for(int i=zind2.x;i<=zind2.y;i++)
00986                                                 zzind2.push_back(i);                    
00987 
00988                                         if(zzind.empty())
00989                                                 zzind.push_back(1);
00990 
00991                                         if(zzind2.empty())
00992                                                 zzind2.push_back(1);
00993 
00994 
00995                                         for(int kk=0;kk<zzind.size();kk++)
00996                                         {
00997                                                 Mat tmp1=MatVid::frame(count[c],zzind[kk]-1);
00998                                                 Mat tmp2=tmp1(Range(yind.x-1,yind.y),Range(xind.x-1,xind.y));
00999                                                 Mat tmp3=MatVid::frame(foo,zzind2[kk]-1);
01000                                                 Mat tmp4=tmp3(Range(yind2.x-1,yind2.y),Range(xind2.x-1,xind2.y));
01001                                                 tmp2=tmp2+tmp4;
01002                                         }
01003 
01004                                 }
01005                         }
01006                 }
01007         }
01008 
01009         for(int i=0;i<count[0].size[0];i++)
01010         {
01011                 for(int j=0;j<count[0].size[1];j++)
01012                 {
01013                         for(int k=0;k<count[0].size[2];k++)
01014                         {
01015                                 unsigned char maxV=0;
01016                                 int maxind=0;
01017                                 for(int m=0;m<count.size();m++)
01018                                 {
01019                                         if(count[m].at<unsigned char>(i,j,k)>maxV)
01020                                         {
01021                                                 maxV=count[m].at<unsigned char>(i,j,k);
01022                                                 maxind=m;
01023                                         }
01024                                 }
01025 
01026                                 J.at<unsigned char>(i,j,k)=maxind;
01027                         }
01028                 }
01029         }
01030 
01031         //make 2d
01032         if(J.size[0]==1)
01033         {
01034                 Mat tmp=MatVid::frame(J,0);
01035                 Mat tmp2;
01036                 tmp.copyTo(tmp2);
01037                 return tmp2;
01038         }
01039         else
01040         {
01041                 return J;
01042         }
01043 }

Generated on 18 Dec 2014 for libdt-OpenCV by  doxygen 1.6.1