ed
mask.h
Go to the documentation of this file.
1 #ifndef ED_MASK_H_
2 #define ED_MASK_H_
3 
4 #include <rgbd/types.h>
5 #include <opencv2/core/core.hpp>
6 
7 #include <iostream>
8 #include <vector>
9 #include <algorithm>
10 #include <iterator>
11 #include <cassert>
12 
13 namespace ed
14 {
15 
17 class ImageMask {
18 
19 public:
20 
21  ImageMask() {}
22 
29  {
30  }
31 
37  inline void setSize(int width, int height)
38  {
39  width_ = width;
40  height_ = height;
41  }
42 
44  inline void clear()
45  {
46  points_.clear();
47  }
48 
53  inline int getSize() const { return points_.size(); }
54 
59  inline int width() const { return width_; }
60 
65  inline int height() const { return height_; }
66 
71  inline void addPoint(const cv::Point2i& p)
72  {
73  points_.push_back(p);
74  }
75 
81  inline void addPoint(int x, int y)
82  {
83  addPoint(cv::Point2i(x, y));
84  }
85 
90  inline void addPoint(int idx)
91  {
92  addPoint(idx % width_, idx / width_);
93  }
94 
99  inline void addPoints(const std::vector<cv::Point2i>& ps)
100  {
101  for(std::vector<cv::Point2i>::const_iterator it = ps.begin(); it != ps.end(); ++it)
102  {
103  addPoint(*it);
104  }
105  }
106 
113  {
114  public:
121  const_iterator(const std::vector<cv::Point2i> &points, size_t index, int factor)
122  : points_(points), index_(index), dx_(0), dy_(0), factor_(factor)
123  {
124  }
125 
126  // post increment operator
128  {
129  const_iterator i(*this);
130  ++*this;
131  return i;
132  }
133 
141  {
142  ++dx_;
143 
144  // If reached end of the scan line of a sub-image, jump to next scan line.
145  if (dx_ == factor_)
146  {
147  dy_++;
148  dx_ = 0;
149  }
150 
151  // Reached underneath the last scan line, jump back to the start.
152  if (dy_ == factor_)
153  {
154  dx_ = 0;
155  dy_ = 0;
156  ++index_;
157  }
158  return *this;
159  }
160 
162  inline cv::Point2i operator()()
163  {
164  const cv::Point2i &pt = points_[index_];
165  return cv::Point2i(pt.x * factor_ + dx_, pt.y * factor_ + dy_);
166  }
167 
168  // Note: iterator equality only checks base-point index, and not dx/dy
169  // sub-image position.
170  inline bool operator==(const const_iterator& rhs) { return index_ == rhs.index_; }
171  inline bool operator!=(const const_iterator& rhs) { return index_ != rhs.index_; }
172  private:
173 
175  size_t index_;
176  int dx_, dy_;
177  int factor_;
178  };
179 
180  const_iterator begin(int width = 0) const
181  {
182  if (width <= 0)
183  return const_iterator(points_, 0, 1);
184 
185  return const_iterator(points_, 0, width / width_);
186  }
187 
189  {
190  return const_iterator(points_, points_.size(), 0);
191  }
192 
193 private:
194  int width_;
195  int height_;
197 };
198 
199 } // end namespace ed
200 
201 #endif
ed::ImageMask::const_iterator::factor_
int factor_
Sub-image X/Y size (sub-image is rectangular).
Definition: mask.h:177
ed::ImageMask::const_iterator::index_
size_t index_
Current sub-image being scanned.
Definition: mask.h:175
ed::ImageMask::const_iterator
Definition: mask.h:112
ed::ImageMask::width_
int width_
Width of the mask.
Definition: mask.h:194
ed::ImageMask::width
int width() const
Definition: mask.h:59
vector
std::vector::size
T size(T... args)
ed::ImageMask::ImageMask
ImageMask(int width, int height)
Definition: mask.h:28
iterator
ed::ImageMask::points_
std::vector< cv::Point2i > points_
Base points of the sub-images.
Definition: mask.h:196
ed::ImageMask::addPoints
void addPoints(const std::vector< cv::Point2i > &ps)
Definition: mask.h:99
iostream
ed::ImageMask::ImageMask
ImageMask()
Definition: mask.h:21
algorithm
std::vector::clear
T clear(T... args)
ed::ImageMask::const_iterator::dx_
int dx_
Definition: mask.h:176
std::vector::push_back
T push_back(T... args)
ed::ImageMask::getSize
int getSize() const
Definition: mask.h:53
ed::ImageMask::const_iterator::operator++
const_iterator & operator++()
Definition: mask.h:140
ed::ImageMask::const_iterator::operator==
bool operator==(const const_iterator &rhs)
Definition: mask.h:170
ed::ImageMask::addPoint
void addPoint(const cv::Point2i &p)
Definition: mask.h:71
ed::ImageMask::addPoint
void addPoint(int x, int y)
Definition: mask.h:81
ed::ImageMask::clear
void clear()
Definition: mask.h:44
ed::ImageMask::addPoint
void addPoint(int idx)
Definition: mask.h:90
ed::ImageMask::const_iterator::points_
const std::vector< cv::Point2i > & points_
Base points of the sub-images.
Definition: mask.h:174
ed::ImageMask::const_iterator::dy_
int dy_
Variables tracking the x/y position in the current sub-image.
Definition: mask.h:176
ed::ImageMask::const_iterator::const_iterator
const_iterator(const std::vector< cv::Point2i > &points, size_t index, int factor)
Definition: mask.h:121
std::vector::begin
T begin(T... args)
cassert
ed::ImageMask::const_iterator::operator!=
bool operator!=(const const_iterator &rhs)
Definition: mask.h:171
ed::ImageMask::const_iterator::operator()
cv::Point2i operator()()
Definition: mask.h:162
ed::ImageMask
Definition: mask.h:17
ed::ImageMask::const_iterator::operator++
const_iterator operator++(int)
Definition: mask.h:127
ed
Definition: convex_hull.h:8
std::vector::end
T end(T... args)
ed::ImageMask::begin
const_iterator begin(int width=0) const
Definition: mask.h:180
ed::ImageMask::setSize
void setSize(int width, int height)
Definition: mask.h:37
types.h
ed::ImageMask::height_
int height_
Height of the mask.
Definition: mask.h:195
ed::ImageMask::end
const_iterator end() const
Definition: mask.h:188
ed::ImageMask::height
int height() const
Definition: mask.h:65