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     while ((c = getopt_long(argc, argv, optstring, longOptions, nullptr)) != -1)
00103     {
00104         switch (c)
00105         {
00106             case 'o':
00107                 output = optarg;
00108                 break;
00109             case 'h':
00110                 usage(hugin_utils::stripPath(argv[0]).c_str());
00111                 return 0;
00112             case 'n':
00113                 n = atof(optarg);
00114                 if(n==0)
00115                 {
00116                     std::cerr << hugin_utils::stripPath(argv[0]) << ": Invalid parameter: " << optarg << " is not valid real number" << std::endl;
00117                     return 1;
00118                 };
00119                 if (n<1.0)
00120                 {
00121                     std::cerr << hugin_utils::stripPath(argv[0]) << ": Invalid parameter: n must be at least 1" << std::endl;
00122                     return 1;
00123                 };
00124                 break;
00125             case 'p':
00126                 onlyPair= true;
00127                 break;
00128             case 'w':
00129                 wholePano = true;
00130                 break;
00131             case 's':
00132                 skipOptimisation = true;
00133                 break;
00134             case 'l':
00135                 includeLineCp = true;
00136                 break;
00137             case 'v':
00138                 verbose = true;
00139                 break;
00140             case ':':
00141             case '?':
00142                 // missing argument or invalid switch
00143                 return 1;
00144                 break;
00145             default:
00146                 // this should not happen
00147                 abort ();
00148         }
00149     }
00150 
00151     if (argc - optind != 1)
00152     {
00153         if (argc - optind < 1)
00154         {
00155             std::cerr << hugin_utils::stripPath(argv[0]) << ": No project file given." << std::endl;
00156         }
00157         else
00158         {
00159             std::cerr << hugin_utils::stripPath(argv[0]) << ": Only one project file expected." << std::endl;
00160         };
00161         return 1;
00162     };
00163 
00164     if (onlyPair && wholePano)
00165     {
00166         std::cerr << hugin_utils::stripPath(argv[0]) << ": Options -p and -w can't used together" << std::endl;
00167         return 1;
00168     };
00169 
00170     std::string input=argv[optind];
00171 
00172     HuginBase::Panorama pano;
00173     std::ifstream prjfile(input.c_str());
00174     if (!prjfile.good())
00175     {
00176         std::cerr << "could not open script : " << input << std::endl;
00177         return 1;
00178     }
00179     pano.setFilePrefix(hugin_utils::getPathPrefix(input));
00180     AppBase::DocumentData::ReadWriteError err = pano.readData(prjfile);
00181     if (err != AppBase::DocumentData::SUCCESSFUL)
00182     {
00183         std::cerr << "error while parsing panos tool script: " << input << std::endl;
00184         std::cerr << "DocumentData::ReadWriteError code: " << err << std::endl;
00185         return 1;
00186     }
00187 
00188     const size_t nrImg=pano.getNrOfImages();
00189     if (nrImg < 2)
00190     {
00191         std::cerr << "Panorama should consist of at least two images" << std::endl;
00192         return 1;
00193     }
00194 
00195     if (pano.getNrOfCtrlPoints() < 3)
00196     {
00197         std::cerr << "Panorama should contain at least 3 control point" << std::endl;
00198     };
00199 
00200     if (!verbose)
00201     {
00202         PT_setProgressFcn(ptProgress);
00203         PT_setInfoDlgFcn(ptinfoDlg);
00204     };
00205 
00206     size_t cpremoved1 = 0;
00207     HuginBase::UIntSet CPtoRemove;
00208     // step 1 with pairwise optimisation
00209     if(!wholePano)
00210     {
00211         AppBase::DummyProgressDisplay dummy;
00212         CPtoRemove=getCPoutsideLimit_pair(pano, dummy, n);
00213         if (CPtoRemove.size()>0)
00214             for (HuginBase::UIntSet::reverse_iterator it = CPtoRemove.rbegin(); it != CPtoRemove.rend(); ++it)
00215             {
00216                 pano.removeCtrlPoint(*it);
00217             }
00218         cpremoved1=CPtoRemove.size();
00219     };
00220 
00221     // step 2 with optimisation of whole panorama
00222     bool unconnected=false;
00223     if(!onlyPair)
00224     {
00225         //check for unconnected images
00226         HuginGraph::ImageGraph graph(pano);
00227         unconnected = !graph.IsConnected();
00228         if (!unconnected)
00229         {
00230             CPtoRemove.clear();
00231             if(skipOptimisation)
00232             {
00233                 std::cout << std::endl << "Skipping optimisation, current image positions will be used." << std::endl;
00234             };
00235             CPtoRemove=getCPoutsideLimit(pano, n, skipOptimisation, includeLineCp);
00236             if (CPtoRemove.size()>0)
00237                 for (HuginBase::UIntSet::reverse_iterator it = CPtoRemove.rbegin(); it != CPtoRemove.rend(); ++it)
00238                 {
00239                     pano.removeCtrlPoint(*it);
00240                 }
00241         };
00242     };
00243 
00244     std::cout << std::endl;
00245     if(!wholePano)
00246     {
00247         std::cout << "Removed " << cpremoved1 << " control points in step 1" << std::endl;
00248     }
00249     if (!onlyPair)
00250     {
00251         if (unconnected)
00252         {
00253             std::cout << "Skipped step 2 because of unconnected image pairs" << std::endl;
00254         }
00255         else
00256         {
00257             std::cout << "Removed " << CPtoRemove.size() << " control points in step 2" << std::endl;
00258         };
00259     };
00260 
00261     //write output
00262     HuginBase::OptimizeVector optvec = pano.getOptimizeVector();
00263     HuginBase::UIntSet imgs;
00264     fill_set(imgs,0, pano.getNrOfImages()-1);
00265     // Set output .pto filename if not given
00266     if (output=="")
00267     {
00268         output=input.substr(0,input.length()-4).append("_clean.pto");
00269     }
00270     std::ofstream of(output.c_str());
00271     pano.printPanoramaScript(of, optvec, pano.getOptions(), imgs, false, hugin_utils::getPathPrefix(input));
00272 
00273     std::cout << std::endl << "Written output to " << output << std::endl;
00274     return 0;
00275 }

Generated on 2 Dec 2016 for Hugintrunk by  doxygen 1.4.7