1 package org.opencv.utils; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.opencv.core.CvType; 7 import org.opencv.core.Mat; 8 import org.opencv.core.MatOfByte; 9 import org.opencv.core.MatOfDMatch; 10 import org.opencv.core.MatOfKeyPoint; 11 import org.opencv.core.MatOfPoint; 12 import org.opencv.core.MatOfPoint2f; 13 import org.opencv.core.MatOfPoint3f; 14 import org.opencv.core.Point; 15 import org.opencv.core.Point3; 16 import org.opencv.core.Rect; 17 import org.opencv.core.DMatch; 18 import org.opencv.core.KeyPoint; 19 20 public class Converters { 21 vector_Point_to_Mat(List<Point> pts)22 public static Mat vector_Point_to_Mat(List<Point> pts) { 23 return vector_Point_to_Mat(pts, CvType.CV_32S); 24 } 25 vector_Point2f_to_Mat(List<Point> pts)26 public static Mat vector_Point2f_to_Mat(List<Point> pts) { 27 return vector_Point_to_Mat(pts, CvType.CV_32F); 28 } 29 vector_Point2d_to_Mat(List<Point> pts)30 public static Mat vector_Point2d_to_Mat(List<Point> pts) { 31 return vector_Point_to_Mat(pts, CvType.CV_64F); 32 } 33 vector_Point_to_Mat(List<Point> pts, int typeDepth)34 public static Mat vector_Point_to_Mat(List<Point> pts, int typeDepth) { 35 Mat res; 36 int count = (pts != null) ? pts.size() : 0; 37 if (count > 0) { 38 switch (typeDepth) { 39 case CvType.CV_32S: { 40 res = new Mat(count, 1, CvType.CV_32SC2); 41 int[] buff = new int[count * 2]; 42 for (int i = 0; i < count; i++) { 43 Point p = pts.get(i); 44 buff[i * 2] = (int) p.x; 45 buff[i * 2 + 1] = (int) p.y; 46 } 47 res.put(0, 0, buff); 48 } 49 break; 50 51 case CvType.CV_32F: { 52 res = new Mat(count, 1, CvType.CV_32FC2); 53 float[] buff = new float[count * 2]; 54 for (int i = 0; i < count; i++) { 55 Point p = pts.get(i); 56 buff[i * 2] = (float) p.x; 57 buff[i * 2 + 1] = (float) p.y; 58 } 59 res.put(0, 0, buff); 60 } 61 break; 62 63 case CvType.CV_64F: { 64 res = new Mat(count, 1, CvType.CV_64FC2); 65 double[] buff = new double[count * 2]; 66 for (int i = 0; i < count; i++) { 67 Point p = pts.get(i); 68 buff[i * 2] = p.x; 69 buff[i * 2 + 1] = p.y; 70 } 71 res.put(0, 0, buff); 72 } 73 break; 74 75 default: 76 throw new IllegalArgumentException("'typeDepth' can be CV_32S, CV_32F or CV_64F"); 77 } 78 } else { 79 res = new Mat(); 80 } 81 return res; 82 } 83 vector_Point3i_to_Mat(List<Point3> pts)84 public static Mat vector_Point3i_to_Mat(List<Point3> pts) { 85 return vector_Point3_to_Mat(pts, CvType.CV_32S); 86 } 87 vector_Point3f_to_Mat(List<Point3> pts)88 public static Mat vector_Point3f_to_Mat(List<Point3> pts) { 89 return vector_Point3_to_Mat(pts, CvType.CV_32F); 90 } 91 vector_Point3d_to_Mat(List<Point3> pts)92 public static Mat vector_Point3d_to_Mat(List<Point3> pts) { 93 return vector_Point3_to_Mat(pts, CvType.CV_64F); 94 } 95 vector_Point3_to_Mat(List<Point3> pts, int typeDepth)96 public static Mat vector_Point3_to_Mat(List<Point3> pts, int typeDepth) { 97 Mat res; 98 int count = (pts != null) ? pts.size() : 0; 99 if (count > 0) { 100 switch (typeDepth) { 101 case CvType.CV_32S: { 102 res = new Mat(count, 1, CvType.CV_32SC3); 103 int[] buff = new int[count * 3]; 104 for (int i = 0; i < count; i++) { 105 Point3 p = pts.get(i); 106 buff[i * 3] = (int) p.x; 107 buff[i * 3 + 1] = (int) p.y; 108 buff[i * 3 + 2] = (int) p.z; 109 } 110 res.put(0, 0, buff); 111 } 112 break; 113 114 case CvType.CV_32F: { 115 res = new Mat(count, 1, CvType.CV_32FC3); 116 float[] buff = new float[count * 3]; 117 for (int i = 0; i < count; i++) { 118 Point3 p = pts.get(i); 119 buff[i * 3] = (float) p.x; 120 buff[i * 3 + 1] = (float) p.y; 121 buff[i * 3 + 2] = (float) p.z; 122 } 123 res.put(0, 0, buff); 124 } 125 break; 126 127 case CvType.CV_64F: { 128 res = new Mat(count, 1, CvType.CV_64FC3); 129 double[] buff = new double[count * 3]; 130 for (int i = 0; i < count; i++) { 131 Point3 p = pts.get(i); 132 buff[i * 3] = p.x; 133 buff[i * 3 + 1] = p.y; 134 buff[i * 3 + 2] = p.z; 135 } 136 res.put(0, 0, buff); 137 } 138 break; 139 140 default: 141 throw new IllegalArgumentException("'typeDepth' can be CV_32S, CV_32F or CV_64F"); 142 } 143 } else { 144 res = new Mat(); 145 } 146 return res; 147 } 148 Mat_to_vector_Point2f(Mat m, List<Point> pts)149 public static void Mat_to_vector_Point2f(Mat m, List<Point> pts) { 150 Mat_to_vector_Point(m, pts); 151 } 152 Mat_to_vector_Point2d(Mat m, List<Point> pts)153 public static void Mat_to_vector_Point2d(Mat m, List<Point> pts) { 154 Mat_to_vector_Point(m, pts); 155 } 156 Mat_to_vector_Point(Mat m, List<Point> pts)157 public static void Mat_to_vector_Point(Mat m, List<Point> pts) { 158 if (pts == null) 159 throw new java.lang.IllegalArgumentException("Output List can't be null"); 160 int count = m.rows(); 161 int type = m.type(); 162 if (m.cols() != 1) 163 throw new java.lang.IllegalArgumentException("Input Mat should have one column\n" + m); 164 165 pts.clear(); 166 if (type == CvType.CV_32SC2) { 167 int[] buff = new int[2 * count]; 168 m.get(0, 0, buff); 169 for (int i = 0; i < count; i++) { 170 pts.add(new Point(buff[i * 2], buff[i * 2 + 1])); 171 } 172 } else if (type == CvType.CV_32FC2) { 173 float[] buff = new float[2 * count]; 174 m.get(0, 0, buff); 175 for (int i = 0; i < count; i++) { 176 pts.add(new Point(buff[i * 2], buff[i * 2 + 1])); 177 } 178 } else if (type == CvType.CV_64FC2) { 179 double[] buff = new double[2 * count]; 180 m.get(0, 0, buff); 181 for (int i = 0; i < count; i++) { 182 pts.add(new Point(buff[i * 2], buff[i * 2 + 1])); 183 } 184 } else { 185 throw new java.lang.IllegalArgumentException( 186 "Input Mat should be of CV_32SC2, CV_32FC2 or CV_64FC2 type\n" + m); 187 } 188 } 189 Mat_to_vector_Point3i(Mat m, List<Point3> pts)190 public static void Mat_to_vector_Point3i(Mat m, List<Point3> pts) { 191 Mat_to_vector_Point3(m, pts); 192 } 193 Mat_to_vector_Point3f(Mat m, List<Point3> pts)194 public static void Mat_to_vector_Point3f(Mat m, List<Point3> pts) { 195 Mat_to_vector_Point3(m, pts); 196 } 197 Mat_to_vector_Point3d(Mat m, List<Point3> pts)198 public static void Mat_to_vector_Point3d(Mat m, List<Point3> pts) { 199 Mat_to_vector_Point3(m, pts); 200 } 201 Mat_to_vector_Point3(Mat m, List<Point3> pts)202 public static void Mat_to_vector_Point3(Mat m, List<Point3> pts) { 203 if (pts == null) 204 throw new java.lang.IllegalArgumentException("Output List can't be null"); 205 int count = m.rows(); 206 int type = m.type(); 207 if (m.cols() != 1) 208 throw new java.lang.IllegalArgumentException("Input Mat should have one column\n" + m); 209 210 pts.clear(); 211 if (type == CvType.CV_32SC3) { 212 int[] buff = new int[3 * count]; 213 m.get(0, 0, buff); 214 for (int i = 0; i < count; i++) { 215 pts.add(new Point3(buff[i * 3], buff[i * 3 + 1], buff[i * 3 + 2])); 216 } 217 } else if (type == CvType.CV_32FC3) { 218 float[] buff = new float[3 * count]; 219 m.get(0, 0, buff); 220 for (int i = 0; i < count; i++) { 221 pts.add(new Point3(buff[i * 3], buff[i * 3 + 1], buff[i * 3 + 2])); 222 } 223 } else if (type == CvType.CV_64FC3) { 224 double[] buff = new double[3 * count]; 225 m.get(0, 0, buff); 226 for (int i = 0; i < count; i++) { 227 pts.add(new Point3(buff[i * 3], buff[i * 3 + 1], buff[i * 3 + 2])); 228 } 229 } else { 230 throw new java.lang.IllegalArgumentException( 231 "Input Mat should be of CV_32SC3, CV_32FC3 or CV_64FC3 type\n" + m); 232 } 233 } 234 vector_Mat_to_Mat(List<Mat> mats)235 public static Mat vector_Mat_to_Mat(List<Mat> mats) { 236 Mat res; 237 int count = (mats != null) ? mats.size() : 0; 238 if (count > 0) { 239 res = new Mat(count, 1, CvType.CV_32SC2); 240 int[] buff = new int[count * 2]; 241 for (int i = 0; i < count; i++) { 242 long addr = mats.get(i).nativeObj; 243 buff[i * 2] = (int) (addr >> 32); 244 buff[i * 2 + 1] = (int) (addr & 0xffffffff); 245 } 246 res.put(0, 0, buff); 247 } else { 248 res = new Mat(); 249 } 250 return res; 251 } 252 Mat_to_vector_Mat(Mat m, List<Mat> mats)253 public static void Mat_to_vector_Mat(Mat m, List<Mat> mats) { 254 if (mats == null) 255 throw new java.lang.IllegalArgumentException("mats == null"); 256 int count = m.rows(); 257 if (CvType.CV_32SC2 != m.type() || m.cols() != 1) 258 throw new java.lang.IllegalArgumentException( 259 "CvType.CV_32SC2 != m.type() || m.cols()!=1\n" + m); 260 261 mats.clear(); 262 int[] buff = new int[count * 2]; 263 m.get(0, 0, buff); 264 for (int i = 0; i < count; i++) { 265 long addr = (((long) buff[i * 2]) << 32) | (((long) buff[i * 2 + 1]) & 0xffffffffL); 266 mats.add(new Mat(addr)); 267 } 268 } 269 vector_float_to_Mat(List<Float> fs)270 public static Mat vector_float_to_Mat(List<Float> fs) { 271 Mat res; 272 int count = (fs != null) ? fs.size() : 0; 273 if (count > 0) { 274 res = new Mat(count, 1, CvType.CV_32FC1); 275 float[] buff = new float[count]; 276 for (int i = 0; i < count; i++) { 277 float f = fs.get(i); 278 buff[i] = f; 279 } 280 res.put(0, 0, buff); 281 } else { 282 res = new Mat(); 283 } 284 return res; 285 } 286 Mat_to_vector_float(Mat m, List<Float> fs)287 public static void Mat_to_vector_float(Mat m, List<Float> fs) { 288 if (fs == null) 289 throw new java.lang.IllegalArgumentException("fs == null"); 290 int count = m.rows(); 291 if (CvType.CV_32FC1 != m.type() || m.cols() != 1) 292 throw new java.lang.IllegalArgumentException( 293 "CvType.CV_32FC1 != m.type() || m.cols()!=1\n" + m); 294 295 fs.clear(); 296 float[] buff = new float[count]; 297 m.get(0, 0, buff); 298 for (int i = 0; i < count; i++) { 299 fs.add(buff[i]); 300 } 301 } 302 vector_uchar_to_Mat(List<Byte> bs)303 public static Mat vector_uchar_to_Mat(List<Byte> bs) { 304 Mat res; 305 int count = (bs != null) ? bs.size() : 0; 306 if (count > 0) { 307 res = new Mat(count, 1, CvType.CV_8UC1); 308 byte[] buff = new byte[count]; 309 for (int i = 0; i < count; i++) { 310 byte b = bs.get(i); 311 buff[i] = b; 312 } 313 res.put(0, 0, buff); 314 } else { 315 res = new Mat(); 316 } 317 return res; 318 } 319 Mat_to_vector_uchar(Mat m, List<Byte> us)320 public static void Mat_to_vector_uchar(Mat m, List<Byte> us) { 321 if (us == null) 322 throw new java.lang.IllegalArgumentException("Output List can't be null"); 323 int count = m.rows(); 324 if (CvType.CV_8UC1 != m.type() || m.cols() != 1) 325 throw new java.lang.IllegalArgumentException( 326 "CvType.CV_8UC1 != m.type() || m.cols()!=1\n" + m); 327 328 us.clear(); 329 byte[] buff = new byte[count]; 330 m.get(0, 0, buff); 331 for (int i = 0; i < count; i++) { 332 us.add(buff[i]); 333 } 334 } 335 vector_char_to_Mat(List<Byte> bs)336 public static Mat vector_char_to_Mat(List<Byte> bs) { 337 Mat res; 338 int count = (bs != null) ? bs.size() : 0; 339 if (count > 0) { 340 res = new Mat(count, 1, CvType.CV_8SC1); 341 byte[] buff = new byte[count]; 342 for (int i = 0; i < count; i++) { 343 byte b = bs.get(i); 344 buff[i] = b; 345 } 346 res.put(0, 0, buff); 347 } else { 348 res = new Mat(); 349 } 350 return res; 351 } 352 vector_int_to_Mat(List<Integer> is)353 public static Mat vector_int_to_Mat(List<Integer> is) { 354 Mat res; 355 int count = (is != null) ? is.size() : 0; 356 if (count > 0) { 357 res = new Mat(count, 1, CvType.CV_32SC1); 358 int[] buff = new int[count]; 359 for (int i = 0; i < count; i++) { 360 int v = is.get(i); 361 buff[i] = v; 362 } 363 res.put(0, 0, buff); 364 } else { 365 res = new Mat(); 366 } 367 return res; 368 } 369 Mat_to_vector_int(Mat m, List<Integer> is)370 public static void Mat_to_vector_int(Mat m, List<Integer> is) { 371 if (is == null) 372 throw new java.lang.IllegalArgumentException("is == null"); 373 int count = m.rows(); 374 if (CvType.CV_32SC1 != m.type() || m.cols() != 1) 375 throw new java.lang.IllegalArgumentException( 376 "CvType.CV_32SC1 != m.type() || m.cols()!=1\n" + m); 377 378 is.clear(); 379 int[] buff = new int[count]; 380 m.get(0, 0, buff); 381 for (int i = 0; i < count; i++) { 382 is.add(buff[i]); 383 } 384 } 385 Mat_to_vector_char(Mat m, List<Byte> bs)386 public static void Mat_to_vector_char(Mat m, List<Byte> bs) { 387 if (bs == null) 388 throw new java.lang.IllegalArgumentException("Output List can't be null"); 389 int count = m.rows(); 390 if (CvType.CV_8SC1 != m.type() || m.cols() != 1) 391 throw new java.lang.IllegalArgumentException( 392 "CvType.CV_8SC1 != m.type() || m.cols()!=1\n" + m); 393 394 bs.clear(); 395 byte[] buff = new byte[count]; 396 m.get(0, 0, buff); 397 for (int i = 0; i < count; i++) { 398 bs.add(buff[i]); 399 } 400 } 401 vector_Rect_to_Mat(List<Rect> rs)402 public static Mat vector_Rect_to_Mat(List<Rect> rs) { 403 Mat res; 404 int count = (rs != null) ? rs.size() : 0; 405 if (count > 0) { 406 res = new Mat(count, 1, CvType.CV_32SC4); 407 int[] buff = new int[4 * count]; 408 for (int i = 0; i < count; i++) { 409 Rect r = rs.get(i); 410 buff[4 * i] = r.x; 411 buff[4 * i + 1] = r.y; 412 buff[4 * i + 2] = r.width; 413 buff[4 * i + 3] = r.height; 414 } 415 res.put(0, 0, buff); 416 } else { 417 res = new Mat(); 418 } 419 return res; 420 } 421 Mat_to_vector_Rect(Mat m, List<Rect> rs)422 public static void Mat_to_vector_Rect(Mat m, List<Rect> rs) { 423 if (rs == null) 424 throw new java.lang.IllegalArgumentException("rs == null"); 425 int count = m.rows(); 426 if (CvType.CV_32SC4 != m.type() || m.cols() != 1) 427 throw new java.lang.IllegalArgumentException( 428 "CvType.CV_32SC4 != m.type() || m.rows()!=1\n" + m); 429 430 rs.clear(); 431 int[] buff = new int[4 * count]; 432 m.get(0, 0, buff); 433 for (int i = 0; i < count; i++) { 434 rs.add(new Rect(buff[4 * i], buff[4 * i + 1], buff[4 * i + 2], buff[4 * i + 3])); 435 } 436 } 437 vector_KeyPoint_to_Mat(List<KeyPoint> kps)438 public static Mat vector_KeyPoint_to_Mat(List<KeyPoint> kps) { 439 Mat res; 440 int count = (kps != null) ? kps.size() : 0; 441 if (count > 0) { 442 res = new Mat(count, 1, CvType.CV_64FC(7)); 443 double[] buff = new double[count * 7]; 444 for (int i = 0; i < count; i++) { 445 KeyPoint kp = kps.get(i); 446 buff[7 * i] = kp.pt.x; 447 buff[7 * i + 1] = kp.pt.y; 448 buff[7 * i + 2] = kp.size; 449 buff[7 * i + 3] = kp.angle; 450 buff[7 * i + 4] = kp.response; 451 buff[7 * i + 5] = kp.octave; 452 buff[7 * i + 6] = kp.class_id; 453 } 454 res.put(0, 0, buff); 455 } else { 456 res = new Mat(); 457 } 458 return res; 459 } 460 Mat_to_vector_KeyPoint(Mat m, List<KeyPoint> kps)461 public static void Mat_to_vector_KeyPoint(Mat m, List<KeyPoint> kps) { 462 if (kps == null) 463 throw new java.lang.IllegalArgumentException("Output List can't be null"); 464 int count = m.rows(); 465 if (CvType.CV_64FC(7) != m.type() || m.cols() != 1) 466 throw new java.lang.IllegalArgumentException( 467 "CvType.CV_64FC(7) != m.type() || m.cols()!=1\n" + m); 468 469 kps.clear(); 470 double[] buff = new double[7 * count]; 471 m.get(0, 0, buff); 472 for (int i = 0; i < count; i++) { 473 kps.add(new KeyPoint((float) buff[7 * i], (float) buff[7 * i + 1], (float) buff[7 * i + 2], (float) buff[7 * i + 3], 474 (float) buff[7 * i + 4], (int) buff[7 * i + 5], (int) buff[7 * i + 6])); 475 } 476 } 477 478 // vector_vector_Point vector_vector_Point_to_Mat(List<MatOfPoint> pts, List<Mat> mats)479 public static Mat vector_vector_Point_to_Mat(List<MatOfPoint> pts, List<Mat> mats) { 480 Mat res; 481 int lCount = (pts != null) ? pts.size() : 0; 482 if (lCount > 0) { 483 for (MatOfPoint vpt : pts) 484 mats.add(vpt); 485 res = vector_Mat_to_Mat(mats); 486 } else { 487 res = new Mat(); 488 } 489 return res; 490 } 491 Mat_to_vector_vector_Point(Mat m, List<MatOfPoint> pts)492 public static void Mat_to_vector_vector_Point(Mat m, List<MatOfPoint> pts) { 493 if (pts == null) 494 throw new java.lang.IllegalArgumentException("Output List can't be null"); 495 496 if (m == null) 497 throw new java.lang.IllegalArgumentException("Input Mat can't be null"); 498 499 List<Mat> mats = new ArrayList<Mat>(m.rows()); 500 Mat_to_vector_Mat(m, mats); 501 for (Mat mi : mats) { 502 MatOfPoint pt = new MatOfPoint(mi); 503 pts.add(pt); 504 mi.release(); 505 } 506 mats.clear(); 507 } 508 509 // vector_vector_Point2f Mat_to_vector_vector_Point2f(Mat m, List<MatOfPoint2f> pts)510 public static void Mat_to_vector_vector_Point2f(Mat m, List<MatOfPoint2f> pts) { 511 if (pts == null) 512 throw new java.lang.IllegalArgumentException("Output List can't be null"); 513 514 if (m == null) 515 throw new java.lang.IllegalArgumentException("Input Mat can't be null"); 516 517 List<Mat> mats = new ArrayList<Mat>(m.rows()); 518 Mat_to_vector_Mat(m, mats); 519 for (Mat mi : mats) { 520 MatOfPoint2f pt = new MatOfPoint2f(mi); 521 pts.add(pt); 522 mi.release(); 523 } 524 mats.clear(); 525 } 526 527 // vector_vector_Point2f vector_vector_Point2f_to_Mat(List<MatOfPoint2f> pts, List<Mat> mats)528 public static Mat vector_vector_Point2f_to_Mat(List<MatOfPoint2f> pts, List<Mat> mats) { 529 Mat res; 530 int lCount = (pts != null) ? pts.size() : 0; 531 if (lCount > 0) { 532 for (MatOfPoint2f vpt : pts) 533 mats.add(vpt); 534 res = vector_Mat_to_Mat(mats); 535 } else { 536 res = new Mat(); 537 } 538 return res; 539 } 540 541 // vector_vector_Point3f Mat_to_vector_vector_Point3f(Mat m, List<MatOfPoint3f> pts)542 public static void Mat_to_vector_vector_Point3f(Mat m, List<MatOfPoint3f> pts) { 543 if (pts == null) 544 throw new java.lang.IllegalArgumentException("Output List can't be null"); 545 546 if (m == null) 547 throw new java.lang.IllegalArgumentException("Input Mat can't be null"); 548 549 List<Mat> mats = new ArrayList<Mat>(m.rows()); 550 Mat_to_vector_Mat(m, mats); 551 for (Mat mi : mats) { 552 MatOfPoint3f pt = new MatOfPoint3f(mi); 553 pts.add(pt); 554 mi.release(); 555 } 556 mats.clear(); 557 } 558 559 // vector_vector_Point3f vector_vector_Point3f_to_Mat(List<MatOfPoint3f> pts, List<Mat> mats)560 public static Mat vector_vector_Point3f_to_Mat(List<MatOfPoint3f> pts, List<Mat> mats) { 561 Mat res; 562 int lCount = (pts != null) ? pts.size() : 0; 563 if (lCount > 0) { 564 for (MatOfPoint3f vpt : pts) 565 mats.add(vpt); 566 res = vector_Mat_to_Mat(mats); 567 } else { 568 res = new Mat(); 569 } 570 return res; 571 } 572 573 // vector_vector_KeyPoint vector_vector_KeyPoint_to_Mat(List<MatOfKeyPoint> kps, List<Mat> mats)574 public static Mat vector_vector_KeyPoint_to_Mat(List<MatOfKeyPoint> kps, List<Mat> mats) { 575 Mat res; 576 int lCount = (kps != null) ? kps.size() : 0; 577 if (lCount > 0) { 578 for (MatOfKeyPoint vkp : kps) 579 mats.add(vkp); 580 res = vector_Mat_to_Mat(mats); 581 } else { 582 res = new Mat(); 583 } 584 return res; 585 } 586 Mat_to_vector_vector_KeyPoint(Mat m, List<MatOfKeyPoint> kps)587 public static void Mat_to_vector_vector_KeyPoint(Mat m, List<MatOfKeyPoint> kps) { 588 if (kps == null) 589 throw new java.lang.IllegalArgumentException("Output List can't be null"); 590 591 if (m == null) 592 throw new java.lang.IllegalArgumentException("Input Mat can't be null"); 593 594 List<Mat> mats = new ArrayList<Mat>(m.rows()); 595 Mat_to_vector_Mat(m, mats); 596 for (Mat mi : mats) { 597 MatOfKeyPoint vkp = new MatOfKeyPoint(mi); 598 kps.add(vkp); 599 mi.release(); 600 } 601 mats.clear(); 602 } 603 vector_double_to_Mat(List<Double> ds)604 public static Mat vector_double_to_Mat(List<Double> ds) { 605 Mat res; 606 int count = (ds != null) ? ds.size() : 0; 607 if (count > 0) { 608 res = new Mat(count, 1, CvType.CV_64FC1); 609 double[] buff = new double[count]; 610 for (int i = 0; i < count; i++) { 611 double v = ds.get(i); 612 buff[i] = v; 613 } 614 res.put(0, 0, buff); 615 } else { 616 res = new Mat(); 617 } 618 return res; 619 } 620 Mat_to_vector_double(Mat m, List<Double> ds)621 public static void Mat_to_vector_double(Mat m, List<Double> ds) { 622 if (ds == null) 623 throw new java.lang.IllegalArgumentException("ds == null"); 624 int count = m.rows(); 625 if (CvType.CV_64FC1 != m.type() || m.cols() != 1) 626 throw new java.lang.IllegalArgumentException( 627 "CvType.CV_64FC1 != m.type() || m.cols()!=1\n" + m); 628 629 ds.clear(); 630 double[] buff = new double[count]; 631 m.get(0, 0, buff); 632 for (int i = 0; i < count; i++) { 633 ds.add(buff[i]); 634 } 635 } 636 vector_DMatch_to_Mat(List<DMatch> matches)637 public static Mat vector_DMatch_to_Mat(List<DMatch> matches) { 638 Mat res; 639 int count = (matches != null) ? matches.size() : 0; 640 if (count > 0) { 641 res = new Mat(count, 1, CvType.CV_64FC4); 642 double[] buff = new double[count * 4]; 643 for (int i = 0; i < count; i++) { 644 DMatch m = matches.get(i); 645 buff[4 * i] = m.queryIdx; 646 buff[4 * i + 1] = m.trainIdx; 647 buff[4 * i + 2] = m.imgIdx; 648 buff[4 * i + 3] = m.distance; 649 } 650 res.put(0, 0, buff); 651 } else { 652 res = new Mat(); 653 } 654 return res; 655 } 656 Mat_to_vector_DMatch(Mat m, List<DMatch> matches)657 public static void Mat_to_vector_DMatch(Mat m, List<DMatch> matches) { 658 if (matches == null) 659 throw new java.lang.IllegalArgumentException("Output List can't be null"); 660 int count = m.rows(); 661 if (CvType.CV_64FC4 != m.type() || m.cols() != 1) 662 throw new java.lang.IllegalArgumentException( 663 "CvType.CV_64FC4 != m.type() || m.cols()!=1\n" + m); 664 665 matches.clear(); 666 double[] buff = new double[4 * count]; 667 m.get(0, 0, buff); 668 for (int i = 0; i < count; i++) { 669 matches.add(new DMatch((int) buff[4 * i], (int) buff[4 * i + 1], (int) buff[4 * i + 2], (float) buff[4 * i + 3])); 670 } 671 } 672 673 // vector_vector_DMatch vector_vector_DMatch_to_Mat(List<MatOfDMatch> lvdm, List<Mat> mats)674 public static Mat vector_vector_DMatch_to_Mat(List<MatOfDMatch> lvdm, List<Mat> mats) { 675 Mat res; 676 int lCount = (lvdm != null) ? lvdm.size() : 0; 677 if (lCount > 0) { 678 for (MatOfDMatch vdm : lvdm) 679 mats.add(vdm); 680 res = vector_Mat_to_Mat(mats); 681 } else { 682 res = new Mat(); 683 } 684 return res; 685 } 686 Mat_to_vector_vector_DMatch(Mat m, List<MatOfDMatch> lvdm)687 public static void Mat_to_vector_vector_DMatch(Mat m, List<MatOfDMatch> lvdm) { 688 if (lvdm == null) 689 throw new java.lang.IllegalArgumentException("Output List can't be null"); 690 691 if (m == null) 692 throw new java.lang.IllegalArgumentException("Input Mat can't be null"); 693 694 List<Mat> mats = new ArrayList<Mat>(m.rows()); 695 Mat_to_vector_Mat(m, mats); 696 lvdm.clear(); 697 for (Mat mi : mats) { 698 MatOfDMatch vdm = new MatOfDMatch(mi); 699 lvdm.add(vdm); 700 mi.release(); 701 } 702 mats.clear(); 703 } 704 705 // vector_vector_char vector_vector_char_to_Mat(List<MatOfByte> lvb, List<Mat> mats)706 public static Mat vector_vector_char_to_Mat(List<MatOfByte> lvb, List<Mat> mats) { 707 Mat res; 708 int lCount = (lvb != null) ? lvb.size() : 0; 709 if (lCount > 0) { 710 for (MatOfByte vb : lvb) 711 mats.add(vb); 712 res = vector_Mat_to_Mat(mats); 713 } else { 714 res = new Mat(); 715 } 716 return res; 717 } 718 Mat_to_vector_vector_char(Mat m, List<List<Byte>> llb)719 public static void Mat_to_vector_vector_char(Mat m, List<List<Byte>> llb) { 720 if (llb == null) 721 throw new java.lang.IllegalArgumentException("Output List can't be null"); 722 723 if (m == null) 724 throw new java.lang.IllegalArgumentException("Input Mat can't be null"); 725 726 List<Mat> mats = new ArrayList<Mat>(m.rows()); 727 Mat_to_vector_Mat(m, mats); 728 for (Mat mi : mats) { 729 List<Byte> lb = new ArrayList<Byte>(); 730 Mat_to_vector_char(mi, lb); 731 llb.add(lb); 732 mi.release(); 733 } 734 mats.clear(); 735 } 736 } 737