www.pudn.com > Estereo.rar > main.cpp
/*************************************************************************** * * Copyright 2004 by the Massachusetts Institute of Technology. All * rights reserved. * * Developed by David Demirdjian * at the Computer Sciences and Artificial Intelligence Laboratory, * MIT, Cambridge, Massachusetts. * * Permission to use, copy, or modify this software and its documentation * for educational and research purposes only and without fee is hereby * granted, provided that this copyright notice and the original authors's * names appear on all copies and supporting documentation. If individual * files are separated from this distribution directory structure, this * copyright notice must be included. For any other uses of this software, * in original or modified form, including but not limited to distribution * in whole or in part, specific prior permission must be obtained from * MIT. These programs shall not be used, rewritten, or adapted as the * basis of a commercial software or hardware product without first * obtaining appropriate licenses from MIT. MIT. makes no representations * about the suitability of this software for any purpose. It is provided * "as is" without express or implied warranty. * **************************************************************************/ #include "stereoMatching.h" #include "imageio.h" #include#include void printcommand() { printf("usage: estereox -l xxx -r xxx [-t xxx]\n"); printf(" -od xxx -oZ xxx\n"); printf(" -cal foc(pix) baseline(mm) u0(pix) v0(pix) \n"); printf(" -d 32(pix)\n"); } int getpos(int argc, char ** argv, char* optname) { for(int i=0; i =0) strcpy(lim, argv[c+1]); c = getpos(argc,argv, "-r"); if (c>=0) strcpy(rim, argv[c+1]); c = getpos(argc,argv, "-t"); if (c>=0) strcpy(tim, argv[c+1]); c = getpos(argc,argv, "-od"); if (c>=0) strcpy(odim, argv[c+1]); c = getpos(argc,argv, "-oZ"); if (c>=0) strcpy(oZim, argv[c+1]); // disp c = getpos(argc,argv, "-d"); if (c>=0) numdisp = atoi(argv[c+1]); // camera parameters c = getpos(argc,argv, "-cal"); if (c>=0) { foc = atoi(argv[c+1]); bline = atoi(argv[c+2])/1000.0f; u0 = atoi(argv[c+3]); v0 = atoi(argv[c+4]); } // ********** load images ***************** char *ldata_orig = NULL,*rdata_orig = NULL,*tdata_orig = NULL; char *ldata,*rdata,*tdata; unsigned char *odata_orig,*odata; if (strcmp(lim,"")) loadPPM(lim, ldata_orig, ldata, width, height); if (strcmp(rim,"")) loadPPM(rim, rdata_orig, rdata, width, height); if (strcmp(tim,"")) loadPPM(tim, tdata_orig, tdata, width, height); int numimages; if (ldata_orig && rdata_orig) { numimages = (tdata_orig)?3:2; } else { printf("Cannot perform stereo on these images\n"); printcommand(); exit(1); } // allocate memory of disparity image ALLOC_ALIGN_MEMORY(odata, odata_orig, unsigned char, width*height); // prepare for stereo StereoMatching* stereo = new StereoMatching(); stereo->setImageSize(width, height); stereo->setHoropter(0); stereo->setNumDepth(numdisp); stereo->initializeContext(); stereo->setCorrelationWindowSize(csize,csize); stereo->setUndefinedDepthValue(0); stereo->setAcceptDisparityThreshold(5); stereo->setCameraParameters(foc, bline, u0,v0); // ********** apply stereo algorithms ***************** // estimate stereo if (numimages==2) stereo->doStereo((unsigned char*)ldata,(unsigned char*)rdata); else stereo->doStereo((unsigned char*)ldata,(unsigned char*)rdata,(unsigned char*)tdata); // improve stereo (optional) stereo->doStereo_grow(1, stereo_grow_ScoreValidation, stereo_grow_nb_passes); if (strcmp(oZim,"")) { // do 3-D reconstruction (uses sub-pixel disparity) stereo->doReconstruction(); float* zimage = new float[width*height]; stereo->getImage_Z(zimage); // save depth image // ... write your code to write depth image here... delete[] zimage; } // save disparity image if (strcmp(odim,"")) savePGM(odim, (char*)stereo->getDisparityImage(), width, height); return 1; }