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"))
50 #include <opencv2/opencv.hpp>
58 #include "arma_ext.hpp"
67 class Size :
public arma::Col<T>::template fixed<2>
77 T
width()
const {
return this->at(0); }
78 T
height()
const {
return this->at(1); }
80 T&
width() {
return this->at(0); }
90 typedef typename get_pod_type<T>::result
pod_type;
98 Image(
const Mat<T>& m): Mat<T>(m) {}
104 template <
typename DT>
105 Image(
const Mat<DT>& m): Mat<T>(m.n_rows, m.n_cols)
107 T* ptr = this->memptr();
111 #elif defined(USE_OPENMP)
112 #pragma omp parallel
for
113 for (
int si = 0 ; si < (int)m.n_elem ; si++) {
116 for (
size_type i = 0 ; i < m.n_elem ; i++)
118 ptr[i] = arma_ext::saturate_cast<T>(m(i));
121 #elif defined(USE_OPENMP)
138 this->set_size(height, width);
144 return static_cast<Mat<T>
>(*this);
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)
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());
171 typename arma::Col<elem_type>::template fixed<2> center_;
172 center_[0] = center[0];
173 center_[1] = center[1];
175 typename arma::Col<elem_type>::template fixed<2> center_ = center;
178 center_[0] -= (elem_type)(patchsize.width() - 1) * (elem_type)0.5;
179 center_[1] -= (elem_type)(patchsize.height() - 1) * (elem_type)0.5;
181 int ipx = (int)std::floor(center_[0]);
182 int ipy = (int)std::floor(center_[1]);
184 elem_type ox = center_[0] - (elem_type)ipx;
185 elem_type oy = center_[1] - (elem_type)ipy;
187 elem_type a11 = (1 - ox) * (1 - oy),
192 if (0 <= ipx && ipx + patchsize.width() < img.n_cols &&
193 0 <= ipy && ipy + patchsize.height() < img.n_rows) {
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;
202 for (size_type j = 0 ; j < out.n_cols ; j++) {
204 pixel_type* ptr = out.colptr(j);
205 const pixel_type* src = img.colptr(ipx + j) + ipy;
207 for (i = 0 ; i < out.n_rows ; i++) {
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);
223 int sox = 0, soy = 0;
231 if (r[0] > (
int)patchsize.width())
232 r[0] = patchsize.width();
235 if (ipx + (
int)patchsize.width() < (int)img.n_cols)
236 r[2] = patchsize.width();
238 r[2] = (int)img.n_cols - ipx - 1;
250 if (r[1] > (
int)patchsize.height())
251 r[1] = patchsize.height();
254 if (ipy + (
int)patchsize.height() < (int)img.n_rows)
255 r[3] = patchsize.height();
257 r[3] = (int)img.n_rows - ipy - 1;
267 elem_type b1 = (elem_type)1.0 - ox,
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;
275 if ((
int)j < r[0] || (
int)j >= r[2])
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);
282 for ( ; i < (size_type)r(3) ; i++) {
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);
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);
307 template <typename pixel_type>
311 return Image<pixel_type>(arma_ext::conv2(conv_to<mat>::from(img), h, arma_ext::same).eval());
315 template <
typename pixel_type>
317 cv::Mat tocvMat(
const Image<pixel_type>& img)
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);
333 template <
typename pixel_type>
334 Image<pixel_type> bgr2gray(
const cv::Mat& img)
336 arma::Mat<pixel_type> gray(img.cols, img.rows);
337 pixel_type* ptr = gray.memptr();
339 concurrency::parallel_for(0, img.rows, [&](
int 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);
349 cv::cvtColor(img, bw, CV_BGR2GRAY);
350 memcpy(ptr, bw.data,
sizeof(pixel_type) * bw.total());
353 return Image<pixel_type>(gray.t());
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