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

Generated on 12 Feb 2016 for Hugintrunk by  doxygen 1.4.7