ExtractPoints.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00030 #include <hugin_basic.h>
00031 
00032 #include <vigra/error.hxx>
00033 #include <vigra/impex.hxx>
00034 
00035 #include <vigra_ext/Pyramid.h>
00036 #include <vigra_ext/impexalpha.hxx>
00037 #include <algorithms/point_sampler/PointSampler.h>
00038 
00039 template<class ImageType>
00040 std::vector<ImageType *> loadImagesPyr(std::vector<std::string> files, int pyrLevel, int verbose, HuginBase::LimitIntensityVector& limits, float& imageStepSize)
00041 {
00042     std::vector<ImageType *> srcImgs(files.size());
00043     limits.resize(srcImgs.size());
00044 #ifdef _WIN32
00045     // init vigra codec manager before threaded section
00046     // otherwise there could be sometimes race conditions
00047     // which results in exceptions
00048     std::string s = vigra::impexListExtensions();
00049 #endif
00050 #pragma omp parallel for schedule(dynamic)
00051     for (int i=0; i < files.size(); i++) {
00052         ImageType * tImg = new ImageType();
00053         ImageType * tImg2 = new ImageType();
00054         std::ostringstream buf;
00055         vigra::ImageImportInfo info(files[i].c_str());
00056         tImg->resize(info.size());
00057         if (verbose)
00058             buf << "loading: " << files[i] << std::endl;
00059         
00060         const std::string pixelType(info.getPixelType());
00061         if (info.numExtraBands() == 1) {
00062             // dummy mask
00063             vigra::BImage mask(info.size());
00064             vigra::importImageAlpha(info, vigra::destImage(*tImg), vigra::destImage(mask));
00065             if (pixelType == "FLOAT" || pixelType == "DOUBLE")
00066             {
00067                 vigra_ext::FindComponentsMinMax<float> minmax;
00068                 vigra::inspectImageIf(vigra::srcImageRange(*tImg), vigra::srcImage(mask), minmax);
00069                 imageStepSize = std::min(imageStepSize, (minmax.max - minmax.min) / 16384.0f);
00070             };
00071         } else {
00072             vigra::importImage(info, vigra::destImage(*tImg));
00073             if (pixelType == "FLOAT" || pixelType == "DOUBLE")
00074             {
00075                 vigra_ext::FindComponentsMinMax<float> minmax;
00076                 vigra::inspectImage(vigra::srcImageRange(*tImg), minmax);
00077                 imageStepSize = std::min(imageStepSize, (minmax.max - minmax.min) / 16384.0f);
00078             };
00079         }
00080         float div = 1;
00081         limits[i] = HuginBase::LimitIntensity(HuginBase::LimitIntensity::LIMIT_FLOAT);
00082         if (pixelType== "UINT8") {
00083             div = 255;
00084             limits[i] = HuginBase::LimitIntensity(HuginBase::LimitIntensity::LIMIT_UINT8);
00085             imageStepSize = std::min(imageStepSize, 1 / 255.0f);
00086 
00087         } else if (pixelType=="UINT16") {
00088             div = (1<<16)-1;
00089             limits[i] = HuginBase::LimitIntensity(HuginBase::LimitIntensity::LIMIT_UINT16);
00090             imageStepSize = std::min(imageStepSize, 1 / 65536.0f);
00091         }
00092         
00093         if (pyrLevel) {
00094             ImageType * swap;
00095             // create downscaled image
00096             if (verbose > 0) {
00097                 buf << "downscaling: ";
00098             }
00099             for (int l=pyrLevel; l > 0; l--) {
00100                 if (verbose > 0) {
00101                     buf << tImg->size().x << "x" << tImg->size().y << "  " << std::flush;
00102                 }
00103                 vigra_ext::reduceToNextLevel(*tImg, *tImg2);
00104                 swap = tImg;
00105                 tImg = tImg2;
00106                 tImg2 = swap;
00107             }
00108             if (verbose > 0)
00109                 buf << std::endl;
00110         }
00111         if (div > 1) {
00112             div = 1/div;
00113             transformImage(vigra::srcImageRange(*tImg), vigra::destImage(*tImg),
00114                            vigra::functor::Arg1()*vigra::functor::Param(div));
00115         }
00116         srcImgs[i]=tImg;
00117         delete tImg2;
00118         if (verbose > 0)
00119         {
00120             std::cout << buf.str();
00121         };
00122     }
00123     return srcImgs;
00124 }
00125 
00126 
00127 // needs 2.0 progress steps
00128 void loadImgsAndExtractPoints(HuginBase::Panorama pano, int nPoints, int pyrLevel, bool randomPoints, AppBase::ProgressDisplay& progress, std::vector<vigra_ext::PointPairRGB> & points, int verbose, float& imageStepSize)
00129 {
00130     // extract file names
00131     std::vector<std::string> files;
00132     for (size_t i=0; i < pano.getNrOfImages(); i++)
00133         files.push_back(pano.getImage(i).getFilename());
00134     
00135     std::vector<vigra::FRGBImage*> images;
00136     HuginBase::LimitIntensityVector limits;
00137     imageStepSize = 1 / 255.0f;
00138     
00139     // try to load the images.
00140     images = loadImagesPyr<vigra::FRGBImage>(files, pyrLevel, verbose, limits, imageStepSize);
00141     
00142     progress.setMessage("Sampling points");
00143     if(randomPoints)
00144         points = HuginBase::RandomPointSampler(pano, &progress, images, limits, nPoints).execute().getResultPoints();
00145     else
00146         points = HuginBase::AllPointSampler(pano, &progress, images, limits, nPoints).execute().getResultPoints();
00147     progress.taskFinished();
00148 }

Generated on 10 Dec 2016 for Hugintrunk by  doxygen 1.4.7