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     
00059     // convert to interpolating images
00060     typedef vigra_ext::ImageInterpolator<vigra::FRGBImage::const_traverser, vigra::FRGBImage::ConstAccessor, vigra_ext::interp_cubic> InterpolImg;
00061     std::vector<InterpolImg> interpolImages;
00062     
00063     vigra_ext::interp_cubic interp;
00064     // adjust sizes in panorama
00065     for (unsigned i=0; i < pano.getNrOfImages(); i++)
00066     {
00067         SrcPanoImage simg = pano.getSrcImage(i);
00068         origsize.push_back(simg.getSize());
00069         simg.resize(images[i]->size());
00070         pano.setSrcImage(i, simg);
00071         interpolImages.push_back(InterpolImg(srcImageRange(*(images[i])), interp, false));
00072         
00073         vigra::FImage * lap = new vigra::FImage(images[i]->size());
00074         lapImgs.push_back(lap);
00075     }
00076 #pragma omp parallel for
00077     for (int i = 0; i < pano.getNrOfImages(); i++)
00078     {
00079         vigra::laplacianOfGaussian(srcImageRange(*(images[i]), vigra::GreenAccessor<vigra::RGBValue<float> >()), destImage(*(lapImgs[i])), 1);
00080     }
00081     
00082     // extract the overlapping points.
00083 //    PanoramaOptions opts = pano.getOptions();
00084 //    double scale = CalculateOptimalScale::calcOptimalPanoScale(pano.getSrcImage(0),opts);
00085 //    opts.setWidth(utils::roundi(opts.getWidth()*scale), true);    
00086 //    pano.setOptions(opts);
00087     // center panorama and do fit to get minimum of black/unused space around panorama
00088     CenterHorizontally(pano).run();
00089     PanoramaOptions opts = pano.getOptions();
00090     CalculateFitPanorama fitPano(pano);
00091     fitPano.run();
00092     opts.setHFOV(fitPano.getResultHorizontalFOV());
00093     opts.setHeight(hugin_utils::roundi(fitPano.getResultHeight()));
00094     // set roi to maximal size, so that the whole panorama is used for sampling
00095     // a more reasonable solution would be to use the maximal
00096     // used area, but this is currently not support to
00097     // calculate optimal roi
00098     opts.setROI(vigra::Rect2D(opts.getSize()));
00099     pano.setOptions(opts);
00100     SetWidthOptimal(pano).run();
00101     
00102     // if random points.
00103     // extract random points.
00104     // need to get the maximum gray value here..
00105     
00106     std::vector<std::multimap<double, vigra_ext::PointPairRGB> > radiusHist(10);
00107     
00108     unsigned nGoodPoints = 0;
00109     unsigned nBadPoints = 0;
00110     
00111     
00112     // call the samplePoints method of this class
00113     progress->setMessage("sampling points");
00114     samplePoints(interpolImages,
00115                  lapImgs,
00116                  pano,
00117                  1/255.0f,
00118                  250/255.0f,
00119                  radiusHist,
00120                  nGoodPoints,
00121                  nBadPoints,
00122                  progress);
00123         
00124     // select points with low laplacian of gaussian values.
00125     progress->setMessage("extracting good points");
00126     sampleRadiusUniform(radiusHist, nPoints, points, progress);
00127     
00128     // scale point coordinates to fit into original panorama.
00129     for (size_t i=0; i < points.size(); i++) {
00130         double scaleF1 = origsize[points[i].imgNr1].x / (float) images[points[i].imgNr1]->size().x;
00131         double scaleF2 = origsize[points[i].imgNr2].x / (float) images[points[i].imgNr2]->size().x;
00132         points[i].p1.x = points[i].p1.x * scaleF1;
00133         points[i].p1.y = points[i].p1.y * scaleF1;
00134         points[i].p2.x = points[i].p2.x * scaleF2;
00135         points[i].p2.y = points[i].p2.y * scaleF2;
00136     }
00137 
00138     for (size_t i=0; i < images.size(); i++) {
00139         delete images[i];
00140         delete lapImgs[i];
00141     }
00142     
00143     delete &pano; // deleting the NewCopy
00144 }
00145 
00146 
00147 
00149 void PointSampler::extractPoints(PanoramaData& pano, std::vector<vigra::FRGBImage*> images, int nPoints,
00150                                  bool randomPoints, AppBase::ProgressDisplay* progress,
00151                                  std::vector<vigra_ext::PointPairRGB>& points)
00152 {
00153     PointSampler* sampler;
00154     if (randomPoints)
00155     {
00156         sampler = new RandomPointSampler(pano, NULL, images, nPoints);
00157     }
00158     else
00159     {
00160         sampler = new AllPointSampler(pano, NULL, images, nPoints);
00161     };
00162     
00163     sampler->sampleAndExtractPoints(progress);
00164     points = sampler->getResultPoints();
00165     
00166     delete sampler;
00167 }
00168 
00169 
00170 }; // namespace
00171 

Generated on 1 May 2016 for Hugintrunk by  doxygen 1.4.7