Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CalibratedPyramid.cpp
1 #include "CalibratedPyramid.hh"
2 
3 #include <Base/Math/Vector2.hh>
4 #include <Geometry/ProjectionParametersBase.hh>
5 #include <Base/Image/ImageIO.hh>
6 
7 using namespace BIAS;
8 using namespace std;
9 
10 template <class StorageType>
13  : PyramidImage_(), Data_(), PositionOffset_(0.)
14 {
15  const int size = PyramidImage_.Size();
16  Data_.resize(size);
17  for (int i=0; i<size; i++){
18  Data_[i].projection = SharedPtr<ProjectionParametersBase>(NULL);
19  Data_[i].image = PyramidImage_[i];
20  }
21 }
22 
23 
24 template <class StorageType>
27  : PyramidImage_(), Data_(PyramidImage_.Size())
28 {
29  operator=(cp);
30 }
31 
32 
33 template <class StorageType>
36  : PyramidImage_(cp), Data_(cp.size()), PositionOffset_(cp.GetPositionOffset())
37 {
38  const unsigned size = Size();
39  for (unsigned i=0; i<size; i++){
40  Data_[i].projection = SharedPtr<ProjectionParametersBase>(NULL);
41  Data_[i].image = PyramidImage_[i];
42  }
43 }
44 
45 
46 template <class StorageType>
49 {
50  Clear();
51 }
52 
53 template <class StorageType>
56  const unsigned pyramid_size, const double rescale_factor)
57 {
58  BIASASSERT(pyramid_size>0);
59  Clear();
60  PyramidImage_.SetRescaleFactor(rescale_factor);
61  const unsigned ui_factor = (unsigned)rint(rescale_factor);
62 
63  if ( (ui_factor==2 || ui_factor==4) &&
64  Equal((double)ui_factor, rescale_factor) && false){
65  // for some mysterious reason, using the own downsampling routine gives
66  // slightly worse results on VisionN/Stereo/Testst/TestTrifocalDisparity
67  // with the Aloe test date, it is tus disabled
68  PositionOffset_ = ((double)(ui_factor)*0.5)-0.5;
69  PyramidImage_.clear();
70  PyramidImage_.Init(*(ci.image), pyramid_size);
71  PyramidImage_.resize(pyramid_size);
72  cout << "CalibratedPyramid: using own downsampling\n";
73  *PyramidImage_[0] = *ci.image;
74  for (unsigned p=1; p<pyramid_size; p++){
75  Downsample_(PyramidImage_[p-1], PyramidImage_[p], ui_factor);
76  }
77  } else {
78  cout << "CalibratedPyramid: using downsampling from Resize class\n";
79  PyramidImage_.Init(*(ci.image), pyramid_size);
80  PyramidImage_.Downsample();
81  PositionOffset_ = PyramidImage_.GetPositionOffset();
82  }
83  cout << " using position offset "<<PositionOffset_<<" for coo transfer\n";
84  cout << " using rescale factor: "<<rescale_factor<<" "
85  <<PyramidImage_.GetRescaleFactor()<<endl;
86 
87  Data_.resize(pyramid_size);
88  if (ci.projection)
89  Data_[0].projection =
91  else
92  Data_[0].projection = SharedPtr<ProjectionParametersBase>(NULL);
93  Data_[0].image = PyramidImage_[0];
94  for (unsigned i=1; i<pyramid_size; i++){
95  if (Data_[i-1].projection){
96  Data_[i].projection =
97  SharedPtr<ProjectionParametersBase>(Data_[i-1].projection->Clone());
98  Data_[i].projection->Rescale(rescale_factor, PositionOffset_);
99  //Data_[i].projection->Rescale(rescale_factor);
100  } else {
101  Data_[i].projection = SharedPtr<ProjectionParametersBase>(NULL);
102  }
103  Data_[i].image = PyramidImage_[i];
104  }
105 }
106 
107 
108 template <class StorageType>
111 {
112  Data_.clear();
113 }
114 
115 template <class StorageType>
118 {
119  Clear();
120  const int size = cp.Size();
121  PyramidImage_ = cp.PyramidImage_;
122  Data_.resize(size);
123  for (int i=0; i<size; i++){
124  if (cp[i].projection)
125  Data_[i].projection = SharedPtr<ProjectionParametersBase>(cp[i].projection->Clone());
126  if (cp[i].image)
127  Data_[i].image = PyramidImage_[i];
128  }
129  PositionOffset_ = cp.PositionOffset_;
130 
131  return *this;
132 }
133 
134 
135 template <class StorageType>
137 CoordinateTransf(const Vector2<double>& coo0, const unsigned index,
138  Vector2<double>& coo_index) const
139 {
140  if (index==0) { coo_index=coo0; return ; }
141  coo_index[0] = (coo0[0]-PositionOffset_) /
142  (PyramidImage_.GetFactors()[index]);
143  coo_index[1] = (coo0[1]-PositionOffset_) /
144  (PyramidImage_.GetFactors()[index]);
145  // coo_index[0] = coo0[0] / PyramidImage_.GetFactors()[index];
146  //coo_index[1] = coo0[1] / PyramidImage_.GetFactors()[index];
147 }
148 
149 
150 template <class StorageType>
153  SharedPtr<Image<StorageType> > dst, const unsigned factor) const
154 {
155  BIASASSERT((factor == 2) | (factor == 4));
158  bin.SetHalfWinSize(factor/2); // 1 or 2, i.e. 3x3 or 5x5
159  Image<StorageType> filtered;
160  bin.Filter(*src, filtered);
161  BIASASSERT(filtered.GetWidth() == src->GetWidth() &&
162  filtered.GetHeight() == src->GetHeight() &&
163  filtered.GetChannelCount() == src->GetChannelCount());
164  const int ow = src->GetWidth(), oh = src->GetHeight();
165  const int cc = src->GetChannelCount();
166  const int nw = ow / factor, nh = oh / factor;
167  if ((int)dst->GetWidth() != nw || (int)dst->GetHeight() != nh ||
168  (int)dst->GetChannelCount() != cc ) {
169  if (!dst->IsEmpty()){
170  dst->Release();
171  }
172  dst->Init(nw, nh, cc);
173  }
174  if (!dst->GetColorModel() != src->GetColorModel()){
175  dst->SetColorModel(src->GetColorModel());
176  }
177 
178  StorageType **dst_ida = dst->GetImageDataArray();
179  StorageType **fil_ida = filtered.GetImageDataArray();
180  int x, y, c, xc, yb, xb, xbc;
181 #ifdef _OPENMP
182 # pragma omp parallel for private (x, y, c, xc, yb, xb, xbc)
183 #endif
184  for (y=0; y<nh; y++){
185  yb = y*factor + (int)PositionOffset_;
186  for (x=0; x<nw; x++){
187  xb = x * factor + (int)PositionOffset_;
188  xbc = xb * cc;
189  xc = x * cc;
190  for (c=0; c<cc; c++){
191  dst_ida[y][xc+c] = fil_ida[yb][xbc+c];
192  }
193  }
194  }
195 }
196 
197 
198 //////////////////////////////////////////////////////////////////////////
199 // instantiation
200 //////////////////////////////////////////////////////////////////////////
201 
202 #define INST(type) template class BIASImage_EXPORT CalibratedPyramid<type>
203 namespace BIAS{
204 INST(unsigned char);
205 INST(float);
206 
207 #ifdef BUILD_IMAGE_INT
208 INST(int);
209 #endif
210 #ifdef BUILD_IMAGE_CHAR
211 INST(char);
212 #endif
213 #ifdef BUILD_IMAGE_SHORT
214 INST(short);
215 #endif
216 #ifdef BUILD_IMAGE_USHORT
217 INST(unsigned short);
218 #endif
219 #ifdef BUILD_IMAGE_UINT
220 INST(unsigned int);
221 #endif
222 #ifdef BUILD_IMAGE_DOUBLE
223 INST(double);
224 #endif
225 }
This class takes care of consisiten re-sampling of images and associated ProjectionParameters.
unsigned Size() const
returns the number of images stored in the pyramid
CalibratedPyramid< StorageType > & operator=(const CalibratedPyramid< StorageType > &cp)
shallow copy operator, only copies the pointers
pointer with reference count and automatic deletion
Definition: SharedPtr.hh:50
virtual void resize(const unsigned size)
virtual parent class for API definition of all (future) filters
Definition: FilterBase.hh:77
void CoordinateTransf(const Vector2< double > &coo0, const unsigned index, Vector2< double > &coo_index) const
unsigned int GetWidth() const
Definition: ImageBase.hh:312
PyramidImage< StorageType > PyramidImage_
Internal data: Data_.size() must always be equal to PyramidImage_.GetPyramidSize() ...
std::vector< CalibratedImage< StorageType > > Data_
holds the projection parameters and ptrs to the associated images
void Clear()
clears the pyramid image and the internal vector, does not explicitly delet the pointers ...
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void SetBorderHandling(const int bh)
Definition: FilterBase.hh:127
INST(unsigned char)
SharedPtr< Image< StorageType > > image
The image template class for specific storage types.
Definition: Image.hh:78
helper class holding image and associated calibration
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
sets kernel if params changed and calls convolution
Definition: Binomial.cpp:69
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
SharedPtr< ProjectionParametersBase > projection
unsigned Size() const
void Downsample_(SharedPtr< const Image< StorageType > > src, SharedPtr< Image< StorageType > > dst, const unsigned factor) const
Downsampling for factor 2 or 4, using a binomial lowpass filter.
void Init(const CalibratedImage< StorageType > &image, const unsigned pyramid_size=2, const double rescale_factor=4.0)
initialization with original image, total number of images in the pyramid and a rescale factor...
binomial low pass filter class
Definition: Binomial.hh:42
void SetHalfWinSize(int hws)
Definition: Binomial.hh:103
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:153