Guest User

Untitled

a guest
Apr 24th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.45 KB | None | 0 0
  1. /*Ivo Brauers (3825345)
  2.   Dion Gerritzen (3220494)
  3. */
  4.  
  5. // Computer Vision Assignment 4
  6. // Public code - Part 2
  7. // Platform for voxel-based 3D reconstruction
  8.  
  9. // By Xinghan Luo, March 2010
  10.  
  11. #include "cv.h"
  12. #include "highgui.h"
  13. #include <iostream>
  14. #include <fstream>
  15. #include <vector>
  16. #include <time.h>
  17.  
  18. #include "cvcCam.h"
  19. #include "xScene3D.h"
  20.  
  21. using namespace std;
  22.  
  23. #define viewNr 4
  24. #define viewW 640
  25. #define viewH 480
  26.  
  27. char* intToChar(int);
  28. IplImage* getFG( IplImage* , IplImage*);
  29.  
  30. int main( int argc, char** argv ){
  31.  
  32.     int key = 0;
  33.     int currentWindow = 0;
  34.  
  35.     bool Pause = false;
  36.     bool topView = false;
  37.     bool rotView = false;
  38.     bool videoEnd = false;
  39.  
  40.     IplImage** frame = new IplImage*[viewNr];
  41.     IplImage** BG = new IplImage*[viewNr];
  42.     IplImage** FG = new IplImage*[viewNr];
  43.  
  44.     IplImage** image = new IplImage*[viewNr];
  45.     CvCapture** capture = new CvCapture*[viewNr];
  46.  
  47.     //create the camera instances
  48.     cvcCam** Cam;
  49.     Cam = new cvcCam*[viewNr];
  50.     // camera calibration files
  51.     Cam[0] = new cvcCam(0,viewW,viewH, "images/img1.ini");
  52.     Cam[1] = new cvcCam(1,viewW,viewH, "images/img2.ini");
  53.     Cam[2] = new cvcCam(2,viewW,viewH, "images/img3.ini");
  54.     Cam[3] = new cvcCam(3,viewW,viewH, "images/img4.ini");
  55.  
  56.     // background image
  57.     BG[0]= cvLoadImage("images/bg1.jpg", 1);  
  58.     BG[1]= cvLoadImage("images/bg2.jpg", 1);
  59.     BG[2]= cvLoadImage("images/bg3.jpg", 1);
  60.     BG[3]= cvLoadImage("images/bg4.jpg", 1);
  61.  
  62.     // the input images
  63.     image[0] = cvLoadImage("images/img1.jpg", 1);
  64.     image[1] = cvLoadImage("images/img2.jpg", 1);
  65.     image[2] = cvLoadImage("images/img3.jpg", 1);
  66.     image[3] = cvLoadImage("images/img4.jpg", 1);
  67.  
  68.     // the input videos
  69.     //capture[0] = cvCaptureFromAVI("data/f.avi");
  70.     //capture[1] = cvCaptureFromAVI("data/l.avi");
  71.     //capture[2] = cvCaptureFromAVI("data/r.avi");
  72.     //capture[3] = cvCaptureFromAVI("data/s.avi");
  73.  
  74.     // background in HSV
  75.     IplImage** b_HSV = new IplImage*[viewNr];
  76.     for (int i = 0; i < viewNr; i++) {
  77.         b_HSV[i] = cvCreateImage( cvGetSize(BG[i]), 8, 3);
  78.         cvCvtColor(BG[i],b_HSV[i],CV_BGR2HSV);
  79.     }
  80.  
  81.     cvNamedWindow( "Video", 1 );
  82.     //cvMoveWindow( "Video", -1280, 0 );
  83.     cvNamedWindow( "Foreground image", 1 );
  84.     //cvMoveWindow( "Foreground image", -1280, 500 );
  85.  
  86.     xScene3D scene3D(viewNr);
  87.  
  88.     // transfer camera coordinate to 3D scene
  89.     for (int i = 0; i < viewNr; i++) {
  90.  
  91.         scene3D.getCamCoord(Cam[i]->camPt);
  92.     }
  93.  
  94.     // volumetric reconstruction
  95.     xVR* VR = new xVR(viewNr, viewW, viewH, Cam);
  96.  
  97.     scene3D.OPenGl3DScene("OpenGL 3D scene");
  98.  
  99.     while( key != 'q' ) {
  100.  
  101.         if (!Pause) {
  102.  
  103.             // get frame from videos
  104.             for (int i = 0; i < viewNr; i++) {
  105.                 frame[i] = image[i];
  106.                 //frame[i] = cvQueryFrame(capture[i]);
  107.                 //  cvFlip(frame[i],frame[i],0);
  108.                 // end condition
  109.                 if( !frame[i]) {
  110.                     videoEnd = true;
  111.                     break;
  112.                 }
  113.                
  114.                 FG[i] = getFG(frame[i],b_HSV[i]);
  115.                 Cam[i]->foreground = FG[i];
  116.             }
  117.  
  118.             if(videoEnd)
  119.                 break;
  120.  
  121.             //scene3D.frmCnt++;
  122.             //cout << "Frame: "<< scene3D.frmCnt << endl;
  123.  
  124.             VR->updateVoxels(Cam);
  125.  
  126.         }
  127.  
  128.         cvShowImage( "Video", frame[currentWindow]);
  129.         cvShowImage( "Foreground image", FG[currentWindow]);
  130.  
  131.         if(topView)
  132.             scene3D.getViewIdx(currentWindow);
  133.         else {
  134.             if(scene3D.cameraView)
  135.                 scene3D.resetTopView();
  136.         }
  137.  
  138.         if(!topView) {
  139.             rotView = false;
  140.             scene3D.angle = 0;
  141.         }
  142.  
  143.         // render the scene
  144.         if(rotView)
  145.             scene3D.rotatView();
  146.         scene3D.Render(VR, Cam);
  147.  
  148.         // release current FGwe
  149.         if(!Pause) {
  150.             for (int i = 0; i < viewNr; i++)
  151.                 cvReleaseImage(&FG[i]);
  152.         }
  153.  
  154.         switch(key){
  155.  
  156.             case 'p':
  157.                 if(Pause){
  158.                     Pause = false;
  159.                     cout << "Resume..." << endl;
  160.                 } else {
  161.                     Pause = true;
  162.                     cout << "Pause..." << endl;
  163.                 }
  164.                 break;
  165.             case 't':
  166.                 if(topView){
  167.                     topView = false;
  168.                     cout << "Disable top view..." << endl;
  169.                 } else {
  170.                     topView = true;
  171.                     cout << "Enable top view..." << endl;
  172.                 }
  173.                 break;
  174.             case 'v':
  175.  
  176.                 if(scene3D.showVolume){
  177.                     scene3D.showVolume = false;
  178.                     cout << "Hide voxel acquistion volume..." << endl;
  179.                 } else {
  180.                     scene3D.showVolume = true;
  181.                     cout << "Display voxel acquistion volume..." << endl;
  182.                 }
  183.                 break;
  184.  
  185.             case 'g':
  186.  
  187.                 if(scene3D.showGrdFlr){
  188.                     cout << "Hide ground floor..." << endl;
  189.                     scene3D.showGrdFlr = false;
  190.  
  191.                 } else {
  192.                     scene3D.showGrdFlr = true;
  193.                     cout << "Display ground floor..." << endl;
  194.                 }
  195.                 break;
  196.             case 'c':
  197.                 if(scene3D.showCam){
  198.                     scene3D.showCam = false;
  199.                     cout << "Hide cameras..." << endl;
  200.                 } else {
  201.                     scene3D.showCam = true;
  202.                     cout << "Display cameras..." << endl;
  203.                 }
  204.                 break;
  205.             case 'o':
  206.                 if(scene3D.showOrg){
  207.                     scene3D.showOrg = false;
  208.                     cout << "Hide world orginal coordinates..." << endl;
  209.                 } else {
  210.                     scene3D.showOrg = true;
  211.                     cout << "Display world orginal coordinates..." << endl;
  212.                 }
  213.                 break;
  214.             case 'r':
  215.                 if(rotView){
  216.                     rotView = false;
  217.                     cout << "Disable rotation view..." << endl;
  218.                 } else {
  219.                     rotView = true;
  220.                     cout << "Enable rotation voxels..." << endl;
  221.                 }
  222.                 break;
  223.  
  224.             case '1':
  225.                 currentWindow = 0;
  226.                 break;
  227.             case '2':
  228.                 currentWindow = 1;
  229.                 break;
  230.             case '3':
  231.                 currentWindow = 2;
  232.                 break;
  233.             case '4':
  234.                 currentWindow = 3;
  235.                 break;
  236.         }
  237.         key = cvWaitKey( 10 );
  238.  
  239.     }
  240.  
  241.     //for (int i = 0; i < viewNr; i++)
  242.         //cvReleaseCapture( &capture[i] );
  243.  
  244.  
  245.     cout << "Done. Press any key to end..." << endl;
  246.     cvWaitKey();
  247.  
  248.     delete[] frame;
  249.     delete[] BG;
  250.     delete[] FG;
  251.  
  252.     cvDestroyWindow( "Video" );
  253.     cvDestroyWindow( "Foreground image" );
  254.     return 0;
  255. }
  256.  
  257. char* intToChar(int i) {
  258.  
  259.     char st[10];
  260.     char * str = _itoa(i, st, 10); // decimal
  261.     return str;
  262. }
  263.  
  264. IplImage* getFG( IplImage* frame, IplImage* BGframe){
  265.  
  266.     // clock the computations
  267.     clock_t start,end;    
  268.     start=clock();
  269.  
  270.     // define temporary parameters
  271.     CvSize sz = cvGetSize(frame);   // size of frame
  272.     IplImage* background = cvCreateImage( sz, IPL_DEPTH_8U, 1 );
  273.  
  274.     IplImage* h;    // image consisting of the h-values of the current frame
  275.     IplImage* s;    // image consisting of the s-values of the current frame
  276.     IplImage* v;    // image consisting of the v-values of the current frame
  277.  
  278.     h = cvCreateImage( sz, IPL_DEPTH_8U, 1 );   // allocating the h-values of the current frame
  279.     s = cvCreateImage( sz, IPL_DEPTH_8U, 1 );   // allocating the s-values of the current frame
  280.     v = cvCreateImage( sz, IPL_DEPTH_8U, 1 );   // allocating the v-values of the current frame
  281.  
  282.     IplImage* hBG;  // image consisting of the h-values of the background frame
  283.     IplImage* sBG;  // image consisting of the s-values of the background frame
  284.     IplImage* vBG;  // image consisting of the v-values of the background frame
  285.  
  286.     hBG = cvCreateImage( sz, IPL_DEPTH_8U, 1 ); // allocating the h-values of the background frame
  287.     sBG = cvCreateImage( sz, IPL_DEPTH_8U, 1 ); // allocating the s-values of the background frame
  288.     vBG = cvCreateImage( sz, IPL_DEPTH_8U, 1 ); // allocating the v-values of the background frame
  289.  
  290.     IplImage *background_t;         // in between result of the background
  291.     IplImage *temp;                 // temporary result image
  292.     background_t = cvCreateImage( sz, IPL_DEPTH_8U, 1 );    // allocating the temporary background
  293.     temp = cvCreateImage( sz, IPL_DEPTH_8U, 1 );            // allocating the temporary background
  294.  
  295.     // Colour transformation from rgb to hsv for the background model
  296.     // -> this part could also be outside this function because now it is performed for every new
  297.     //      frame, while it only has to be done once the background frame is captured!!
  298.  
  299.     //IplImage* b_HSV = cvCreateImage( sz, 8, 3);   // definition and allocation
  300.     //cvCvtColor(BGframe,b_HSV,CV_BGR2HSV); // color conversion
  301.     cvSplit( BGframe, hBG, sBG, vBG, NULL );    // split the hsv- channels for further analysis
  302.  
  303.     // Colour transformation from rgb to hsv for the current frame
  304.     IplImage* f_HSV = cvCreateImage( sz, 8, 3); // definition and allocation
  305.     cvCvtColor(frame,f_HSV,CV_BGR2HSV);     // color conversion
  306.     cvSplit( f_HSV, h, s, v, NULL );        // split the hsv- channels for further analysis
  307.  
  308.     // Definition of the thresholds
  309.     int threshold_H = 20;//20;
  310.     int threshold_S = 20;//20;
  311.     int threshold_V = 40;
  312.  
  313.     // BACKGROUND SUBTRACTION
  314.  
  315.     // initialize background by h-channel  
  316.     cvAbsDiff(h,hBG,temp); // absolute difference between background model and current frame
  317.     cvThreshold(temp , background, threshold_H, 255, CV_THRESH_BINARY ); // thresholding
  318.  
  319.     // update background by s-channel
  320.     cvAbsDiff(s,sBG,temp); // absolute difference between background model and current frame
  321.     cvThreshold( temp, background_t, threshold_S, 255, CV_THRESH_BINARY ); // thresholding
  322.     cvAnd(background, background_t, background); // perform the AND operator to update background
  323.  
  324.     // update background by v-channel
  325.     cvAbsDiff(v,vBG,temp); // absolute difference between background model and current frame
  326.     cvThreshold( temp, background_t, threshold_V, 255, CV_THRESH_BINARY ); // thresholding
  327.     cvOr(background, background_t, background); // perform the OR operator to update background
  328.  
  329.     // deallocate all temporary variables
  330.     cvReleaseImage(&temp);
  331.     cvReleaseImage(&background_t);
  332.     cvReleaseImage(&h);
  333.     cvReleaseImage(&s);
  334.     cvReleaseImage(&v);
  335.     cvReleaseImage(&hBG);
  336.     cvReleaseImage(&sBG);
  337.     cvReleaseImage(&vBG);
  338.  
  339.     cvReleaseImage(&f_HSV);
  340.     //  cout << "Nico code" << endl;
  341.     // clock the computations
  342.     end=clock();
  343.     double time_elapsed = double(end - start)/CLOCKS_PER_SEC;
  344.  
  345.     // erosion kernel
  346.     IplConvKernel* element_e = cvCreateStructuringElementEx(2,2,1,1,CV_SHAPE_RECT);
  347.     IplConvKernel* element_d = cvCreateStructuringElementEx(5,5,3,3,CV_SHAPE_CROSS);//CV_SHAPE_CROSS CV_SHAPE_RECT CV_SHAPE_ELLIPSE
  348.     // erosion & dilation to connect blobs and remove noisy points
  349.     cvErode(background, background,0,1);
  350.     cvDilate(background, background,element_d,2);
  351.     cvErode(background, background,0,1);
  352.  
  353.     return background;
  354. }
Add Comment
Please, Sign In to add comment