Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CenterHorizontally.cpp
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
26 #include "CenterHorizontally.h"
27 
28 #include <vigra/copyimage.hxx>
29 #include <panodata/PanoramaData.h>
30 #include <nona/RemappedPanoImage.h>
32 
33 
34 namespace HuginBase {
35 
36 
38 {
39  vigra::Size2D panoSize(360,180);
40 
41  // remap into minature pano.
42  PanoramaOptions opts;
43  opts.setHFOV(360);
45  opts.setWidth(360);
46  opts.setHeight(180);
47 
48  // remap image
49  vigra::BImage panoAlpha(panoSize);
51 
52  // use selected images.
53  const UIntSet allActiveImgs(panorama.getActiveImages());
54 
55  if (allActiveImgs.empty())
56  {
57  // do nothing if there are no images
58  return;
59  }
60 
61  //only check unlinked images
62  UIntSet activeImgs;
63  for (UIntSet::const_iterator it = allActiveImgs.begin(); it!= allActiveImgs.end(); ++it)
64  {
65  const SrcPanoImage & img=panorama.getImage(*it);
66  bool consider=true;
67  if(img.YawisLinked())
68  {
69  for(UIntSet::const_iterator it2=activeImgs.begin(); it2!=activeImgs.end(); ++it2)
70  {
71  if(img.YawisLinkedWith(panorama.getSrcImage(*it2)))
72  {
73  consider=false;
74  break;
75  };
76  };
77  };
78  if(consider)
79  activeImgs.insert(*it);
80  };
81 
82  for (UIntSet::iterator it = activeImgs.begin(); it != activeImgs.end(); ++it)
83  {
84  remapped.setPanoImage(panorama.getSrcImage(*it), opts, vigra::Rect2D(0,0,360,180));
85  // calculate alpha channel
86  remapped.calcAlpha();
87  // copy into global alpha channel.
89  vigra_ext::srcMaskRange(remapped)),
91  vigra_ext::srcMask(remapped)),
93  destImage(panoAlpha)));
94  }
95 
96  // get field of view
97  std::vector<int> borders;
98  bool colOccupied = false;
99  for (int h=0; h < 360; h++) {
100  bool curColOccupied = false;
101  for (int v=0; v< 180 && !curColOccupied; v++) {
102  if (panoAlpha(h,v)) {
103  // pixel is valid
104  curColOccupied = true;
105  }
106  }
107  if ((colOccupied && !curColOccupied) ||
108  (!colOccupied && curColOccupied))
109  {
110  // change in position, save point.
111  borders.push_back(h-180);
112  colOccupied = curColOccupied;
113  }
114  }
115 
116 
117  const int lastidx = borders.size() -1;
118  if (lastidx == -1) {
119  // empty pano
120  return;
121  }
122 
123  if (colOccupied) {
124  // we have reached the right border, and the pano is still valid
125  if (borders.size() == 1 && borders[0] == -180)
126  {
127  // pano covers full range, nothing to do
128  return;
129  };
130  // shift right fragments by 360 deg
131  // |11 2222| -> | 222211 |
132  std::vector<int> newBorders;
133  newBorders.push_back(borders[lastidx]);
134  for (int i = 0; i < lastidx; i++) {
135  newBorders.push_back(borders[i]+360);
136  }
137  borders = newBorders;
138  }
139 
140  const double dYaw=(borders[0] + borders[lastidx])/2;
141 
142  // apply yaw shift, takes also translation parameters into account
143  RotatePanorama(panorama, -dYaw, 0, 0).run();
144 }
145 
146 }
vigra::triple< ImgIter, ImgIter, ImgAcc > applyRect(vigra::Rect2D &r, vigra::triple< ImgIter, ImgIter, ImgAcc > img)
apply a roi to an image area
Definition: ROIImage.h:46
void setHeight(unsigned int h)
set panorama height
static void centerHorizontically(PanoramaData &panorama)
void setPanoImage(const SrcPanoImage &src, const PanoramaOptions &dest, vigra::Rect2D roi)
void copyImageIf(SrcImageIterator src_upperleft, SrcImageIterator src_lowerright, SrcAccessor src_acc, MaskImageIterator mask_upperleft, MaskAccessor mask_acc, DestImageIterator dest_upperleft, DestAccessor dest_acc)
Definition: openmp_vigra.h:316
Contains functions to transform whole images.
virtual void run()
runs the algorithm.
std::set< unsigned int > UIntSet
Definition: PanoramaData.h:51
virtual UIntSet getActiveImages() const =0
get active images
IMPEX double h[25][1024]
Definition: emor.cpp:169
Model for a panorama.
Definition: PanoramaData.h:81
virtual const SrcPanoImage & getImage(std::size_t nr) const =0
get a panorama image, counting starts with 0
vigra::pair< typename ROIImage< Image, Alpha >::image_traverser, typename ROIImage< Image, Alpha >::ImageAccessor > destImage(ROIImage< Image, Alpha > &img)
Definition: ROIImage.h:324
void setHFOV(double h, bool keepView=true)
set the horizontal field of view.
vigra::triple< typename ROIImage< Image, Alpha >::mask_const_traverser, typename ROIImage< Image, Alpha >::mask_const_traverser, typename ROIImage< Image, Alpha >::MaskConstAccessor > srcMaskRange(const ROIImage< Image, Alpha > &img)
Definition: ROIImage.h:335
vigra::pair< typename ROIImage< Image, Alpha >::mask_const_traverser, typename ROIImage< Image, Alpha >::MaskConstAccessor > srcMask(const ROIImage< Image, Alpha > &img)
Definition: ROIImage.h:347
void calcAlpha()
calculate only the alpha channel.
virtual SrcPanoImage getSrcImage(unsigned imgNr) const =0
get a complete description of a source image
All variables of a source image.
Definition: SrcPanoImage.h:194
void setProjection(ProjectionFormat f)
set the Projection format and adjust the hfov/vfov if nessecary
Panorama image options.
struct to hold a image state for stitching
void setWidth(unsigned int w, bool keepView=true)
set panorama width keep the HFOV, if keepView=true