vig_optimize.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00027 #include <hugin_config.h>
00028 #include <fstream>
00029 #include <sstream>
00030 
00031 #include <algorithms/optimizer/PhotometricOptimizer.h>
00032 #include "ExtractPoints.h"
00033 
00034 #ifdef WIN32
00035 #include <getopt.h>
00036 #else
00037 #include <unistd.h>
00038 #endif
00039 
00040 #include <hugin_basic.h>
00041 #include <nona/Stitcher.h>
00042 
00043 #include <tiff.h>
00044 
00045 using namespace std;
00046 using namespace vigra;
00047 using namespace vigra_ext;
00048 using namespace AppBase;
00049 using namespace HuginBase;
00050 
00051 
00052 
00053 static void usage(const char* name)
00054 {
00055     cerr << name << ": Try to determine the radial vignetting" << std::endl
00056          << "vig_optimize version " << hugin_utils::GetHuginVersion() << endl
00057          << std::endl
00058          << "Usage: " << name  << " [options] -o output.pto input.pto" << std::endl
00059          << "Valid options are:" << std::endl
00060          << "  -o file   write results to output project" << std::endl
00061          << "  -v        Verbose, print progress messages" << std::endl
00062          << "  -p n      Number of points to extract" << std::endl
00063          << "  -s level  Work on downscaled images, every step halves width and height" << std::endl
00064          << "  -h        Display help (this text)" << std::endl
00065          << std::endl
00066          << " Expert and debugging options:" << std::endl
00067          << "  -i file   Read corresponding points from file" << std::endl
00068          << "  -w file   Dump corresponding points to file" << std::endl;
00069 }
00070 
00071 
00072 /*
00073 template<class RIMG>
00074 void importROIImage(ImageImportInfo & info, RIMG & img)
00075 {
00076     vigra::Rect2D roi(Point2D(info.getPosition()), info.size());
00077     img.resize(roi);
00078     importImageAlpha(info, destImage(img.m_image), destImage(img.m_mask));
00079 }
00080 */
00081 
00082 
00083 void loadPointsC(FILE* f, std::vector<PointPairRGB>& vec)
00084 {
00085     double dummy1, dummy2;
00086     PointPairRGB point;
00087     double i1, i2;
00088 
00089     int n=0;
00090     do
00091     {
00092         n = fscanf(f, " %lf %lf %lf %lf %lf %lf %f %f %f %f %f %f %lf %f %f %lf",
00093                    &i1, &i2, &(point.p1.x), &(point.p1.y),
00094                    &(point.p2.x), &(point.p2.y),
00095                    &(point.i1.red()), &(point.i1.green()), &(point.i1.blue()),
00096                    &(point.i2.red()), &(point.i2.green()), &(point.i2.blue()),
00097                    // matlab index.. set to zero
00098                    &dummy1,
00099                    &(point.r1), &(point.r2),
00100                    // edge score at this point, zero so far
00101                    &(dummy2) );
00102 
00103 
00104         point.imgNr1 = hugin_utils::roundi(i1);
00105         point.imgNr2 = hugin_utils::roundi(i2);
00106         if (n==16)
00107         {
00108             vec.push_back(point);
00109         }
00110     }
00111     while (n == 16);
00112 }
00113 
00114 void loadPoints(istream& i, std::vector<PointPairRGB>& vec )
00115 {
00116     while(!i.eof() && i.good())
00117     {
00118         double dummy1, dummy2;
00119         PointPairRGB point;
00120         double i1, i2;
00121         i >> i1 >> i2
00122           >> point.p1.x >> point.p1.y
00123           >> point.p2.x  >> point.p2.y
00124           >> point.i1.red()  >> point.i1.green()  >> point.i1.blue()
00125           >> point.i2.red()  >> point.i2.green()  >> point.i2.blue()
00126           // matlab index.. set to zero
00127           >> dummy1
00128           >> point.r1  >> point.r2
00129           // edge score at this point, zero so far
00130           >> dummy2;
00131         point.imgNr1 = hugin_utils::roundi(i1);
00132         point.imgNr2 = hugin_utils::roundi(i2);
00133         if (i.good())
00134         {
00135             vec.push_back(point);
00136         }
00137     }
00138 }
00139 
00140 bool hasphotometricParams(Panorama& pano)
00141 {
00142     OptimizeVector vars=pano.getOptimizeVector();
00143 
00144     for (OptimizeVector::const_iterator it=vars.begin(); it != vars.end(); ++it)
00145     {
00146         for (std::set<std::string>::const_iterator itv = (*it).begin();
00147                 itv != (*it).end(); ++itv)
00148         {
00149             if ((*itv)[0] == 'E' || (*itv)[0] == 'R' || (*itv)[0] == 'V')
00150             {
00151                 return true;
00152             }
00153         }
00154     }
00155     return false;
00156 }
00157 
00158 
00159 int main(int argc, char* argv[])
00160 {
00161     StreamProgressDisplay progressDisplay(std::cout);
00162 
00163     // parse arguments
00164     const char* optstring = "hi:o:p:s:vw:";
00165     int c;
00166 
00167     opterr = 0;
00168 
00169     int pyrLevel=3;
00170     int verbose = 0;
00171     int nPoints = 200;
00172     std::string outputFile;
00173     std::string outputPointsFile;
00174     std::string inputPointsFile;
00175     while ((c = getopt (argc, argv, optstring)) != -1)
00176         switch (c)
00177         {
00178             case 'i':
00179                 inputPointsFile = optarg;
00180                 break;
00181             case 'o':
00182                 outputFile = optarg;
00183                 break;
00184             case 'p':
00185                 nPoints = atoi(optarg);
00186                 break;
00187             case 's':
00188                 pyrLevel=atoi(optarg);
00189                 break;
00190             case 'v':
00191                 verbose++;
00192                 break;
00193             case 'h':
00194                 usage(hugin_utils::stripPath(argv[0]).c_str());
00195                 return 1;
00196             case 'w':
00197                 outputPointsFile = optarg;
00198                 break;
00199             default:
00200                 cerr << "Invalid parameter: " << optarg << std::endl;
00201                 usage(hugin_utils::stripPath(argv[0]).c_str());
00202                 return 1;
00203         }
00204 
00205     unsigned nFiles = argc - optind;
00206     if (nFiles != 1)
00207     {
00208         std::cerr << std::endl << "Error: one pto file needs to be specified" << std::endl <<std::endl;
00209         usage(hugin_utils::stripPath(argv[0]).c_str());
00210         return 1;
00211     }
00212 
00213     const char* scriptFile = argv[optind];
00214     Panorama pano;
00215     ifstream prjfile(scriptFile);
00216     if (!prjfile.good())
00217     {
00218         cerr << "could not open script : " << scriptFile << endl;
00219         return 1;
00220     }
00221     pano.setFilePrefix(hugin_utils::getPathPrefix(scriptFile));
00222     DocumentData::ReadWriteError err = pano.readData(prjfile);
00223     if (err != DocumentData::SUCCESSFUL)
00224     {
00225         cerr << "error while parsing panos tool script: " << scriptFile << endl;
00226         cerr << "DocumentData::ReadWriteError code: " << err << endl;
00227         return 1;
00228     }
00229 
00230     // Ensure photometric parameters are selected for optimizaion
00231     if (!hasphotometricParams(pano))
00232     {
00233         cerr << "ERROR:no photometric parameters were selected for optimization" << endl;
00234         cerr << "please update 'v' line in .pto script and try again." << endl;
00235         return 1;
00236     }
00237 
00238     // suppress tiff warnings
00239     TIFFSetWarningHandler(0);
00240 
00241     // todo: handle images with alpha masks
00242     try
00243     {
00244         std::vector<vigra_ext::PointPairRGB> points;
00245 
00246         if (inputPointsFile != "" )
00247         {
00248             //ifstream ifs(inputPointsFile.c_str());
00249             //loadPoints(ifs, points);
00250             FILE* f = fopen(inputPointsFile.c_str(), "r");
00251             if (f == 0)
00252             {
00253                 perror("Could not open input point file");
00254                 return 1;
00255             }
00256             loadPointsC(f, points);
00257             fclose(f);
00258         }
00259         else
00260         {
00261             loadImgsAndExtractPoints(pano, nPoints, pyrLevel, true, progressDisplay, points, verbose);
00262         }
00263         if (verbose)
00264         {
00265             cout << "\rSelected " << points.size() << " points" << std::endl;
00266         }
00267         if (points.size() == 0)
00268         {
00269             std::cerr << "Error: no overlapping points found, exiting" << std::endl;
00270             return 1;
00271         }
00272 
00273         if (outputPointsFile.size() > 0)
00274         {
00275             ofstream of(outputPointsFile.c_str());
00276             for (std::vector<PointPairRGB>::iterator it = points.begin(); it != points.end(); ++it)
00277             {
00278                 of << (*it).imgNr1 << " " << (*it).imgNr2 << " "
00279                    << (*it).p1.x << " " << (*it).p1.y<< " "
00280                    << (*it).p2.x << " " << (*it).p2.y<< " "
00281                    << (*it).i1.red() << " " << (*it).i1.green() << " " << (*it).i1.blue() << " "
00282                    << (*it).i2.red() << " " << (*it).i2.green() << " " << (*it).i2.blue() << " "
00283                    // matlab index.. set to zero
00284                    << 0 << " "
00285                    << (*it).r1 << " " << (*it).r2 << " "
00286                    // edge score at this point, zero so far
00287                    << 0 << std::endl;
00288             }
00289         }
00290 
00291 
00292         progressDisplay.setMessage("Vignetting Optimization");
00293         PhotometricOptimizer photoopt(pano, &progressDisplay, pano.getOptimizeVector(), points);
00294         photoopt.run();
00295         //              double error = photoopt.getResultError();
00296 
00297         progressDisplay.taskFinished();
00298 
00299         UIntSet allImgs;
00300         fill_set(allImgs,0, pano.getNrOfImages()-1);
00301         // save project
00302         if (outputFile == "" || outputFile == "-")
00303         {
00304             pano.printPanoramaScript(std::cout, pano.getOptimizeVector(), pano.getOptions(), allImgs, false, hugin_utils::getPathPrefix(scriptFile));
00305         }
00306         else
00307         {
00308             ofstream of(outputFile.c_str());
00309             pano.printPanoramaScript(of, pano.getOptimizeVector(), pano.getOptions(), allImgs, false, hugin_utils::getPathPrefix(scriptFile));
00310         }
00311 
00312     }
00313     catch (std::exception& e)
00314     {
00315         cerr << "caught exception: " << e.what() << std::endl;
00316         return 1;
00317     }
00318     return 0;
00319 }

Generated on 3 Aug 2015 for Hugintrunk by  doxygen 1.4.7