001package org.opencv.utils; 002 003import java.util.ArrayList; 004import java.util.List; 005 006import org.opencv.core.CvType; 007import org.opencv.core.Mat; 008import org.opencv.core.MatOfByte; 009import org.opencv.core.MatOfDMatch; 010import org.opencv.core.MatOfKeyPoint; 011import org.opencv.core.MatOfPoint; 012import org.opencv.core.MatOfPoint2f; 013import org.opencv.core.MatOfPoint3f; 014import org.opencv.core.Point; 015import org.opencv.core.Point3; 016import org.opencv.core.Size; 017import org.opencv.core.Rect; 018import org.opencv.core.RotatedRect; 019import org.opencv.core.Rect2d; 020import org.opencv.core.DMatch; 021import org.opencv.core.KeyPoint; 022 023public class Converters { 024 025 public static Mat vector_Point_to_Mat(List<Point> pts) { 026 return vector_Point_to_Mat(pts, CvType.CV_32S); 027 } 028 029 public static Mat vector_Point2f_to_Mat(List<Point> pts) { 030 return vector_Point_to_Mat(pts, CvType.CV_32F); 031 } 032 033 public static Mat vector_Point2d_to_Mat(List<Point> pts) { 034 return vector_Point_to_Mat(pts, CvType.CV_64F); 035 } 036 037 public static Mat vector_Point_to_Mat(List<Point> pts, int typeDepth) { 038 Mat res; 039 int count = (pts != null) ? pts.size() : 0; 040 if (count > 0) { 041 switch (typeDepth) { 042 case CvType.CV_32S: { 043 res = new Mat(count, 1, CvType.CV_32SC2); 044 int[] buff = new int[count * 2]; 045 for (int i = 0; i < count; i++) { 046 Point p = pts.get(i); 047 buff[i * 2] = (int) p.x; 048 buff[i * 2 + 1] = (int) p.y; 049 } 050 res.put(0, 0, buff); 051 } 052 break; 053 054 case CvType.CV_32F: { 055 res = new Mat(count, 1, CvType.CV_32FC2); 056 float[] buff = new float[count * 2]; 057 for (int i = 0; i < count; i++) { 058 Point p = pts.get(i); 059 buff[i * 2] = (float) p.x; 060 buff[i * 2 + 1] = (float) p.y; 061 } 062 res.put(0, 0, buff); 063 } 064 break; 065 066 case CvType.CV_64F: { 067 res = new Mat(count, 1, CvType.CV_64FC2); 068 double[] buff = new double[count * 2]; 069 for (int i = 0; i < count; i++) { 070 Point p = pts.get(i); 071 buff[i * 2] = p.x; 072 buff[i * 2 + 1] = p.y; 073 } 074 res.put(0, 0, buff); 075 } 076 break; 077 078 default: 079 throw new IllegalArgumentException("'typeDepth' can be CV_32S, CV_32F or CV_64F"); 080 } 081 } else { 082 res = new Mat(); 083 } 084 return res; 085 } 086 087 public static Mat vector_Point3i_to_Mat(List<Point3> pts) { 088 return vector_Point3_to_Mat(pts, CvType.CV_32S); 089 } 090 091 public static Mat vector_Point3f_to_Mat(List<Point3> pts) { 092 return vector_Point3_to_Mat(pts, CvType.CV_32F); 093 } 094 095 public static Mat vector_Point3d_to_Mat(List<Point3> pts) { 096 return vector_Point3_to_Mat(pts, CvType.CV_64F); 097 } 098 099 public static Mat vector_Point3_to_Mat(List<Point3> pts, int typeDepth) { 100 Mat res; 101 int count = (pts != null) ? pts.size() : 0; 102 if (count > 0) { 103 switch (typeDepth) { 104 case CvType.CV_32S: { 105 res = new Mat(count, 1, CvType.CV_32SC3); 106 int[] buff = new int[count * 3]; 107 for (int i = 0; i < count; i++) { 108 Point3 p = pts.get(i); 109 buff[i * 3] = (int) p.x; 110 buff[i * 3 + 1] = (int) p.y; 111 buff[i * 3 + 2] = (int) p.z; 112 } 113 res.put(0, 0, buff); 114 } 115 break; 116 117 case CvType.CV_32F: { 118 res = new Mat(count, 1, CvType.CV_32FC3); 119 float[] buff = new float[count * 3]; 120 for (int i = 0; i < count; i++) { 121 Point3 p = pts.get(i); 122 buff[i * 3] = (float) p.x; 123 buff[i * 3 + 1] = (float) p.y; 124 buff[i * 3 + 2] = (float) p.z; 125 } 126 res.put(0, 0, buff); 127 } 128 break; 129 130 case CvType.CV_64F: { 131 res = new Mat(count, 1, CvType.CV_64FC3); 132 double[] buff = new double[count * 3]; 133 for (int i = 0; i < count; i++) { 134 Point3 p = pts.get(i); 135 buff[i * 3] = p.x; 136 buff[i * 3 + 1] = p.y; 137 buff[i * 3 + 2] = p.z; 138 } 139 res.put(0, 0, buff); 140 } 141 break; 142 143 default: 144 throw new IllegalArgumentException("'typeDepth' can be CV_32S, CV_32F or CV_64F"); 145 } 146 } else { 147 res = new Mat(); 148 } 149 return res; 150 } 151 152 public static void Mat_to_vector_Point2f(Mat m, List<Point> pts) { 153 Mat_to_vector_Point(m, pts); 154 } 155 156 public static void Mat_to_vector_Point2d(Mat m, List<Point> pts) { 157 Mat_to_vector_Point(m, pts); 158 } 159 160 public static void Mat_to_vector_Point(Mat m, List<Point> pts) { 161 if (pts == null) 162 throw new IllegalArgumentException("Output List can't be null"); 163 int count = m.rows(); 164 int type = m.type(); 165 if (m.cols() != 1) 166 throw new IllegalArgumentException("Input Mat should have one column\n" + m); 167 168 pts.clear(); 169 if (type == CvType.CV_32SC2) { 170 int[] buff = new int[2 * count]; 171 m.get(0, 0, buff); 172 for (int i = 0; i < count; i++) { 173 pts.add(new Point(buff[i * 2], buff[i * 2 + 1])); 174 } 175 } else if (type == CvType.CV_32FC2) { 176 float[] buff = new float[2 * count]; 177 m.get(0, 0, buff); 178 for (int i = 0; i < count; i++) { 179 pts.add(new Point(buff[i * 2], buff[i * 2 + 1])); 180 } 181 } else if (type == CvType.CV_64FC2) { 182 double[] buff = new double[2 * count]; 183 m.get(0, 0, buff); 184 for (int i = 0; i < count; i++) { 185 pts.add(new Point(buff[i * 2], buff[i * 2 + 1])); 186 } 187 } else { 188 throw new IllegalArgumentException( 189 "Input Mat should be of CV_32SC2, CV_32FC2 or CV_64FC2 type\n" + m); 190 } 191 } 192 193 public static void Mat_to_vector_Point3i(Mat m, List<Point3> pts) { 194 Mat_to_vector_Point3(m, pts); 195 } 196 197 public static void Mat_to_vector_Point3f(Mat m, List<Point3> pts) { 198 Mat_to_vector_Point3(m, pts); 199 } 200 201 public static void Mat_to_vector_Point3d(Mat m, List<Point3> pts) { 202 Mat_to_vector_Point3(m, pts); 203 } 204 205 public static void Mat_to_vector_Point3(Mat m, List<Point3> pts) { 206 if (pts == null) 207 throw new IllegalArgumentException("Output List can't be null"); 208 int count = m.rows(); 209 int type = m.type(); 210 if (m.cols() != 1) 211 throw new IllegalArgumentException("Input Mat should have one column\n" + m); 212 213 pts.clear(); 214 if (type == CvType.CV_32SC3) { 215 int[] buff = new int[3 * count]; 216 m.get(0, 0, buff); 217 for (int i = 0; i < count; i++) { 218 pts.add(new Point3(buff[i * 3], buff[i * 3 + 1], buff[i * 3 + 2])); 219 } 220 } else if (type == CvType.CV_32FC3) { 221 float[] buff = new float[3 * count]; 222 m.get(0, 0, buff); 223 for (int i = 0; i < count; i++) { 224 pts.add(new Point3(buff[i * 3], buff[i * 3 + 1], buff[i * 3 + 2])); 225 } 226 } else if (type == CvType.CV_64FC3) { 227 double[] buff = new double[3 * count]; 228 m.get(0, 0, buff); 229 for (int i = 0; i < count; i++) { 230 pts.add(new Point3(buff[i * 3], buff[i * 3 + 1], buff[i * 3 + 2])); 231 } 232 } else { 233 throw new IllegalArgumentException( 234 "Input Mat should be of CV_32SC3, CV_32FC3 or CV_64FC3 type\n" + m); 235 } 236 } 237 238 public static Mat vector_Mat_to_Mat(List<Mat> mats) { 239 Mat res; 240 int count = (mats != null) ? mats.size() : 0; 241 if (count > 0) { 242 res = new Mat(count, 1, CvType.CV_32SC2); 243 int[] buff = new int[count * 2]; 244 for (int i = 0; i < count; i++) { 245 long addr = mats.get(i).nativeObj; 246 buff[i * 2] = (int) (addr >> 32); 247 buff[i * 2 + 1] = (int) (addr & 0xffffffff); 248 } 249 res.put(0, 0, buff); 250 } else { 251 res = new Mat(); 252 } 253 return res; 254 } 255 256 public static void Mat_to_vector_Mat(Mat m, List<Mat> mats) { 257 if (mats == null) 258 throw new IllegalArgumentException("mats == null"); 259 int count = m.rows(); 260 if (CvType.CV_32SC2 != m.type() || m.cols() != 1) 261 throw new IllegalArgumentException( 262 "CvType.CV_32SC2 != m.type() || m.cols()!=1\n" + m); 263 264 mats.clear(); 265 int[] buff = new int[count * 2]; 266 m.get(0, 0, buff); 267 for (int i = 0; i < count; i++) { 268 long addr = (((long) buff[i * 2]) << 32) | (((long) buff[i * 2 + 1]) & 0xffffffffL); 269 mats.add(new Mat(addr)); 270 } 271 } 272 273 public static Mat vector_float_to_Mat(List<Float> fs) { 274 Mat res; 275 int count = (fs != null) ? fs.size() : 0; 276 if (count > 0) { 277 res = new Mat(count, 1, CvType.CV_32FC1); 278 float[] buff = new float[count]; 279 for (int i = 0; i < count; i++) { 280 float f = fs.get(i); 281 buff[i] = f; 282 } 283 res.put(0, 0, buff); 284 } else { 285 res = new Mat(); 286 } 287 return res; 288 } 289 290 public static void Mat_to_vector_float(Mat m, List<Float> fs) { 291 if (fs == null) 292 throw new IllegalArgumentException("fs == null"); 293 int count = m.rows(); 294 if (CvType.CV_32FC1 != m.type() || m.cols() != 1) 295 throw new IllegalArgumentException( 296 "CvType.CV_32FC1 != m.type() || m.cols()!=1\n" + m); 297 298 fs.clear(); 299 float[] buff = new float[count]; 300 m.get(0, 0, buff); 301 for (int i = 0; i < count; i++) { 302 fs.add(buff[i]); 303 } 304 } 305 306 public static Mat vector_uchar_to_Mat(List<Byte> bs) { 307 Mat res; 308 int count = (bs != null) ? bs.size() : 0; 309 if (count > 0) { 310 res = new Mat(count, 1, CvType.CV_8UC1); 311 byte[] buff = new byte[count]; 312 for (int i = 0; i < count; i++) { 313 byte b = bs.get(i); 314 buff[i] = b; 315 } 316 res.put(0, 0, buff); 317 } else { 318 res = new Mat(); 319 } 320 return res; 321 } 322 323 public static void Mat_to_vector_uchar(Mat m, List<Byte> us) { 324 if (us == null) 325 throw new IllegalArgumentException("Output List can't be null"); 326 int count = m.rows(); 327 if (CvType.CV_8UC1 != m.type() || m.cols() != 1) 328 throw new IllegalArgumentException( 329 "CvType.CV_8UC1 != m.type() || m.cols()!=1\n" + m); 330 331 us.clear(); 332 byte[] buff = new byte[count]; 333 m.get(0, 0, buff); 334 for (int i = 0; i < count; i++) { 335 us.add(buff[i]); 336 } 337 } 338 339 public static Mat vector_char_to_Mat(List<Byte> bs) { 340 Mat res; 341 int count = (bs != null) ? bs.size() : 0; 342 if (count > 0) { 343 res = new Mat(count, 1, CvType.CV_8SC1); 344 byte[] buff = new byte[count]; 345 for (int i = 0; i < count; i++) { 346 byte b = bs.get(i); 347 buff[i] = b; 348 } 349 res.put(0, 0, buff); 350 } else { 351 res = new Mat(); 352 } 353 return res; 354 } 355 356 public static Mat vector_int_to_Mat(List<Integer> is) { 357 Mat res; 358 int count = (is != null) ? is.size() : 0; 359 if (count > 0) { 360 res = new Mat(count, 1, CvType.CV_32SC1); 361 int[] buff = new int[count]; 362 for (int i = 0; i < count; i++) { 363 int v = is.get(i); 364 buff[i] = v; 365 } 366 res.put(0, 0, buff); 367 } else { 368 res = new Mat(); 369 } 370 return res; 371 } 372 373 public static void Mat_to_vector_int(Mat m, List<Integer> is) { 374 if (is == null) 375 throw new IllegalArgumentException("is == null"); 376 int count = m.rows(); 377 if (CvType.CV_32SC1 != m.type() || m.cols() != 1) 378 throw new IllegalArgumentException( 379 "CvType.CV_32SC1 != m.type() || m.cols()!=1\n" + m); 380 381 is.clear(); 382 int[] buff = new int[count]; 383 m.get(0, 0, buff); 384 for (int i = 0; i < count; i++) { 385 is.add(buff[i]); 386 } 387 } 388 389 public static void Mat_to_vector_char(Mat m, List<Byte> bs) { 390 if (bs == null) 391 throw new IllegalArgumentException("Output List can't be null"); 392 int count = m.rows(); 393 if (CvType.CV_8SC1 != m.type() || m.cols() != 1) 394 throw new IllegalArgumentException( 395 "CvType.CV_8SC1 != m.type() || m.cols()!=1\n" + m); 396 397 bs.clear(); 398 byte[] buff = new byte[count]; 399 m.get(0, 0, buff); 400 for (int i = 0; i < count; i++) { 401 bs.add(buff[i]); 402 } 403 } 404 405 public static Mat vector_Rect_to_Mat(List<Rect> rs) { 406 Mat res; 407 int count = (rs != null) ? rs.size() : 0; 408 if (count > 0) { 409 res = new Mat(count, 1, CvType.CV_32SC4); 410 int[] buff = new int[4 * count]; 411 for (int i = 0; i < count; i++) { 412 Rect r = rs.get(i); 413 buff[4 * i] = r.x; 414 buff[4 * i + 1] = r.y; 415 buff[4 * i + 2] = r.width; 416 buff[4 * i + 3] = r.height; 417 } 418 res.put(0, 0, buff); 419 } else { 420 res = new Mat(); 421 } 422 return res; 423 } 424 425 public static void Mat_to_vector_Rect(Mat m, List<Rect> rs) { 426 if (rs == null) 427 throw new IllegalArgumentException("rs == null"); 428 int count = m.rows(); 429 if (CvType.CV_32SC4 != m.type() || m.cols() != 1) 430 throw new IllegalArgumentException( 431 "CvType.CV_32SC4 != m.type() || m.rows()!=1\n" + m); 432 433 rs.clear(); 434 int[] buff = new int[4 * count]; 435 m.get(0, 0, buff); 436 for (int i = 0; i < count; i++) { 437 rs.add(new Rect(buff[4 * i], buff[4 * i + 1], buff[4 * i + 2], buff[4 * i + 3])); 438 } 439 } 440 441 public static Mat vector_Rect2d_to_Mat(List<Rect2d> rs) { 442 Mat res; 443 int count = (rs != null) ? rs.size() : 0; 444 if (count > 0) { 445 res = new Mat(count, 1, CvType.CV_64FC4); 446 double[] buff = new double[4 * count]; 447 for (int i = 0; i < count; i++) { 448 Rect2d r = rs.get(i); 449 buff[4 * i] = r.x; 450 buff[4 * i + 1] = r.y; 451 buff[4 * i + 2] = r.width; 452 buff[4 * i + 3] = r.height; 453 } 454 res.put(0, 0, buff); 455 } else { 456 res = new Mat(); 457 } 458 return res; 459 } 460 461 public static void Mat_to_vector_Rect2d(Mat m, List<Rect2d> rs) { 462 if (rs == null) 463 throw new IllegalArgumentException("rs == null"); 464 int count = m.rows(); 465 if (CvType.CV_64FC4 != m.type() || m.cols() != 1) 466 throw new IllegalArgumentException( 467 "CvType.CV_64FC4 != m.type() || m.rows()!=1\n" + m); 468 469 rs.clear(); 470 double[] buff = new double[4 * count]; 471 m.get(0, 0, buff); 472 for (int i = 0; i < count; i++) { 473 rs.add(new Rect2d(buff[4 * i], buff[4 * i + 1], buff[4 * i + 2], buff[4 * i + 3])); 474 } 475 } 476 477 public static Mat vector_KeyPoint_to_Mat(List<KeyPoint> kps) { 478 Mat res; 479 int count = (kps != null) ? kps.size() : 0; 480 if (count > 0) { 481 res = new Mat(count, 1, CvType.CV_64FC(7)); 482 double[] buff = new double[count * 7]; 483 for (int i = 0; i < count; i++) { 484 KeyPoint kp = kps.get(i); 485 buff[7 * i] = kp.pt.x; 486 buff[7 * i + 1] = kp.pt.y; 487 buff[7 * i + 2] = kp.size; 488 buff[7 * i + 3] = kp.angle; 489 buff[7 * i + 4] = kp.response; 490 buff[7 * i + 5] = kp.octave; 491 buff[7 * i + 6] = kp.class_id; 492 } 493 res.put(0, 0, buff); 494 } else { 495 res = new Mat(); 496 } 497 return res; 498 } 499 500 public static void Mat_to_vector_KeyPoint(Mat m, List<KeyPoint> kps) { 501 if (kps == null) 502 throw new IllegalArgumentException("Output List can't be null"); 503 int count = m.rows(); 504 if (CvType.CV_64FC(7) != m.type() || m.cols() != 1) 505 throw new IllegalArgumentException( 506 "CvType.CV_64FC(7) != m.type() || m.cols()!=1\n" + m); 507 508 kps.clear(); 509 double[] buff = new double[7 * count]; 510 m.get(0, 0, buff); 511 for (int i = 0; i < count; i++) { 512 kps.add(new KeyPoint((float) buff[7 * i], (float) buff[7 * i + 1], (float) buff[7 * i + 2], (float) buff[7 * i + 3], 513 (float) buff[7 * i + 4], (int) buff[7 * i + 5], (int) buff[7 * i + 6])); 514 } 515 } 516 517 // vector_vector_Point 518 public static Mat vector_vector_Point_to_Mat(List<MatOfPoint> pts, List<Mat> mats) { 519 Mat res; 520 int lCount = (pts != null) ? pts.size() : 0; 521 if (lCount > 0) { 522 mats.addAll(pts); 523 res = vector_Mat_to_Mat(mats); 524 } else { 525 res = new Mat(); 526 } 527 return res; 528 } 529 530 public static void Mat_to_vector_vector_Point(Mat m, List<MatOfPoint> pts) { 531 if (pts == null) 532 throw new IllegalArgumentException("Output List can't be null"); 533 534 if (m == null) 535 throw new IllegalArgumentException("Input Mat can't be null"); 536 537 List<Mat> mats = new ArrayList<Mat>(m.rows()); 538 Mat_to_vector_Mat(m, mats); 539 for (Mat mi : mats) { 540 MatOfPoint pt = new MatOfPoint(mi); 541 pts.add(pt); 542 mi.release(); 543 } 544 mats.clear(); 545 } 546 547 // vector_vector_Point2f 548 public static void Mat_to_vector_vector_Point2f(Mat m, List<MatOfPoint2f> pts) { 549 if (pts == null) 550 throw new IllegalArgumentException("Output List can't be null"); 551 552 if (m == null) 553 throw new IllegalArgumentException("Input Mat can't be null"); 554 555 List<Mat> mats = new ArrayList<Mat>(m.rows()); 556 Mat_to_vector_Mat(m, mats); 557 for (Mat mi : mats) { 558 MatOfPoint2f pt = new MatOfPoint2f(mi); 559 pts.add(pt); 560 mi.release(); 561 } 562 mats.clear(); 563 } 564 565 // vector_vector_Point2f 566 public static Mat vector_vector_Point2f_to_Mat(List<MatOfPoint2f> pts, List<Mat> mats) { 567 Mat res; 568 int lCount = (pts != null) ? pts.size() : 0; 569 if (lCount > 0) { 570 mats.addAll(pts); 571 res = vector_Mat_to_Mat(mats); 572 } else { 573 res = new Mat(); 574 } 575 return res; 576 } 577 578 // vector_vector_Point3f 579 public static void Mat_to_vector_vector_Point3f(Mat m, List<MatOfPoint3f> pts) { 580 if (pts == null) 581 throw new IllegalArgumentException("Output List can't be null"); 582 583 if (m == null) 584 throw new IllegalArgumentException("Input Mat can't be null"); 585 586 List<Mat> mats = new ArrayList<Mat>(m.rows()); 587 Mat_to_vector_Mat(m, mats); 588 for (Mat mi : mats) { 589 MatOfPoint3f pt = new MatOfPoint3f(mi); 590 pts.add(pt); 591 mi.release(); 592 } 593 mats.clear(); 594 } 595 596 // vector_vector_Point3f 597 public static Mat vector_vector_Point3f_to_Mat(List<MatOfPoint3f> pts, List<Mat> mats) { 598 Mat res; 599 int lCount = (pts != null) ? pts.size() : 0; 600 if (lCount > 0) { 601 mats.addAll(pts); 602 res = vector_Mat_to_Mat(mats); 603 } else { 604 res = new Mat(); 605 } 606 return res; 607 } 608 609 // vector_vector_KeyPoint 610 public static Mat vector_vector_KeyPoint_to_Mat(List<MatOfKeyPoint> kps, List<Mat> mats) { 611 Mat res; 612 int lCount = (kps != null) ? kps.size() : 0; 613 if (lCount > 0) { 614 mats.addAll(kps); 615 res = vector_Mat_to_Mat(mats); 616 } else { 617 res = new Mat(); 618 } 619 return res; 620 } 621 622 public static void Mat_to_vector_vector_KeyPoint(Mat m, List<MatOfKeyPoint> kps) { 623 if (kps == null) 624 throw new IllegalArgumentException("Output List can't be null"); 625 626 if (m == null) 627 throw new IllegalArgumentException("Input Mat can't be null"); 628 629 List<Mat> mats = new ArrayList<Mat>(m.rows()); 630 Mat_to_vector_Mat(m, mats); 631 for (Mat mi : mats) { 632 MatOfKeyPoint vkp = new MatOfKeyPoint(mi); 633 kps.add(vkp); 634 mi.release(); 635 } 636 mats.clear(); 637 } 638 639 public static Mat vector_double_to_Mat(List<Double> ds) { 640 Mat res; 641 int count = (ds != null) ? ds.size() : 0; 642 if (count > 0) { 643 res = new Mat(count, 1, CvType.CV_64FC1); 644 double[] buff = new double[count]; 645 for (int i = 0; i < count; i++) { 646 double v = ds.get(i); 647 buff[i] = v; 648 } 649 res.put(0, 0, buff); 650 } else { 651 res = new Mat(); 652 } 653 return res; 654 } 655 656 public static void Mat_to_vector_double(Mat m, List<Double> ds) { 657 if (ds == null) 658 throw new IllegalArgumentException("ds == null"); 659 int count = m.rows(); 660 if (CvType.CV_64FC1 != m.type() || m.cols() != 1) 661 throw new IllegalArgumentException( 662 "CvType.CV_64FC1 != m.type() || m.cols()!=1\n" + m); 663 664 ds.clear(); 665 double[] buff = new double[count]; 666 m.get(0, 0, buff); 667 for (int i = 0; i < count; i++) { 668 ds.add(buff[i]); 669 } 670 } 671 672 public static Mat vector_DMatch_to_Mat(List<DMatch> matches) { 673 Mat res; 674 int count = (matches != null) ? matches.size() : 0; 675 if (count > 0) { 676 res = new Mat(count, 1, CvType.CV_64FC4); 677 double[] buff = new double[count * 4]; 678 for (int i = 0; i < count; i++) { 679 DMatch m = matches.get(i); 680 buff[4 * i] = m.queryIdx; 681 buff[4 * i + 1] = m.trainIdx; 682 buff[4 * i + 2] = m.imgIdx; 683 buff[4 * i + 3] = m.distance; 684 } 685 res.put(0, 0, buff); 686 } else { 687 res = new Mat(); 688 } 689 return res; 690 } 691 692 public static void Mat_to_vector_DMatch(Mat m, List<DMatch> matches) { 693 if (matches == null) 694 throw new IllegalArgumentException("Output List can't be null"); 695 int count = m.rows(); 696 if (CvType.CV_64FC4 != m.type() || m.cols() != 1) 697 throw new IllegalArgumentException( 698 "CvType.CV_64FC4 != m.type() || m.cols()!=1\n" + m); 699 700 matches.clear(); 701 double[] buff = new double[4 * count]; 702 m.get(0, 0, buff); 703 for (int i = 0; i < count; i++) { 704 matches.add(new DMatch((int) buff[4 * i], (int) buff[4 * i + 1], (int) buff[4 * i + 2], (float) buff[4 * i + 3])); 705 } 706 } 707 708 // vector_vector_DMatch 709 public static Mat vector_vector_DMatch_to_Mat(List<MatOfDMatch> lvdm, List<Mat> mats) { 710 Mat res; 711 int lCount = (lvdm != null) ? lvdm.size() : 0; 712 if (lCount > 0) { 713 mats.addAll(lvdm); 714 res = vector_Mat_to_Mat(mats); 715 } else { 716 res = new Mat(); 717 } 718 return res; 719 } 720 721 public static void Mat_to_vector_vector_DMatch(Mat m, List<MatOfDMatch> lvdm) { 722 if (lvdm == null) 723 throw new IllegalArgumentException("Output List can't be null"); 724 725 if (m == null) 726 throw new IllegalArgumentException("Input Mat can't be null"); 727 728 List<Mat> mats = new ArrayList<Mat>(m.rows()); 729 Mat_to_vector_Mat(m, mats); 730 lvdm.clear(); 731 for (Mat mi : mats) { 732 MatOfDMatch vdm = new MatOfDMatch(mi); 733 lvdm.add(vdm); 734 mi.release(); 735 } 736 mats.clear(); 737 } 738 739 // vector_vector_char 740 public static Mat vector_vector_char_to_Mat(List<MatOfByte> lvb, List<Mat> mats) { 741 Mat res; 742 int lCount = (lvb != null) ? lvb.size() : 0; 743 if (lCount > 0) { 744 mats.addAll(lvb); 745 res = vector_Mat_to_Mat(mats); 746 } else { 747 res = new Mat(); 748 } 749 return res; 750 } 751 752 public static void Mat_to_vector_vector_char(Mat m, List<List<Byte>> llb) { 753 if (llb == null) 754 throw new IllegalArgumentException("Output List can't be null"); 755 756 if (m == null) 757 throw new IllegalArgumentException("Input Mat can't be null"); 758 759 List<Mat> mats = new ArrayList<Mat>(m.rows()); 760 Mat_to_vector_Mat(m, mats); 761 for (Mat mi : mats) { 762 List<Byte> lb = new ArrayList<Byte>(); 763 Mat_to_vector_char(mi, lb); 764 llb.add(lb); 765 mi.release(); 766 } 767 mats.clear(); 768 } 769 770 public static Mat vector_RotatedRect_to_Mat(List<RotatedRect> rs) { 771 Mat res; 772 int count = (rs != null) ? rs.size() : 0; 773 if (count > 0) { 774 res = new Mat(count, 1, CvType.CV_32FC(5)); 775 float[] buff = new float[5 * count]; 776 for (int i = 0; i < count; i++) { 777 RotatedRect r = rs.get(i); 778 buff[5 * i] = (float)r.center.x; 779 buff[5 * i + 1] = (float)r.center.y; 780 buff[5 * i + 2] = (float)r.size.width; 781 buff[5 * i + 3] = (float)r.size.height; 782 buff[5 * i + 4] = (float)r.angle; 783 } 784 res.put(0, 0, buff); 785 } else { 786 res = new Mat(); 787 } 788 return res; 789 } 790 791 public static void Mat_to_vector_RotatedRect(Mat m, List<RotatedRect> rs) { 792 if (rs == null) 793 throw new IllegalArgumentException("rs == null"); 794 int count = m.rows(); 795 if (CvType.CV_32FC(5) != m.type() || m.cols() != 1) 796 throw new IllegalArgumentException( 797 "CvType.CV_32FC5 != m.type() || m.rows()!=1\n" + m); 798 799 rs.clear(); 800 float[] buff = new float[5 * count]; 801 m.get(0, 0, buff); 802 for (int i = 0; i < count; i++) { 803 rs.add(new RotatedRect(new Point(buff[5 * i], buff[5 * i + 1]), new Size(buff[5 * i + 2], buff[5 * i + 3]), buff[5 * i + 4])); 804 } 805 } 806}