1 package org.opencv.test.core;
3 import java.util.ArrayList;
4 import java.util.Arrays;
7 import org.opencv.core.Core;
8 import org.opencv.core.Core.MinMaxLocResult;
9 import org.opencv.core.CvException;
10 import org.opencv.core.CvType;
11 import org.opencv.core.Mat;
12 import org.opencv.core.MatOfDouble;
13 import org.opencv.core.MatOfInt;
14 import org.opencv.core.MatOfPoint;
15 import org.opencv.core.Point;
16 import org.opencv.core.Rect;
17 import org.opencv.core.RotatedRect;
18 import org.opencv.core.Scalar;
19 import org.opencv.core.Size;
20 import org.opencv.core.TermCriteria;
21 import org.opencv.test.OpenCVTestCase;
22 import org.opencv.imgproc.Imgproc;
24 public class CoreTest extends OpenCVTestCase {
26 public void testAbsdiff() {
27 Core.absdiff(gray128, gray255, dst);
29 assertMatEqual(gray127, dst);
32 public void testAddMatMatMat() {
33 Core.add(gray128, gray128, dst);
35 assertMatEqual(gray255, dst);
38 public void testAddMatMatMatMatInt() {
39 Core.add(gray0, gray1, dst, gray1, CvType.CV_32F);
41 assertEquals(CvType.CV_32F, dst.depth());
42 assertMatEqual(gray1_32f, dst, EPS);
45 public void testAddWeightedMatDoubleMatDoubleDoubleMat() {
46 Core.addWeighted(gray1, 120.0, gray127, 1.0, 10.0, dst);
48 assertMatEqual(gray255, dst);
51 public void testAddWeightedMatDoubleMatDoubleDoubleMatInt() {
52 Core.addWeighted(gray1, 126.0, gray127, 1.0, 2.0, dst, CvType.CV_32F);
54 assertEquals(CvType.CV_32F, dst.depth());
55 assertMatEqual(gray255_32f, dst, EPS);
58 public void testBitwise_andMatMatMat() {
59 Core.bitwise_and(gray127, gray3, dst);
61 assertMatEqual(gray3, dst);
64 public void testBitwise_andMatMatMatMat() {
65 Core.bitwise_and(gray3, gray1, dst, gray255);
67 assertMatEqual(gray1, dst);
70 public void testBitwise_notMatMat() {
71 Core.bitwise_not(gray255, dst);
73 assertMatEqual(gray0, dst);
76 public void testBitwise_notMatMatMat() {
77 Core.bitwise_not(gray0, dst, gray1);
79 assertMatEqual(gray255, dst);
82 public void testBitwise_orMatMatMat() {
83 Core.bitwise_or(gray1, gray2, dst);
85 assertMatEqual(gray3, dst);
88 public void testBitwise_orMatMatMatMat() {
89 Core.bitwise_or(gray127, gray3, dst, gray255);
91 assertMatEqual(gray127, dst);
94 public void testBitwise_xorMatMatMat() {
95 Core.bitwise_xor(gray3, gray2, dst);
97 assertMatEqual(gray1, dst);
100 public void testBitwise_xorMatMatMatMat() {
101 Core.bitwise_or(gray127, gray128, dst, gray255);
103 assertMatEqual(gray255, dst);
106 public void testCalcCovarMatrixMatMatMatInt() {
107 Mat covar = new Mat(matSize, matSize, CvType.CV_64FC1);
108 Mat mean = new Mat(1, matSize, CvType.CV_64FC1);
110 Core.calcCovarMatrix(gray0_32f, covar, mean, Core.COVAR_ROWS | Core.COVAR_NORMAL);
112 assertMatEqual(gray0_64f, covar, EPS);
113 assertMatEqual(gray0_64f_1d, mean, EPS);
116 public void testCalcCovarMatrixMatMatMatIntInt() {
117 Mat covar = new Mat(matSize, matSize, CvType.CV_32F);
118 Mat mean = new Mat(1, matSize, CvType.CV_32F);
120 Core.calcCovarMatrix(gray0_32f, covar, mean, Core.COVAR_ROWS | Core.COVAR_NORMAL, CvType.CV_32F);
122 assertMatEqual(gray0_32f, covar, EPS);
123 assertMatEqual(gray0_32f_1d, mean, EPS);
126 public void testCartToPolarMatMatMatMat() {
127 Mat x = new Mat(1, 3, CvType.CV_32F) {
129 put(0, 0, 3.0, 6.0, 5, 0);
132 Mat y = new Mat(1, 3, CvType.CV_32F) {
134 put(0, 0, 4.0, 8.0, 12.0);
137 Mat dst_angle = new Mat();
139 Core.cartToPolar(x, y, dst, dst_angle);
141 Mat expected_magnitude = new Mat(1, 3, CvType.CV_32F) {
143 put(0, 0, 5.0, 10.0, 13.0);
146 Mat expected_angle = new Mat(1, 3, CvType.CV_32F) {
148 put(0, 0, atan2rad(4,3), atan2rad(8,6), atan2rad(12,5));
151 assertMatEqual(expected_magnitude, dst, EPS);
152 assertMatEqual(expected_angle, dst_angle, EPS);
155 public void testCartToPolarMatMatMatMatBoolean() {
156 Mat x = new Mat(1, 3, CvType.CV_32F) {
158 put(0, 0, 3.0, 6.0, 5, 0);
161 Mat y = new Mat(1, 3, CvType.CV_32F) {
163 put(0, 0, 4.0, 8.0, 12.0);
166 Mat dst_angle = new Mat();
168 Core.cartToPolar(x, y, dst, dst_angle, true);
170 Mat expected_magnitude = new Mat(1, 3, CvType.CV_32F) {
172 put(0, 0, 5.0, 10.0, 13.0);
175 Mat expected_angle = new Mat(1, 3, CvType.CV_32F) {
177 put(0, 0, atan2deg(4,3), atan2deg(8,6), atan2deg(12,5));
180 assertMatEqual(expected_magnitude, dst, EPS);
181 assertMatEqual(expected_angle, dst_angle, EPS * 180/Math.PI);
185 public void testCheckRangeMat() {
186 Mat outOfRange = new Mat(2, 2, CvType.CV_64F);
187 outOfRange.put(0, 0, Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0);
189 assertTrue(Core.checkRange(grayRnd_32f));
190 assertTrue(Core.checkRange(new Mat()));
191 assertFalse(Core.checkRange(outOfRange));
195 public void testCheckRangeMatBooleanPointDoubleDouble() {
196 Mat inRange = new Mat(2, 3, CvType.CV_64F) {
198 put(0, 0, 14, 48, 76, 33, 5, 99);
202 assertTrue(Core.checkRange(inRange, true, 5, 100));
204 Mat outOfRange = new Mat(2, 3, CvType.CV_64F) {
206 put(0, 0, -4, 0, 6, 33, 4, 109);
210 assertFalse(Core.checkRange(outOfRange, true, 5, 100));
213 public void testCompare() {
214 Core.compare(gray0, gray0, dst, Core.CMP_EQ);
216 assertMatEqual(dst, gray255);
218 Core.compare(gray0, gray1, dst, Core.CMP_EQ);
220 assertMatEqual(dst, gray0);
222 grayRnd.put(0, 0, 0, 0);
224 Core.compare(gray0, grayRnd, dst, Core.CMP_GE);
226 int expected = (int) (grayRnd.total() - Core.countNonZero(grayRnd));
227 assertEquals(expected, Core.countNonZero(dst));
230 public void testCompleteSymmMat() {
231 Core.completeSymm(grayRnd_32f);
233 assertMatEqual(grayRnd_32f, grayRnd_32f.t(), EPS);
236 public void testCompleteSymmMatBoolean() {
237 Mat grayRnd_32f2 = grayRnd_32f.clone();
239 Core.completeSymm(grayRnd_32f, true);
241 assertMatEqual(grayRnd_32f, grayRnd_32f.t(), EPS);
242 Core.completeSymm(grayRnd_32f2, false);
243 assertMatNotEqual(grayRnd_32f2, grayRnd_32f, EPS);
246 public void testConvertScaleAbsMatMat() {
247 Core.convertScaleAbs(gray0, dst);
249 assertMatEqual(gray0, dst, EPS);
251 Core.convertScaleAbs(gray_16u_256, dst);
253 assertMatEqual(gray255, dst, EPS);
256 public void testConvertScaleAbsMatMatDoubleDouble() {
257 Core.convertScaleAbs(gray_16u_256, dst, 2, -513);
259 assertMatEqual(gray1, dst);
262 public void testCountNonZero() {
263 assertEquals(0, Core.countNonZero(gray0));
265 gray0.put(0, 0, 255);
266 gray0.put(gray0.rows() - 1, gray0.cols() - 1, 255);
268 assertEquals(2, Core.countNonZero(gray0));
271 public void testCubeRoot() {
272 float res = Core.cubeRoot(-27.0f);
274 assertEquals(-3.0f, res);
277 public void testDctMatMat() {
278 Mat in = new Mat(1, 4, CvType.CV_32F) {
280 put(0, 0, 135.22211, 50.811096, 102.27016, 207.6682);
283 Mat dst1 = new Mat();
284 Mat dst2 = new Mat();
286 Core.dct(gray0_32f_1d, dst1);
289 truth = new Mat(1, 4, CvType.CV_32F) {
291 put(0, 0, 247.98576, -61.252407, 94.904533, 14.013477);
294 assertMatEqual(gray0_32f_1d, dst1, EPS);
295 assertMatEqual(truth, dst2, EPS);
298 public void testDctMatMatInt() {
299 Mat in = new Mat(1, 4, CvType.CV_32F) {
301 put(0, 0, 247.98576, -61.252407, 94.904533, 14.013477);
304 Mat dst1 = new Mat();
305 Mat dst2 = new Mat();
307 Core.dct(gray0_32f_1d, dst1, Core.DCT_INVERSE);
308 Core.dct(in, dst2, Core.DCT_INVERSE);
310 truth = new Mat(1, 4, CvType.CV_32F) {
312 put(0, 0, 135.22211, 50.811096, 102.27016, 207.6682);
315 assertMatEqual(gray0_32f_1d, dst1, EPS);
316 assertMatEqual(truth, dst2, EPS);
319 public void testDeterminant() {
320 Mat mat = new Mat(2, 2, CvType.CV_32F) {
329 double det = Core.determinant(mat);
331 assertEquals(8.0, det);
334 public void testDftMatMat() {
335 Core.dft(gray0_32f_1d, dst);
337 assertMatEqual(gray0_32f_1d, dst, EPS);
340 public void testDftMatMatIntInt() {
341 Mat src1 = new Mat(2, 4, CvType.CV_32F) {
343 put(0, 0, 1, 2, 3, 4);
344 put(1, 0, 1, 1, 1, 1);
347 Mat src2 = new Mat(2, 4, CvType.CV_32F) {
349 put(0, 0, 1, 2, 3, 4);
350 put(1, 0, 0, 0, 0, 0);
353 Mat dst1 = new Mat();
354 Mat dst2 = new Mat();
356 Core.dft(src1, dst1, Core.DFT_REAL_OUTPUT, 1);
357 Core.dft(src2, dst2, Core.DFT_REAL_OUTPUT, 0);
359 assertMatEqual(dst2, dst1, EPS);
362 public void testDivideDoubleMatMat() {
363 Core.divide(4.0, gray2, dst);
365 assertMatEqual(gray2, dst);
367 Core.divide(4.0, gray0, dst);
369 assertMatEqual(gray0, dst);
372 public void testDivideDoubleMatMatInt() {
373 Core.divide(9.0, gray3, dst, CvType.CV_32F);
375 assertMatEqual(gray3_32f, dst, EPS);
378 public void testDivideMatMatMat() {
379 Core.divide(gray9, gray3, dst);
381 assertMatEqual(gray3, dst);
384 public void testDivideMatMatMatDouble() {
385 Core.divide(gray1, gray2, dst, 6.0);
387 assertMatEqual(gray3, dst);
390 public void testDivideMatMatMatDoubleInt() {
391 Core.divide(gray1, gray2, dst, 6.0, CvType.CV_32F);
393 assertMatEqual(gray3_32f, dst, EPS);
396 public void testEigen() {
397 Mat src = new Mat(3, 3, CvType.CV_32FC1, new Scalar(2.0));
398 Mat eigenVals = new Mat();
399 Mat eigenVecs = new Mat();
401 Core.eigen(src, eigenVals, eigenVecs);
403 Mat expectedEigenVals = new Mat(3, 1, CvType.CV_32FC1) {
408 Mat expectedEigenVecs = new Mat(3, 3, CvType.CV_32FC1) {
410 put(0, 0, 0.57735026, 0.57735026, 0.57735032);
411 put(1, 0, 0.70710677, -0.70710677, 0);
412 put(2, 0, -0.40824831, -0.40824831, 0.81649661);
415 assertMatEqual(eigenVals, expectedEigenVals, EPS);
416 assertMatEqual(eigenVecs, expectedEigenVecs, EPS);
419 public void testExp() {
420 Core.exp(gray0_32f, dst);
422 assertMatEqual(gray1_32f, dst, EPS);
425 public void testExtractChannel() {
426 Core.extractChannel(rgba128, dst, 0);
428 assertMatEqual(gray128, dst);
431 public void testFastAtan2() {
434 float res = Core.fastAtan2(50, 50);
436 assertEquals(45, res, eps);
438 float res2 = Core.fastAtan2(80, 20);
440 assertEquals(Math.atan2(80, 20) * 180 / Math.PI, res2, eps);
443 public void testFillConvexPolyMatListOfPointScalar() {
444 MatOfPoint polyline = new MatOfPoint(new Point[]{new Point(1, 1), new Point(5, 0), new Point(6, 8), new Point(0, 9)});
446 Imgproc.fillConvexPoly(gray0, polyline, new Scalar(150));
448 assertTrue(0 < Core.countNonZero(gray0));
449 assertTrue(gray0.total() > Core.countNonZero(gray0));
452 public void testFillConvexPolyMatListOfPointScalarIntInt() {
453 MatOfPoint polyline1 = new MatOfPoint(new Point(2, 1), new Point(5, 1), new Point(5, 7), new Point(2, 7));
454 MatOfPoint polyline2 = new MatOfPoint(new Point(4, 2), new Point(10, 2), new Point(10, 14), new Point(4, 14));
456 // current implementation of fixed-point version of fillConvexPoly
457 // requires image to be at least 2-pixel wider in each direction than
459 Imgproc.fillConvexPoly(gray0, polyline1, colorWhite, Imgproc.LINE_8, 0);
461 assertTrue(0 < Core.countNonZero(gray0));
462 assertTrue(gray0.total() > Core.countNonZero(gray0));
464 Imgproc.fillConvexPoly(gray0, polyline2, colorBlack, Imgproc.LINE_8, 1);
466 assertEquals("see http://code.opencv.org/issues/1284", 0, Core.countNonZero(gray0));
469 public void testFillPolyMatListOfListOfPointScalar() {
471 Mat gray0 = Mat.zeros(matSize, matSize, CvType.CV_8U);
472 MatOfPoint polyline = new MatOfPoint(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4));
473 List<MatOfPoint> polylines = new ArrayList<MatOfPoint>();
474 polylines.add(polyline);
476 Imgproc.fillPoly(gray0, polylines, new Scalar(1));
478 final byte[] truth = new byte[] {
479 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
480 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
481 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
482 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
483 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
484 0, 1, 1, 0, 0, 0, 1, 1, 0, 0,
485 0, 1, 1, 0, 0, 0, 1, 1, 0, 0,
486 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
487 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
488 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
490 assertMatEqual(new Mat(gray0.size(), CvType.CV_8U) {
497 public void testFillPolyMatListOfListOfPointScalarIntIntPoint() {
498 MatOfPoint polyline1 = new MatOfPoint(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4));
499 MatOfPoint polyline2 = new MatOfPoint(new Point(0, 3), new Point(0, 7), new Point(3, 0), new Point(6, 7), new Point(6, 3));
501 List<MatOfPoint> polylines1 = new ArrayList<MatOfPoint>();
502 polylines1.add(polyline1);
504 List<MatOfPoint> polylines2 = new ArrayList<MatOfPoint>();
505 polylines2.add(polyline2);
507 Imgproc.fillPoly(gray0, polylines1, new Scalar(1), Imgproc.LINE_8, 0, new Point(0, 0));
509 assertTrue(0 < Core.countNonZero(gray0));
511 Imgproc.fillPoly(gray0, polylines2, new Scalar(0), Imgproc.LINE_8, 0, new Point(1, 1));
513 assertEquals(0, Core.countNonZero(gray0));
516 public void testFlip() {
517 Mat src = new Mat(2, 2, CvType.CV_32F) {
525 Mat dst1 = new Mat();
526 Mat dst2 = new Mat();
528 Core.flip(src, dst1, 0);
529 Core.flip(src, dst2, 1);
531 Mat dst_f1 = new Mat(2, 2, CvType.CV_32F) {
539 Mat dst_f2 = new Mat(2, 2, CvType.CV_32F) {
547 assertMatEqual(dst_f1, dst1, EPS);
548 assertMatEqual(dst_f2, dst2, EPS);
551 public void testGemmMatMatDoubleMatDoubleMat() {
552 Mat m1 = new Mat(2, 2, CvType.CV_32FC1) {
558 Mat m2 = new Mat(2, 2, CvType.CV_32FC1) {
564 Mat dmatrix = new Mat(2, 2, CvType.CV_32FC1) {
566 put(0, 0, 0.001, 0.001);
567 put(1, 0, 0.001, 0.001);
571 Core.gemm(m1, m2, 1.0, dmatrix, 1.0, dst);
573 Mat expected = new Mat(2, 2, CvType.CV_32FC1) {
575 put(0, 0, 1.001, 0.001);
576 put(1, 0, 1.001, 0.001);
579 assertMatEqual(expected, dst, EPS);
582 public void testGemmMatMatDoubleMatDoubleMatInt() {
583 Mat m1 = new Mat(2, 2, CvType.CV_32FC1) {
589 Mat m2 = new Mat(2, 2, CvType.CV_32FC1) {
595 Mat dmatrix = new Mat(2, 2, CvType.CV_32FC1) {
597 put(0, 0, 0.001, 0.001);
598 put(1, 0, 0.001, 0.001);
602 Core.gemm(m1, m2, 1.0, dmatrix, 1.0, dst, Core.GEMM_1_T);
604 Mat expected = new Mat(2, 2, CvType.CV_32FC1) {
606 put(0, 0, 2.001, 0.001);
607 put(1, 0, 0.001, 0.001);
610 assertMatEqual(expected, dst, EPS);
613 public void testGetCPUTickCount() {
614 long cpuCountStart = 0, actualTickCount;
616 cpuCountStart = Core.getCPUTickCount();
617 Core.sumElems(gray255);
618 actualTickCount = Core.getCPUTickCount();
620 long expectedTickCount = actualTickCount - cpuCountStart;
621 assertTrue(expectedTickCount > 0);
624 public void testGetNumberOfCPUs() {
625 int cpus = Core.getNumberOfCPUs();
627 assertTrue(Runtime.getRuntime().availableProcessors() <= cpus);
630 public void testGetOptimalDFTSize() {
631 assertEquals(1, Core.getOptimalDFTSize(0));
632 assertEquals(135, Core.getOptimalDFTSize(133));
633 assertEquals(15, Core.getOptimalDFTSize(13));
636 public void testGetTickCount() {
637 long startCount, endCount, count;
639 startCount = Core.getTickCount();
640 Core.divide(gray2, gray1, dst);
641 endCount = Core.getTickCount();
643 count = endCount - startCount;
644 assertTrue(count > 0);
647 public void testGetTickFrequency() {
648 double freq1 = Core.getTickFrequency();
649 Core.divide(gray2, gray1, dst);
650 double freq2 = Core.getTickFrequency();
652 assertTrue(0 < freq1);
653 assertEquals(freq1, freq2);
656 public void testHconcat() {
657 List<Mat> mats = Arrays.asList(Mat.eye(3, 3, CvType.CV_8U), Mat.zeros(3, 2, CvType.CV_8U));
659 Core.hconcat(mats, dst);
661 assertMatEqual(Mat.eye(3, 5, CvType.CV_8U), dst);
664 public void testIdctMatMat() {
665 Mat in = new Mat(1, 8, CvType.CV_32F) {
667 put(0, 0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0);
673 truth = new Mat(1, 8, CvType.CV_32F) {
675 put(0, 0, 3.3769724, -1.6215782, 2.3608727, 0.20730907, -0.86502546, 0.028082132, -0.7673766, 0.10917115);
678 assertMatEqual(truth, dst, EPS);
681 public void testIdctMatMatInt() {
682 Mat in = new Mat(2, 8, CvType.CV_32F) {
684 put(0, 0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0);
685 put(1, 0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0);
689 Core.idct(in, dst, Core.DCT_ROWS);
691 truth = new Mat(2, 8, CvType.CV_32F) {
693 put(0, 0, 3.3769724, -1.6215782, 2.3608727, 0.20730907, -0.86502546, 0.028082132, -0.7673766, 0.10917115);
694 put(1, 0, 3.3769724, -1.6215782, 2.3608727, 0.20730907, -0.86502546, 0.028082132, -0.7673766, 0.10917115);
697 assertMatEqual(truth, dst, EPS);
700 public void testIdftMatMat() {
701 Mat in = new Mat(1, 4, CvType.CV_32F) {
703 put(0, 0, 1.0, 2.0, 3.0, 4.0);
709 truth = new Mat(1, 4, CvType.CV_32F) {
711 put(0, 0, 9, -9, 1, 3);
714 assertMatEqual(truth, dst, EPS);
717 public void testIdftMatMatIntInt() {
718 Mat in = new Mat(2, 4, CvType.CV_32F) {
720 put(0, 0, 1.0, 2.0, 3.0, 4.0);
721 put(1, 0, 1.0, 2.0, 3.0, 4.0);
726 Core.idft(in, dst, Core.DFT_REAL_OUTPUT, 1);
728 truth = new Mat(2, 4, CvType.CV_32F) {
730 put(0, 0, 18, -18, 2, 6);
731 put(1, 0, 0, 0, 0, 0);
734 assertMatEqual(truth, dst, EPS);
737 public void testInRange() {
738 gray0.put(1, 1, 100, 150, 200);
740 Core.inRange(gray0, new Scalar(120), new Scalar(160), dst);
742 byte vals[] = new byte[3];
745 assertEquals(0, vals[0]);
746 assertEquals(-1, vals[1]);
747 assertEquals(0, vals[2]);
748 assertEquals(1, Core.countNonZero(dst));
751 public void testInsertChannel() {
752 Core.insertChannel(gray0, rgba128, 0);
753 Core.insertChannel(gray0, rgba128, 1);
754 Core.insertChannel(gray0, rgba128, 2);
755 Core.insertChannel(gray0, rgba128, 3);
757 assertMatEqual(rgba0, rgba128);
760 public void testInvertMatMat() {
761 Mat src = new Mat(2, 2, CvType.CV_32F) {
770 Core.invert(src, dst);
772 truth = new Mat(2, 2, CvType.CV_32F) {
780 assertMatEqual(truth, dst, EPS);
783 public void testInvertMatMatInt() {
784 Mat src = Mat.eye(3, 3, CvType.CV_32FC1);
787 double cond = Core.invert(src, dst, Core.DECOMP_SVD);
789 truth = Mat.eye(3, 3, CvType.CV_32FC1);
791 assertMatEqual(truth, dst, EPS);
792 assertEquals(0.3819660544395447, cond, EPS);
795 public void testKmeansMatIntMatTermCriteriaIntInt() {
796 Mat data = new Mat(4, 5, CvType.CV_32FC1) {
798 put(0, 0, 1, 2, 3, 4, 5);
799 put(1, 0, 2, 3, 4, 5, 6);
800 put(2, 0, 5, 4, 3, 2, 1);
801 put(3, 0, 6, 5, 4, 3, 2);
804 TermCriteria criteria = new TermCriteria(TermCriteria.EPS, 0, EPS);
805 Mat labels = new Mat();
807 Core.kmeans(data, 2, labels, criteria, 1, Core.KMEANS_PP_CENTERS);
809 int[] first_center = new int[1];
810 labels.get(0, 0, first_center);
811 final int c1 = first_center[0];
812 Mat expected_labels = new Mat(4, 1, CvType.CV_32S) {
814 put(0, 0, c1, c1, 1 - c1, 1 - c1);
817 assertMatEqual(expected_labels, labels);
820 public void testKmeansMatIntMatTermCriteriaIntIntMat() {
821 Mat data = new Mat(4, 5, CvType.CV_32FC1) {
823 put(0, 0, 1, 2, 3, 4, 5);
824 put(1, 0, 2, 3, 4, 5, 6);
825 put(2, 0, 5, 4, 3, 2, 1);
826 put(3, 0, 6, 5, 4, 3, 2);
829 TermCriteria criteria = new TermCriteria(TermCriteria.EPS, 0, EPS);
830 Mat labels = new Mat();
831 Mat centers = new Mat();
833 Core.kmeans(data, 2, labels, criteria, 6, Core.KMEANS_RANDOM_CENTERS, centers);
835 int[] first_center = new int[1];
836 labels.get(0, 0, first_center);
837 final int c1 = first_center[0];
838 Mat expected_labels = new Mat(4, 1, CvType.CV_32S) {
840 put(0, 0, c1, c1, 1 - c1, 1 - c1);
843 Mat expected_centers = new Mat(2, 5, CvType.CV_32FC1) {
845 put(c1, 0, 1.5, 2.5, 3.5, 4.5, 5.5);
846 put(1 - c1, 0, 5.5, 4.5, 3.5, 2.5, 1.5);
849 assertMatEqual(expected_labels, labels);
850 assertMatEqual(expected_centers, centers, EPS);
853 public void testLineMatPointPointScalar() {
854 int nPoints = Math.min(gray0.cols(), gray0.rows());
855 Point point1 = new Point(0, 0);
856 Point point2 = new Point(nPoints, nPoints);
857 Scalar color = new Scalar(255);
859 Imgproc.line(gray0, point1, point2, color);
861 assertTrue(nPoints == Core.countNonZero(gray0));
864 public void testLineMatPointPointScalarInt() {
865 int nPoints = Math.min(gray0.cols(), gray0.rows());
866 Point point1 = new Point(0, 0);
867 Point point2 = new Point(nPoints, nPoints);
869 Imgproc.line(gray0, point1, point2, colorWhite, 0);
871 assertTrue(nPoints == Core.countNonZero(gray0));
874 public void testLineMatPointPointScalarIntIntInt() {
875 int nPoints = Math.min(gray0.cols(), gray0.rows());
876 Point point1 = new Point(3, 4);
877 Point point2 = new Point(nPoints, nPoints);
878 Point point1_4 = new Point(3 * 4, 4 * 4);
879 Point point2_4 = new Point(nPoints * 4, nPoints * 4);
881 Imgproc.line(gray0, point2, point1, colorWhite, 2, Imgproc.LINE_8, 0);
883 assertFalse(0 == Core.countNonZero(gray0));
885 Imgproc.line(gray0, point2_4, point1_4, colorBlack, 2, Imgproc.LINE_8, 2);
887 assertEquals(0, Core.countNonZero(gray0));
890 public void testLog() {
891 Mat in = new Mat(1, 4, CvType.CV_32FC1) {
893 put(0, 0, 1.0, 10.0, 100.0, 1000.0);
899 Mat expected = new Mat(1, 4, CvType.CV_32FC1) {
901 put(0, 0, 0, 2.3025851, 4.6051702, 6.9077554);
904 assertMatEqual(expected, dst, EPS);
907 public void testLUTMatMatMat() {
908 Mat lut = new Mat(1, 256, CvType.CV_8UC1);
909 lut.setTo(new Scalar(0));
911 Core.LUT(grayRnd, lut, dst);
913 assertMatEqual(gray0, dst);
915 lut.setTo(new Scalar(255));
917 Core.LUT(grayRnd, lut, dst);
919 assertMatEqual(gray255, dst);
922 public void testMagnitude() {
923 Mat x = new Mat(1, 4, CvType.CV_32F);
924 Mat y = new Mat(1, 4, CvType.CV_32F);
925 x.put(0, 0, 3.0, 5.0, 9.0, 6.0);
926 y.put(0, 0, 4.0, 12.0, 40.0, 8.0);
928 Core.magnitude(x, y, dst);
930 Mat out = new Mat(1, 4, CvType.CV_32F);
931 out.put(0, 0, 5.0, 13.0, 41.0, 10.0);
932 assertMatEqual(out, dst, EPS);
934 Core.magnitude(gray0_32f, gray255_32f, dst);
936 assertMatEqual(gray255_32f, dst, EPS);
939 public void testMahalanobis() {
940 Mat covar = new Mat(matSize, matSize, CvType.CV_32F);
941 Mat mean = new Mat(1, matSize, CvType.CV_32F);
942 Core.calcCovarMatrix(grayRnd_32f, covar, mean, Core.COVAR_ROWS | Core.COVAR_NORMAL, CvType.CV_32F);
944 Mat line1 = grayRnd_32f.row(0);
945 Mat line2 = grayRnd_32f.row(1);
947 double d = Core.Mahalanobis(line1, line1, covar);
949 assertEquals(0.0, d);
951 d = Core.Mahalanobis(line1, line2, covar);
956 public void testMax() {
957 Core.max(gray0, gray255, dst);
959 assertMatEqual(gray255, dst);
961 Mat x = new Mat(1, 1, CvType.CV_32F);
962 Mat y = new Mat(1, 1, CvType.CV_32F);
968 Mat truth = new Mat(1, 1, CvType.CV_32F);
969 truth.put(0, 0, 23.0);
970 assertMatEqual(truth, dst, EPS);
973 public void testMeanMat() {
974 Scalar mean = Core.mean(makeMask(gray128));
976 assertScalarEqual(new Scalar(64), mean, EPS);
979 public void testMeanMatMat() {
980 Mat mask1 = makeMask(gray1.clone());
981 Mat mask2 = makeMask(gray0, 1);
983 Scalar mean1 = Core.mean(grayRnd, mask1);
984 Scalar mean2 = Core.mean(grayRnd, mask2);
985 Scalar mean = Core.mean(grayRnd, gray1);
987 assertScalarEqual(mean, new Scalar(0.5 * (mean1.val[0] + mean2.val[0])), EPS);
990 public void testMeanStdDevMatMatMat() {
991 MatOfDouble mean = new MatOfDouble();
992 MatOfDouble stddev = new MatOfDouble();
994 Core.meanStdDev(rgbLena, mean, stddev);
996 double expectedMean[] = new double[]
997 {105.3989906311035, 99.56269836425781, 179.7303047180176};
998 double expectedDev[] = new double[]
999 {33.74205485167219, 52.8734582803278, 49.01569488056406};
1001 assertArrayEquals(expectedMean, mean.toArray(), EPS);
1002 assertArrayEquals(expectedDev, stddev.toArray(), EPS);
1005 public void testMeanStdDevMatMatMatMat() {
1006 Mat submat = grayRnd.submat(0, grayRnd.rows() / 2, 0, grayRnd.cols() / 2);
1007 submat.setTo(new Scalar(33));
1008 Mat mask = gray0.clone();
1009 submat = mask.submat(0, mask.rows() / 2, 0, mask.cols() / 2);
1010 submat.setTo(new Scalar(1));
1011 MatOfDouble mean = new MatOfDouble();
1012 MatOfDouble stddev = new MatOfDouble();
1014 Core.meanStdDev(grayRnd, mean, stddev, mask);
1016 double expectedMean[] = new double[] {33d};
1017 double expectedDev[] = new double[] {0d};
1019 assertArrayEquals(expectedMean, mean.toArray(), EPS);
1020 assertArrayEquals(expectedDev, stddev.toArray(), EPS);
1023 public void testMerge() {
1024 Mat src1 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(1));
1025 Mat src2 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(2));
1026 Mat src3 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(3));
1027 List<Mat> listMat = Arrays.asList(src1, src2, src3);
1029 Core.merge(listMat, dst);
1031 truth = new Mat(2, 2, CvType.CV_32FC3, new Scalar(1, 2, 3));
1032 assertMatEqual(truth, dst, EPS);
1035 public void testMin() {
1036 Core.min(gray0, gray255, dst);
1038 assertMatEqual(gray0, dst);
1041 public void testMinMaxLocMat() {
1044 Point minLoc = new Point(gray3.cols() / 4, gray3.rows() / 2);
1045 Point maxLoc = new Point(gray3.cols() / 2, gray3.rows() / 4);
1046 gray3.put((int) minLoc.y, (int) minLoc.x, minVal);
1047 gray3.put((int) maxLoc.y, (int) maxLoc.x, maxVal);
1049 Core.MinMaxLocResult mmres = Core.minMaxLoc(gray3);
1051 assertEquals(minVal, mmres.minVal);
1052 assertEquals(maxVal, mmres.maxVal);
1053 assertPointEquals(minLoc, mmres.minLoc, EPS);
1054 assertPointEquals(maxLoc, mmres.maxLoc, EPS);
1057 public void testMinMaxLocMatMat() {
1058 Mat src = new Mat(4, 4, CvType.CV_8U) {
1060 put(0, 0, 2, 4, 27, 3);
1061 put(1, 0, 0, 8, 7, 130);
1062 put(2, 0, 13, 4, 13, 4);
1063 put(3, 0, 6, 4, 2, 13);
1066 Mat mask = new Mat(4, 4, CvType.CV_8U, new Scalar(0));
1067 mask.submat(1, 3, 1, 4).setTo(new Scalar(1));
1069 MinMaxLocResult res = Core.minMaxLoc(src, mask);
1071 assertEquals(4.0, res.minVal);
1072 assertEquals(130.0, res.maxVal);
1073 assertPointEquals(new Point(1, 2), res.minLoc, EPS);
1074 assertPointEquals(new Point(3, 1), res.maxLoc, EPS);
1077 public void testMixChannels() {
1078 rgba0.setTo(new Scalar(10, 20, 30, 40));
1079 List<Mat> src = Arrays.asList(rgba0);
1080 List<Mat> dst = Arrays.asList(gray3, gray2, gray1, gray0, getMat(CvType.CV_8UC3, 0, 0, 0));
1081 MatOfInt fromTo = new MatOfInt(
1091 Core.mixChannels(src, dst, fromTo);
1093 assertMatEqual(getMat(CvType.CV_8U, 40), dst.get(0));
1094 assertMatEqual(getMat(CvType.CV_8U, 40), dst.get(1));
1095 assertMatEqual(getMat(CvType.CV_8U, 30), dst.get(2));
1096 assertMatEqual(getMat(CvType.CV_8U, 10), dst.get(3));
1097 assertMatEqual(getMat(CvType.CV_8UC3, 30, 20, 10), dst.get(4));
1100 public void testMulSpectrumsMatMatMatInt() {
1101 Mat src1 = new Mat(1, 4, CvType.CV_32F) {
1103 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1106 Mat src2 = new Mat(1, 4, CvType.CV_32F) {
1108 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1112 Core.mulSpectrums(src1, src2, dst, Core.DFT_ROWS);
1114 Mat expected = new Mat(1, 4, CvType.CV_32F) {
1116 put(0, 0, 1, -5, 12, 16);
1119 assertMatEqual(expected, dst, EPS);
1122 public void testMulSpectrumsMatMatMatIntBoolean() {
1123 Mat src1 = new Mat(1, 4, CvType.CV_32F) {
1125 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1128 Mat src2 = new Mat(1, 4, CvType.CV_32F) {
1130 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1134 Core.mulSpectrums(src1, src2, dst, Core.DFT_ROWS, true);
1136 Mat expected = new Mat(1, 4, CvType.CV_32F) {
1138 put(0, 0, 1, 13, 0, 16);
1141 assertMatEqual(expected, dst, EPS);
1144 public void testMultiplyMatMatMat() {
1145 Core.multiply(gray0, gray255, dst);
1147 assertMatEqual(gray0, dst);
1150 public void testMultiplyMatMatMatDouble() {
1151 Core.multiply(gray1, gray1, dst, 2.0);
1153 assertMatEqual(gray2, dst);
1157 public void testMultiplyMatMatMatDoubleInt() {
1158 Core.multiply(gray1, gray2, dst, 1.5, CvType.CV_32F);
1160 assertMatEqual(gray3_32f, dst, EPS);
1163 public void testMulTransposedMatMatBoolean() {
1164 Core.mulTransposed(grayE_32f, dst, true);
1166 assertMatEqual(grayE_32f, dst, EPS);
1169 public void testMulTransposedMatMatBooleanMatDouble() {
1170 Core.mulTransposed(grayE_32f, dst, true, gray0_32f, 2);
1173 truth.diag().setTo(new Scalar(2));
1174 assertMatEqual(truth, dst, EPS);
1177 public void testMulTransposedMatMatBooleanMatDoubleInt() {
1178 Mat a = getMat(CvType.CV_32F, 1);
1180 Core.mulTransposed(a, dst, true, gray0_32f, 3, CvType.CV_64F);
1182 assertMatEqual(getMat(CvType.CV_64F, 3 * a.rows()), dst, EPS);
1185 public void testNormalizeMatMat() {
1186 Mat m = gray0.clone();
1187 m.diag().setTo(new Scalar(2));
1189 Core.normalize(m, dst);
1191 assertMatEqual(gray0, dst);
1194 public void testNormalizeMatMatDoubleDoubleInt() {
1195 Mat src = new Mat(1, 4, CvType.CV_32F) {
1197 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1201 Core.normalize(src, dst, 1.0, 2.0, Core.NORM_INF);
1203 Mat expected = new Mat(1, 4, CvType.CV_32F) {
1205 put(0, 0, 0.25, 0.5, 0.75, 1);
1208 assertMatEqual(expected, dst, EPS);
1211 public void testNormalizeMatMatDoubleDoubleIntInt() {
1212 Mat src = new Mat(1, 5, CvType.CV_32F) {
1214 put(0, 0, 0, 1, 2, 3, 4);
1218 Core.normalize(src, dst, 1, 2, Core.NORM_MINMAX, CvType.CV_64F);
1220 Mat expected = new Mat(1, 5, CvType.CV_64F) {
1222 put(0, 0, 1, 1.25, 1.5, 1.75, 2);
1225 assertMatEqual(expected, dst, EPS);
1228 public void testNormalizeMatMatDoubleDoubleIntIntMat() {
1229 Mat src = new Mat(1, 5, CvType.CV_32F) {
1231 put(0, 0, 0, 1, 2, 3, 4);
1234 Mat mask = new Mat(1, 5, CvType.CV_8U) {
1236 put(0, 0, 1, 0, 0, 0, 1);
1241 Core.normalize(src, dst, 1, 2, Core.NORM_MINMAX, CvType.CV_32F, mask);
1243 Mat expected = new Mat(1, 5, CvType.CV_32F) {
1245 put(0, 0, 1, 1, 2, 3, 2);
1248 assertMatEqual(expected, dst, EPS);
1251 public void testNormMat() {
1252 double n = Core.norm(gray1);
1254 assertEquals(10., n);
1257 public void testNormMatInt() {
1258 double n = Core.norm(gray127, Core.NORM_INF);
1260 assertEquals(127., n);
1263 public void testNormMatIntMat() {
1264 double n = Core.norm(gray3, Core.NORM_L1, gray0);
1266 assertEquals(0.0, n);
1269 public void testNormMatMat() {
1270 double n = Core.norm(gray0, gray1);
1272 assertEquals(10.0, n);
1275 public void testNormMatMatInt() {
1276 double n = Core.norm(gray127, gray1, Core.NORM_INF);
1278 assertEquals(126.0, n);
1281 public void testNormMatMatIntMat() {
1282 double n = Core.norm(gray3, gray0, Core.NORM_L1, makeMask(gray0.clone(), 1));
1284 assertEquals(150.0, n);
1287 public void testPCABackProject() {
1288 Mat mean = new Mat(1, 4, CvType.CV_32F) {
1290 put(0, 0, 2, 4, 4, 8);
1293 Mat vectors = new Mat(1, 4, CvType.CV_32F, new Scalar(0)) {
1295 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1298 Mat data = new Mat(3, 1, CvType.CV_32F) {
1300 put(0, 0, -5, 0, -10);
1303 Mat result = new Mat();
1305 Core.PCABackProject(data, mean, vectors, result);
1307 Mat truth = new Mat(3, 4, CvType.CV_32F) {
1309 put(0, 0, 1, 2, 2, 4);
1310 put(1, 0, 2, 4, 4, 8);
1311 put(2, 0, 0, 0, 0, 0);
1314 assertMatEqual(truth, result, EPS);
1317 public void testPCAComputeMatMatMat() {
1318 Mat data = new Mat(3, 4, CvType.CV_32F) {
1320 put(0, 0, 1, 2, 2, 4);
1321 put(1, 0, 2, 4, 4, 8);
1322 put(2, 0, 3, 6, 6, 12);
1325 Mat mean = new Mat();
1326 Mat vectors = new Mat();
1328 Core.PCACompute(data, mean, vectors);
1330 Mat mean_truth = new Mat(1, 4, CvType.CV_32F) {
1332 put(0, 0, 2, 4, 4, 8);
1335 Mat vectors_truth = new Mat(3, 4, CvType.CV_32F, new Scalar(0)) {
1337 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1340 assertMatEqual(mean_truth, mean, EPS);
1341 assertMatEqual(vectors_truth, vectors, EPS);
1344 public void testPCAComputeMatMatMatInt() {
1345 Mat data = new Mat(3, 4, CvType.CV_32F) {
1347 put(0, 0, 1, 2, 2, 4);
1348 put(1, 0, 2, 4, 4, 8);
1349 put(2, 0, 3, 6, 6, 12);
1352 Mat mean = new Mat();
1353 Mat vectors = new Mat();
1355 Core.PCACompute(data, mean, vectors, 1);
1357 Mat mean_truth = new Mat(1, 4, CvType.CV_32F) {
1359 put(0, 0, 2, 4, 4, 8);
1362 Mat vectors_truth = new Mat(1, 4, CvType.CV_32F, new Scalar(0)) {
1364 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1367 assertMatEqual(mean_truth, mean, EPS);
1368 assertMatEqual(vectors_truth, vectors, EPS);
1371 public void testPCAProject() {
1372 Mat mean = new Mat(1, 4, CvType.CV_32F) {
1374 put(0, 0, 2, 4, 4, 8);
1377 Mat vectors = new Mat(1, 4, CvType.CV_32F, new Scalar(0)) {
1379 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1382 Mat data = new Mat(3, 4, CvType.CV_32F) {
1384 put(0, 0, 1, 2, 2, 4);
1385 put(1, 0, 2, 4, 4, 8);
1386 put(2, 0, 0, 0, 0, 0);
1389 Mat result = new Mat();
1391 Core.PCAProject(data, mean, vectors, result);
1393 Mat truth = new Mat(3, 1, CvType.CV_32F) {
1395 put(0, 0, -5, 0, -10);
1398 assertMatEqual(truth, result, EPS);
1401 public void testPerspectiveTransform() {
1402 Mat src = new Mat(matSize, matSize, CvType.CV_32FC2);
1403 Core.randu(src, 0, 256);
1404 Mat transformMatrix = Mat.eye(3, 3, CvType.CV_32F);
1406 Core.perspectiveTransform(src, dst, transformMatrix);
1407 assertMatEqual(src, dst, EPS);
1410 public void testPerspectiveTransform3D() {
1411 Mat src = new Mat(matSize, matSize, CvType.CV_32FC3);
1412 Core.randu(src, 0, 256);
1413 Mat transformMatrix = Mat.eye(4, 4, CvType.CV_32F);
1415 Core.perspectiveTransform(src, dst, transformMatrix);
1417 assertMatEqual(src, dst, EPS);
1420 private static double atan2deg(double y, double x)
1422 double res = Math.atan2(y, x);
1424 res = Math.PI * 2 + res;
1425 return res * 180 / Math.PI;
1428 private static double atan2rad(double y, double x)
1430 double res = Math.atan2(y, x);
1432 res = Math.PI * 2 + res;
1436 public void testPhaseMatMatMat() {
1437 Mat x = new Mat(1, 4, CvType.CV_32F) {
1439 put(0, 0, 10.0, 10.0, 20.0, 5.0);
1442 Mat y = new Mat(1, 4, CvType.CV_32F) {
1444 put(0, 0, 20.0, 15.0, 20.0, 20.0);
1447 Mat gold = new Mat(1, 4, CvType.CV_32F) {
1449 put(0, 0, atan2rad(20, 10), atan2rad(15, 10), atan2rad(20, 20), atan2rad(20, 5));
1453 Core.phase(x, y, dst);
1455 assertMatEqual(gold, dst, EPS);
1458 public void testPhaseMatMatMatBoolean() {
1459 Mat x = new Mat(1, 4, CvType.CV_32F) {
1461 put(0, 0, 10.0, 10.0, 20.0, 5.0);
1464 Mat y = new Mat(1, 4, CvType.CV_32F) {
1466 put(0, 0, 20.0, 15.0, 20.0, 20.0);
1469 Mat gold = new Mat(1, 4, CvType.CV_32F) {
1471 put(0, 0, atan2deg(20, 10), atan2deg(15, 10), atan2deg(20, 20), atan2deg(20, 5));
1475 Core.phase(x, y, dst, true);
1477 assertMatEqual(gold, dst, EPS * 180 / Math.PI);
1480 public void testPolarToCartMatMatMatMat() {
1481 Mat magnitude = new Mat(1, 3, CvType.CV_32F) {
1483 put(0, 0, 5.0, 10.0, 13.0);
1486 Mat angle = new Mat(1, 3, CvType.CV_32F) {
1488 put(0, 0, 0.92729962, 0.92729962, 1.1759995);
1491 Mat xCoordinate = new Mat();
1492 Mat yCoordinate = new Mat();
1494 Core.polarToCart(magnitude, angle, xCoordinate, yCoordinate);
1496 Mat x = new Mat(1, 3, CvType.CV_32F) {
1498 put(0, 0, 3.0, 6.0, 5, 0);
1501 Mat y = new Mat(1, 3, CvType.CV_32F) {
1503 put(0, 0, 4.0, 8.0, 12.0);
1506 assertMatEqual(x, xCoordinate, EPS);
1507 assertMatEqual(y, yCoordinate, EPS);
1510 public void testPolarToCartMatMatMatMatBoolean() {
1511 Mat magnitude = new Mat(1, 3, CvType.CV_32F) {
1513 put(0, 0, 5.0, 10.0, 13.0);
1516 Mat angle = new Mat(1, 3, CvType.CV_32F) {
1518 put(0, 0, 0.92729962, 0.92729962, 1.1759995);
1521 Mat xCoordinate = new Mat();
1522 Mat yCoordinate = new Mat();
1524 Core.polarToCart(magnitude, angle, xCoordinate, yCoordinate, true);
1526 Mat x = new Mat(1, 3, CvType.CV_32F) {
1528 put(0, 0, 4.9993458, 9.9986916, 12.997262);
1531 Mat y = new Mat(1, 3, CvType.CV_32F) {
1533 put(0, 0, 0.080918625, 0.16183725, 0.26680708);
1536 assertMatEqual(x, xCoordinate, EPS);
1537 assertMatEqual(y, yCoordinate, EPS);
1540 public void testPow() {
1541 Core.pow(gray2, 7, dst);
1543 assertMatEqual(gray128, dst);
1546 public void testRandn() {
1547 Core.randn(gray0, 100, 23);
1549 assertEquals(100., Core.mean(gray0).val[0], 23 / 2);
1552 public void testRandShuffleMat() {
1553 Mat original = new Mat(1, 10, CvType.CV_32F) {
1555 put(0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
1558 Mat shuffled = original.clone();
1560 Core.randShuffle(shuffled);
1562 assertMatNotEqual(original, shuffled, EPS);
1563 Mat dst1 = new Mat();
1564 Mat dst2 = new Mat();
1565 Core.sort(original, dst1, Core.SORT_ASCENDING);
1566 Core.sort(shuffled, dst2, Core.SORT_ASCENDING);
1567 assertMatEqual(dst1, dst2, EPS);
1570 public void testRandShuffleMatDouble() {
1571 Mat original = new Mat(1, 10, CvType.CV_32F) {
1573 put(0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
1576 Mat shuffled = original.clone();
1578 Core.randShuffle(shuffled, 10);
1580 assertMatNotEqual(original, shuffled, EPS);
1581 Mat dst1 = new Mat();
1582 Mat dst2 = new Mat();
1583 Core.sort(original, dst1, Core.SORT_ASCENDING);
1584 Core.sort(shuffled, dst2, Core.SORT_ASCENDING);
1585 assertMatEqual(dst1, dst2, EPS);
1588 public void testRandu() {
1589 Core.randu(gray0, 3, 23);
1590 fail("Not yet implemented");
1591 //assertTrue(Core.checkRange(gray0, true, null, 3, 23));
1594 public void testRectangleMatPointPointScalar() {
1595 Point bottomRight = new Point(gray0.cols() / 2, gray0.rows() / 2);
1596 Point topLeft = new Point(0, 0);
1597 Scalar color = new Scalar(128);
1599 Imgproc.rectangle(gray0, bottomRight, topLeft, color);
1601 assertTrue(0 != Core.countNonZero(gray0));
1604 public void testRectangleMatPointPointScalarInt() {
1605 Point bottomRight = new Point(gray0.cols(), gray0.rows());
1606 Point topLeft = new Point(0, 0);
1607 Scalar color = new Scalar(128);
1609 Imgproc.rectangle(gray0, bottomRight, topLeft, color, 2);
1610 Imgproc.rectangle(gray0, bottomRight, topLeft, colorBlack);
1612 assertTrue(0 != Core.countNonZero(gray0));
1615 public void testRectangleMatPointPointScalarIntInt() {
1616 Point bottomRight = new Point(gray0.cols() / 2, gray0.rows() / 2);
1617 Point topLeft = new Point(0, 0);
1618 Scalar color = new Scalar(128);
1620 Imgproc.rectangle(gray0, bottomRight, topLeft, color, 2, Imgproc.LINE_AA, 0);
1621 Imgproc.rectangle(gray0, bottomRight, topLeft, colorBlack, 2, Imgproc.LINE_4, 0);
1623 assertTrue(0 != Core.countNonZero(gray0));
1626 public void testRectangleMatPointPointScalarIntIntInt() {
1627 Point bottomRight1 = new Point(gray0.cols(), gray0.rows());
1628 Point bottomRight2 = new Point(gray0.cols() / 2, gray0.rows() / 2);
1629 Point topLeft = new Point(0, 0);
1630 Scalar color = new Scalar(128);
1632 Imgproc.rectangle(gray0, bottomRight1, topLeft, color, 2, Imgproc.LINE_8, 1);
1634 assertTrue(0 != Core.countNonZero(gray0));
1636 Imgproc.rectangle(gray0, bottomRight2, topLeft, colorBlack, 2, Imgproc.LINE_8, 0);
1638 assertEquals(0, Core.countNonZero(gray0));
1641 public void testReduceMatMatIntInt() {
1642 Mat src = new Mat(2, 2, CvType.CV_32F) {
1649 Core.reduce(src, dst, 0, Core.REDUCE_AVG);
1651 Mat out = new Mat(1, 2, CvType.CV_32F) {
1656 assertMatEqual(out, dst, EPS);
1659 public void testReduceMatMatIntIntInt() {
1660 Mat src = new Mat(2, 2, CvType.CV_32F) {
1667 Core.reduce(src, dst, 1, Core.REDUCE_SUM, CvType.CV_64F);
1669 Mat out = new Mat(2, 1, CvType.CV_64F) {
1674 assertMatEqual(out, dst, EPS);
1677 public void testRepeat() {
1678 Mat src = new Mat(1, 2, CvType.CV_32F, new Scalar(0));
1680 Core.repeat(src, matSize, matSize / 2, dst);
1682 assertMatEqual(gray0_32f, dst, EPS);
1685 public void testScaleAdd() {
1686 Core.scaleAdd(gray3, 2.0, gray3, dst);
1688 assertMatEqual(gray9, dst);
1691 public void testSetIdentityMat() {
1692 Core.setIdentity(gray0_32f);
1694 assertMatEqual(grayE_32f, gray0_32f, EPS);
1697 public void testSetIdentityMatScalar() {
1700 Core.setIdentity(m, new Scalar(5));
1702 truth = new Mat(m.size(), m.type(), new Scalar(0));
1703 truth.diag().setTo(new Scalar(5));
1704 assertMatEqual(truth, m, EPS);
1707 public void testSolveCubic() {
1708 Mat coeffs = new Mat(1, 4, CvType.CV_32F) {
1710 put(0, 0, 1, 6, 11, 6);
1714 assertEquals(3, Core.solveCubic(coeffs, dst));
1716 Mat roots = new Mat(3, 1, CvType.CV_32F) {
1718 put(0, 0, -3, -1, -2);
1721 assertMatEqual(roots, dst, EPS);
1724 public void testSolveMatMatMat() {
1725 Mat a = new Mat(3, 3, CvType.CV_32F) {
1728 put(1, 0, 1, -2, 2);
1732 Mat b = new Mat(3, 1, CvType.CV_32F) {
1738 assertTrue(Core.solve(a, b, dst));
1740 Mat res = new Mat(3, 1, CvType.CV_32F) {
1742 put(0, 0, -12, 2, 10);
1745 assertMatEqual(res, dst, EPS);
1748 public void testSolveMatMatMatInt() {
1749 Mat a = new Mat(3, 3, CvType.CV_32F) {
1752 put(1, 0, 1, -2, 2);
1756 Mat b = new Mat(3, 1, CvType.CV_32F) {
1762 assertTrue(Core.solve(a, b, dst, Core.DECOMP_QR | Core.DECOMP_NORMAL));
1764 Mat res = new Mat(3, 1, CvType.CV_32F) {
1766 put(0, 0, -12, 2, 10);
1769 assertMatEqual(res, dst, EPS);
1772 public void testSolvePolyMatMat() {
1773 Mat coeffs = new Mat(4, 1, CvType.CV_32F) {
1775 put(0, 0, -6, 11, -6, 1);
1778 Mat roots = new Mat();
1780 assertEquals(0.0, Core.solvePoly(coeffs, roots));
1782 truth = new Mat(3, 1, CvType.CV_32FC2) {
1784 put(0, 0, 1, 0, 2, 0, 3, 0);
1787 assertMatEqual(truth, roots, EPS);
1790 public void testSolvePolyMatMatInt() {
1791 Mat coeffs = new Mat(4, 1, CvType.CV_32F) {
1793 put(0, 0, -6, 11, -6, 1);
1796 Mat roots = new Mat();
1798 assertEquals(10.198039027185569, Core.solvePoly(coeffs, roots, 1));
1800 truth = new Mat(3, 1, CvType.CV_32FC2) {
1802 put(0, 0, 1, 0, -1, 2, -2, 12);
1805 assertMatEqual(truth, roots, EPS);
1808 public void testSort() {
1809 Mat submat = gray0.submat(0, gray0.rows() / 2, 0, gray0.cols() / 2);
1810 submat.setTo(new Scalar(1.0));
1812 Core.sort(gray0, dst, Core.SORT_EVERY_ROW);
1814 submat = dst.submat(0, dst.rows() / 2, dst.cols() / 2, dst.cols());
1815 assertTrue(submat.total() == Core.countNonZero(submat));
1817 Core.sort(gray0, dst, Core.SORT_EVERY_COLUMN);
1819 submat = dst.submat(dst.rows() / 2, dst.rows(), 0, dst.cols() / 2);
1821 assertTrue(submat.total() == Core.countNonZero(submat));
1824 public void testSortIdx() {
1825 Mat a = Mat.eye(3, 3, CvType.CV_8UC1);
1828 Core.sortIdx(a, b, Core.SORT_EVERY_ROW | Core.SORT_ASCENDING);
1830 truth = new Mat(3, 3, CvType.CV_32SC1) {
1837 assertMatEqual(truth, b);
1840 public void testSplit() {
1841 Mat m = getMat(CvType.CV_8UC3, 1, 2, 3);
1842 ArrayList<Mat> cois = new ArrayList<Mat>();
1844 Core.split(m, cois);
1846 assertMatEqual(gray1, cois.get(0));
1847 assertMatEqual(gray2, cois.get(1));
1848 assertMatEqual(gray3, cois.get(2));
1851 public void testSqrt() {
1852 Core.sqrt(gray9_32f, dst);
1854 assertMatEqual(gray3_32f, dst, EPS);
1856 Mat rgba144 = new Mat(matSize, matSize, CvType.CV_32FC4, Scalar.all(144));
1857 Mat rgba12 = new Mat(matSize, matSize, CvType.CV_32FC4, Scalar.all(12));
1859 Core.sqrt(rgba144, dst);
1861 assertMatEqual(rgba12, dst, EPS);
1864 public void testSubtractMatMatMat() {
1865 Core.subtract(gray128, gray1, dst);
1867 assertMatEqual(gray127, dst);
1870 public void testSubtractMatMatMatMat() {
1871 Mat mask = makeMask(gray1.clone());
1872 dst = gray128.clone();
1874 Core.subtract(gray128, gray1, dst, mask);
1876 assertMatEqual(makeMask(gray127, 128), dst);
1879 public void testSubtractMatMatMatMatInt() {
1880 Core.subtract(gray3, gray2, dst, gray1, CvType.CV_32F);
1882 assertMatEqual(gray1_32f, dst, EPS);
1885 public void testSumElems() {
1886 Mat src = new Mat(4, 4, CvType.CV_8U, new Scalar(10));
1888 Scalar res1 = Core.sumElems(src);
1890 assertScalarEqual(new Scalar(160), res1, EPS);
1893 public void testSVBackSubst() {
1894 Mat w = new Mat(2, 2, CvType.CV_32FC1, new Scalar(2));
1895 Mat u = new Mat(2, 2, CvType.CV_32FC1, new Scalar(4));
1896 Mat vt = new Mat(2, 2, CvType.CV_32FC1, new Scalar(2));
1897 Mat rhs = new Mat(2, 2, CvType.CV_32FC1, new Scalar(1));
1899 Core.SVBackSubst(w, u, vt, rhs, dst);
1901 Mat truth = new Mat(2, 2, CvType.CV_32FC1, new Scalar(16));
1902 assertMatEqual(truth, dst, EPS);
1905 public void testSVDecompMatMatMatMat() {
1906 Mat src = new Mat(1, 4, CvType.CV_32FC1) {
1908 put(0, 0, 1, 4, 8, 6);
1915 Core.SVDecomp(src, w, u, vt);
1917 Mat truthW = new Mat(1, 1, CvType.CV_32FC1, new Scalar(10.816654));
1918 Mat truthU = new Mat(1, 1, CvType.CV_32FC1, new Scalar(1));
1919 Mat truthVT = new Mat(1, 4, CvType.CV_32FC1) {
1921 put(0, 0, 0.09245003, 0.36980012, 0.73960024, 0.5547002);
1924 assertMatEqual(truthW, w, EPS);
1925 assertMatEqual(truthU, u, EPS);
1926 assertMatEqual(truthVT, vt, EPS);
1929 public void testSVDecompMatMatMatMatInt() {
1930 Mat src = new Mat(1, 4, CvType.CV_32FC1) {
1932 put(0, 0, 1, 4, 8, 6);
1939 Core.SVDecomp(src, w, u, vt, Core.SVD_NO_UV);
1941 Mat truthW = new Mat(1, 1, CvType.CV_32FC1, new Scalar(10.816654));
1942 assertMatEqual(truthW, w, EPS);
1943 assertTrue(u.empty());
1944 assertTrue(vt.empty());
1947 public void testTrace() {
1948 Scalar s = Core.trace(gray1);
1950 assertEquals(new Scalar(matSize), s);
1953 public void testTransform() {
1954 Mat src = new Mat(2, 2, CvType.CV_32F, new Scalar(55));
1955 Mat m = Mat.eye(2, 2, CvType.CV_32FC1);
1957 Core.transform(src, dst, m);
1959 truth = new Mat(2, 2, CvType.CV_32FC2, new Scalar(55, 1));
1960 assertMatEqual(truth, dst, EPS);
1963 public void testTranspose() {
1964 gray0.submat(0, gray0.rows() / 2, 0, gray0.cols()).setTo(new Scalar(1));
1965 Mat destination = getMat(CvType.CV_8U, 0);
1967 Core.transpose(gray0, destination);
1969 Mat subdst = destination.submat(0, destination.rows(), 0, destination.cols() / 2);
1970 assertTrue(subdst.total() == Core.countNonZero(subdst));
1973 public void testVconcat() {
1974 List<Mat> mats = Arrays.asList(Mat.eye(3, 3, CvType.CV_8U), Mat.zeros(2, 3, CvType.CV_8U));
1976 Core.vconcat(mats, dst);
1978 assertMatEqual(Mat.eye(5, 3, CvType.CV_8U), dst);
1982 public void testCopyMakeBorderMatMatIntIntIntIntInt() {
1983 Mat src = new Mat(2, 2, CvType.CV_32F, new Scalar(1));
1986 Core.copyMakeBorder(src, dst, border, border, border, border, Core.BORDER_REPLICATE);
1988 truth = new Mat(6, 6, CvType.CV_32F, new Scalar(1));
1989 assertMatEqual(truth, dst, EPS);
1992 public void testCopyMakeBorderMatMatIntIntIntIntIntScalar() {
1993 Mat src = new Mat(2, 2, CvType.CV_32F, new Scalar(1));
1995 Scalar value = new Scalar(0);
1998 Core.copyMakeBorder(src, dst, border, border, border, border, Core.BORDER_REPLICATE, value);
1999 // TODO_: write better test (use Core.BORDER_CONSTANT)
2001 truth = new Mat(6, 6, CvType.CV_32F, new Scalar(1));
2002 assertMatEqual(truth, dst, EPS);
2005 public void testBorderInterpolate() {
2006 float val1 = Core.borderInterpolate(100, 150, Core.BORDER_REFLECT_101);
2007 assertEquals(100f, val1);
2009 float val2 = Core.borderInterpolate(-5, 10, Core.BORDER_WRAP);
2010 assertEquals(5f, val2);