1 #define LOG_TAG "org.opencv.utils.Converters" 2 #include "common.h" 3 4 using namespace cv; 5 6 #define CHECK_MAT(cond) if(!(cond)){ LOGD("FAILED: " #cond); return; } 7 8 9 // vector_int 10 11 void Mat_to_vector_int(Mat& mat, std::vector<int>& v_int) 12 { 13 v_int.clear(); 14 CHECK_MAT(mat.type()==CV_32SC1 && mat.cols==1); 15 v_int = (std::vector<int>) mat; 16 } 17 18 void vector_int_to_Mat(std::vector<int>& v_int, Mat& mat) 19 { 20 mat = Mat(v_int, true); 21 } 22 23 24 //vector_double 25 26 void Mat_to_vector_double(Mat& mat, std::vector<double>& v_double) 27 { 28 v_double.clear(); 29 CHECK_MAT(mat.type()==CV_64FC1 && mat.cols==1); 30 v_double = (std::vector<double>) mat; 31 } 32 33 void vector_double_to_Mat(std::vector<double>& v_double, Mat& mat) 34 { 35 mat = Mat(v_double, true); 36 } 37 38 39 // vector_float 40 41 void Mat_to_vector_float(Mat& mat, std::vector<float>& v_float) 42 { 43 v_float.clear(); 44 CHECK_MAT(mat.type()==CV_32FC1 && mat.cols==1); 45 v_float = (std::vector<float>) mat; 46 } 47 48 void vector_float_to_Mat(std::vector<float>& v_float, Mat& mat) 49 { 50 mat = Mat(v_float, true); 51 } 52 53 54 //vector_uchar 55 56 void Mat_to_vector_uchar(Mat& mat, std::vector<uchar>& v_uchar) 57 { 58 v_uchar.clear(); 59 CHECK_MAT(mat.type()==CV_8UC1 && mat.cols==1); 60 v_uchar = (std::vector<uchar>) mat; 61 } 62 63 void vector_uchar_to_Mat(std::vector<uchar>& v_uchar, Mat& mat) 64 { 65 mat = Mat(v_uchar, true); 66 } 67 68 void Mat_to_vector_char(Mat& mat, std::vector<char>& v_char) 69 { 70 v_char.clear(); 71 CHECK_MAT(mat.type()==CV_8SC1 && mat.cols==1); 72 v_char = (std::vector<char>) mat; 73 } 74 75 void vector_char_to_Mat(std::vector<char>& v_char, Mat& mat) 76 { 77 mat = Mat(v_char, true); 78 } 79 80 81 //vector_Rect 82 83 void Mat_to_vector_Rect(Mat& mat, std::vector<Rect>& v_rect) 84 { 85 v_rect.clear(); 86 CHECK_MAT(mat.type()==CV_32SC4 && mat.cols==1); 87 v_rect = (std::vector<Rect>) mat; 88 } 89 90 void vector_Rect_to_Mat(std::vector<Rect>& v_rect, Mat& mat) 91 { 92 mat = Mat(v_rect, true); 93 } 94 95 96 //vector_Point 97 void Mat_to_vector_Point(Mat& mat, std::vector<Point>& v_point) 98 { 99 v_point.clear(); 100 CHECK_MAT(mat.type()==CV_32SC2 && mat.cols==1); 101 v_point = (std::vector<Point>) mat; 102 } 103 104 //vector_Point2f 105 void Mat_to_vector_Point2f(Mat& mat, std::vector<Point2f>& v_point) 106 { 107 v_point.clear(); 108 CHECK_MAT(mat.type()==CV_32FC2 && mat.cols==1); 109 v_point = (std::vector<Point2f>) mat; 110 } 111 112 //vector_Point2d 113 void Mat_to_vector_Point2d(Mat& mat, std::vector<Point2d>& v_point) 114 { 115 v_point.clear(); 116 CHECK_MAT(mat.type()==CV_64FC2 && mat.cols==1); 117 v_point = (std::vector<Point2d>) mat; 118 } 119 120 121 //vector_Point3i 122 void Mat_to_vector_Point3i(Mat& mat, std::vector<Point3i>& v_point) 123 { 124 v_point.clear(); 125 CHECK_MAT(mat.type()==CV_32SC3 && mat.cols==1); 126 v_point = (std::vector<Point3i>) mat; 127 } 128 129 //vector_Point3f 130 void Mat_to_vector_Point3f(Mat& mat, std::vector<Point3f>& v_point) 131 { 132 v_point.clear(); 133 CHECK_MAT(mat.type()==CV_32FC3 && mat.cols==1); 134 v_point = (std::vector<Point3f>) mat; 135 } 136 137 //vector_Point3d 138 void Mat_to_vector_Point3d(Mat& mat, std::vector<Point3d>& v_point) 139 { 140 v_point.clear(); 141 CHECK_MAT(mat.type()==CV_64FC3 && mat.cols==1); 142 v_point = (std::vector<Point3d>) mat; 143 } 144 145 146 void vector_Point_to_Mat(std::vector<Point>& v_point, Mat& mat) 147 { 148 mat = Mat(v_point, true); 149 } 150 151 void vector_Point2f_to_Mat(std::vector<Point2f>& v_point, Mat& mat) 152 { 153 mat = Mat(v_point, true); 154 } 155 156 void vector_Point2d_to_Mat(std::vector<Point2d>& v_point, Mat& mat) 157 { 158 mat = Mat(v_point, true); 159 } 160 161 void vector_Point3i_to_Mat(std::vector<Point3i>& v_point, Mat& mat) 162 { 163 mat = Mat(v_point, true); 164 } 165 166 void vector_Point3f_to_Mat(std::vector<Point3f>& v_point, Mat& mat) 167 { 168 mat = Mat(v_point, true); 169 } 170 171 void vector_Point3d_to_Mat(std::vector<Point3d>& v_point, Mat& mat) 172 { 173 mat = Mat(v_point, true); 174 } 175 176 //vector_Mat 177 void Mat_to_vector_Mat(cv::Mat& mat, std::vector<cv::Mat>& v_mat) 178 { 179 v_mat.clear(); 180 if(mat.type() == CV_32SC2 && mat.cols == 1) 181 { 182 v_mat.reserve(mat.rows); 183 for(int i=0; i<mat.rows; i++) 184 { 185 Vec<int, 2> a = mat.at< Vec<int, 2> >(i, 0); 186 long long addr = (((long long)a[0])<<32) | (a[1]&0xffffffff); 187 Mat& m = *( (Mat*) addr ); 188 v_mat.push_back(m); 189 } 190 } else { 191 LOGD("Mat_to_vector_Mat() FAILED: mat.type() == CV_32SC2 && mat.cols == 1"); 192 } 193 } 194 195 196 void vector_Mat_to_Mat(std::vector<cv::Mat>& v_mat, cv::Mat& mat) 197 { 198 int count = (int)v_mat.size(); 199 mat.create(count, 1, CV_32SC2); 200 for(int i=0; i<count; i++) 201 { 202 long long addr = (long long) new Mat(v_mat[i]); 203 mat.at< Vec<int, 2> >(i, 0) = Vec<int, 2>(addr>>32, addr&0xffffffff); 204 } 205 } 206 207 void Mat_to_vector_vector_Point(Mat& mat, std::vector< std::vector< Point > >& vv_pt) 208 { 209 std::vector<Mat> vm; 210 vm.reserve( mat.rows ); 211 Mat_to_vector_Mat(mat, vm); 212 for(size_t i=0; i<vm.size(); i++) 213 { 214 std::vector<Point> vpt; 215 Mat_to_vector_Point(vm[i], vpt); 216 vv_pt.push_back(vpt); 217 } 218 } 219 220 void Mat_to_vector_vector_Point2f(Mat& mat, std::vector< std::vector< Point2f > >& vv_pt) 221 { 222 std::vector<Mat> vm; 223 vm.reserve( mat.rows ); 224 Mat_to_vector_Mat(mat, vm); 225 for(size_t i=0; i<vm.size(); i++) 226 { 227 std::vector<Point2f> vpt; 228 Mat_to_vector_Point2f(vm[i], vpt); 229 vv_pt.push_back(vpt); 230 } 231 } 232 233 void Mat_to_vector_vector_Point3f(Mat& mat, std::vector< std::vector< Point3f > >& vv_pt) 234 { 235 std::vector<Mat> vm; 236 vm.reserve( mat.rows ); 237 Mat_to_vector_Mat(mat, vm); 238 for(size_t i=0; i<vm.size(); i++) 239 { 240 std::vector<Point3f> vpt; 241 Mat_to_vector_Point3f(vm[i], vpt); 242 vv_pt.push_back(vpt); 243 } 244 } 245 246 void Mat_to_vector_vector_char(Mat& mat, std::vector< std::vector< char > >& vv_ch) 247 { 248 std::vector<Mat> vm; 249 vm.reserve( mat.rows ); 250 Mat_to_vector_Mat(mat, vm); 251 for(size_t i=0; i<vm.size(); i++) 252 { 253 std::vector<char> vch; 254 Mat_to_vector_char(vm[i], vch); 255 vv_ch.push_back(vch); 256 } 257 } 258 259 void vector_vector_char_to_Mat(std::vector< std::vector< char > >& vv_ch, Mat& mat) 260 { 261 std::vector<Mat> vm; 262 vm.reserve( vv_ch.size() ); 263 for(size_t i=0; i<vv_ch.size(); i++) 264 { 265 Mat m; 266 vector_char_to_Mat(vv_ch[i], m); 267 vm.push_back(m); 268 } 269 vector_Mat_to_Mat(vm, mat); 270 } 271 272 void vector_vector_Point_to_Mat(std::vector< std::vector< Point > >& vv_pt, Mat& mat) 273 { 274 std::vector<Mat> vm; 275 vm.reserve( vv_pt.size() ); 276 for(size_t i=0; i<vv_pt.size(); i++) 277 { 278 Mat m; 279 vector_Point_to_Mat(vv_pt[i], m); 280 vm.push_back(m); 281 } 282 vector_Mat_to_Mat(vm, mat); 283 } 284 285 void vector_vector_Point2f_to_Mat(std::vector< std::vector< Point2f > >& vv_pt, Mat& mat) 286 { 287 std::vector<Mat> vm; 288 vm.reserve( vv_pt.size() ); 289 for(size_t i=0; i<vv_pt.size(); i++) 290 { 291 Mat m; 292 vector_Point2f_to_Mat(vv_pt[i], m); 293 vm.push_back(m); 294 } 295 vector_Mat_to_Mat(vm, mat); 296 } 297 298 void vector_vector_Point3f_to_Mat(std::vector< std::vector< Point3f > >& vv_pt, Mat& mat) 299 { 300 std::vector<Mat> vm; 301 vm.reserve( vv_pt.size() ); 302 for(size_t i=0; i<vv_pt.size(); i++) 303 { 304 Mat m; 305 vector_Point3f_to_Mat(vv_pt[i], m); 306 vm.push_back(m); 307 } 308 vector_Mat_to_Mat(vm, mat); 309 } 310 311 void vector_Vec4i_to_Mat(std::vector<Vec4i>& v_vec, Mat& mat) 312 { 313 mat = Mat(v_vec, true); 314 } 315 316 void vector_Vec4f_to_Mat(std::vector<Vec4f>& v_vec, Mat& mat) 317 { 318 mat = Mat(v_vec, true); 319 } 320 321 void vector_Vec6f_to_Mat(std::vector<Vec6f>& v_vec, Mat& mat) 322 { 323 mat = Mat(v_vec, true); 324 } 325