CenterHorizontally.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00026 #include "CenterHorizontally.h"
00027 
00028 #include <vigra/copyimage.hxx>
00029 #include <panodata/PanoramaData.h>
00030 #include <nona/RemappedPanoImage.h>
00031 #include <algorithms/basic/RotatePanorama.h>
00032 
00033 
00034 namespace HuginBase {
00035 
00036 
00037 void CenterHorizontally::centerHorizontically(PanoramaData& panorama)
00038 {
00039     vigra::Size2D panoSize(360,180);
00040     
00041     // remap into minature pano.
00042     PanoramaOptions opts;
00043     opts.setHFOV(360);
00044     opts.setProjection(PanoramaOptions::EQUIRECTANGULAR);
00045     opts.setWidth(360);
00046     opts.setHeight(180);
00047     
00048     // remap image
00049     vigra::BImage panoAlpha(panoSize);
00050     Nona::RemappedPanoImage<vigra::BImage, vigra::BImage> remapped;
00051     
00052     // use selected images.
00053     const UIntSet allActiveImgs(panorama.getActiveImages());
00054 
00055     if (allActiveImgs.empty())
00056     {
00057         // do nothing if there are no images
00058         return;
00059     }
00060     
00061     //only check unlinked images
00062     UIntSet activeImgs;
00063     for (UIntSet::const_iterator it = allActiveImgs.begin(); it!= allActiveImgs.end(); ++it)
00064     {
00065         const SrcPanoImage & img=panorama.getImage(*it);
00066         bool consider=true;
00067         if(img.YawisLinked())
00068         {
00069             for(UIntSet::const_iterator it2=activeImgs.begin(); it2!=activeImgs.end(); ++it2)
00070             {
00071                 if(img.YawisLinkedWith(panorama.getSrcImage(*it2)))
00072                 {
00073                     consider=false;
00074                     break;
00075                 };
00076             };
00077         };
00078         if(consider)
00079             activeImgs.insert(*it);
00080     };
00081 
00082     for (UIntSet::iterator it = activeImgs.begin(); it != activeImgs.end(); ++it)
00083     {
00084         remapped.setPanoImage(panorama.getSrcImage(*it), opts, vigra::Rect2D(0,0,360,180));
00085         // calculate alpha channel
00086         remapped.calcAlpha();
00087         // copy into global alpha channel.
00088         vigra::copyImageIf(vigra_ext::applyRect(remapped.boundingBox(),
00089                                                 vigra_ext::srcMaskRange(remapped)),
00090                            vigra_ext::applyRect(remapped.boundingBox(),
00091                                                 vigra_ext::srcMask(remapped)),
00092                            vigra_ext::applyRect(remapped.boundingBox(),
00093                                                 destImage(panoAlpha)));
00094         }
00095     
00096     // get field of view
00097     std::vector<int> borders;
00098     bool colOccupied = false;
00099     for (int h=0; h < 360; h++) {
00100         bool curColOccupied = false;
00101         for (int v=0; v< 180; v++) {
00102             if (panoAlpha(h,v)) {
00103                 // pixel is valid
00104                 curColOccupied = true;
00105             }
00106         }
00107         if ((colOccupied && !curColOccupied) ||
00108             (!colOccupied && curColOccupied))
00109         {
00110             // change in position, save point.
00111             borders.push_back(h-180);
00112             colOccupied = curColOccupied;
00113         }
00114     }
00115     
00116     
00117     int lastidx = borders.size() -1;
00118     if (lastidx == -1) {
00119         // empty pano
00120         return;
00121     }
00122     
00123     if (colOccupied) {
00124         // we have reached the right border, and the pano is still valid
00125         // shift right fragments by 360 deg
00126         // |11    2222|  -> |      222211     |
00127         std::vector<int> newBorders;
00128         newBorders.push_back(borders[lastidx]);
00129         for (int i = 0; i < lastidx; i++) {
00130             newBorders.push_back(borders[i]+360);
00131         }
00132         borders = newBorders;
00133     }
00134     
00135     const double dYaw=(borders[0] + borders[lastidx])/2;
00136     
00137     // apply yaw shift, takes also translation parameters into account
00138     RotatePanorama(panorama, -dYaw, 0, 0).run();
00139 }
00140 
00141 } 

Generated on 24 Jul 2016 for Hugintrunk by  doxygen 1.4.7