cpclean.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00015 /*  This program is free software; you can redistribute it and/or
00016  *  modify it under the terms of the GNU General Public
00017  *  License as published by the Free Software Foundation; either
00018  *  version 2 of the License, or (at your option) any later version.
00019  *
00020  *  This software is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00023  *  General Public License for more details.
00024  *
00025  *  You should have received a copy of the GNU General Public
00026  *  License along with this software. If not, see
00027  *  <http://www.gnu.org/licenses/>.
00028  *
00029  */
00030 
00031 #include <fstream>
00032 #include <sstream>
00033 #include <getopt.h>
00034 
00035 #include <algorithms/optimizer/PTOptimizer.h>
00036 #include <algorithms/optimizer/ImageGraph.h>
00037 #include <algorithms/control_points/CleanCP.h>
00038 #include "panotools/PanoToolsInterface.h"
00039 
00040 static void usage(const char* name)
00041 {
00042     std::cout << name << ": remove wrong control points by statistic method" << std::endl
00043         << "cpclean version " << hugin_utils::GetHuginVersion() << std::endl
00044         << std::endl
00045         << "Usage:  " << name << " [options] input.pto" << std::endl
00046         << std::endl
00047         << "CPClean uses statistical methods to remove wrong control points" << std::endl << std::endl
00048         << "Step 1 optimises all images pairs, calculates for each pair mean " << std::endl
00049         << "       and standard deviation and removes all control points " << std::endl
00050         << "       with error bigger than mean+n*sigma" << std::endl
00051         << "Step 2 optimises the whole panorama, calculates mean and standard deviation" << std::endl
00052         << "       for all control points and removes all control points with error" << std::endl
00053         << "       bigger than mean+n*sigma" << std::endl << std::endl
00054         << "  Options:" << std::endl
00055         << "     --output|-o file.pto     Output Hugin PTO file." << std::endl
00056         << "                              Default: '<filename>_clean.pto'." << std::endl
00057         << "     --max-distance|-n num    distance factor for checking (default: 2)" << std::endl
00058         << "     --pairwise-checking|-p   do only pairwise optimisation (skip step 2)" << std::endl
00059         << "     --whole-pano-checking|-w do optimise whole panorama (skip step 1)" << std::endl
00060         << "     --dont-optimize|-s       skip optimisation step when optimisation the" << std::endl
00061         << "                              whole panorama" << std::endl
00062         << "     --check-line-cp|-l       also include line control points for calculation" << std::endl
00063         << "                              and filtering in step 2" << std::endl
00064         << "     --help|-h                 shows help" << std::endl
00065         << std::endl;
00066 }
00067 
00068 // dummy panotools progress functions
00069 static int ptProgress(int command, char* argument)
00070 {
00071     return 1;
00072 }
00073 
00074 static int ptinfoDlg(int command, char* argument)
00075 {
00076     return 1;
00077 }
00078 
00079 int main(int argc, char* argv[])
00080 {
00081     // parse arguments
00082     const char* optstring = "o:hn:pwslv";
00083     static struct option longOptions[] =
00084     {
00085         { "output", required_argument, NULL, 'o'},
00086         { "max-distance", required_argument, NULL, 'n'},
00087         { "pairwise-checking", no_argument, NULL, 'p'},
00088         { "whole-pano-checking", no_argument, NULL, 'w'},
00089         { "dont-optimize", no_argument, NULL, 's'},
00090         { "check-line-cp", no_argument, NULL, 'l' },
00091         { "help", no_argument, NULL, 'h' },
00092         0
00093     };
00094     int c;
00095     std::string output;
00096     bool onlyPair = false;
00097     bool wholePano = false;
00098     bool skipOptimisation = false;
00099     bool includeLineCp = false;
00100     bool verbose = false;
00101     double n = 2.0;
00102     int optionIndex = 0;
00103     while ((c = getopt_long(argc, argv, optstring, longOptions, &optionIndex)) != -1)
00104     {
00105         switch (c)
00106         {
00107             case 'o':
00108                 output = optarg;
00109                 break;
00110             case 'h':
00111                 usage(hugin_utils::stripPath(argv[0]).c_str());
00112                 return 0;
00113             case 'n':
00114                 n = atof(optarg);
00115                 if(n==0)
00116                 {
00117                     std::cerr <<"Invalid parameter: " << optarg << " is not valid real number" << std::endl;
00118                     return 1;
00119                 };
00120                 if (n<1.0)
00121                 {
00122                     std::cerr << "Invalid parameter: n must be at least 1" << std::endl;
00123                     return 1;
00124                 };
00125                 break;
00126             case 'p':
00127                 onlyPair= true;
00128                 break;
00129             case 'w':
00130                 wholePano = true;
00131                 break;
00132             case 's':
00133                 skipOptimisation = true;
00134                 break;
00135             case 'l':
00136                 includeLineCp = true;
00137                 break;
00138             case 'v':
00139                 verbose = true;
00140                 break;
00141             case ':':
00142                 std::cerr <<"Option -n requires a number" << std::endl;
00143                 return 1;
00144                 break;
00145             case '?':
00146                 break;
00147             default:
00148                 abort ();
00149         }
00150     }
00151 
00152     if (argc - optind != 1)
00153     {
00154         usage(hugin_utils::stripPath(argv[0]).c_str());
00155         return 1;
00156     };
00157 
00158     if (onlyPair && wholePano)
00159     {
00160         std::cerr << "Options -p and -w can't used together" << std::endl;
00161         return 1;
00162     };
00163 
00164     std::string input=argv[optind];
00165 
00166     HuginBase::Panorama pano;
00167     std::ifstream prjfile(input.c_str());
00168     if (!prjfile.good())
00169     {
00170         std::cerr << "could not open script : " << input << std::endl;
00171         return 1;
00172     }
00173     pano.setFilePrefix(hugin_utils::getPathPrefix(input));
00174     AppBase::DocumentData::ReadWriteError err = pano.readData(prjfile);
00175     if (err != AppBase::DocumentData::SUCCESSFUL)
00176     {
00177         std::cerr << "error while parsing panos tool script: " << input << std::endl;
00178         std::cerr << "DocumentData::ReadWriteError code: " << err << std::endl;
00179         return 1;
00180     }
00181 
00182     const size_t nrImg=pano.getNrOfImages();
00183     if (nrImg < 2)
00184     {
00185         std::cerr << "Panorama should consist of at least two images" << std::endl;
00186         return 1;
00187     }
00188 
00189     if (pano.getNrOfCtrlPoints() < 3)
00190     {
00191         std::cerr << "Panorama should contain at least 3 control point" << std::endl;
00192     };
00193 
00194     if (!verbose)
00195     {
00196         PT_setProgressFcn(ptProgress);
00197         PT_setInfoDlgFcn(ptinfoDlg);
00198     };
00199 
00200     size_t cpremoved1 = 0;
00201     HuginBase::UIntSet CPtoRemove;
00202     // step 1 with pairwise optimisation
00203     if(!wholePano)
00204     {
00205         AppBase::DummyProgressDisplay dummy;
00206         CPtoRemove=getCPoutsideLimit_pair(pano, dummy, n);
00207         if (CPtoRemove.size()>0)
00208             for (HuginBase::UIntSet::reverse_iterator it = CPtoRemove.rbegin(); it != CPtoRemove.rend(); ++it)
00209             {
00210                 pano.removeCtrlPoint(*it);
00211             }
00212         cpremoved1=CPtoRemove.size();
00213     };
00214 
00215     // step 2 with optimisation of whole panorama
00216     bool unconnected=false;
00217     if(!onlyPair)
00218     {
00219         //check for unconnected images
00220         HuginGraph::ImageGraph graph(pano);
00221         unconnected = !graph.IsConnected();
00222         if (!unconnected)
00223         {
00224             CPtoRemove.clear();
00225             if(skipOptimisation)
00226             {
00227                 std::cout << std::endl << "Skipping optimisation, current image positions will be used." << std::endl;
00228             };
00229             CPtoRemove=getCPoutsideLimit(pano, n, skipOptimisation, includeLineCp);
00230             if (CPtoRemove.size()>0)
00231                 for (HuginBase::UIntSet::reverse_iterator it = CPtoRemove.rbegin(); it != CPtoRemove.rend(); ++it)
00232                 {
00233                     pano.removeCtrlPoint(*it);
00234                 }
00235         };
00236     };
00237 
00238     std::cout << std::endl;
00239     if(!wholePano)
00240     {
00241         std::cout << "Removed " << cpremoved1 << " control points in step 1" << std::endl;
00242     }
00243     if (!onlyPair)
00244     {
00245         if (unconnected)
00246         {
00247             std::cout << "Skipped step 2 because of unconnected image pairs" << std::endl;
00248         }
00249         else
00250         {
00251             std::cout << "Removed " << CPtoRemove.size() << " control points in step 2" << std::endl;
00252         };
00253     };
00254 
00255     //write output
00256     HuginBase::OptimizeVector optvec = pano.getOptimizeVector();
00257     HuginBase::UIntSet imgs;
00258     fill_set(imgs,0, pano.getNrOfImages()-1);
00259     // Set output .pto filename if not given
00260     if (output=="")
00261     {
00262         output=input.substr(0,input.length()-4).append("_clean.pto");
00263     }
00264     std::ofstream of(output.c_str());
00265     pano.printPanoramaScript(of, optvec, pano.getOptions(), imgs, false, hugin_utils::getPathPrefix(input));
00266 
00267     std::cout << std::endl << "Written output to " << output << std::endl;
00268     return 0;
00269 }

Generated on 12 Feb 2016 for Hugintrunk by  doxygen 1.4.7