PointSampler.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00024 #include "PointSampler.h"
00025 
00026 #include <algorithms/basic/CalculateOptimalScale.h>
00027 #include <algorithms/nona/FitPanorama.h>
00028 #include <algorithms/nona/CenterHorizontally.h>
00029 #include <vigra/convolution.hxx>
00030 
00031 namespace HuginBase
00032 {
00033 
00034 bool PointSampler::runAlgorithm()
00035 {
00036     // is this correct? how much progress requierd?
00037     sampleAndExtractPoints(getProgressDisplay());
00038     
00039     if (getProgressDisplay()->wasCancelled())
00040     {
00041         cancelAlgorithm();
00042     };
00043 
00044     return wasCancelled();
00045 }
00046 
00047 
00048 
00049 void PointSampler::sampleAndExtractPoints(AppBase::ProgressDisplay* progress)
00050 {
00051     PanoramaData& pano = *(o_panorama.getNewCopy()); // don't forget to delete!
00052     std::vector<vigra::FRGBImage*>& images = o_images;
00053     int& nPoints = o_numPoints;
00054     std::vector<vigra_ext::PointPairRGB>& points = o_resultPoints;
00055     
00056     std::vector<vigra::FImage *> lapImgs;
00057     std::vector<vigra::Size2D> origsize;
00058     std::vector<SrcPanoImage> srcDescr;
00059     
00060     // convert to interpolating images
00061     typedef vigra_ext::ImageInterpolator<vigra::FRGBImage::const_traverser, vigra::FRGBImage::ConstAccessor, vigra_ext::interp_cubic> InterpolImg;
00062     std::vector<InterpolImg> interpolImages;
00063     
00064     vigra_ext::interp_cubic interp;
00065     // adjust sizes in panorama
00066     for (unsigned i=0; i < pano.getNrOfImages(); i++)
00067     {
00068         SrcPanoImage simg = pano.getSrcImage(i);
00069         origsize.push_back(simg.getSize());
00070         simg.resize(images[i]->size());
00071         srcDescr.push_back(simg);
00072         pano.setSrcImage(i, simg);
00073         interpolImages.push_back(InterpolImg(srcImageRange(*(images[i])), interp, false));
00074         
00075         vigra::FImage * lap = new vigra::FImage(images[i]->size());
00076         lapImgs.push_back(lap);
00077     }
00078 #pragma omp parallel for
00079     for (int i = 0; i < pano.getNrOfImages(); i++)
00080     {
00081         vigra::laplacianOfGaussian(srcImageRange(*(images[i]), vigra::GreenAccessor<vigra::RGBValue<float> >()), destImage(*(lapImgs[i])), 1);
00082     }
00083     
00084     // extract the overlapping points.
00085 //    PanoramaOptions opts = pano.getOptions();
00086 //    double scale = CalculateOptimalScale::calcOptimalPanoScale(pano.getSrcImage(0),opts);
00087 //    opts.setWidth(utils::roundi(opts.getWidth()*scale), true);    
00088 //    pano.setOptions(opts);
00089     // center panorama and do fit to get minimum of black/unused space around panorama
00090     CenterHorizontally(pano).run();
00091     PanoramaOptions opts = pano.getOptions();
00092     CalculateFitPanorama fitPano(pano);
00093     fitPano.run();
00094     opts.setHFOV(fitPano.getResultHorizontalFOV());
00095     opts.setHeight(roundi(fitPano.getResultHeight()));
00096     pano.setOptions(opts);
00097     SetWidthOptimal(pano).run();
00098     
00099     // if random points.
00100     // extract random points.
00101     // need to get the maximum gray value here..
00102     
00103     std::vector<std::multimap<double, vigra_ext::PointPairRGB> > radiusHist(10);
00104     
00105     unsigned nGoodPoints = 0;
00106     unsigned nBadPoints = 0;
00107     
00108     
00109     // call the samplePoints method of this class
00110     progress->setMessage("sampling points");
00111     samplePoints(interpolImages,
00112                  lapImgs,
00113                  srcDescr,
00114                  pano.getOptions(),
00115                  1/255.0f,
00116                  250/255.0f,
00117                  radiusHist,
00118                  nGoodPoints,
00119                  nBadPoints,
00120                  progress);
00121         
00122     // select points with low laplacian of gaussian values.
00123     progress->setMessage("extracting good points");
00124     sampleRadiusUniform(radiusHist, nPoints, points, progress);
00125     
00126     // scale point coordinates to fit into original panorama.
00127     for (size_t i=0; i < points.size(); i++) {
00128         double scaleF1 = origsize[points[i].imgNr1].x / (float) images[points[i].imgNr1]->size().x;
00129         double scaleF2 = origsize[points[i].imgNr2].x / (float) images[points[i].imgNr2]->size().x;
00130         points[i].p1.x = points[i].p1.x * scaleF1;
00131         points[i].p1.y = points[i].p1.y * scaleF1;
00132         points[i].p2.x = points[i].p2.x * scaleF2;
00133         points[i].p2.y = points[i].p2.y * scaleF2;
00134     }
00135 
00136     for (size_t i=0; i < images.size(); i++) {
00137         delete images[i];
00138         delete lapImgs[i];
00139     }
00140     
00141     delete &pano; // deleting the NewCopy
00142 }
00143 
00144 
00145 
00147 void PointSampler::extractPoints(PanoramaData& pano, std::vector<vigra::FRGBImage*> images, int nPoints,
00148                                  bool randomPoints, AppBase::ProgressDisplay* progress,
00149                                  std::vector<vigra_ext::PointPairRGB>& points)
00150 {
00151     PointSampler* sampler;
00152     if (randomPoints)
00153     {
00154         sampler = new RandomPointSampler(pano, NULL, images, nPoints);
00155     }
00156     else
00157     {
00158         sampler = new AllPointSampler(pano, NULL, images, nPoints);
00159     };
00160     
00161     sampler->sampleAndExtractPoints(progress);
00162     points = sampler->getResultPoints();
00163     
00164     delete sampler;
00165 }
00166 
00167 
00168 }; // namespace
00169 

Generated on 28 Jul 2015 for Hugintrunk by  doxygen 1.4.7