Advertisement
tameraslan

BScThesis - panopticon.cpp

Dec 19th, 2011
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.93 KB | None | 0 0
  1. // Name        : panopticon.cpp
  2.  
  3. #include "panopticon.h"
  4.  
  5. #include <stdio.h>
  6.  
  7.  
  8. int main(int argc, char *argv[]){
  9.  
  10.     // variable declarations
  11.  
  12.     const char* filename= VideoDir; //VideoDir is defined in the header
  13.  
  14.     IplImage* temp;
  15.  
  16.     // video stream in
  17.     CvCapture* videofile=cvCreateFileCapture( filename );  //videofile dosyasina video atiyor
  18.     //temp'e attigimiz ilk frame, dosya boyutu,
  19.     //bitchannel hakkinda bilgi vercek
  20.     temp=cvQueryFrame( videofile );
  21.  
  22.     IplImage* temp_gray=cvCreateImage(cvSize(temp->width,temp->height),temp->depth,1);
  23.     cvCvtColor( temp, temp_gray, CV_RGB2GRAY );
  24.  
  25.     IplImage* image0=cvCloneImage(temp_gray);
  26.     //IplImage* temp_gray=cvCreateImage(cvSize(image0->width,image0->height),image0->depth,1);
  27.  
  28.     //BG stat arrays:
  29.     IplImage * MedianFrame =cvCreateImage(cvGetSize(image0),IPL_DEPTH_32F,1);
  30.     IplImage * StddevFrame =cvCreateImage(cvGetSize(image0),IPL_DEPTH_32F,1);
  31.     IplImage * bgmFrame =cvCreateImage(cvGetSize(image0),image0->depth,1);
  32.     IplImage * bgnFrame =cvCreateImage(cvGetSize(image0),image0->depth,1);
  33.     IplImage * bgdFrame =cvCreateImage(cvGetSize(image0),image0->depth,1);
  34.     IplImage* BGFrameArray[bgLength];
  35.     IplImage* image;
  36.  
  37.     char fname[50];
  38.  
  39.     // read BG images and build BG model
  40.     if(has_BG_Model)
  41.     {
  42.         sprintf(fname, "/bgmFrame_%d.bmp",bgLength);
  43.         bgmFrame = cvLoadImage(fname,CV_LOAD_IMAGE_GRAYSCALE);
  44.         sprintf(fname, "/bgnFrame_%d.bmp",bgLength);
  45.         bgnFrame = cvLoadImage(fname,CV_LOAD_IMAGE_GRAYSCALE);
  46.         sprintf(fname, "/bgdFrame_%d.bmp",bgLength);
  47.         bgdFrame = cvLoadImage(fname,CV_LOAD_IMAGE_GRAYSCALE);
  48.     }
  49.  
  50.     // BG stat operations, for creating bg model from video input:
  51.     else
  52.     {
  53.         BGFrameArray[0]=image0;
  54.         //collect first 450 frame in BGFrameArray
  55.  
  56.         for( int bgCounter=1; bgCounter<bgLength;bgCounter++)
  57.         {
  58.             temp=cvQueryFrame( videofile );
  59.             cvCvtColor( temp, temp_gray, CV_RGB2GRAY );
  60.             image=cvCloneImage(temp_gray);
  61.             BGFrameArray[bgCounter]=image; //BGFrameArray, ilk 450 frame ile dolduruluyor
  62.         }
  63.         //returns: std dev, median, max, min, maxdiff
  64.         bgInitStats(StddevFrame, BGFrameArray, MedianFrame,bgmFrame, bgnFrame, bgdFrame);
  65.  
  66.         //record bg stat values:
  67.         sprintf(fname,"C:/Users/kullaniciadi/Desktop/bitirme son hal/PanopticonV.0.2/bgmFrame_%d.bmp",bgLength);
  68.         cvSaveImage(fname,bgmFrame);
  69.         sprintf(fname,"C:/Users/kullaniciadi/Desktop/bitirme son hal/PanopticonV.0.2/bgnFrame_%d.bmp",bgLength);
  70.         cvSaveImage(fname,bgnFrame);
  71.         sprintf(fname,"C:/Users/kullaniciadi/Desktop/bitirme son hal/PanopticonV.0.2/bgdFrame_%d.bmp",bgLength);
  72.         cvSaveImage(fname,bgdFrame);
  73.     }
  74.     //end of BG stat operations
  75.  
  76.     CvScalar dm;
  77.     dm.val[0]=FindMedian(bgdFrame);
  78.  
  79.     cvNamedWindow( "Results", CV_WINDOW_AUTOSIZE );
  80.     cvNamedWindow( "Boundary Boxes", CV_WINDOW_AUTOSIZE );
  81.     //cvNamedWindow( "Boundary Boxes2", CV_WINDOW_AUTOSIZE );
  82.  
  83.     //Routine for displaying the foreground parts
  84.     IplImage* temp2;
  85.     CvScalar temp3=cvScalar(0);
  86.     CvScalar tempMax=cvScalar(0);
  87.     CvScalar tempMin=cvScalar(0);
  88.  
  89.     char key;  //check for esc key
  90.  
  91.     //variables for the boundaries
  92.     int num = 6;
  93.     CvRect boundaryArray [num];
  94.     CvPoint center[num];
  95.  
  96.     unsigned int PeopleInScene=6;
  97.     IplImage* croppedImage2 [PeopleInScene];   //max number of peoples
  98.     IplImage** CovMatrix = new IplImage*[PeopleInScene];
  99.     IplImage** AvgMatrix = new IplImage*[PeopleInScene];
  100.  
  101.  
  102.  
  103.  
  104.     printf("ekrana bisi bas   \n");
  105.  
  106.     //fastforward for debugging
  107.     for (int bgDCounter=0;bgDCounter<12;bgDCounter++)
  108.         temp2 = cvQueryFrame( videofile );
  109.  
  110.     for (int bgDCounter=0;bgDCounter<2000;bgDCounter++)
  111.     {
  112.  
  113.  
  114.  
  115.         if(  !has_BG_Model && bgDCounter<bgLength )
  116.             temp_gray = BGFrameArray[bgDCounter];
  117.  
  118.         else{
  119.             temp2 = cvQueryFrame( videofile );
  120.  
  121.             if (!temp2->imageData)
  122.                 break;
  123.             cvCvtColor( temp2, temp_gray, CV_RGB2GRAY );
  124.         }
  125.  
  126.         for (int row=0;row<image0->height ;row++)
  127.             for(int col =0; col<image0->width;col++)
  128.             {
  129.             temp3=cvGet2D(temp_gray,row,col);
  130.             tempMax=cvGet2D(bgmFrame,row,col);
  131.             tempMin=cvGet2D(bgnFrame,row,col);
  132.  
  133.  
  134.             if(((temp3.val[0]-tempMax.val[0])<bgThreshold*dm.val[0]) && ((tempMin.val[0]-temp3.val[0])<bgThreshold*dm.val[0]))
  135.                 cvSet2D(temp_gray,row,col,cvScalar(0));
  136.             else
  137.                 cvSet2D(temp_gray,row,col,cvScalar(255));
  138.             }
  139.  
  140.  
  141.         find_connected_components(temp_gray,1,4,& num,boundaryArray,center);
  142.  
  143.         cvShowImage( "Results" ,temp_gray);
  144.         //sprintf(fname,"C:/cc.bmp");
  145.         //cvSaveImage(fname,temp_gray);
  146.  
  147.  
  148.         //Print Boundary Boxes and their centre points on image named boxedImage
  149.         IplImage* boxedImage =cvCreateImage(cvSize(temp_gray->width,temp_gray->height), temp_gray->depth,temp_gray->nChannels);
  150.         for (int t=0; t<num;t++)
  151.         {
  152.             //cx=center[t].x;
  153.             //cy=center[t].y;
  154.  
  155.  
  156.             for (int row=0;row<image0->height ;row++)
  157.                 for(int col =0; col<image0->width;col++)
  158.                 {
  159.  
  160.                 /*if(row < (center[t].y + 10) && row > (center[t].y - 10)
  161.                     && col < (center[t].x + 10) && col > (center[t].x - 10))
  162.  
  163.                     cvSet2D(boxedImage,row,col,cvScalar(0));
  164.                 else */
  165.                 if(row < (boundaryArray[t].y + boundaryArray[t].height) && row > boundaryArray[t].y
  166.                         && col < (boundaryArray[t].x + boundaryArray[t].width) && col > boundaryArray[t].x )
  167.                     cvSet2D(boxedImage,row,col,cvScalar(127));
  168.  
  169.             }
  170.  
  171.  
  172.         }
  173.         //sprintf(fname,"C:/box2.bmp");
  174.         //cvSaveImage(fname,boxedImage);
  175.  
  176.         //cvShowImage( "Boundary Boxes" ,boxedImage);
  177.  
  178.  
  179.  
  180.         //next: get the boundary boxes by changing ROI / taking first one is done
  181.         //how to allocate memory to save all of them?
  182.  
  183.         //next: apply pca on them
  184.         //first find cov mat and then find largest eigenVec
  185.  
  186.  
  187.         for (int t=0; num!=0 && t<num;t++)
  188.         {
  189.             int medX, medY;
  190.             croppedImage2[t] =cvCreateImage(cvSize(boundaryArray[t].width,boundaryArray[t].height), temp_gray->depth,temp_gray->nChannels);
  191.             cvSetImageROI(temp_gray,boundaryArray[t]);
  192.             cvCopy(temp_gray, croppedImage2[t]);
  193.             cvResetImageROI(temp_gray);
  194.  
  195.             CovMatrix[t] = cvCreateImage(cvSize(2,2), temp_gray->depth,temp_gray->nChannels);
  196.             AvgMatrix[t] = cvCreateImage(cvSize(2,2), temp_gray->depth,temp_gray->nChannels);
  197.  
  198.  
  199.             double sum =cvSumPixels(croppedImage2[t])/255;
  200.  
  201.             IplImage** pixPositions = new IplImage*[2];
  202.             pixPositions[0] =cvCreateImage(cvSize(1,(int)sum),IPL_DEPTH_32F,1);
  203.             pixPositions[1] =cvCreateImage(cvSize(1,(int)sum),IPL_DEPTH_32F,1);
  204.             int k=0;
  205.             for(int i=0; i< croppedImage2[t]->width;i++)
  206.                 for(int j=0; j<croppedImage2[t]->height; j++)
  207.                     if(cvGet2D(croppedImage2[t],j,i).val[0]==255)
  208.                     {
  209.                         cvSet1D(pixPositions[0],k,cvScalar(i));
  210.                         cvSet1D(pixPositions[1],k,cvScalar(j));
  211.                         k++;
  212.                     }
  213.  
  214.             // median of the objects are calculated here
  215.             medX= (int)FindMedian(pixPositions[0]) + boundaryArray[t].x;
  216.             medY= (int)FindMedian(pixPositions[1]) + boundaryArray[t].y;
  217.  
  218.             for (int row=(medX-5);row<=(medX+5);row++)
  219.                 for(int col =(medY-5); col<=(medY+5);col++)
  220.                     cvSet2D(boxedImage,col,row,cvScalar(255));
  221.  
  222.  
  223.             //const IplImage ** CPixPos = cvClone(& pixPositions);
  224.      //       cvCalcCovarMatrix( (const CvArr **)pixPositions, 2, CovMatrix[t], AvgMatrix[t], CV_COVAR_ROWS);
  225. //
  226. //            sprintf(fname,"C:/cov%d1.bmp",t);
  227. //            cvSaveImage(fname,CovMatrix[t]);
  228. //            sprintf(fname,"C:/cov%d2.bmp",t);
  229. //            cvSaveImage(fname,AvgMatrix[t]);
  230.  
  231.             //PCA:
  232.  
  233.  
  234.             //PrintMatrix((CvMat*) CovMatrix[t]);
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.         }
  242.  
  243.  
  244.         //sprintf(fname,"C:/box3.bmp");
  245.         //cvSaveImage(fname,boxedImage);
  246.         cvShowImage( "Boundary Boxes" ,boxedImage);
  247.  
  248.  
  249.  
  250.         //release image files created for current frame
  251.         for (int t=0; num!=0 && t<num;t++)
  252.         {
  253.             cvReleaseImage( &croppedImage2[t] );
  254.         }
  255.  
  256.  
  257.         key= cvWaitKey(3);
  258.         if( key== 27 ) break;        //cikmak icin escape
  259.     }
  260.  
  261.     //  cvReleaseImage( &temp_gray );
  262.     //  cvReleaseImage( &temp2 );
  263.  
  264.  
  265.     cvReleaseCapture(&videofile);
  266.     cvDestroyWindow( "Results" );
  267.     cvDestroyWindow( "Boundary Boxes" );
  268.  
  269.     return 0;
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement