Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Ivo Brauers (3825345)
- Dion Gerritzen (3220494)
- */
- // Computer Vision Assignment 4
- // Public code - Part 2
- // Platform for voxel-based 3D reconstruction
- // By Xinghan Luo, March 2010
- #include "cv.h"
- #include "highgui.h"
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <time.h>
- #include "cvcCam.h"
- #include "xScene3D.h"
- using namespace std;
- #define viewNr 4
- #define viewW 640
- #define viewH 480
- char* intToChar(int);
- IplImage* getFG( IplImage* , IplImage*);
- int main( int argc, char** argv ){
- int key = 0;
- int currentWindow = 0;
- bool Pause = false;
- bool topView = false;
- bool rotView = false;
- bool videoEnd = false;
- IplImage** frame = new IplImage*[viewNr];
- IplImage** BG = new IplImage*[viewNr];
- IplImage** FG = new IplImage*[viewNr];
- IplImage** image = new IplImage*[viewNr];
- CvCapture** capture = new CvCapture*[viewNr];
- //create the camera instances
- cvcCam** Cam;
- Cam = new cvcCam*[viewNr];
- // camera calibration files
- Cam[0] = new cvcCam(0,viewW,viewH, "images/img1.ini");
- Cam[1] = new cvcCam(1,viewW,viewH, "images/img2.ini");
- Cam[2] = new cvcCam(2,viewW,viewH, "images/img3.ini");
- Cam[3] = new cvcCam(3,viewW,viewH, "images/img4.ini");
- // background image
- BG[0]= cvLoadImage("images/bg1.jpg", 1);
- BG[1]= cvLoadImage("images/bg2.jpg", 1);
- BG[2]= cvLoadImage("images/bg3.jpg", 1);
- BG[3]= cvLoadImage("images/bg4.jpg", 1);
- // the input images
- image[0] = cvLoadImage("images/img1.jpg", 1);
- image[1] = cvLoadImage("images/img2.jpg", 1);
- image[2] = cvLoadImage("images/img3.jpg", 1);
- image[3] = cvLoadImage("images/img4.jpg", 1);
- // the input videos
- //capture[0] = cvCaptureFromAVI("data/f.avi");
- //capture[1] = cvCaptureFromAVI("data/l.avi");
- //capture[2] = cvCaptureFromAVI("data/r.avi");
- //capture[3] = cvCaptureFromAVI("data/s.avi");
- // background in HSV
- IplImage** b_HSV = new IplImage*[viewNr];
- for (int i = 0; i < viewNr; i++) {
- b_HSV[i] = cvCreateImage( cvGetSize(BG[i]), 8, 3);
- cvCvtColor(BG[i],b_HSV[i],CV_BGR2HSV);
- }
- cvNamedWindow( "Video", 1 );
- //cvMoveWindow( "Video", -1280, 0 );
- cvNamedWindow( "Foreground image", 1 );
- //cvMoveWindow( "Foreground image", -1280, 500 );
- xScene3D scene3D(viewNr);
- // transfer camera coordinate to 3D scene
- for (int i = 0; i < viewNr; i++) {
- scene3D.getCamCoord(Cam[i]->camPt);
- }
- // volumetric reconstruction
- xVR* VR = new xVR(viewNr, viewW, viewH, Cam);
- scene3D.OPenGl3DScene("OpenGL 3D scene");
- while( key != 'q' ) {
- if (!Pause) {
- // get frame from videos
- for (int i = 0; i < viewNr; i++) {
- frame[i] = image[i];
- //frame[i] = cvQueryFrame(capture[i]);
- // cvFlip(frame[i],frame[i],0);
- // end condition
- if( !frame[i]) {
- videoEnd = true;
- break;
- }
- FG[i] = getFG(frame[i],b_HSV[i]);
- Cam[i]->foreground = FG[i];
- }
- if(videoEnd)
- break;
- //scene3D.frmCnt++;
- //cout << "Frame: "<< scene3D.frmCnt << endl;
- VR->updateVoxels(Cam);
- }
- cvShowImage( "Video", frame[currentWindow]);
- cvShowImage( "Foreground image", FG[currentWindow]);
- if(topView)
- scene3D.getViewIdx(currentWindow);
- else {
- if(scene3D.cameraView)
- scene3D.resetTopView();
- }
- if(!topView) {
- rotView = false;
- scene3D.angle = 0;
- }
- // render the scene
- if(rotView)
- scene3D.rotatView();
- scene3D.Render(VR, Cam);
- // release current FGwe
- if(!Pause) {
- for (int i = 0; i < viewNr; i++)
- cvReleaseImage(&FG[i]);
- }
- switch(key){
- case 'p':
- if(Pause){
- Pause = false;
- cout << "Resume..." << endl;
- } else {
- Pause = true;
- cout << "Pause..." << endl;
- }
- break;
- case 't':
- if(topView){
- topView = false;
- cout << "Disable top view..." << endl;
- } else {
- topView = true;
- cout << "Enable top view..." << endl;
- }
- break;
- case 'v':
- if(scene3D.showVolume){
- scene3D.showVolume = false;
- cout << "Hide voxel acquistion volume..." << endl;
- } else {
- scene3D.showVolume = true;
- cout << "Display voxel acquistion volume..." << endl;
- }
- break;
- case 'g':
- if(scene3D.showGrdFlr){
- cout << "Hide ground floor..." << endl;
- scene3D.showGrdFlr = false;
- } else {
- scene3D.showGrdFlr = true;
- cout << "Display ground floor..." << endl;
- }
- break;
- case 'c':
- if(scene3D.showCam){
- scene3D.showCam = false;
- cout << "Hide cameras..." << endl;
- } else {
- scene3D.showCam = true;
- cout << "Display cameras..." << endl;
- }
- break;
- case 'o':
- if(scene3D.showOrg){
- scene3D.showOrg = false;
- cout << "Hide world orginal coordinates..." << endl;
- } else {
- scene3D.showOrg = true;
- cout << "Display world orginal coordinates..." << endl;
- }
- break;
- case 'r':
- if(rotView){
- rotView = false;
- cout << "Disable rotation view..." << endl;
- } else {
- rotView = true;
- cout << "Enable rotation voxels..." << endl;
- }
- break;
- case '1':
- currentWindow = 0;
- break;
- case '2':
- currentWindow = 1;
- break;
- case '3':
- currentWindow = 2;
- break;
- case '4':
- currentWindow = 3;
- break;
- }
- key = cvWaitKey( 10 );
- }
- //for (int i = 0; i < viewNr; i++)
- //cvReleaseCapture( &capture[i] );
- cout << "Done. Press any key to end..." << endl;
- cvWaitKey();
- delete[] frame;
- delete[] BG;
- delete[] FG;
- cvDestroyWindow( "Video" );
- cvDestroyWindow( "Foreground image" );
- return 0;
- }
- char* intToChar(int i) {
- char st[10];
- char * str = _itoa(i, st, 10); // decimal
- return str;
- }
- IplImage* getFG( IplImage* frame, IplImage* BGframe){
- // clock the computations
- clock_t start,end;
- start=clock();
- // define temporary parameters
- CvSize sz = cvGetSize(frame); // size of frame
- IplImage* background = cvCreateImage( sz, IPL_DEPTH_8U, 1 );
- IplImage* h; // image consisting of the h-values of the current frame
- IplImage* s; // image consisting of the s-values of the current frame
- IplImage* v; // image consisting of the v-values of the current frame
- h = cvCreateImage( sz, IPL_DEPTH_8U, 1 ); // allocating the h-values of the current frame
- s = cvCreateImage( sz, IPL_DEPTH_8U, 1 ); // allocating the s-values of the current frame
- v = cvCreateImage( sz, IPL_DEPTH_8U, 1 ); // allocating the v-values of the current frame
- IplImage* hBG; // image consisting of the h-values of the background frame
- IplImage* sBG; // image consisting of the s-values of the background frame
- IplImage* vBG; // image consisting of the v-values of the background frame
- hBG = cvCreateImage( sz, IPL_DEPTH_8U, 1 ); // allocating the h-values of the background frame
- sBG = cvCreateImage( sz, IPL_DEPTH_8U, 1 ); // allocating the s-values of the background frame
- vBG = cvCreateImage( sz, IPL_DEPTH_8U, 1 ); // allocating the v-values of the background frame
- IplImage *background_t; // in between result of the background
- IplImage *temp; // temporary result image
- background_t = cvCreateImage( sz, IPL_DEPTH_8U, 1 ); // allocating the temporary background
- temp = cvCreateImage( sz, IPL_DEPTH_8U, 1 ); // allocating the temporary background
- // Colour transformation from rgb to hsv for the background model
- // -> this part could also be outside this function because now it is performed for every new
- // frame, while it only has to be done once the background frame is captured!!
- //IplImage* b_HSV = cvCreateImage( sz, 8, 3); // definition and allocation
- //cvCvtColor(BGframe,b_HSV,CV_BGR2HSV); // color conversion
- cvSplit( BGframe, hBG, sBG, vBG, NULL ); // split the hsv- channels for further analysis
- // Colour transformation from rgb to hsv for the current frame
- IplImage* f_HSV = cvCreateImage( sz, 8, 3); // definition and allocation
- cvCvtColor(frame,f_HSV,CV_BGR2HSV); // color conversion
- cvSplit( f_HSV, h, s, v, NULL ); // split the hsv- channels for further analysis
- // Definition of the thresholds
- int threshold_H = 20;//20;
- int threshold_S = 20;//20;
- int threshold_V = 40;
- // BACKGROUND SUBTRACTION
- // initialize background by h-channel
- cvAbsDiff(h,hBG,temp); // absolute difference between background model and current frame
- cvThreshold(temp , background, threshold_H, 255, CV_THRESH_BINARY ); // thresholding
- // update background by s-channel
- cvAbsDiff(s,sBG,temp); // absolute difference between background model and current frame
- cvThreshold( temp, background_t, threshold_S, 255, CV_THRESH_BINARY ); // thresholding
- cvAnd(background, background_t, background); // perform the AND operator to update background
- // update background by v-channel
- cvAbsDiff(v,vBG,temp); // absolute difference between background model and current frame
- cvThreshold( temp, background_t, threshold_V, 255, CV_THRESH_BINARY ); // thresholding
- cvOr(background, background_t, background); // perform the OR operator to update background
- // deallocate all temporary variables
- cvReleaseImage(&temp);
- cvReleaseImage(&background_t);
- cvReleaseImage(&h);
- cvReleaseImage(&s);
- cvReleaseImage(&v);
- cvReleaseImage(&hBG);
- cvReleaseImage(&sBG);
- cvReleaseImage(&vBG);
- cvReleaseImage(&f_HSV);
- // cout << "Nico code" << endl;
- // clock the computations
- end=clock();
- double time_elapsed = double(end - start)/CLOCKS_PER_SEC;
- // erosion kernel
- IplConvKernel* element_e = cvCreateStructuringElementEx(2,2,1,1,CV_SHAPE_RECT);
- IplConvKernel* element_d = cvCreateStructuringElementEx(5,5,3,3,CV_SHAPE_CROSS);//CV_SHAPE_CROSS CV_SHAPE_RECT CV_SHAPE_ELLIPSE
- // erosion & dilation to connect blobs and remove noisy points
- cvErode(background, background,0,1);
- cvDilate(background, background,element_d,2);
- cvErode(background, background,0,1);
- return background;
- }
Add Comment
Please, Sign In to add comment