1 package org.opencv.test.core; 2 3 import java.util.Arrays; 4 5 import org.opencv.core.Core; 6 import org.opencv.core.CvException; 7 import org.opencv.core.CvType; 8 import org.opencv.core.Mat; 9 import org.opencv.core.Point; 10 import org.opencv.core.Range; 11 import org.opencv.core.Rect; 12 import org.opencv.core.Scalar; 13 import org.opencv.core.Size; 14 import org.opencv.test.OpenCVTestCase; 15 16 public class MatTest extends OpenCVTestCase { 17 18 public void testAdjustROI() { 19 Mat roi = gray0.submat(3, 5, 7, 10); 20 Mat originalroi = roi.clone(); 21 22 Mat adjusted = roi.adjustROI(2, 2, 2, 2); 23 24 assertMatEqual(adjusted, roi); 25 assertSizeEquals(new Size(5, 6), adjusted.size(), EPS); 26 assertEquals(originalroi.type(), adjusted.type()); 27 assertTrue(adjusted.isSubmatrix()); 28 assertFalse(adjusted.isContinuous()); 29 30 Point offset = new Point(); 31 Size size = new Size(); 32 adjusted.locateROI(size, offset); 33 assertPointEquals(new Point(5, 1), offset, EPS); 34 assertSizeEquals(gray0.size(), size, EPS); 35 } 36 37 public void testAssignToMat() { 38 gray0.assignTo(dst); 39 40 assertMatEqual(gray0, dst); 41 42 gray255.assignTo(dst); 43 44 assertMatEqual(gray255, dst); 45 } 46 47 public void testAssignToMatInt() { 48 gray255.assignTo(dst, CvType.CV_32F); 49 50 assertMatEqual(gray255_32f, dst, EPS); 51 } 52 53 public void testChannels() { 54 assertEquals(1, gray0.channels()); 55 assertEquals(3, rgbLena.channels()); 56 assertEquals(4, rgba0.channels()); 57 } 58 59 public void testCheckVectorInt() { 60 // ! returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel 61 // (1 x N) or (N x 1); negative number otherwise 62 assertEquals(2, new Mat(2, 10, CvType.CV_8U).checkVector(10)); 63 assertEquals(2, new Mat(1, 2, CvType.CV_8UC(10)).checkVector(10)); 64 assertEquals(2, new Mat(2, 1, CvType.CV_8UC(10)).checkVector(10)); 65 assertEquals(10, new Mat(1, 10, CvType.CV_8UC2).checkVector(2)); 66 67 assertTrue(0 > new Mat().checkVector(0)); 68 assertTrue(0 > new Mat(10, 1, CvType.CV_8U).checkVector(10)); 69 assertTrue(0 > new Mat(10, 20, CvType.CV_8U).checkVector(10)); 70 } 71 72 public void testCheckVectorIntInt() { 73 assertEquals(2, new Mat(2, 10, CvType.CV_8U).checkVector(10, CvType.CV_8U)); 74 assertEquals(2, new Mat(1, 2, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8U)); 75 assertEquals(2, new Mat(2, 1, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8U)); 76 assertEquals(10, new Mat(1, 10, CvType.CV_8UC2).checkVector(2, CvType.CV_8U)); 77 78 assertTrue(0 > new Mat(2, 10, CvType.CV_8U).checkVector(10, CvType.CV_8S)); 79 assertTrue(0 > new Mat(1, 2, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8S)); 80 assertTrue(0 > new Mat(2, 1, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8S)); 81 assertTrue(0 > new Mat(1, 10, CvType.CV_8UC2).checkVector(10, CvType.CV_8S)); 82 } 83 84 public void testCheckVectorIntIntBoolean() { 85 Mat mm = new Mat(5, 1, CvType.CV_8UC(10)); 86 Mat roi = new Mat(5, 3, CvType.CV_8UC(10)).submat(1, 3, 2, 3); 87 88 assertEquals(5, mm.checkVector(10, CvType.CV_8U, true)); 89 assertEquals(5, mm.checkVector(10, CvType.CV_8U, false)); 90 assertEquals(2, roi.checkVector(10, CvType.CV_8U, false)); 91 assertTrue(0 > roi.checkVector(10, CvType.CV_8U, true)); 92 } 93 94 public void testClone() { 95 dst = gray0.clone(); 96 assertMatEqual(gray0, dst); 97 assertFalse(gray0.getNativeObjAddr() == dst.getNativeObjAddr()); 98 assertFalse(gray0.dataAddr() == dst.dataAddr()); 99 } 100 101 public void testCol() { 102 Mat col = gray0.col(0); 103 assertEquals(1, col.cols()); 104 assertEquals(gray0.rows(), col.rows()); 105 } 106 107 public void testColRangeIntInt() { 108 Mat cols = gray0.colRange(0, gray0.cols() / 2); 109 110 assertEquals(gray0.cols() / 2, cols.cols()); 111 assertEquals(gray0.rows(), cols.rows()); 112 } 113 114 public void testColRangeRange() { 115 Range range = new Range(0, 5); 116 dst = gray0.colRange(range); 117 118 truth = new Mat(10, 5, CvType.CV_8UC1, new Scalar(0.0)); 119 assertMatEqual(truth, dst); 120 } 121 122 public void testCols() { 123 assertEquals(matSize, gray0.cols()); 124 } 125 126 public void testConvertToMatInt() { 127 gray255.convertTo(dst, CvType.CV_32F); 128 129 truth = new Mat(matSize, matSize, CvType.CV_32F, new Scalar(255)); 130 assertMatEqual(truth, dst, EPS); 131 } 132 133 public void testConvertToMatIntDouble() { 134 gray2.convertTo(dst, CvType.CV_16U, 2.0); 135 136 truth = new Mat(matSize, matSize, CvType.CV_16U, new Scalar(4)); 137 assertMatEqual(truth, dst); 138 } 139 140 public void testConvertToMatIntDoubleDouble() { 141 gray0_32f.convertTo(dst, CvType.CV_8U, 2.0, 4.0); 142 143 truth = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(4)); 144 assertMatEqual(truth, dst); 145 } 146 147 public void testCopyToMat() { 148 rgbLena.copyTo(dst); 149 assertMatEqual(rgbLena, dst); 150 } 151 152 public void testCopyToMatMat() { 153 Mat src = new Mat(4, 4, CvType.CV_8U, new Scalar(5)); 154 Mat mask = makeMask(src.clone()); 155 156 src.copyTo(dst, mask); 157 158 truth = new Mat(4, 4, CvType.CV_8U) { 159 { 160 put(0, 0, 0, 0, 5, 5); 161 put(1, 0, 0, 0, 5, 5); 162 put(2, 0, 0, 0, 5, 5); 163 put(3, 0, 0, 0, 5, 5); 164 } 165 }; 166 assertMatEqual(truth, dst); 167 168 } 169 170 public void testCreateIntIntInt() { 171 gray255.create(4, 5, CvType.CV_32F); 172 173 assertEquals(4, gray255.rows()); 174 assertEquals(5, gray255.cols()); 175 assertEquals(CvType.CV_32F, gray255.type()); 176 } 177 178 public void testCreateSizeInt() { 179 Size size = new Size(5, 5); 180 dst.create(size, CvType.CV_16U); 181 182 assertEquals(5, dst.rows()); 183 assertEquals(5, dst.cols()); 184 assertEquals(CvType.CV_16U, dst.type()); 185 } 186 187 public void testCross() { 188 Mat answer = new Mat(1, 3, CvType.CV_32F); 189 answer.put(0, 0, 7.0, 1.0, -5.0); 190 191 Mat cross = v1.cross(v2); 192 assertMatEqual(answer, cross, EPS); 193 } 194 195 public void testDataAddr() { 196 assertTrue(0 != gray0.dataAddr()); 197 assertEquals(0, new Mat().dataAddr()); 198 } 199 200 public void testDepth() { 201 assertEquals(CvType.CV_8U, gray0.depth()); 202 assertEquals(CvType.CV_32F, gray0_32f.depth()); 203 } 204 205 public void testDiag() { 206 dst = gray0.diag(); 207 208 truth = new Mat(10, 1, CvType.CV_8UC1, new Scalar(0)); 209 assertMatEqual(truth, dst); 210 } 211 212 public void testDiagInt() { 213 dst = gray255.diag(2); 214 215 truth = new Mat(8, 1, CvType.CV_8UC1, new Scalar(255)); 216 assertMatEqual(truth, dst); 217 } 218 219 public void testDiagMat() { 220 Mat diagVector = new Mat(matSize, 1, CvType.CV_32F, new Scalar(1)); 221 222 dst = Mat.diag(diagVector); 223 224 assertMatEqual(grayE_32f, dst, EPS); 225 } 226 227 public void testDiagMat_sqrMatrix() { 228 try { 229 dst = Mat.diag(gray255); 230 } catch (CvException e) { 231 // expected 232 } 233 } 234 235 public void testDot() { 236 double s = v1.dot(v2); 237 assertEquals(11.0, s); 238 } 239 240 public void testDump() { 241 assertEquals("[1, 3, 2]", v1.dump()); 242 } 243 244 public void testElemSize() { 245 assertEquals(Byte.SIZE / 8 * gray0.channels(), gray0.elemSize()); 246 assertEquals(Float.SIZE / 8 * gray0_32f.channels(), gray0_32f.elemSize()); 247 assertEquals(Byte.SIZE / 8 * rgbLena.channels(), rgbLena.elemSize()); 248 } 249 250 public void testElemSize1() { 251 assertEquals(Byte.SIZE / 8, gray255.elemSize1()); 252 assertEquals(Double.SIZE / 8, gray0_64f.elemSize1()); 253 assertEquals(Byte.SIZE / 8, rgbLena.elemSize1()); 254 } 255 256 public void testEmpty() { 257 assertTrue(dst.empty()); 258 assertTrue(!gray0.empty()); 259 } 260 261 public void testEyeIntIntInt() { 262 Mat eye = Mat.eye(3, 3, CvType.CV_32FC1); 263 264 assertMatEqual(eye, eye.inv(), EPS); 265 } 266 267 public void testEyeSizeInt() { 268 Size size = new Size(5, 5); 269 270 Mat eye = Mat.eye(size, CvType.CV_32S); 271 272 assertEquals(5, Core.countNonZero(eye)); 273 274 } 275 276 public Mat getTestMat(int size, int type) { 277 Mat m = new Mat(size, size, type); 278 final int ch = CvType.channels(type); 279 double buff[] = new double[size*size * ch]; 280 for(int i=0; i<size; i++) 281 for(int j=0; j<size; j++) 282 for(int k=0; k<ch; k++) { 283 buff[i*size*ch + j*ch + k] = 100*i + 10*j + k; 284 } 285 m.put(0, 0, buff); 286 return m; 287 } 288 289 public void testGetIntInt_8U() { 290 Mat m = getTestMat(5, CvType.CV_8UC2); 291 292 // whole Mat 293 assertTrue(Arrays.equals(new double[] {0, 1}, m.get(0, 0))); 294 assertTrue(Arrays.equals(new double[] {240, 241}, m.get(2, 4))); 295 assertTrue(Arrays.equals(new double[] {255, 255}, m.get(4, 4))); 296 297 // sub-Mat 298 Mat sm = m.submat(2, 4, 3, 5); 299 assertTrue(Arrays.equals(new double[] {230, 231}, sm.get(0, 0))); 300 assertTrue(Arrays.equals(new double[] {255, 255}, sm.get(1, 1))); 301 } 302 303 public void testGetIntInt_32S() { 304 Mat m = getTestMat(5, CvType.CV_32SC3); 305 306 // whole Mat 307 assertTrue(Arrays.equals(new double[] {0, 1, 2}, m.get(0, 0))); 308 assertTrue(Arrays.equals(new double[] {240, 241, 242}, m.get(2, 4))); 309 assertTrue(Arrays.equals(new double[] {440, 441, 442}, m.get(4, 4))); 310 311 // sub-Mat 312 Mat sm = m.submat(2, 4, 3, 5); 313 assertTrue(Arrays.equals(new double[] {230, 231, 232}, sm.get(0, 0))); 314 assertTrue(Arrays.equals(new double[] {340, 341, 342}, sm.get(1, 1))); 315 } 316 317 public void testGetIntInt_64F() { 318 Mat m = getTestMat(5, CvType.CV_64FC1); 319 320 // whole Mat 321 assertTrue(Arrays.equals(new double[] {0}, m.get(0, 0))); 322 assertTrue(Arrays.equals(new double[] {240}, m.get(2, 4))); 323 assertTrue(Arrays.equals(new double[] {440}, m.get(4, 4))); 324 325 // sub-Mat 326 Mat sm = m.submat(2, 4, 3, 5); 327 assertTrue(Arrays.equals(new double[] {230}, sm.get(0, 0))); 328 assertTrue(Arrays.equals(new double[] {340}, sm.get(1, 1))); 329 } 330 331 public void testGetIntIntByteArray() { 332 Mat m = getTestMat(5, CvType.CV_8UC3); 333 byte[] goodData = new byte[9]; 334 byte[] badData = new byte[7]; 335 336 // whole Mat 337 int bytesNum = m.get(1, 1, goodData); 338 339 assertEquals(9, bytesNum); 340 assertTrue(Arrays.equals(new byte[] { 110, 111, 112, 120, 121, 122, (byte) 130, (byte) 131, (byte) 132 }, goodData)); 341 342 try { 343 m.get(2, 2, badData); 344 fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)"); 345 } catch (UnsupportedOperationException e) { 346 // expected 347 } 348 349 // sub-Mat 350 Mat sm = m.submat(2, 4, 3, 5); 351 byte buff00[] = new byte[3]; 352 bytesNum = sm.get(0, 0, buff00); 353 assertEquals(3, bytesNum); 354 assertTrue(Arrays.equals(new byte[] {(byte) 230, (byte) 231, (byte) 232}, buff00)); 355 byte buff11[] = new byte[3]; 356 bytesNum = sm.get(1, 1, buff11); 357 assertEquals(3, bytesNum); 358 assertTrue(Arrays.equals(new byte[] {(byte) 255, (byte) 255, (byte) 255}, buff11)); 359 } 360 361 public void testGetIntIntDoubleArray() { 362 Mat m = getTestMat(5, CvType.CV_64F); 363 double buff[] = new double[4]; 364 365 // whole Mat 366 int bytesNum = m.get(1, 1, buff); 367 368 assertEquals(32, bytesNum); 369 assertTrue(Arrays.equals(new double[] { 110, 120, 130, 140 }, buff)); 370 371 // sub-Mat 372 Mat sm = m.submat(2, 4, 3, 5); 373 double buff00[] = new double[2]; 374 bytesNum = sm.get(0, 0, buff00); 375 assertEquals(16, bytesNum); 376 assertTrue(Arrays.equals(new double[] {230, 240}, buff00)); 377 double buff11[] = new double[] {0, 0}; 378 bytesNum = sm.get(1, 1, buff11); 379 assertEquals(8, bytesNum); 380 assertTrue(Arrays.equals(new double[] {340, 0}, buff11)); 381 } 382 383 public void testGetIntIntFloatArray() { 384 Mat m = getTestMat(5, CvType.CV_32F); 385 float buff[] = new float[4]; 386 387 // whole Mat 388 int bytesNum = m.get(1, 1, buff); 389 390 assertEquals(16, bytesNum); 391 assertTrue(Arrays.equals(new float[] { 110, 120, 130, 140 }, buff)); 392 393 // sub-Mat 394 Mat sm = m.submat(2, 4, 3, 5); 395 float buff00[] = new float[2]; 396 bytesNum = sm.get(0, 0, buff00); 397 assertEquals(8, bytesNum); 398 assertTrue(Arrays.equals(new float[] {230, 240}, buff00)); 399 float buff11[] = new float[] {0, 0}; 400 bytesNum = sm.get(1, 1, buff11); 401 assertEquals(4, bytesNum); 402 assertTrue(Arrays.equals(new float[] {340, 0}, buff11)); 403 } 404 405 public void testGetIntIntIntArray() { 406 Mat m = getTestMat(5, CvType.CV_32SC2); 407 int[] buff = new int[6]; 408 409 // whole Mat 410 int bytesNum = m.get(1, 1, buff); 411 412 assertEquals(24, bytesNum); 413 assertTrue(Arrays.equals(new int[] { 110, 111, 120, 121, 130, 131 }, buff)); 414 415 // sub-Mat 416 Mat sm = m.submat(2, 4, 3, 5); 417 int buff00[] = new int[4]; 418 bytesNum = sm.get(0, 0, buff00); 419 assertEquals(16, bytesNum); 420 assertTrue(Arrays.equals(new int[] {230, 231, 240, 241}, buff00)); 421 int buff11[] = new int[]{0, 0, 0, 0}; 422 bytesNum = sm.get(1, 1, buff11); 423 assertEquals(8, bytesNum); 424 assertTrue(Arrays.equals(new int[] {340, 341, 0, 0}, buff11)); 425 } 426 427 public void testGetIntIntShortArray() { 428 Mat m = getTestMat(5, CvType.CV_16SC2); 429 short[] buff = new short[6]; 430 431 // whole Mat 432 int bytesNum = m.get(1, 1, buff); 433 434 assertEquals(12, bytesNum); 435 assertTrue(Arrays.equals(new short[] { 110, 111, 120, 121, 130, 131 }, buff)); 436 437 // sub-Mat 438 Mat sm = m.submat(2, 4, 3, 5); 439 short buff00[] = new short[4]; 440 bytesNum = sm.get(0, 0, buff00); 441 assertEquals(8, bytesNum); 442 assertTrue(Arrays.equals(new short[] {230, 231, 240, 241}, buff00)); 443 short buff11[] = new short[]{0, 0, 0, 0}; 444 bytesNum = sm.get(1, 1, buff11); 445 assertEquals(4, bytesNum); 446 assertTrue(Arrays.equals(new short[] {340, 341, 0, 0}, buff11)); 447 } 448 449 public void testGetNativeObjAddr() { 450 assertTrue(0 != gray0.getNativeObjAddr()); 451 } 452 453 public void testHeight() { 454 assertEquals(gray0.rows(), gray0.height()); 455 assertEquals(rgbLena.rows(), rgbLena.height()); 456 assertEquals(rgba128.rows(), rgba128.height()); 457 } 458 459 public void testInv() { 460 dst = grayE_32f.inv(); 461 assertMatEqual(grayE_32f, dst, EPS); 462 } 463 464 public void testInvInt() { 465 Mat src = new Mat(2, 2, CvType.CV_32F) { 466 { 467 put(0, 0, 1.0); 468 put(0, 1, 2.0); 469 put(1, 0, 1.5); 470 put(1, 1, 4.0); 471 } 472 }; 473 474 dst = src.inv(Core.DECOMP_CHOLESKY); 475 476 truth = new Mat(2, 2, CvType.CV_32F) { 477 { 478 put(0, 0, 4.0); 479 put(0, 1, -2.0); 480 put(1, 0, -1.5); 481 put(1, 1, 1.0); 482 } 483 }; 484 485 assertMatEqual(truth, dst, EPS); 486 } 487 488 public void testIsContinuous() { 489 assertTrue(gray0.isContinuous()); 490 491 Mat subMat = gray0.submat(0, 0, gray0.rows() / 2, gray0.cols() / 2); 492 assertFalse(subMat.isContinuous()); 493 } 494 495 public void testIsSubmatrix() { 496 assertFalse(gray0.isSubmatrix()); 497 Mat subMat = gray0.submat(0, 0, gray0.rows() / 2, gray0.cols() / 2); 498 assertTrue(subMat.isSubmatrix()); 499 } 500 501 public void testLocateROI() { 502 Mat roi = gray0.submat(3, 5, 7, 10); 503 Point offset = new Point(); 504 Size size = new Size(); 505 506 roi.locateROI(size, offset); 507 508 assertPointEquals(new Point(7, 3), offset, EPS); 509 assertSizeEquals(new Size(10, 10), size, EPS); 510 } 511 512 public void testMat() { 513 Mat m = new Mat(); 514 assertNotNull(m); 515 assertTrue(m.empty()); 516 } 517 518 public void testMatIntIntCvType() { 519 Mat gray = new Mat(1, 1, CvType.CV_8UC1); 520 assertFalse(gray.empty()); 521 522 Mat rgb = new Mat(1, 1, CvType.CV_8UC3); 523 assertFalse(rgb.empty()); 524 } 525 526 public void testMatIntIntCvTypeScalar() { 527 dst = new Mat(gray127.rows(), gray127.cols(), CvType.CV_8U, new Scalar(127)); 528 assertFalse(dst.empty()); 529 assertMatEqual(dst, gray127); 530 531 dst = new Mat(rgba128.rows(), rgba128.cols(), CvType.CV_8UC4, Scalar.all(128)); 532 assertFalse(dst.empty()); 533 assertMatEqual(dst, rgba128); 534 } 535 536 public void testMatIntIntInt() { 537 Mat gray = new Mat(1, 1, CvType.CV_8U); 538 assertFalse(gray.empty()); 539 540 Mat rgb = new Mat(1, 1, CvType.CV_8U); 541 assertFalse(rgb.empty()); 542 } 543 544 public void testMatIntIntIntScalar() { 545 Mat m1 = new Mat(gray127.rows(), gray127.cols(), CvType.CV_8U, new Scalar(127)); 546 assertFalse(m1.empty()); 547 assertMatEqual(m1, gray127); 548 549 Mat m2 = new Mat(gray0_32f.rows(), gray0_32f.cols(), CvType.CV_32F, new Scalar(0)); 550 assertFalse(m2.empty()); 551 assertMatEqual(m2, gray0_32f, EPS); 552 } 553 554 public void testMatMatRange() { 555 dst = new Mat(gray0, new Range(0, 5)); 556 557 truth = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0)); 558 assertFalse(dst.empty()); 559 assertMatEqual(truth, dst); 560 } 561 562 public void testMatMatRangeRange() { 563 dst = new Mat(gray255_32f, new Range(0, 5), new Range(0, 5)); 564 565 truth = new Mat(5, 5, CvType.CV_32FC1, new Scalar(255)); 566 567 assertFalse(dst.empty()); 568 assertMatEqual(truth, dst, EPS); 569 } 570 571 public void testMatMatRect() { 572 Mat m = new Mat(7, 6, CvType.CV_32SC1); 573 m.put(0, 0, 574 0, 1, 2, 3, 4, 5, 575 10, 11, 12, 13, 14, 15, 576 20, 21, 22, 23, 24, 25, 577 30, 31, 32, 33, 34, 35, 578 40, 41, 42, 43, 44, 45, 579 50, 51, 52, 53, 54, 55, 580 60, 61, 62, 63, 64, 65 ); 581 582 dst = new Mat(m, new Rect(1, 2, 3, 4)); 583 584 truth = new Mat(4, 3, CvType.CV_32SC1); 585 truth.put(0, 0, 586 21, 22, 23, 587 31, 32, 33, 588 41, 42, 43, 589 51, 52, 53 ); 590 591 assertFalse(dst.empty()); 592 assertMatEqual(truth, dst); 593 } 594 595 public void testMatSizeInt() { 596 dst = new Mat(new Size(10, 10), CvType.CV_8U); 597 598 assertFalse(dst.empty()); 599 } 600 601 public void testMatSizeIntScalar() { 602 dst = new Mat(new Size(10, 10), CvType.CV_32F, new Scalar(255)); 603 604 assertFalse(dst.empty()); 605 assertMatEqual(gray255_32f, dst, EPS); 606 } 607 608 public void testMulMat() { 609 assertMatEqual(gray0, gray0.mul(gray255)); 610 611 Mat m1 = new Mat(2, 2, CvType.CV_32F, new Scalar(2)); 612 Mat m2 = new Mat(2, 2, CvType.CV_32F, new Scalar(3)); 613 614 dst = m1.mul(m2); 615 616 truth = new Mat(2, 2, CvType.CV_32F, new Scalar(6)); 617 assertMatEqual(truth, dst, EPS); 618 619 } 620 621 public void testMulMatDouble() { 622 Mat m1 = new Mat(2, 2, CvType.CV_32F, new Scalar(2)); 623 Mat m2 = new Mat(2, 2, CvType.CV_32F, new Scalar(3)); 624 625 dst = m1.mul(m2, 3.0); 626 627 truth = new Mat(2, 2, CvType.CV_32F, new Scalar(18)); 628 assertMatEqual(truth, dst, EPS); 629 } 630 631 public void testOnesIntIntInt() { 632 dst = Mat.ones(matSize, matSize, CvType.CV_32F); 633 634 truth = new Mat(matSize, matSize, CvType.CV_32F, new Scalar(1)); 635 assertMatEqual(truth, dst, EPS); 636 } 637 638 public void testOnesSizeInt() { 639 dst = Mat.ones(new Size(2, 2), CvType.CV_16S); 640 truth = new Mat(2, 2, CvType.CV_16S, new Scalar(1)); 641 assertMatEqual(truth, dst); 642 } 643 644 public void testPush_back() { 645 Mat m1 = new Mat(2, 4, CvType.CV_32F, new Scalar(2)); 646 Mat m2 = new Mat(3, 4, CvType.CV_32F, new Scalar(3)); 647 648 m1.push_back(m2); 649 650 truth = new Mat(5, 4, CvType.CV_32FC1) { 651 { 652 put(0, 0, 2, 2, 2, 2); 653 put(1, 0, 2, 2, 2, 2); 654 put(2, 0, 3, 3, 3, 3); 655 put(3, 0, 3, 3, 3, 3); 656 put(4, 0, 3, 3, 3, 3); 657 } 658 }; 659 660 assertMatEqual(truth, m1, EPS); 661 } 662 663 public void testPutIntIntByteArray() { 664 Mat m = new Mat(5, 5, CvType.CV_8UC3, new Scalar(1, 2, 3)); 665 Mat sm = m.submat(2, 4, 3, 5); 666 byte[] buff = new byte[] { 0, 0, 0, 0, 0, 0 }; 667 byte[] buff0 = new byte[] { 10, 20, 30, 40, 50, 60 }; 668 byte[] buff1 = new byte[] { -1, -2, -3, -4, -5, -6 }; 669 670 int bytesNum = m.put(1, 2, buff0); 671 672 assertEquals(6, bytesNum); 673 bytesNum = m.get(1, 2, buff); 674 assertEquals(6, bytesNum); 675 assertTrue(Arrays.equals(buff, buff0)); 676 677 bytesNum = sm.put(0, 0, buff1); 678 679 assertEquals(6, bytesNum); 680 bytesNum = sm.get(0, 0, buff); 681 assertEquals(6, bytesNum); 682 assertTrue(Arrays.equals(buff, buff1)); 683 bytesNum = m.get(2, 3, buff); 684 assertEquals(6, bytesNum); 685 assertTrue(Arrays.equals(buff, buff1)); 686 687 Mat m1 = m.row(1); 688 bytesNum = m1.get(0, 2, buff); 689 assertEquals(6, bytesNum); 690 assertTrue(Arrays.equals(buff, buff0)); 691 692 try { 693 byte[] bytes2 = new byte[] { 10, 20, 30, 40, 50 }; 694 m.put(2, 2, bytes2); 695 fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)"); 696 } catch (UnsupportedOperationException e) { 697 // expected 698 } 699 } 700 701 public void testPutIntIntDoubleArray() { 702 Mat m = new Mat(5, 5, CvType.CV_8UC3, new Scalar(1, 2, 3)); 703 Mat sm = m.submat(2, 4, 3, 5); 704 byte[] buff = new byte[] { 0, 0, 0, 0, 0, 0 }; 705 706 int bytesNum = m.put(1, 2, 10, 20, 30, 40, 50, 60); 707 708 assertEquals(6, bytesNum); 709 bytesNum = m.get(1, 2, buff); 710 assertEquals(6, bytesNum); 711 assertTrue(Arrays.equals(buff, new byte[]{10, 20, 30, 40, 50, 60})); 712 713 bytesNum = sm.put(0, 0, 255, 254, 253, 252, 251, 250); 714 715 assertEquals(6, bytesNum); 716 bytesNum = sm.get(0, 0, buff); 717 assertEquals(6, bytesNum); 718 assertTrue(Arrays.equals(buff, new byte[]{-1, -2, -3, -4, -5, -6})); 719 bytesNum = m.get(2, 3, buff); 720 assertEquals(6, bytesNum); 721 assertTrue(Arrays.equals(buff, new byte[]{-1, -2, -3, -4, -5, -6})); 722 } 723 724 public void testPutIntIntFloatArray() { 725 Mat m = new Mat(5, 5, CvType.CV_32FC3, new Scalar(1, 2, 3)); 726 float[] elements = new float[] { 10, 20, 30, 40, 50, 60 }; 727 728 int bytesNum = m.put(4, 3, elements); 729 730 assertEquals(elements.length * 4, bytesNum); 731 Mat m1 = m.row(4); 732 float buff[] = new float[3]; 733 bytesNum = m1.get(0, 4, buff); 734 assertEquals(buff.length * 4, bytesNum); 735 assertTrue(Arrays.equals(new float[]{40, 50, 60}, buff)); 736 assertArrayEquals(new double[]{10, 20, 30}, m.get(4, 3), EPS); 737 738 try { 739 float[] elements2 = new float[] { 10, 20, 30, 40, 50 }; 740 m.put(2, 2, elements2); 741 fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)"); 742 } catch (UnsupportedOperationException e) { 743 // expected 744 } 745 } 746 747 public void testPutIntIntIntArray() { 748 Mat m = new Mat(5, 5, CvType.CV_32SC3, new Scalar(-1, -2, -3)); 749 int[] elements = new int[] { 10, 20, 30, 40, 50, 60 }; 750 751 int bytesNum = m.put(0, 4, elements); 752 753 assertEquals(elements.length * 4, bytesNum); 754 Mat m1 = m.col(4); 755 int buff[] = new int[3]; 756 bytesNum = m1.get(0, 0, buff); 757 assertEquals(buff.length * 4, bytesNum); 758 assertTrue(Arrays.equals(new int[]{10, 20, 30}, buff)); 759 assertArrayEquals(new double[]{40, 50, 60}, m.get(1, 0), EPS); 760 761 try { 762 int[] elements2 = new int[] { 10, 20, 30, 40, 50 }; 763 m.put(2, 2, elements2); 764 fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)"); 765 } catch (UnsupportedOperationException e) { 766 // expected 767 } 768 } 769 770 public void testPutIntIntShortArray() { 771 Mat m = new Mat(5, 5, CvType.CV_16SC3, new Scalar(-1, -2, -3)); 772 short[] elements = new short[] { 10, 20, 30, 40, 50, 60 }; 773 774 int bytesNum = m.put(2, 3, elements); 775 776 assertEquals(elements.length * 2, bytesNum); 777 Mat m1 = m.col(3); 778 short buff[] = new short[3]; 779 bytesNum = m1.get(2, 0, buff); 780 assertTrue(Arrays.equals(new short[]{10, 20, 30}, buff)); 781 assertArrayEquals(new double[]{40, 50, 60}, m.get(2, 4), EPS); 782 783 try { 784 short[] elements2 = new short[] { 10, 20, 30, 40, 50 }; 785 m.put(2, 2, elements2); 786 fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)"); 787 } catch (UnsupportedOperationException e) { 788 // expected 789 } 790 } 791 792 public void testRelease() { 793 assertFalse(gray0.empty()); 794 assertTrue(gray0.rows() > 0); 795 796 gray0.release(); 797 798 assertTrue(gray0.empty()); 799 assertEquals(0, gray0.rows()); 800 assertEquals(0, gray0.dataAddr()); 801 } 802 803 public void testReshapeInt() { 804 Mat src = new Mat(4, 4, CvType.CV_8U, new Scalar(0)); 805 dst = src.reshape(4); 806 807 truth = new Mat(4, 1, CvType.CV_8UC4, new Scalar(0)); 808 assertMatEqual(truth, dst); 809 } 810 811 public void testReshapeIntInt() { 812 Mat src = new Mat(5, 7, CvType.CV_8U, new Scalar(0)); 813 dst = src.reshape(7, 5); 814 815 truth = new Mat(5, 1, CvType.CV_8UC(7), new Scalar(0)); 816 assertMatEqual(truth, dst); 817 } 818 819 public void testRow() { 820 Mat row = gray0.row(0); 821 assertEquals(1, row.rows()); 822 assertEquals(gray0.cols(), row.cols()); 823 } 824 825 public void testRowRangeIntInt() { 826 Mat rows = gray0.rowRange(0, gray0.rows() / 2); 827 assertEquals(gray0.rows() / 2, rows.rows()); 828 assertEquals(gray0.cols(), rows.cols()); 829 } 830 831 public void testRowRangeRange() { 832 Mat rows = gray255.rowRange(new Range(0, 5)); 833 834 assertEquals(gray255.rows() / 2, rows.rows()); 835 assertEquals(gray255.cols(), rows.cols()); 836 } 837 838 public void testRows() { 839 assertEquals(matSize, gray0.rows()); 840 } 841 842 public void testSetToMat() { 843 Mat vals = new Mat(7, 1, CvType.CV_8U) { 844 { 845 put(0, 0, 1, 2, 3, 4, 5, 6, 7); 846 } 847 }; 848 Mat dst = new Mat(1, 1, CvType.CV_8UC(7)); 849 850 dst.setTo(vals); 851 852 Mat truth = new Mat(1, 1, CvType.CV_8UC(7)) { 853 { 854 put(0, 0, 1, 2, 3, 4, 5, 6, 7); 855 } 856 }; 857 assertMatEqual(truth, dst); 858 } 859 860 public void testSetToMatMat() { 861 Mat vals = new Mat(7, 1, CvType.CV_8U) { 862 { 863 put(0, 0, 1, 2, 3, 4, 5, 6, 7); 864 } 865 }; 866 Mat dst = Mat.zeros(2, 1, CvType.CV_8UC(7)); 867 Mat mask = new Mat(2, 1, CvType.CV_8U) { 868 { 869 put(0, 0, 0, 1); 870 } 871 }; 872 873 dst.setTo(vals, mask); 874 875 Mat truth = new Mat(2, 1, CvType.CV_8UC(7)) { 876 { 877 put(0, 0, 0, 0, 0, 0, 0, 0, 0); 878 put(1, 0, 1, 2, 3, 4, 5, 6, 7); 879 } 880 }; 881 assertMatEqual(truth, dst); 882 } 883 884 public void testSetToScalar() { 885 gray0.setTo(new Scalar(127)); 886 assertMatEqual(gray127, gray0); 887 } 888 889 public void testSetToScalarMask() { 890 Mat mask = gray0.clone(); 891 mask.put(1, 1, 1, 2, 3); 892 gray0.setTo(new Scalar(1), mask); 893 assertEquals(3, Core.countNonZero(gray0)); 894 Core.subtract(gray0, mask, gray0); 895 assertEquals(0, Core.countNonZero(gray0)); 896 } 897 898 public void testSize() { 899 assertEquals(new Size(matSize, matSize), gray0.size()); 900 901 assertEquals(new Size(3, 1), v1.size()); 902 } 903 904 public void testStep1() { 905 assertEquals(matSize * CvType.channels(CvType.CV_8U), gray0.step1()); 906 907 assertEquals(3, v2.step1()); 908 } 909 910 public void testStep1Int() { 911 Mat roi = rgba0.submat(3, 5, 7, 10); 912 Mat m = roi.clone(); 913 914 assertTrue(rgba0.channels() * rgba0.cols() <= roi.step1(0)); 915 assertEquals(rgba0.channels(), roi.step1(1)); 916 assertTrue(m.channels() * (10 - 7) <= m.step1(0)); 917 assertEquals(m.channels(), m.step1(1)); 918 } 919 920 public void testSubmatIntIntIntInt() { 921 Mat submat = gray0.submat(0, gray0.rows() / 2, 0, gray0.cols() / 2); 922 923 assertTrue(submat.isSubmatrix()); 924 assertFalse(submat.isContinuous()); 925 assertEquals(gray0.rows() / 2, submat.rows()); 926 assertEquals(gray0.cols() / 2, submat.cols()); 927 } 928 929 public void testSubmatRangeRange() { 930 Mat submat = gray255.submat(new Range(2, 4), new Range(2, 4)); 931 assertTrue(submat.isSubmatrix()); 932 assertFalse(submat.isContinuous()); 933 934 assertEquals(2, submat.rows()); 935 assertEquals(2, submat.cols()); 936 } 937 938 public void testSubmatRect() { 939 Mat submat = gray255.submat(new Rect(5, gray255.rows() / 2, 5, gray255.cols() / 2)); 940 assertTrue(submat.isSubmatrix()); 941 assertFalse(submat.isContinuous()); 942 943 assertEquals(gray255.rows() / 2, submat.rows()); 944 assertEquals(gray255.cols() / 2, submat.cols()); 945 } 946 947 public void testT() { 948 assertMatEqual(gray255, gray255.t()); 949 950 Mat src = new Mat(3, 3, CvType.CV_16U) { 951 { 952 put(0, 0, 1, 2, 4); 953 put(1, 0, 7, 5, 0); 954 put(2, 0, 3, 4, 6); 955 } 956 }; 957 958 dst = src.t(); 959 960 truth = new Mat(3, 3, CvType.CV_16U) { 961 { 962 put(0, 0, 1, 7, 3); 963 put(1, 0, 2, 5, 4); 964 put(2, 0, 4, 0, 6); 965 } 966 }; 967 assertMatEqual(truth, dst); 968 } 969 970 public void testToString() { 971 assertTrue(null != gray0.toString()); 972 } 973 974 public void testTotal() { 975 int nElements = gray0.rows() * gray0.cols(); 976 assertEquals(nElements, gray0.total()); 977 } 978 979 public void testType() { 980 assertEquals(CvType.CV_8UC1, gray0.type()); 981 assertEquals(CvType.CV_32FC1, gray0_32f.type()); 982 assertEquals(CvType.CV_8UC3, rgbLena.type()); 983 } 984 985 public void testWidth() { 986 assertEquals(gray0.cols(), gray0.width()); 987 assertEquals(rgbLena.cols(), rgbLena.width()); 988 assertEquals(rgba128.cols(), rgba128.width()); 989 } 990 991 public void testZerosIntIntInt() { 992 dst = Mat.zeros(matSize, matSize, CvType.CV_32F); 993 994 assertMatEqual(gray0_32f, dst, EPS); 995 } 996 997 public void testZerosSizeInt() { 998 dst = Mat.zeros(new Size(2, 2), CvType.CV_16S); 999 1000 truth = new Mat(2, 2, CvType.CV_16S, new Scalar(0)); 1001 assertMatEqual(truth, dst); 1002 } 1003 1004 } 1005