Basic Image AlgorithmS Library 2.8.0

ExampleMultipleDepthWarp.cpp

00001 /*
00002  * ExampleMultipleDepthWarp.cpp
00003  *
00004  *  Created on: Jan 28, 2010
00005  *      Author: africk
00006  */
00007 
00008 /*
00009  This file is part of the BIAS library (Basic ImageAlgorithmS).
00010 
00011  Copyright (C) 2003-2009    (see file CONTACT for details)
00012  Multimediale Systeme der Informationsverarbeitung
00013  Institut fuer Informatik
00014  Christian-Albrechts-Universitaet Kiel
00015 
00016 
00017  BIAS is free software; you can redistribute it and/or modify
00018  it under the terms of the GNU Lesser General Public License as published by
00019  the Free Software Foundation; either version 2.1 of the License, or
00020  (at your option) any later version.
00021 
00022  BIAS is distributed in the hope that it will be useful,
00023  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025  GNU Lesser General Public License for more details.
00026 
00027  You should have received a copy of the GNU Lesser General Public License
00028  along with BIAS; if not, write to the Free Software
00029  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00030  */
00031 
00032 
00033 /**
00034    @example ExampleMultipleDepthWarp.cpp
00035    @relates MultipleDepthWarp
00036    @brief Example for using the multidepthwarp
00037    @ingroup g_examples
00038    @ingroup g_openglframework
00039    @author Anatol Frick
00040 */
00041 
00042 
00043 #include <Base/Image/ImageConvert.hh>
00044 
00045 #include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
00046 #include <OpenGLFramework/SpecializedBatches/MultipleDepthWarp.hh>
00047 #include <OpenGLFramework/Base/glfException.hh>
00048 
00049 #include <Utils/Param.hh>
00050 #include <Utils/IOUtils.hh>
00051 #include <Base/Image/ImageIO.hh>
00052 #include <Base/Image/Image.hh>
00053 #include <Image/Camera.hh>
00054 #include <Filter/Gauss.hh>
00055 #include <Base/Debug/TimeMeasure.hh>
00056 #include <OpenGLFramework/SpecializedBatches/DisparityInterpolator.hh>
00057 #include <OpenGLFramework/SpecializedBatches/SeparabelBoxFilter.hh>
00058 #include <Filter/Rescale.hh>
00059 #include <Filter/Median.hh>
00060 
00061 #include <Gui/biasgl.h>
00062 
00063 using namespace std;
00064 using namespace BIAS;
00065 
00066 
00067 void ShiftAndAdjustFrustum(ProjectionParametersPerspective* ppp, double distFromCam, double shift) {
00068     unsigned int width, height;
00069     ppp->GetImageSize(width,height);
00070 
00071     cout << ppp->GetK() << endl;
00072 
00073     HomgPoint2D leftTop(0.5,0.5,1);
00074     HomgPoint2D rightBottom((double)width-0.5, (double) height - 0.5,1);
00075     Vector3<double> p;
00076 
00077     Vector3<double> d1;
00078     ppp->UnProjectLocal(leftTop, p, d1, true);
00079 
00080     d1 = (d1 / d1[2]) * distFromCam;
00081 
00082     d1[0] -= shift;
00083 
00084     Vector3<double> d2;
00085     ppp->UnProjectLocal(rightBottom, p , d2, true);
00086 
00087     d2 = (d2 / d2[2]) * distFromCam;
00088 
00089     d2[0] -= shift;
00090 
00091     ppp->SetIntrinsics(d1,d2,width,height);
00092 
00093     ppp->SetC(ppp->GetC() + Vector3<double>(shift,0,0));
00094 
00095     cout << ppp->GetK() << endl;
00096 
00097 }
00098 
00099 
00100 int main(int argc, char* argv[]) {
00101     Param params(true);
00102 
00103     string* firstDepthImageName = params.AddParamString("firstDepthImage",
00104                                                         "image of first source camera", "", 'f');
00105     string* secondDepthImageName = params.AddParamString("secondDepthImage",
00106                                                          "image of second source camera", "", 's');
00107 
00108 // unused    string* dispImageName = params.AddParamString("resultDispImage",
00109 //                                                  "result disparity image name", "depthResult.mip", 'd');
00110 // unused    string* colorImageName = params.AddParamString("resultColorImage",
00111 //                                                   "result color image name", "colorResult.mip", 'c');
00112 
00113     string* textureForTheFirstSourceProj = params.AddParamString("projectiveTexture",
00114                                                                  "texture for the first source proj", "", 'p');
00115 
00116     string* targetProjName = params.AddParamString("targetProj",
00117                                                    "projection with params of target perspective cam", "", 't');
00118     string* firstSourceProjName = params.AddParamString("firstSourceProj",
00119                                                         "projection with params of first source cam", "", 'a');
00120     string* secondSourceProjName = params.AddParamString("secondSourceProj",
00121                                                          "projection with params of second source cam", "", 'b');
00122 
00123     double* zNear = params.AddParamDouble("zNear", "", 100);
00124 
00125 // unused    double* distFromCam = params.AddParamDouble("distFromCam", "distance from camera centrum to the display in millimeter", 4000);
00126 // unused    double* eyeDist = params.AddParamDouble("eyeDist", "distance between the eyes in millimeter", 65);
00127     double* zFar = params.AddParamDouble("zFar", "", 8000);
00128 // unused    int* step = params.AddParamInt("step","", 0);
00129 
00130     if (!IOUtils::ParseCommandLineEvalHelp(params, argc, argv))
00131         return 0;
00132 
00133     vector<ProjectionParametersPerspectiveDepth> pmdParams;
00134 
00135     vector<Projection> sourceProjs;
00136     Projection targetProj;
00137 
00138     if ((*firstSourceProjName).compare("")!=0) {
00139         sourceProjs.push_back(Projection());
00140         if (sourceProjs.back().Load(*firstSourceProjName) != 0) {
00141             BIASERR("Could not load source tof projection: "<<*firstSourceProjName);
00142             return -1;
00143         }
00144     }
00145 
00146     if ((*secondSourceProjName).compare("")!=0) {
00147         sourceProjs.push_back(Projection());
00148         if (sourceProjs.back().Load(*secondSourceProjName) != 0) {
00149             BIASERR("Could not load source tof projection: "<<*secondSourceProjName);
00150             return -1;
00151         }
00152     }
00153 
00154     if (targetProj.Load(*targetProjName) != 0) {
00155         BIASERR("Could not load target tof projection:"<<*targetProjName);
00156         return -1;
00157     }
00158 
00159     Image<float> firstDepthMap, secondDepthMap;
00160     if ((*firstDepthImageName).compare("")!=0) {
00161         if (ImageIO::Load(*firstDepthImageName, firstDepthMap)) {
00162             BIASERR("Could not load first depth map :"<< *firstDepthImageName);
00163             return -1;
00164         }
00165     }
00166     if ((*secondDepthImageName).compare("")!=0) {
00167         if (ImageIO::Load(*secondDepthImageName, secondDepthMap)) {
00168             BIASERR("Could not load second depth map :"<< *secondDepthImageName);
00169             //return -1;
00170         }
00171     }
00172 
00173     Image<unsigned char> projTexture;
00174 
00175     if ((*textureForTheFirstSourceProj).compare("")!=0) {
00176         if (ImageIO::Load(*textureForTheFirstSourceProj, projTexture)) {
00177             BIASERR("Could not load second depth map :"<< *textureForTheFirstSourceProj);
00178             return -1;
00179         }
00180     }
00181 
00182 
00183     MultipleDepthWarp mpmdWarp;
00184 
00185 
00186     ProjectionParametersBase* ppb = targetProj.GetParameters();
00187     ProjectionParametersPerspective* ppp =
00188             dynamic_cast<ProjectionParametersPerspective*> (ppb);
00189 
00190     ppp->Rescale((unsigned int)960, (unsigned int) 540);
00191 
00192    //ShiftAndAdjustFrustum(ppp,(*distFromCam),((*distFromCam)/4000.0)*(*eyeDist)*(*step));
00193 
00194 
00195     ProjectionParametersBase* source0_ppb = sourceProjs[0].GetParameters();
00196     ProjectionParametersPerspective* source0_ppp =
00197             dynamic_cast<ProjectionParametersPerspective*> (source0_ppb);
00198 
00199     source0_ppp->Rescale((unsigned int) 960, (unsigned int) 540);
00200 
00201     if (ppp == NULL) {
00202         BIASERR("target parameters are not from type perspective");
00203         return -1;
00204     }
00205 
00206 
00207 
00208 
00209  Image<float> tmpDepthMap;
00210     Gauss<float,float> gaussFilter;
00211     gaussFilter.SetHalfWinSize(30);
00212     gaussFilter.Filter(firstDepthMap,tmpDepthMap);
00213     firstDepthMap = tmpDepthMap;
00214 
00215     TimeMeasure tm;
00216     Image<float> warpedDepth;
00217     Image<unsigned char> warpedColor;
00218     Image<unsigned char> redGreen;
00219     glfPBuffer pbuffer;
00220     try {
00221 
00222         glfRenderingContextConfig cfg;
00223         cfg.width = 5;
00224         cfg.height = 5;
00225         cfg.doubleBuffer = 0;
00226         cfg.redSize = 8;
00227         cfg.greenSize = 8;
00228         cfg.blueSize = 8;
00229         cfg.alphaSize = 0;
00230         cfg.depthSize = 24;
00231         cfg.stencilSize = 0;
00232 
00233         pbuffer.Init(cfg);
00234 
00235         mpmdWarp.SetDepthScaleBeforeWarping(1.0f);
00236 
00237         //      ppp->Rescale((unsigned int) 960, (unsigned int) 540);
00238         mpmdWarp.Init(*ppp, sourceProjs);
00239         mpmdWarp.SetupNViewsWarp(2000,1.0,20, 8, 4, false);
00240         tm.Start();
00241         //       for (unsigned int i = 0; i < 1000; i++) {
00242         mpmdWarp.UploadDepthMap(firstDepthMap, 0);
00243         //mpmdWarp.UploadDepthMap(secondDepthMap, 1);
00244         mpmdWarp.SetProjectiveTextureCam(*source0_ppp);
00245         mpmdWarp.UploadProjectiveTexture(projTexture);
00246         mpmdWarp.UseProjectiveTexturing(true);
00247 
00248         mpmdWarp.SetWarpType(MultipleDepthWarp::DEPTH);
00249         mpmdWarp.SetDiscardAngle(80);
00250         mpmdWarp.SetDispForDistance(3000, 128);
00251         mpmdWarp.SetZNearAndZFar((float) *zNear, (float) *zFar);
00252        mpmdWarp.WarpNViews();
00253 
00254         //    }
00255         tm.Stop();
00256 
00257         cout << "frame rate = " << (1000.0 / (tm.GetRealTime() / 1000000.0)) << endl;
00258 
00259         tm.Print(cout);
00260 
00261        // mpmdWarp.GetProjectedTexture(warpedColor);
00262      //   mpmdWarp.GetWarpedDisparity(warpedDepth);
00263         //boxFilter.GetFilteredTexture(result);
00264 
00265         vector<glfTexture2D*>* textures = mpmdWarp.GetResultTexturesFromNViewWarp();
00266 
00267         //1
00268         ((*textures)[0])->CopyToImage(warpedColor);
00269         warpedColor.Flip();
00270         ImageIO::Save("colorImage1.png",warpedColor);
00271 
00272 //        //2
00273 //        ((*textures)[1])->CopyToImage(warpedColor);
00274 //        warpedColor.Flip();
00275 //        ImageIO::Save("colorImage2.png",warpedColor);
00276 //
00277 //        //3
00278 //        ((*textures)[2])->CopyToImage(warpedColor);
00279 //        warpedColor.Flip();
00280 //        ImageIO::Save("colorImage3.png",warpedColor);
00281 //
00282 //        //4
00283 //        ((*textures)[3])->CopyToImage(warpedColor);
00284 //        warpedColor.Flip();
00285 //        ImageIO::Save("colorImage4.png",warpedColor);
00286 //
00287 //        //5
00288 //        ((*textures)[4])->CopyToImage(warpedColor);
00289 //        warpedColor.Flip();
00290 //        ImageIO::Save("colorImage5.png",warpedColor);
00291 //
00292 //        //6
00293 //        ((*textures)[5])->CopyToImage(warpedColor);
00294 //        warpedColor.Flip();
00295 //        ImageIO::Save("colorImage6.png",warpedColor);
00296 //
00297 //        //7
00298 //        ((*textures)[6])->CopyToImage(warpedColor);
00299 //        warpedColor.Flip();
00300 //        ImageIO::Save("colorImage7.png",warpedColor);
00301 //
00302 //        //8
00303 //        ((*textures)[7])->CopyToImage(warpedColor);
00304 //        warpedColor.Flip();
00305 //        ImageIO::Save("colorImage8.png",warpedColor);
00306 
00307 
00308      //   warpedColor.Flip();
00309       //  warpedDepth.Flip();
00310 
00311 
00312 //        if (ImageIO::Save(*dispImageName, warpedDepth) == 0) {
00313 //            cout << "written disparity map " << *dispImageName << endl;
00314 //        } else {
00315 //            BIASERR("could not write " << *dispImageName);
00316 //            pbuffer.Destroy();
00317 //            return -1;
00318 //        }
00319 //
00320 //
00321 //        if (ImageIO::Save(*colorImageName, warpedColor) == 0) {
00322 //            cout << "written disparity map " << *colorImageName << endl;
00323 //        } else {
00324 //            BIASERR("could not write " << *colorImageName);
00325 //            pbuffer.Destroy();
00326 //            return -1;
00327 //        }
00328 //
00329 //
00330 //        redGreen = projTexture;
00331 //        Image<unsigned char> grey1;
00332 //        ImageConvert::Convert(projTexture,grey1,ImageBase::CM_Grey);
00333 //        Image<unsigned char> grey2;
00334 //        ImageConvert::Convert(warpedColor, grey2,ImageBase::CM_Grey);
00335 //
00336 //        redGreen.Clear(0);
00337 //        for (unsigned int x = 0; x < warpedColor.GetWidth(); x++) {
00338 //            for (unsigned int y = 0; y < warpedColor.GetHeight(); y++) {
00339 //                redGreen.SetPixel(grey1.PixelValue(x,y),x,y,0);
00340 //                redGreen.SetPixel(grey2.PixelValue(x,y),x,y,1);
00341 //              //  redGreen.SetPixel(grey1.PixelValue(x,y),x,y,2);
00342 //            }
00343 //        }
00344 //
00345 //        ImageIO::Save("redGreenTexture.png",redGreen);
00346 
00347     } catch (glfException& e) {
00348         std::cout << "Error: " << e.GetMessageString() << std::endl;
00349         pbuffer.Destroy();
00350         return -1;
00351     }
00352 
00353     pbuffer.Destroy();
00354 
00355     return 0;
00356 }
00357 
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends