auxiliary
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Macros
imgproc_aux.hpp
Go to the documentation of this file.
1 
39 #pragma once
40 
41 #ifdef USE_OPENCV
42 
43 #ifdef _MSC_VER
44 #include <mycv.hpp> // for OpenCV
45 #pragma comment(lib, OPENCV_LIB_EXPAND("core"))
46 #pragma comment(lib, OPENCV_LIB_EXPAND("imgproc"))
47 #pragma comment(lib, OPENCV_LIB_EXPAND("highgui"))
48 #pragma comment(lib, OPENCV_LIB_EXPAND("video"))
49 #else
50 #include <opencv2/opencv.hpp>
51 #endif
52 
53 #endif
54 
55 #include <armadillo>
56 using namespace arma;
57 
58 #include "arma_ext.hpp"
59 
60 namespace auxiliary
61 {
66  template <typename T>
67  class Size : public arma::Col<T>::template fixed<2>
68  {
69  public:
70 
71  Size(T w = 0, T h = 0)
72  {
73  this->at(0) = w;
74  this->at(1) = h;
75  }
76 
77  T width() const { return this->at(0); }
78  T height() const { return this->at(1); }
79 
80  T& width() { return this->at(0); }
81  T& height() { return this->at(1); }
82  };
83 
85  template <typename T>
86  class Image : public Mat<T>
87  {
88  public:
89  typedef T elem_type;
90  typedef typename get_pod_type<T>::result pod_type;
91 
92  typedef arma::uword size_type;
93 
95  Image(): Mat<T>() {}
96 
98  Image(const Mat<T>& m): Mat<T>(m) {}
99 
101  Image(const size_type width, const size_type height) : Mat<T>(height, width) {}
102 
104  template <typename DT>
105  Image(const Mat<DT>& m): Mat<T>(m.n_rows, m.n_cols)
106  {
107  T* ptr = this->memptr();
108  // type conversion
109 #if defined(USE_PPL)
110  concurrency::parallel_for(size_type(0), m.n_elem, [&](size_type i) {
111 #elif defined(USE_OPENMP)
112  #pragma omp parallel for
113  for (int si = 0 ; si < (int)m.n_elem ; si++) {
114  size_type i = (size_type)si;
115 #else
116  for (size_type i = 0 ; i < m.n_elem ; i++)
117 #endif
118  ptr[i] = arma_ext::saturate_cast<T>(m(i));
119 #if defined(USE_PPL)
120  });
121 #elif defined(USE_OPENMP)
122  }
123 #endif
124  }
125 
127  void release() {}
128 
130  inline size_type width() const { return this->n_cols; }
131 
133  inline size_type height() const { return this->n_rows; }
134 
136  inline void resize(size_type width, size_type height)
137  {
138  this->set_size(height, width);
139  }
140 
141  // Operator overloading
142  operator Mat<T>&()
143  {
144  return static_cast<Mat<T> >(*this);
145  }
146  };
147 
163  template <typename pixel_type, typename vec_type>
164  static arma::Mat<pixel_type> getRectSubPix(const Image<pixel_type>& img, Size<arma_ext::uword> patchsize, const vec_type center)
165  {
166  typedef typename vec_type::elem_type elem_type;
167  typedef typename arma_ext::size_type size_type;
168  arma::Mat<pixel_type> out(patchsize.height(), patchsize.width());
169 
170 #ifdef __VXWORKS__
171  typename arma::Col<elem_type>::template fixed<2> center_;
172  center_[0] = center[0];
173  center_[1] = center[1];
174 #else
175  typename arma::Col<elem_type>::template fixed<2> center_ = center;
176 #endif
177 
178  center_[0] -= (elem_type)(patchsize.width() - 1) * (elem_type)0.5;
179  center_[1] -= (elem_type)(patchsize.height() - 1) * (elem_type)0.5;
180 
181  int ipx = (int)std::floor(center_[0]);
182  int ipy = (int)std::floor(center_[1]);
183 
184  elem_type ox = center_[0] - (elem_type)ipx;
185  elem_type oy = center_[1] - (elem_type)ipy;
186 
187  elem_type a11 = (1 - ox) * (1 - oy),
188  a12 = ox * (1 - oy),
189  a21 = (1 - ox) * oy,
190  a22 = ox * oy;
191 
192  if (0 <= ipx && ipx + patchsize.width() < img.n_cols &&
193  0 <= ipy && ipy + patchsize.height() < img.n_rows) {
194  // extracted rectangle is totally inside the image
195 #ifdef USE_PPL
196  concurrency::parallel_for(arma_ext::size_type(0), out.n_cols, [&](arma_ext::size_type j) {
197 #elif defined(USE_OPENMP)
198  #pragma omp parallel for
199  for (int sj = 0 ; sj < (int)out.n_cols ; sj++) {
200  size_type j = (size_type)sj;
201 #else
202  for (size_type j = 0 ; j < out.n_cols ; j++) {
203 #endif
204  pixel_type* ptr = out.colptr(j);
205  const pixel_type* src = img.colptr(ipx + j) + ipy;
206  size_type i;
207  for (i = 0 ; i < out.n_rows ; i++) {
208  // bilinear interpolation
209  ptr[i] = arma_ext::saturate_cast<pixel_type>((elem_type)src[i ] * a11 +
210  (elem_type)src[i + 1 ] * a21 +
211  (elem_type)src[i + img.n_rows ] * a12 +
212  (elem_type)src[i + img.n_rows + 1] * a22);
213  }
214 #ifdef USE_PPL
215  });
216 #else
217  }
218 #endif
219  } else {
220  arma::ivec4 r;
221 
222  // adjust rectangle
223  int sox = 0, soy = 0;
224 
225  // -------- begin --------
226  if (ipx >= 0) {
227  sox += ipx; // + ipx
228  r[0] = 0;
229  } else {
230  r[0] = -ipx;
231  if (r[0] > (int)patchsize.width())
232  r[0] = patchsize.width();
233  }
234 
235  if (ipx + (int)patchsize.width() < (int)img.n_cols)
236  r[2] = patchsize.width();
237  else {
238  r[2] = (int)img.n_cols - ipx - 1;
239  if (r[2] < 0) {
240  sox += r(2); // + width
241  r[2] = 0;
242  }
243  }
244 
245  if (ipy >= 0) {
246  soy += ipy; // + ipy
247  r[1] = 0;
248  } else {
249  r[1] = -ipy;
250  if (r[1] > (int)patchsize.height())
251  r[1] = patchsize.height();
252  }
253 
254  if (ipy + (int)patchsize.height() < (int)img.n_rows)
255  r[3] = patchsize.height();
256  else {
257  r[3] = (int)img.n_rows - ipy - 1;
258  if (r[3] < 0) {
259  soy += r[3]; // + height
260  r[3] = 0;
261  }
262  }
263  // --------- end ---------
264 
265  //soy -= r(1);
266 
267  elem_type b1 = (elem_type)1.0 - ox,
268  b2 = ox;
269 
270  const pixel_type* src1 = img.colptr(sox) + soy;
271  for (size_type j = 0 ; j < out.n_cols ; j++) {
272  pixel_type* ptr = out.colptr(j);
273  const pixel_type* src2 = src1 + img.n_rows;
274 
275  if ((int)j < r[0] || (int)j >= r[2])
276  src2 -= img.n_rows;
277 
278  size_type i = 0;
279  for (; i < (size_type)r(1) ; i++)
280  ptr[i] = arma_ext::saturate_cast<pixel_type>((elem_type)src1[r[1]] * b1 + (elem_type)src2[r[1]] * b2);
281 
282  for ( ; i < (size_type)r(3) ; i++) {
283  // bilinear interpolation
284  ptr[i] = arma_ext::saturate_cast<pixel_type>((elem_type)src1[i ] * a11 +
285  (elem_type)src1[i + 1] * a21 +
286  (elem_type)src2[i ] * a12 +
287  (elem_type)src2[i + 1] * a22);
288  }
289 
290  for ( ; i < out.n_rows ; i++)
291  ptr[i] = arma_ext::saturate_cast<pixel_type>((elem_type)src1[r[3]] * b1 + (elem_type)src2[r[3]] * b2);
292 
293  if ((int)j < r[2])
294  src1 = src2;
295  }
296  }
297 
298  return out;
299  }
300 
307  template <typename pixel_type>
308  inline Image<pixel_type> blur(const Image<pixel_type>& img, const mat& h)
309  {
310  typedef typename Image<pixel_type>::size_type size_type;
311  return Image<pixel_type>(arma_ext::conv2(conv_to<mat>::from(img), h, arma_ext::same).eval());
312  }
313 
314 #ifdef USE_OPENCV
315  template <typename pixel_type>
317  cv::Mat tocvMat(const Image<pixel_type>& img)
318  {
319  cv::Mat out(img.n_rows, img.n_cols, cv::DataType<pixel_type>::type);
320  Image<pixel_type> t = img.t().eval();
321  memcpy(out.data, t.memptr(), sizeof(pixel_type) * t.n_elem);
322 
323  //cv::imshow(name, out);
324  return out;
325  }
326 
333  template <typename pixel_type>
334  Image<pixel_type> bgr2gray(const cv::Mat& img)
335  {
336  arma::Mat<pixel_type> gray(img.cols, img.rows);
337  pixel_type* ptr = gray.memptr();
338 #if 0
339  concurrency::parallel_for(0, img.rows, [&](int r) {
340  //for (int r = 0 ; r < img.rows ; r++) {
341  const uchar* src = img.ptr(r);
342  uchar* dst = ptr + gray.n_rows * r;
343  for (int c = 0 ; c < img.cols ; c++, src += 3)
344  dst[c] = arma_ext::round<uchar>(src[2] * 0.298936021293776 + src[1] * 0.587043074451121 + src[0] * 0.114020904255103);
345  //}
346  });
347 #else
348  cv::Mat bw;
349  cv::cvtColor(img, bw, CV_BGR2GRAY);
350  memcpy(ptr, bw.data, sizeof(pixel_type) * bw.total());
351 #endif
352 
353  return Image<pixel_type>(gray.t());
354  }
355 #endif
356 }
get_pod_type< T >::result pod_type
if eT is non-complex, pod_type is same as eT. otherwise, pod_type is the underlying type used by std:...
Definition: imgproc_aux.hpp:90
Image(const Mat< T > &m)
Constructor.
Definition: imgproc_aux.hpp:98
Image(const Mat< DT > &m)
Constructor.
Definition: imgproc_aux.hpp:105
T & width()
Definition: imgproc_aux.hpp:80
Size(T w=0, T h=0)
Definition: imgproc_aux.hpp:71
void release()
Dummy function.
Definition: imgproc_aux.hpp:127
T height() const
Definition: imgproc_aux.hpp:78
T width() const
Definition: imgproc_aux.hpp:77
Image(const size_type width, const size_type height)
Constructor.
Definition: imgproc_aux.hpp:101
Image< pixel_type > blur(const Image< pixel_type > &img, const mat &h)
Gaussian blur with given blur kernel.
Definition: imgproc_aux.hpp:308
A template image class.
Definition: imgproc_aux.hpp:86
void resize(size_type width, size_type height)
Resize image.
Definition: imgproc_aux.hpp:136
Image()
Constructor.
Definition: imgproc_aux.hpp:95
size_type height() const
Get image height.
Definition: imgproc_aux.hpp:133
Template class for size type.
Definition: imgproc_aux.hpp:67
size_type width() const
Get image width.
Definition: imgproc_aux.hpp:130
T & height()
Definition: imgproc_aux.hpp:81
T elem_type
the type of elements stored in the matrix
Definition: imgproc_aux.hpp:89
arma::uword size_type
Definition: imgproc_aux.hpp:92