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

Generated on 5 Dec 2016 for Hugintrunk by  doxygen 1.4.7