MTF
mexUtils.h
1 #ifndef MTF_MEX_UTILS_H
2 #define MTF_MEX_UTILS_H
3 
4 #include "mtf/Macros/common.h"
5 
6 #include <mex.h>
7 
8 #define _A3D_IDX_COLUMN_MAJOR(i,j,k,nrows,ncols) ((i)+((j)+(k)*ncols)*nrows)
9 // interleaved row-major indexing for 2-D OpenCV images
10 //#define _A3D_IDX_OPENCV(x,y,c,mat) (((y)*mat.step[0]) + ((x)*mat.step[1]) + (c))
11 #define _A3D_IDX_OPENCV(i,j,k,nrows,ncols,nchannels) (((i*ncols + j)*nchannels) + (k))
12 
13 _MTF_BEGIN_NAMESPACE
14 namespace utils{
20  template <typename T>
21  inline void
22  copyMatrixFromMatlab(const T* from, cv::Mat& to, int n_channels){
23 
24  const int n_rows = to.rows;
25  const int n_cols = to.cols;
26 
27  T* pdata = (T*)to.data;
28 
29  for(int c = 0; c < n_channels; ++c){
30  for(int x = 0; x < n_cols; ++x){
31  for(int y = 0; y < n_rows; ++y){
32  const T element = from[_A3D_IDX_COLUMN_MAJOR(y, x, c, n_rows, n_cols)];
33  pdata[_A3D_IDX_OPENCV(y, x, n_channels - c, rows, n_cols, n_channels)] = element;
34  }
35  }
36  }
37  }
38  template <typename T>
39  inline void
40  copyMatrixToMatlab(const cv::Mat& from, T* to, int n_channels)
41  {
42  const int n_rows = from.rows;
43  const int n_cols = from.cols;
44 
45  const T* pdata = (T*)from.data;
46 
47  for(int c = 0; c < n_channels; ++c){
48  for(int x = 0; x < n_cols; ++x){
49  for(int y = 0; y < n_rows; ++y){
50  //const T element = pdata[_A3D_IDX_OPENCV(x,y,c,from)];
51  const T element = pdata[_A3D_IDX_OPENCV(y, x, c, rows, n_cols, n_channels)];
52  to[_A3D_IDX_COLUMN_MAJOR(y, x, n_channels - c - 1, n_rows, n_cols)] = element;
53  }
54  }
55  }
56  }
57  inline unsigned int getID(const mxArray *mx_id){
58  if(!mxIsClass(mx_id, "uint32")){
59  mexErrMsgTxt("ID must be of 32 bit unsigned integral type");
60  }
61  unsigned int* id_ptr = (unsigned int*)mxGetData(mx_id);
62  return *id_ptr;
63  }
64  inline cv::Mat getImage(const mxArray *mx_img){
65  int img_n_dims = mxGetNumberOfDimensions(mx_img);
66  if(!mxIsClass(mx_img, "uint8")){
67  mexErrMsgTxt("Input image must be of 8 bit unsigned integral type");
68  }
69  if(img_n_dims < 2 || img_n_dims > 3){
70  mexErrMsgTxt("Input image must have 2 or 3 dimensions");
71  }
72  int img_type = img_n_dims == 2 ? CV_8UC1 : CV_8UC3;
73  const mwSize *img_dims = mxGetDimensions(mx_img);
74  int height = img_dims[0];
75  int width = img_dims[1];
76  //printf("width: %d\t height=%d\t img_n_dims: %d\n", width, height, img_n_dims);
77  unsigned char *img_ptr = (unsigned char*)mxGetData(mx_img);
78  cv::Mat img(height, width, img_type);
79  if(img_n_dims == 2){
80  cv::Mat img_transpose(width, height, img_type, img_ptr);
81  cv::transpose(img_transpose, img);
82  } else{
83  copyMatrixFromMatlab(img_ptr, img, 3);
84  }
85  return img;
86  }
87  inline cv::Mat getCorners(const mxArray *mx_corners){
88  int corners_n_dims = mxGetNumberOfDimensions(mx_corners);
89  if(!mxIsClass(mx_corners, "double")){
90  mexErrMsgTxt("Input corner array must be of 64 bit floating point type");
91  }
92  if(corners_n_dims != 2){
93  mexErrMsgTxt("Input corner array must be 2 dimensional");
94  }
95  const mwSize *corners_dims = mxGetDimensions(mx_corners);
96  if((corners_dims[0] != 1 && corners_dims[0] != 2) || corners_dims[1] != 4){
97  mexErrMsgTxt("Input corner array must be of size 1 x 4 or 2 x 4");
98  }
99  double *corners_ptr = mxGetPr(mx_corners);
100  cv::Mat corners(2, 4, CV_64FC1);
101  if(corners_dims[0] == 1){
102  double ulx = corners_ptr[0];
103  double uly = corners_ptr[1];
104  double width = corners_ptr[2];
105  double height = corners_ptr[3];
106  if(width <= 0 || height <= 0) {
107  mexErrMsgTxt("Both height and width of the rectangle must be greater than 0");
108  }
109  double brx = ulx + width - 1;
110  double bry = uly + height - 1;
111  corners.at<double>(0, 0) = ulx; corners.at<double>(1, 0) = uly;
112  corners.at<double>(0, 1) = brx; corners.at<double>(1, 1) = uly;
113  corners.at<double>(0, 2) = brx; corners.at<double>(1, 2) = bry;
114  corners.at<double>(0, 3) = ulx; corners.at<double>(1, 3) = bry;
115  } else {
116  cv::transpose(cv::Mat(4, 2, CV_64FC1, corners_ptr), corners);
117  }
118  return corners;
119  }
120  inline const char* toString(const mxArray *prhs){
121  int param_str_len = mxGetM(prhs)*mxGetN(prhs) + 1;
122  char* param_str = (char *)mxMalloc(param_str_len);
123  mxGetString(prhs, param_str, param_str_len);
124  return param_str;
125  }
126 
127  inline mxArray *setCorners(const cv::Mat &corners){
128  mxArray *mx_corners = mxCreateDoubleMatrix(2, 4, mxREAL);
129  /* get a pointer to the real data in the output matrix */
130  double *out_corners_ptr = mxGetPr(mx_corners);
131  cv::Mat out_corners(4, 2, CV_64FC1, out_corners_ptr);
132  cv::transpose(corners, out_corners);
133  return mx_corners;
134  }
135 }
136 _MTF_END_NAMESPACE
137 #endif
basic functions for preprocessing the raw input image using filtering, resizing and histogram equaliz...
Definition: histUtils.h:20
void copyMatrixFromMatlab(const T *from, cv::Mat &to, int n_channels)
Copy the (image) data from Matlab-algorithm compatible (column-major) representation to cv::Mat...
Definition: mexUtils.h:22