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;
23 public class CoreTest extends OpenCVTestCase {
25 public void testAbsdiff() {
26 Core.absdiff(gray128, gray255, dst);
28 assertMatEqual(gray127, dst);
31 public void testAddMatMatMat() {
32 Core.add(gray128, gray128, dst);
34 assertMatEqual(gray255, dst);
37 public void testAddMatMatMatMatInt() {
38 Core.add(gray0, gray1, dst, gray1, CvType.CV_32F);
40 assertEquals(CvType.CV_32F, dst.depth());
41 assertMatEqual(gray1_32f, dst, EPS);
44 public void testAddWeightedMatDoubleMatDoubleDoubleMat() {
45 Core.addWeighted(gray1, 120.0, gray127, 1.0, 10.0, dst);
47 assertMatEqual(gray255, dst);
50 public void testAddWeightedMatDoubleMatDoubleDoubleMatInt() {
51 Core.addWeighted(gray1, 126.0, gray127, 1.0, 2.0, dst, CvType.CV_32F);
53 assertEquals(CvType.CV_32F, dst.depth());
54 assertMatEqual(gray255_32f, dst, EPS);
57 public void testBitwise_andMatMatMat() {
58 Core.bitwise_and(gray127, gray3, dst);
60 assertMatEqual(gray3, dst);
63 public void testBitwise_andMatMatMatMat() {
64 Core.bitwise_and(gray3, gray1, dst, gray255);
66 assertMatEqual(gray1, dst);
69 public void testBitwise_notMatMat() {
70 Core.bitwise_not(gray255, dst);
72 assertMatEqual(gray0, dst);
75 public void testBitwise_notMatMatMat() {
76 Core.bitwise_not(gray0, dst, gray1);
78 assertMatEqual(gray255, dst);
81 public void testBitwise_orMatMatMat() {
82 Core.bitwise_or(gray1, gray2, dst);
84 assertMatEqual(gray3, dst);
87 public void testBitwise_orMatMatMatMat() {
88 Core.bitwise_or(gray127, gray3, dst, gray255);
90 assertMatEqual(gray127, dst);
93 public void testBitwise_xorMatMatMat() {
94 Core.bitwise_xor(gray3, gray2, dst);
96 assertMatEqual(gray1, dst);
99 public void testBitwise_xorMatMatMatMat() {
100 Core.bitwise_or(gray127, gray128, dst, gray255);
102 assertMatEqual(gray255, dst);
105 public void testCalcCovarMatrixMatMatMatInt() {
106 Mat covar = new Mat(matSize, matSize, CvType.CV_64FC1);
107 Mat mean = new Mat(1, matSize, CvType.CV_64FC1);
109 Core.calcCovarMatrix(gray0_32f, covar, mean, Core.COVAR_ROWS | Core.COVAR_NORMAL);
111 assertMatEqual(gray0_64f, covar, EPS);
112 assertMatEqual(gray0_64f_1d, mean, EPS);
115 public void testCalcCovarMatrixMatMatMatIntInt() {
116 Mat covar = new Mat(matSize, matSize, CvType.CV_32F);
117 Mat mean = new Mat(1, matSize, CvType.CV_32F);
119 Core.calcCovarMatrix(gray0_32f, covar, mean, Core.COVAR_ROWS | Core.COVAR_NORMAL, CvType.CV_32F);
121 assertMatEqual(gray0_32f, covar, EPS);
122 assertMatEqual(gray0_32f_1d, mean, EPS);
125 public void testCartToPolarMatMatMatMat() {
126 Mat x = new Mat(1, 3, CvType.CV_32F) {
128 put(0, 0, 3.0, 6.0, 5, 0);
131 Mat y = new Mat(1, 3, CvType.CV_32F) {
133 put(0, 0, 4.0, 8.0, 12.0);
136 Mat dst_angle = new Mat();
138 Core.cartToPolar(x, y, dst, dst_angle);
140 Mat expected_magnitude = new Mat(1, 3, CvType.CV_32F) {
142 put(0, 0, 5.0, 10.0, 13.0);
145 Mat expected_angle = new Mat(1, 3, CvType.CV_32F) {
147 put(0, 0, atan2rad(4,3), atan2rad(8,6), atan2rad(12,5));
150 assertMatEqual(expected_magnitude, dst, EPS);
151 assertMatEqual(expected_angle, dst_angle, EPS);
154 public void testCartToPolarMatMatMatMatBoolean() {
155 Mat x = new Mat(1, 3, CvType.CV_32F) {
157 put(0, 0, 3.0, 6.0, 5, 0);
160 Mat y = new Mat(1, 3, CvType.CV_32F) {
162 put(0, 0, 4.0, 8.0, 12.0);
165 Mat dst_angle = new Mat();
167 Core.cartToPolar(x, y, dst, dst_angle, true);
169 Mat expected_magnitude = new Mat(1, 3, CvType.CV_32F) {
171 put(0, 0, 5.0, 10.0, 13.0);
174 Mat expected_angle = new Mat(1, 3, CvType.CV_32F) {
176 put(0, 0, atan2deg(4,3), atan2deg(8,6), atan2deg(12,5));
179 assertMatEqual(expected_magnitude, dst, EPS);
180 assertMatEqual(expected_angle, dst_angle, EPS * 180/Math.PI);
184 public void testCheckRangeMat() {
185 Mat outOfRange = new Mat(2, 2, CvType.CV_64F);
186 outOfRange.put(0, 0, Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0);
188 assertTrue(Core.checkRange(grayRnd_32f));
189 assertTrue(Core.checkRange(new Mat()));
190 assertFalse(Core.checkRange(outOfRange));
194 public void testCheckRangeMatBooleanPointDoubleDouble() {
195 Mat inRange = new Mat(2, 3, CvType.CV_64F) {
197 put(0, 0, 14, 48, 76, 33, 5, 99);
201 assertTrue(Core.checkRange(inRange, true, 5, 100));
203 Mat outOfRange = new Mat(2, 3, CvType.CV_64F) {
205 put(0, 0, -4, 0, 6, 33, 4, 109);
209 assertFalse(Core.checkRange(outOfRange, true, 5, 100));
212 public void testCompare() {
213 Core.compare(gray0, gray0, dst, Core.CMP_EQ);
215 assertMatEqual(dst, gray255);
217 Core.compare(gray0, gray1, dst, Core.CMP_EQ);
219 assertMatEqual(dst, gray0);
221 grayRnd.put(0, 0, 0, 0);
223 Core.compare(gray0, grayRnd, dst, Core.CMP_GE);
225 int expected = (int) (grayRnd.total() - Core.countNonZero(grayRnd));
226 assertEquals(expected, Core.countNonZero(dst));
229 public void testCompleteSymmMat() {
230 Core.completeSymm(grayRnd_32f);
232 assertMatEqual(grayRnd_32f, grayRnd_32f.t(), EPS);
235 public void testCompleteSymmMatBoolean() {
236 Mat grayRnd_32f2 = grayRnd_32f.clone();
238 Core.completeSymm(grayRnd_32f, true);
240 assertMatEqual(grayRnd_32f, grayRnd_32f.t(), EPS);
241 Core.completeSymm(grayRnd_32f2, false);
242 assertMatNotEqual(grayRnd_32f2, grayRnd_32f, EPS);
245 public void testConvertScaleAbsMatMat() {
246 Core.convertScaleAbs(gray0, dst);
248 assertMatEqual(gray0, dst, EPS);
250 Core.convertScaleAbs(gray_16u_256, dst);
252 assertMatEqual(gray255, dst, EPS);
255 public void testConvertScaleAbsMatMatDoubleDouble() {
256 Core.convertScaleAbs(gray_16u_256, dst, 2, -513);
258 assertMatEqual(gray1, dst);
261 public void testCountNonZero() {
262 assertEquals(0, Core.countNonZero(gray0));
264 gray0.put(0, 0, 255);
265 gray0.put(gray0.rows() - 1, gray0.cols() - 1, 255);
267 assertEquals(2, Core.countNonZero(gray0));
270 public void testCubeRoot() {
271 float res = Core.cubeRoot(-27.0f);
273 assertEquals(-3.0f, res);
276 public void testDctMatMat() {
277 Mat in = new Mat(1, 4, CvType.CV_32F) {
279 put(0, 0, 135.22211, 50.811096, 102.27016, 207.6682);
282 Mat dst1 = new Mat();
283 Mat dst2 = new Mat();
285 Core.dct(gray0_32f_1d, dst1);
288 truth = new Mat(1, 4, CvType.CV_32F) {
290 put(0, 0, 247.98576, -61.252407, 94.904533, 14.013477);
293 assertMatEqual(gray0_32f_1d, dst1, EPS);
294 assertMatEqual(truth, dst2, EPS);
297 public void testDctMatMatInt() {
298 Mat in = new Mat(1, 4, CvType.CV_32F) {
300 put(0, 0, 247.98576, -61.252407, 94.904533, 14.013477);
303 Mat dst1 = new Mat();
304 Mat dst2 = new Mat();
306 Core.dct(gray0_32f_1d, dst1, Core.DCT_INVERSE);
307 Core.dct(in, dst2, Core.DCT_INVERSE);
309 truth = new Mat(1, 4, CvType.CV_32F) {
311 put(0, 0, 135.22211, 50.811096, 102.27016, 207.6682);
314 assertMatEqual(gray0_32f_1d, dst1, EPS);
315 assertMatEqual(truth, dst2, EPS);
318 public void testDeterminant() {
319 Mat mat = new Mat(2, 2, CvType.CV_32F) {
328 double det = Core.determinant(mat);
330 assertEquals(8.0, det);
333 public void testDftMatMat() {
334 Core.dft(gray0_32f_1d, dst);
336 assertMatEqual(gray0_32f_1d, dst, EPS);
339 public void testDftMatMatIntInt() {
340 Mat src1 = new Mat(2, 4, CvType.CV_32F) {
342 put(0, 0, 1, 2, 3, 4);
343 put(1, 0, 1, 1, 1, 1);
346 Mat src2 = new Mat(2, 4, CvType.CV_32F) {
348 put(0, 0, 1, 2, 3, 4);
349 put(1, 0, 0, 0, 0, 0);
352 Mat dst1 = new Mat();
353 Mat dst2 = new Mat();
355 Core.dft(src1, dst1, Core.DFT_REAL_OUTPUT, 1);
356 Core.dft(src2, dst2, Core.DFT_REAL_OUTPUT, 0);
358 assertMatEqual(dst2, dst1, EPS);
361 public void testDivideDoubleMatMat() {
362 Core.divide(4.0, gray2, dst);
364 assertMatEqual(gray2, dst);
366 Core.divide(4.0, gray0, dst);
368 assertMatEqual(gray0, dst);
371 public void testDivideDoubleMatMatInt() {
372 Core.divide(9.0, gray3, dst, CvType.CV_32F);
374 assertMatEqual(gray3_32f, dst, EPS);
377 public void testDivideMatMatMat() {
378 Core.divide(gray9, gray3, dst);
380 assertMatEqual(gray3, dst);
383 public void testDivideMatMatMatDouble() {
384 Core.divide(gray1, gray2, dst, 6.0);
386 assertMatEqual(gray3, dst);
389 public void testDivideMatMatMatDoubleInt() {
390 Core.divide(gray1, gray2, dst, 6.0, CvType.CV_32F);
392 assertMatEqual(gray3_32f, dst, EPS);
395 public void testEigen() {
396 Mat src = new Mat(3, 3, CvType.CV_32FC1, new Scalar(2.0));
397 Mat eigenVals = new Mat();
398 Mat eigenVecs = new Mat();
400 Core.eigen(src, eigenVals, eigenVecs);
402 Mat expectedEigenVals = new Mat(3, 1, CvType.CV_32FC1) {
407 Mat expectedEigenVecs = new Mat(3, 3, CvType.CV_32FC1) {
409 put(0, 0, 0.57735026, 0.57735026, 0.57735032);
410 put(1, 0, 0.70710677, -0.70710677, 0);
411 put(2, 0, -0.40824831, -0.40824831, 0.81649661);
414 assertMatEqual(eigenVals, expectedEigenVals, EPS);
415 assertMatEqual(eigenVecs, expectedEigenVecs, EPS);
418 public void testExp() {
419 Core.exp(gray0_32f, dst);
421 assertMatEqual(gray1_32f, dst, EPS);
424 public void testExtractChannel() {
425 Core.extractChannel(rgba128, dst, 0);
427 assertMatEqual(gray128, dst);
430 public void testFastAtan2() {
433 float res = Core.fastAtan2(50, 50);
435 assertEquals(45, res, eps);
437 float res2 = Core.fastAtan2(80, 20);
439 assertEquals(Math.atan2(80, 20) * 180 / Math.PI, res2, eps);
442 public void testFillConvexPolyMatListOfPointScalar() {
443 MatOfPoint polyline = new MatOfPoint(new Point[]{new Point(1, 1), new Point(5, 0), new Point(6, 8), new Point(0, 9)});
445 Imgproc.fillConvexPoly(gray0, polyline, new Scalar(150));
447 assertTrue(0 < Core.countNonZero(gray0));
448 assertTrue(gray0.total() > Core.countNonZero(gray0));
451 public void testFillConvexPolyMatListOfPointScalarIntInt() {
452 MatOfPoint polyline1 = new MatOfPoint(new Point(2, 1), new Point(5, 1), new Point(5, 7), new Point(2, 7));
453 MatOfPoint polyline2 = new MatOfPoint(new Point(4, 2), new Point(10, 2), new Point(10, 14), new Point(4, 14));
455 // current implementation of fixed-point version of fillConvexPoly
456 // requires image to be at least 2-pixel wider in each direction than
458 Imgproc.fillConvexPoly(gray0, polyline1, colorWhite, Imgproc.line_8, 0);
460 assertTrue(0 < Core.countNonZero(gray0));
461 assertTrue(gray0.total() > Core.countNonZero(gray0));
463 Imgproc.fillConvexPoly(gray0, polyline2, colorBlack, Imgproc.line_8, 1);
465 assertEquals("see http://code.opencv.org/issues/1284", 0, Core.countNonZero(gray0));
468 public void testFillPolyMatListOfListOfPointScalar() {
470 Mat gray0 = Mat.zeros(matSize, matSize, CvType.CV_8U);
471 MatOfPoint polyline = new MatOfPoint(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4));
472 List<MatOfPoint> polylines = new ArrayList<MatOfPoint>();
473 polylines.add(polyline);
475 Imgproc.fillPoly(gray0, polylines, new Scalar(1));
477 final byte[] truth = new byte[] {
478 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
479 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
480 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
481 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
482 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
483 0, 1, 1, 0, 0, 0, 1, 1, 0, 0,
484 0, 1, 1, 0, 0, 0, 1, 1, 0, 0,
485 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
486 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
487 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
489 assertMatEqual(new Mat(gray0.size(), CvType.CV_8U) {
496 public void testFillPolyMatListOfListOfPointScalarIntIntPoint() {
497 MatOfPoint polyline1 = new MatOfPoint(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4));
498 MatOfPoint polyline2 = new MatOfPoint(new Point(0, 3), new Point(0, 7), new Point(3, 0), new Point(6, 7), new Point(6, 3));
500 List<MatOfPoint> polylines1 = new ArrayList<MatOfPoint>();
501 polylines1.add(polyline1);
503 List<MatOfPoint> polylines2 = new ArrayList<MatOfPoint>();
504 polylines2.add(polyline2);
506 Imgproc.fillPoly(gray0, polylines1, new Scalar(1), Imgproc.line_8, 0, new Point(0, 0));
508 assertTrue(0 < Core.countNonZero(gray0));
510 Imgproc.fillPoly(gray0, polylines2, new Scalar(0), Imgproc.line_8, 0, new Point(1, 1));
512 assertEquals(0, Core.countNonZero(gray0));
515 public void testFlip() {
516 Mat src = new Mat(2, 2, CvType.CV_32F) {
524 Mat dst1 = new Mat();
525 Mat dst2 = new Mat();
527 Core.flip(src, dst1, 0);
528 Core.flip(src, dst2, 1);
530 Mat dst_f1 = new Mat(2, 2, CvType.CV_32F) {
538 Mat dst_f2 = new Mat(2, 2, CvType.CV_32F) {
546 assertMatEqual(dst_f1, dst1, EPS);
547 assertMatEqual(dst_f2, dst2, EPS);
550 public void testGemmMatMatDoubleMatDoubleMat() {
551 Mat m1 = new Mat(2, 2, CvType.CV_32FC1) {
557 Mat m2 = new Mat(2, 2, CvType.CV_32FC1) {
563 Mat dmatrix = new Mat(2, 2, CvType.CV_32FC1) {
565 put(0, 0, 0.001, 0.001);
566 put(1, 0, 0.001, 0.001);
570 Core.gemm(m1, m2, 1.0, dmatrix, 1.0, dst);
572 Mat expected = new Mat(2, 2, CvType.CV_32FC1) {
574 put(0, 0, 1.001, 0.001);
575 put(1, 0, 1.001, 0.001);
578 assertMatEqual(expected, dst, EPS);
581 public void testGemmMatMatDoubleMatDoubleMatInt() {
582 Mat m1 = new Mat(2, 2, CvType.CV_32FC1) {
588 Mat m2 = new Mat(2, 2, CvType.CV_32FC1) {
594 Mat dmatrix = new Mat(2, 2, CvType.CV_32FC1) {
596 put(0, 0, 0.001, 0.001);
597 put(1, 0, 0.001, 0.001);
601 Core.gemm(m1, m2, 1.0, dmatrix, 1.0, dst, Core.GEMM_1_T);
603 Mat expected = new Mat(2, 2, CvType.CV_32FC1) {
605 put(0, 0, 2.001, 0.001);
606 put(1, 0, 0.001, 0.001);
609 assertMatEqual(expected, dst, EPS);
612 public void testGetCPUTickCount() {
613 long cpuCountStart = 0, actualTickCount;
615 cpuCountStart = Core.getCPUTickCount();
616 Core.sumElems(gray255);
617 actualTickCount = Core.getCPUTickCount();
619 long expectedTickCount = actualTickCount - cpuCountStart;
620 assertTrue(expectedTickCount > 0);
623 public void testGetNumberOfCPUs() {
624 int cpus = Core.getNumberOfCPUs();
626 assertTrue(Runtime.getRuntime().availableProcessors() <= cpus);
629 public void testGetOptimalDFTSize() {
630 assertEquals(1, Core.getOptimalDFTSize(0));
631 assertEquals(135, Core.getOptimalDFTSize(133));
632 assertEquals(15, Core.getOptimalDFTSize(13));
635 public void testGetTickCount() {
636 long startCount, endCount, count;
638 startCount = Core.getTickCount();
639 Core.divide(gray2, gray1, dst);
640 endCount = Core.getTickCount();
642 count = endCount - startCount;
643 assertTrue(count > 0);
646 public void testGetTickFrequency() {
647 double freq1 = Core.getTickFrequency();
648 Core.divide(gray2, gray1, dst);
649 double freq2 = Core.getTickFrequency();
651 assertTrue(0 < freq1);
652 assertEquals(freq1, freq2);
655 public void testHconcat() {
656 List<Mat> mats = Arrays.asList(Mat.eye(3, 3, CvType.CV_8U), Mat.zeros(3, 2, CvType.CV_8U));
658 Core.hconcat(mats, dst);
660 assertMatEqual(Mat.eye(3, 5, CvType.CV_8U), dst);
663 public void testIdctMatMat() {
664 Mat in = new Mat(1, 8, CvType.CV_32F) {
666 put(0, 0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0);
672 truth = new Mat(1, 8, CvType.CV_32F) {
674 put(0, 0, 3.3769724, -1.6215782, 2.3608727, 0.20730907, -0.86502546, 0.028082132, -0.7673766, 0.10917115);
677 assertMatEqual(truth, dst, EPS);
680 public void testIdctMatMatInt() {
681 Mat in = new Mat(2, 8, CvType.CV_32F) {
683 put(0, 0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0);
684 put(1, 0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0);
688 Core.idct(in, dst, Core.DCT_ROWS);
690 truth = new Mat(2, 8, CvType.CV_32F) {
692 put(0, 0, 3.3769724, -1.6215782, 2.3608727, 0.20730907, -0.86502546, 0.028082132, -0.7673766, 0.10917115);
693 put(1, 0, 3.3769724, -1.6215782, 2.3608727, 0.20730907, -0.86502546, 0.028082132, -0.7673766, 0.10917115);
696 assertMatEqual(truth, dst, EPS);
699 public void testIdftMatMat() {
700 Mat in = new Mat(1, 4, CvType.CV_32F) {
702 put(0, 0, 1.0, 2.0, 3.0, 4.0);
708 truth = new Mat(1, 4, CvType.CV_32F) {
710 put(0, 0, 9, -9, 1, 3);
713 assertMatEqual(truth, dst, EPS);
716 public void testIdftMatMatIntInt() {
717 Mat in = new Mat(2, 4, CvType.CV_32F) {
719 put(0, 0, 1.0, 2.0, 3.0, 4.0);
720 put(1, 0, 1.0, 2.0, 3.0, 4.0);
725 Core.idft(in, dst, Core.DFT_REAL_OUTPUT, 1);
727 truth = new Mat(2, 4, CvType.CV_32F) {
729 put(0, 0, 18, -18, 2, 6);
730 put(1, 0, 0, 0, 0, 0);
733 assertMatEqual(truth, dst, EPS);
736 public void testInRange() {
737 gray0.put(1, 1, 100, 150, 200);
739 Core.inRange(gray0, new Scalar(120), new Scalar(160), dst);
741 byte vals[] = new byte[3];
744 assertEquals(0, vals[0]);
745 assertEquals(-1, vals[1]);
746 assertEquals(0, vals[2]);
747 assertEquals(1, Core.countNonZero(dst));
750 public void testInsertChannel() {
751 Core.insertChannel(gray0, rgba128, 0);
752 Core.insertChannel(gray0, rgba128, 1);
753 Core.insertChannel(gray0, rgba128, 2);
754 Core.insertChannel(gray0, rgba128, 3);
756 assertMatEqual(rgba0, rgba128);
759 public void testInvertMatMat() {
760 Mat src = new Mat(2, 2, CvType.CV_32F) {
769 Core.invert(src, dst);
771 truth = new Mat(2, 2, CvType.CV_32F) {
779 assertMatEqual(truth, dst, EPS);
782 public void testInvertMatMatInt() {
783 Mat src = Mat.eye(3, 3, CvType.CV_32FC1);
786 double cond = Core.invert(src, dst, Core.DECOMP_SVD);
788 truth = Mat.eye(3, 3, CvType.CV_32FC1);
790 assertMatEqual(truth, dst, EPS);
791 assertEquals(0.3819660544395447, cond, EPS);
794 public void testKmeansMatIntMatTermCriteriaIntInt() {
795 Mat data = new Mat(4, 5, CvType.CV_32FC1) {
797 put(0, 0, 1, 2, 3, 4, 5);
798 put(1, 0, 2, 3, 4, 5, 6);
799 put(2, 0, 5, 4, 3, 2, 1);
800 put(3, 0, 6, 5, 4, 3, 2);
803 TermCriteria criteria = new TermCriteria(TermCriteria.EPS, 0, EPS);
804 Mat labels = new Mat();
806 Core.kmeans(data, 2, labels, criteria, 1, Core.KMEANS_PP_CENTERS);
808 int[] first_center = new int[1];
809 labels.get(0, 0, first_center);
810 final int c1 = first_center[0];
811 Mat expected_labels = new Mat(4, 1, CvType.CV_32S) {
813 put(0, 0, c1, c1, 1 - c1, 1 - c1);
816 assertMatEqual(expected_labels, labels);
819 public void testKmeansMatIntMatTermCriteriaIntIntMat() {
820 Mat data = new Mat(4, 5, CvType.CV_32FC1) {
822 put(0, 0, 1, 2, 3, 4, 5);
823 put(1, 0, 2, 3, 4, 5, 6);
824 put(2, 0, 5, 4, 3, 2, 1);
825 put(3, 0, 6, 5, 4, 3, 2);
828 TermCriteria criteria = new TermCriteria(TermCriteria.EPS, 0, EPS);
829 Mat labels = new Mat();
830 Mat centers = new Mat();
832 Core.kmeans(data, 2, labels, criteria, 6, Core.KMEANS_RANDOM_CENTERS, centers);
834 int[] first_center = new int[1];
835 labels.get(0, 0, first_center);
836 final int c1 = first_center[0];
837 Mat expected_labels = new Mat(4, 1, CvType.CV_32S) {
839 put(0, 0, c1, c1, 1 - c1, 1 - c1);
842 Mat expected_centers = new Mat(2, 5, CvType.CV_32FC1) {
844 put(c1, 0, 1.5, 2.5, 3.5, 4.5, 5.5);
845 put(1 - c1, 0, 5.5, 4.5, 3.5, 2.5, 1.5);
848 assertMatEqual(expected_labels, labels);
849 assertMatEqual(expected_centers, centers, EPS);
852 public void testLineMatPointPointScalar() {
853 int nPoints = Math.min(gray0.cols(), gray0.rows());
854 Point point1 = new Point(0, 0);
855 Point point2 = new Point(nPoints, nPoints);
856 Scalar color = new Scalar(255);
858 Imgproc.line(gray0, point1, point2, color);
860 assertTrue(nPoints == Core.countNonZero(gray0));
863 public void testLineMatPointPointScalarInt() {
864 int nPoints = Math.min(gray0.cols(), gray0.rows());
865 Point point1 = new Point(0, 0);
866 Point point2 = new Point(nPoints, nPoints);
868 Imgproc.line(gray0, point1, point2, colorWhite, 0);
870 assertTrue(nPoints == Core.countNonZero(gray0));
873 public void testLineMatPointPointScalarIntIntInt() {
874 int nPoints = Math.min(gray0.cols(), gray0.rows());
875 Point point1 = new Point(3, 4);
876 Point point2 = new Point(nPoints, nPoints);
877 Point point1_4 = new Point(3 * 4, 4 * 4);
878 Point point2_4 = new Point(nPoints * 4, nPoints * 4);
880 Imgproc.line(gray0, point2, point1, colorWhite, 2, Imgproc.line_8, 0);
882 assertFalse(0 == Core.countNonZero(gray0));
884 Imgproc.line(gray0, point2_4, point1_4, colorBlack, 2, Imgproc.line_8, 2);
886 assertEquals(0, Core.countNonZero(gray0));
889 public void testLog() {
890 Mat in = new Mat(1, 4, CvType.CV_32FC1) {
892 put(0, 0, 1.0, 10.0, 100.0, 1000.0);
898 Mat expected = new Mat(1, 4, CvType.CV_32FC1) {
900 put(0, 0, 0, 2.3025851, 4.6051702, 6.9077554);
903 assertMatEqual(expected, dst, EPS);
906 public void testLUTMatMatMat() {
907 Mat lut = new Mat(1, 256, CvType.CV_8UC1);
908 lut.setTo(new Scalar(0));
910 Core.LUT(grayRnd, lut, dst);
912 assertMatEqual(gray0, dst);
914 lut.setTo(new Scalar(255));
916 Core.LUT(grayRnd, lut, dst);
918 assertMatEqual(gray255, dst);
921 public void testMagnitude() {
922 Mat x = new Mat(1, 4, CvType.CV_32F);
923 Mat y = new Mat(1, 4, CvType.CV_32F);
924 x.put(0, 0, 3.0, 5.0, 9.0, 6.0);
925 y.put(0, 0, 4.0, 12.0, 40.0, 8.0);
927 Core.magnitude(x, y, dst);
929 Mat out = new Mat(1, 4, CvType.CV_32F);
930 out.put(0, 0, 5.0, 13.0, 41.0, 10.0);
931 assertMatEqual(out, dst, EPS);
933 Core.magnitude(gray0_32f, gray255_32f, dst);
935 assertMatEqual(gray255_32f, dst, EPS);
938 public void testMahalanobis() {
939 Mat covar = new Mat(matSize, matSize, CvType.CV_32F);
940 Mat mean = new Mat(1, matSize, CvType.CV_32F);
941 Core.calcCovarMatrix(grayRnd_32f, covar, mean, Core.COVAR_ROWS | Core.COVAR_NORMAL, CvType.CV_32F);
943 Mat line1 = grayRnd_32f.row(0);
944 Mat line2 = grayRnd_32f.row(1);
946 double d = Core.Mahalanobis(line1, line1, covar);
948 assertEquals(0.0, d);
950 d = Core.Mahalanobis(line1, line2, covar);
955 public void testMax() {
956 Core.max(gray0, gray255, dst);
958 assertMatEqual(gray255, dst);
960 Mat x = new Mat(1, 1, CvType.CV_32F);
961 Mat y = new Mat(1, 1, CvType.CV_32F);
967 Mat truth = new Mat(1, 1, CvType.CV_32F);
968 truth.put(0, 0, 23.0);
969 assertMatEqual(truth, dst, EPS);
972 public void testMeanMat() {
973 Scalar mean = Core.mean(makeMask(gray128));
975 assertScalarEqual(new Scalar(64), mean, EPS);
978 public void testMeanMatMat() {
979 Mat mask1 = makeMask(gray1.clone());
980 Mat mask2 = makeMask(gray0, 1);
982 Scalar mean1 = Core.mean(grayRnd, mask1);
983 Scalar mean2 = Core.mean(grayRnd, mask2);
984 Scalar mean = Core.mean(grayRnd, gray1);
986 assertScalarEqual(mean, new Scalar(0.5 * (mean1.val[0] + mean2.val[0])), EPS);
989 public void testMeanStdDevMatMatMat() {
990 MatOfDouble mean = new MatOfDouble();
991 MatOfDouble stddev = new MatOfDouble();
993 Core.meanStdDev(rgbLena, mean, stddev);
995 double expectedMean[] = new double[]
996 {105.3989906311035, 99.56269836425781, 179.7303047180176};
997 double expectedDev[] = new double[]
998 {33.74205485167219, 52.8734582803278, 49.01569488056406};
1000 assertArrayEquals(expectedMean, mean.toArray(), EPS);
1001 assertArrayEquals(expectedDev, stddev.toArray(), EPS);
1004 public void testMeanStdDevMatMatMatMat() {
1005 Mat submat = grayRnd.submat(0, grayRnd.rows() / 2, 0, grayRnd.cols() / 2);
1006 submat.setTo(new Scalar(33));
1007 Mat mask = gray0.clone();
1008 submat = mask.submat(0, mask.rows() / 2, 0, mask.cols() / 2);
1009 submat.setTo(new Scalar(1));
1010 MatOfDouble mean = new MatOfDouble();
1011 MatOfDouble stddev = new MatOfDouble();
1013 Core.meanStdDev(grayRnd, mean, stddev, mask);
1015 double expectedMean[] = new double[] {33d};
1016 double expectedDev[] = new double[] {0d};
1018 assertArrayEquals(expectedMean, mean.toArray(), EPS);
1019 assertArrayEquals(expectedDev, stddev.toArray(), EPS);
1022 public void testMerge() {
1023 Mat src1 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(1));
1024 Mat src2 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(2));
1025 Mat src3 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(3));
1026 List<Mat> listMat = Arrays.asList(src1, src2, src3);
1028 Core.merge(listMat, dst);
1030 truth = new Mat(2, 2, CvType.CV_32FC3, new Scalar(1, 2, 3));
1031 assertMatEqual(truth, dst, EPS);
1034 public void testMin() {
1035 Core.min(gray0, gray255, dst);
1037 assertMatEqual(gray0, dst);
1040 public void testMinMaxLocMat() {
1043 Point minLoc = new Point(gray3.cols() / 4, gray3.rows() / 2);
1044 Point maxLoc = new Point(gray3.cols() / 2, gray3.rows() / 4);
1045 gray3.put((int) minLoc.y, (int) minLoc.x, minVal);
1046 gray3.put((int) maxLoc.y, (int) maxLoc.x, maxVal);
1048 Core.MinMaxLocResult mmres = Core.minMaxLoc(gray3);
1050 assertEquals(minVal, mmres.minVal);
1051 assertEquals(maxVal, mmres.maxVal);
1052 assertPointEquals(minLoc, mmres.minLoc, EPS);
1053 assertPointEquals(maxLoc, mmres.maxLoc, EPS);
1056 public void testMinMaxLocMatMat() {
1057 Mat src = new Mat(4, 4, CvType.CV_8U) {
1059 put(0, 0, 2, 4, 27, 3);
1060 put(1, 0, 0, 8, 7, 130);
1061 put(2, 0, 13, 4, 13, 4);
1062 put(3, 0, 6, 4, 2, 13);
1065 Mat mask = new Mat(4, 4, CvType.CV_8U, new Scalar(0));
1066 mask.submat(1, 3, 1, 4).setTo(new Scalar(1));
1068 MinMaxLocResult res = Core.minMaxLoc(src, mask);
1070 assertEquals(4.0, res.minVal);
1071 assertEquals(130.0, res.maxVal);
1072 assertPointEquals(new Point(1, 2), res.minLoc, EPS);
1073 assertPointEquals(new Point(3, 1), res.maxLoc, EPS);
1076 public void testMixChannels() {
1077 rgba0.setTo(new Scalar(10, 20, 30, 40));
1078 List<Mat> src = Arrays.asList(rgba0);
1079 List<Mat> dst = Arrays.asList(gray3, gray2, gray1, gray0, getMat(CvType.CV_8UC3, 0, 0, 0));
1080 MatOfInt fromTo = new MatOfInt(
1090 Core.mixChannels(src, dst, fromTo);
1092 assertMatEqual(getMat(CvType.CV_8U, 40), dst.get(0));
1093 assertMatEqual(getMat(CvType.CV_8U, 40), dst.get(1));
1094 assertMatEqual(getMat(CvType.CV_8U, 30), dst.get(2));
1095 assertMatEqual(getMat(CvType.CV_8U, 10), dst.get(3));
1096 assertMatEqual(getMat(CvType.CV_8UC3, 30, 20, 10), dst.get(4));
1099 public void testMulSpectrumsMatMatMatInt() {
1100 Mat src1 = new Mat(1, 4, CvType.CV_32F) {
1102 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1105 Mat src2 = new Mat(1, 4, CvType.CV_32F) {
1107 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1111 Core.mulSpectrums(src1, src2, dst, Core.DFT_ROWS);
1113 Mat expected = new Mat(1, 4, CvType.CV_32F) {
1115 put(0, 0, 1, -5, 12, 16);
1118 assertMatEqual(expected, dst, EPS);
1121 public void testMulSpectrumsMatMatMatIntBoolean() {
1122 Mat src1 = new Mat(1, 4, CvType.CV_32F) {
1124 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1127 Mat src2 = new Mat(1, 4, CvType.CV_32F) {
1129 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1133 Core.mulSpectrums(src1, src2, dst, Core.DFT_ROWS, true);
1135 Mat expected = new Mat(1, 4, CvType.CV_32F) {
1137 put(0, 0, 1, 13, 0, 16);
1140 assertMatEqual(expected, dst, EPS);
1143 public void testMultiplyMatMatMat() {
1144 Core.multiply(gray0, gray255, dst);
1146 assertMatEqual(gray0, dst);
1149 public void testMultiplyMatMatMatDouble() {
1150 Core.multiply(gray1, gray1, dst, 2.0);
1152 assertMatEqual(gray2, dst);
1156 public void testMultiplyMatMatMatDoubleInt() {
1157 Core.multiply(gray1, gray2, dst, 1.5, CvType.CV_32F);
1159 assertMatEqual(gray3_32f, dst, EPS);
1162 public void testMulTransposedMatMatBoolean() {
1163 Core.mulTransposed(grayE_32f, dst, true);
1165 assertMatEqual(grayE_32f, dst, EPS);
1168 public void testMulTransposedMatMatBooleanMatDouble() {
1169 Core.mulTransposed(grayE_32f, dst, true, gray0_32f, 2);
1172 truth.diag().setTo(new Scalar(2));
1173 assertMatEqual(truth, dst, EPS);
1176 public void testMulTransposedMatMatBooleanMatDoubleInt() {
1177 Mat a = getMat(CvType.CV_32F, 1);
1179 Core.mulTransposed(a, dst, true, gray0_32f, 3, CvType.CV_64F);
1181 assertMatEqual(getMat(CvType.CV_64F, 3 * a.rows()), dst, EPS);
1184 public void testNormalizeMatMat() {
1185 Mat m = gray0.clone();
1186 m.diag().setTo(new Scalar(2));
1188 Core.normalize(m, dst);
1190 assertMatEqual(gray0, dst);
1193 public void testNormalizeMatMatDoubleDoubleInt() {
1194 Mat src = new Mat(1, 4, CvType.CV_32F) {
1196 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1200 Core.normalize(src, dst, 1.0, 2.0, Core.NORM_INF);
1202 Mat expected = new Mat(1, 4, CvType.CV_32F) {
1204 put(0, 0, 0.25, 0.5, 0.75, 1);
1207 assertMatEqual(expected, dst, EPS);
1210 public void testNormalizeMatMatDoubleDoubleIntInt() {
1211 Mat src = new Mat(1, 5, CvType.CV_32F) {
1213 put(0, 0, 0, 1, 2, 3, 4);
1217 Core.normalize(src, dst, 1, 2, Core.NORM_MINMAX, CvType.CV_64F);
1219 Mat expected = new Mat(1, 5, CvType.CV_64F) {
1221 put(0, 0, 1, 1.25, 1.5, 1.75, 2);
1224 assertMatEqual(expected, dst, EPS);
1227 public void testNormalizeMatMatDoubleDoubleIntIntMat() {
1228 Mat src = new Mat(1, 5, CvType.CV_32F) {
1230 put(0, 0, 0, 1, 2, 3, 4);
1233 Mat mask = new Mat(1, 5, CvType.CV_8U) {
1235 put(0, 0, 1, 0, 0, 0, 1);
1240 Core.normalize(src, dst, 1, 2, Core.NORM_MINMAX, CvType.CV_32F, mask);
1242 Mat expected = new Mat(1, 5, CvType.CV_32F) {
1244 put(0, 0, 1, 1, 2, 3, 2);
1247 assertMatEqual(expected, dst, EPS);
1250 public void testNormMat() {
1251 double n = Core.norm(gray1);
1253 assertEquals(10., n);
1256 public void testNormMatInt() {
1257 double n = Core.norm(gray127, Core.NORM_INF);
1259 assertEquals(127., n);
1262 public void testNormMatIntMat() {
1263 double n = Core.norm(gray3, Core.NORM_L1, gray0);
1265 assertEquals(0.0, n);
1268 public void testNormMatMat() {
1269 double n = Core.norm(gray0, gray1);
1271 assertEquals(10.0, n);
1274 public void testNormMatMatInt() {
1275 double n = Core.norm(gray127, gray1, Core.NORM_INF);
1277 assertEquals(126.0, n);
1280 public void testNormMatMatIntMat() {
1281 double n = Core.norm(gray3, gray0, Core.NORM_L1, makeMask(gray0.clone(), 1));
1283 assertEquals(150.0, n);
1286 public void testPCABackProject() {
1287 Mat mean = new Mat(1, 4, CvType.CV_32F) {
1289 put(0, 0, 2, 4, 4, 8);
1292 Mat vectors = new Mat(1, 4, CvType.CV_32F, new Scalar(0)) {
1294 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1297 Mat data = new Mat(3, 1, CvType.CV_32F) {
1299 put(0, 0, -5, 0, -10);
1302 Mat result = new Mat();
1304 Core.PCABackProject(data, mean, vectors, result);
1306 Mat truth = new Mat(3, 4, CvType.CV_32F) {
1308 put(0, 0, 1, 2, 2, 4);
1309 put(1, 0, 2, 4, 4, 8);
1310 put(2, 0, 0, 0, 0, 0);
1313 assertMatEqual(truth, result, EPS);
1316 public void testPCAComputeMatMatMat() {
1317 Mat data = new Mat(3, 4, CvType.CV_32F) {
1319 put(0, 0, 1, 2, 2, 4);
1320 put(1, 0, 2, 4, 4, 8);
1321 put(2, 0, 3, 6, 6, 12);
1324 Mat mean = new Mat();
1325 Mat vectors = new Mat();
1327 Core.PCACompute(data, mean, vectors);
1329 Mat mean_truth = new Mat(1, 4, CvType.CV_32F) {
1331 put(0, 0, 2, 4, 4, 8);
1334 Mat vectors_truth = new Mat(3, 4, CvType.CV_32F, new Scalar(0)) {
1336 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1339 assertMatEqual(mean_truth, mean, EPS);
1340 assertMatEqual(vectors_truth, vectors, EPS);
1343 public void testPCAComputeMatMatMatInt() {
1344 Mat data = new Mat(3, 4, CvType.CV_32F) {
1346 put(0, 0, 1, 2, 2, 4);
1347 put(1, 0, 2, 4, 4, 8);
1348 put(2, 0, 3, 6, 6, 12);
1351 Mat mean = new Mat();
1352 Mat vectors = new Mat();
1354 Core.PCACompute(data, mean, vectors, 1);
1356 Mat mean_truth = new Mat(1, 4, CvType.CV_32F) {
1358 put(0, 0, 2, 4, 4, 8);
1361 Mat vectors_truth = new Mat(1, 4, CvType.CV_32F, new Scalar(0)) {
1363 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1366 assertMatEqual(mean_truth, mean, EPS);
1367 assertMatEqual(vectors_truth, vectors, EPS);
1370 public void testPCAProject() {
1371 Mat mean = new Mat(1, 4, CvType.CV_32F) {
1373 put(0, 0, 2, 4, 4, 8);
1376 Mat vectors = new Mat(1, 4, CvType.CV_32F, new Scalar(0)) {
1378 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1381 Mat data = new Mat(3, 4, CvType.CV_32F) {
1383 put(0, 0, 1, 2, 2, 4);
1384 put(1, 0, 2, 4, 4, 8);
1385 put(2, 0, 0, 0, 0, 0);
1388 Mat result = new Mat();
1390 Core.PCAProject(data, mean, vectors, result);
1392 Mat truth = new Mat(3, 1, CvType.CV_32F) {
1394 put(0, 0, -5, 0, -10);
1397 assertMatEqual(truth, result, EPS);
1400 public void testPerspectiveTransform() {
1401 Mat src = new Mat(matSize, matSize, CvType.CV_32FC2);
1402 Core.randu(src, 0, 256);
1403 Mat transformMatrix = Mat.eye(3, 3, CvType.CV_32F);
1405 Core.perspectiveTransform(src, dst, transformMatrix);
1406 assertMatEqual(src, dst, EPS);
1409 public void testPerspectiveTransform3D() {
1410 Mat src = new Mat(matSize, matSize, CvType.CV_32FC3);
1411 Core.randu(src, 0, 256);
1412 Mat transformMatrix = Mat.eye(4, 4, CvType.CV_32F);
1414 Core.perspectiveTransform(src, dst, transformMatrix);
1416 assertMatEqual(src, dst, EPS);
1419 private static double atan2deg(double y, double x)
1421 double res = Math.atan2(y, x);
1423 res = Math.PI * 2 + res;
1424 return res * 180 / Math.PI;
1427 private static double atan2rad(double y, double x)
1429 double res = Math.atan2(y, x);
1431 res = Math.PI * 2 + res;
1435 public void testPhaseMatMatMat() {
1436 Mat x = new Mat(1, 4, CvType.CV_32F) {
1438 put(0, 0, 10.0, 10.0, 20.0, 5.0);
1441 Mat y = new Mat(1, 4, CvType.CV_32F) {
1443 put(0, 0, 20.0, 15.0, 20.0, 20.0);
1446 Mat gold = new Mat(1, 4, CvType.CV_32F) {
1448 put(0, 0, atan2rad(20, 10), atan2rad(15, 10), atan2rad(20, 20), atan2rad(20, 5));
1452 Core.phase(x, y, dst);
1454 assertMatEqual(gold, dst, EPS);
1457 public void testPhaseMatMatMatBoolean() {
1458 Mat x = new Mat(1, 4, CvType.CV_32F) {
1460 put(0, 0, 10.0, 10.0, 20.0, 5.0);
1463 Mat y = new Mat(1, 4, CvType.CV_32F) {
1465 put(0, 0, 20.0, 15.0, 20.0, 20.0);
1468 Mat gold = new Mat(1, 4, CvType.CV_32F) {
1470 put(0, 0, atan2deg(20, 10), atan2deg(15, 10), atan2deg(20, 20), atan2deg(20, 5));
1474 Core.phase(x, y, dst, true);
1476 assertMatEqual(gold, dst, EPS * 180 / Math.PI);
1479 public void testPolarToCartMatMatMatMat() {
1480 Mat magnitude = new Mat(1, 3, CvType.CV_32F) {
1482 put(0, 0, 5.0, 10.0, 13.0);
1485 Mat angle = new Mat(1, 3, CvType.CV_32F) {
1487 put(0, 0, 0.92729962, 0.92729962, 1.1759995);
1490 Mat xCoordinate = new Mat();
1491 Mat yCoordinate = new Mat();
1493 Core.polarToCart(magnitude, angle, xCoordinate, yCoordinate);
1495 Mat x = new Mat(1, 3, CvType.CV_32F) {
1497 put(0, 0, 3.0, 6.0, 5, 0);
1500 Mat y = new Mat(1, 3, CvType.CV_32F) {
1502 put(0, 0, 4.0, 8.0, 12.0);
1505 assertMatEqual(x, xCoordinate, EPS);
1506 assertMatEqual(y, yCoordinate, EPS);
1509 public void testPolarToCartMatMatMatMatBoolean() {
1510 Mat magnitude = new Mat(1, 3, CvType.CV_32F) {
1512 put(0, 0, 5.0, 10.0, 13.0);
1515 Mat angle = new Mat(1, 3, CvType.CV_32F) {
1517 put(0, 0, 0.92729962, 0.92729962, 1.1759995);
1520 Mat xCoordinate = new Mat();
1521 Mat yCoordinate = new Mat();
1523 Core.polarToCart(magnitude, angle, xCoordinate, yCoordinate, true);
1525 Mat x = new Mat(1, 3, CvType.CV_32F) {
1527 put(0, 0, 4.9993458, 9.9986916, 12.997262);
1530 Mat y = new Mat(1, 3, CvType.CV_32F) {
1532 put(0, 0, 0.080918625, 0.16183725, 0.26680708);
1535 assertMatEqual(x, xCoordinate, EPS);
1536 assertMatEqual(y, yCoordinate, EPS);
1539 public void testPow() {
1540 Core.pow(gray2, 7, dst);
1542 assertMatEqual(gray128, dst);
1545 public void testRandn() {
1546 Core.randn(gray0, 100, 23);
1548 assertEquals(100., Core.mean(gray0).val[0], 23 / 2);
1551 public void testRandShuffleMat() {
1552 Mat original = new Mat(1, 10, CvType.CV_32F) {
1554 put(0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
1557 Mat shuffled = original.clone();
1559 Core.randShuffle(shuffled);
1561 assertMatNotEqual(original, shuffled, EPS);
1562 Mat dst1 = new Mat();
1563 Mat dst2 = new Mat();
1564 Core.sort(original, dst1, Core.SORT_ASCENDING);
1565 Core.sort(shuffled, dst2, Core.SORT_ASCENDING);
1566 assertMatEqual(dst1, dst2, EPS);
1569 public void testRandShuffleMatDouble() {
1570 Mat original = new Mat(1, 10, CvType.CV_32F) {
1572 put(0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
1575 Mat shuffled = original.clone();
1577 Core.randShuffle(shuffled, 10);
1579 assertMatNotEqual(original, shuffled, EPS);
1580 Mat dst1 = new Mat();
1581 Mat dst2 = new Mat();
1582 Core.sort(original, dst1, Core.SORT_ASCENDING);
1583 Core.sort(shuffled, dst2, Core.SORT_ASCENDING);
1584 assertMatEqual(dst1, dst2, EPS);
1587 public void testRandu() {
1588 Core.randu(gray0, 3, 23);
1589 fail("Not yet implemented");
1590 //assertTrue(Core.checkRange(gray0, true, null, 3, 23));
1593 public void testRectangleMatPointPointScalar() {
1594 Point bottomRight = new Point(gray0.cols() / 2, gray0.rows() / 2);
1595 Point topLeft = new Point(0, 0);
1596 Scalar color = new Scalar(128);
1598 Imgproc.rectangle(gray0, bottomRight, topLeft, color);
1600 assertTrue(0 != Core.countNonZero(gray0));
1603 public void testRectangleMatPointPointScalarInt() {
1604 Point bottomRight = new Point(gray0.cols(), gray0.rows());
1605 Point topLeft = new Point(0, 0);
1606 Scalar color = new Scalar(128);
1608 Imgproc.rectangle(gray0, bottomRight, topLeft, color, 2);
1609 Imgproc.rectangle(gray0, bottomRight, topLeft, colorBlack);
1611 assertTrue(0 != Core.countNonZero(gray0));
1614 public void testRectangleMatPointPointScalarIntInt() {
1615 Point bottomRight = new Point(gray0.cols() / 2, gray0.rows() / 2);
1616 Point topLeft = new Point(0, 0);
1617 Scalar color = new Scalar(128);
1619 Imgproc.rectangle(gray0, bottomRight, topLeft, color, 2, Imgproc.line_AA, 0);
1620 Imgproc.rectangle(gray0, bottomRight, topLeft, colorBlack, 2, Imgproc.line_4, 0);
1622 assertTrue(0 != Core.countNonZero(gray0));
1625 public void testRectangleMatPointPointScalarIntIntInt() {
1626 Point bottomRight1 = new Point(gray0.cols(), gray0.rows());
1627 Point bottomRight2 = new Point(gray0.cols() / 2, gray0.rows() / 2);
1628 Point topLeft = new Point(0, 0);
1629 Scalar color = new Scalar(128);
1631 Imgproc.rectangle(gray0, bottomRight1, topLeft, color, 2, Imgproc.line_8, 1);
1633 assertTrue(0 != Core.countNonZero(gray0));
1635 Imgproc.rectangle(gray0, bottomRight2, topLeft, colorBlack, 2, Imgproc.line_8, 0);
1637 assertEquals(0, Core.countNonZero(gray0));
1640 public void testReduceMatMatIntInt() {
1641 Mat src = new Mat(2, 2, CvType.CV_32F) {
1648 Core.reduce(src, dst, 0, Core.REDUCE_AVG);
1650 Mat out = new Mat(1, 2, CvType.CV_32F) {
1655 assertMatEqual(out, dst, EPS);
1658 public void testReduceMatMatIntIntInt() {
1659 Mat src = new Mat(2, 2, CvType.CV_32F) {
1666 Core.reduce(src, dst, 1, Core.REDUCE_SUM, CvType.CV_64F);
1668 Mat out = new Mat(2, 1, CvType.CV_64F) {
1673 assertMatEqual(out, dst, EPS);
1676 public void testRepeat() {
1677 Mat src = new Mat(1, 2, CvType.CV_32F, new Scalar(0));
1679 Core.repeat(src, matSize, matSize / 2, dst);
1681 assertMatEqual(gray0_32f, dst, EPS);
1684 public void testScaleAdd() {
1685 Core.scaleAdd(gray3, 2.0, gray3, dst);
1687 assertMatEqual(gray9, dst);
1690 public void testSetIdentityMat() {
1691 Core.setIdentity(gray0_32f);
1693 assertMatEqual(grayE_32f, gray0_32f, EPS);
1696 public void testSetIdentityMatScalar() {
1699 Core.setIdentity(m, new Scalar(5));
1701 truth = new Mat(m.size(), m.type(), new Scalar(0));
1702 truth.diag().setTo(new Scalar(5));
1703 assertMatEqual(truth, m, EPS);
1706 public void testSolveCubic() {
1707 Mat coeffs = new Mat(1, 4, CvType.CV_32F) {
1709 put(0, 0, 1, 6, 11, 6);
1713 assertEquals(3, Core.solveCubic(coeffs, dst));
1715 Mat roots = new Mat(3, 1, CvType.CV_32F) {
1717 put(0, 0, -3, -1, -2);
1720 assertMatEqual(roots, dst, EPS);
1723 public void testSolveMatMatMat() {
1724 Mat a = new Mat(3, 3, CvType.CV_32F) {
1727 put(1, 0, 1, -2, 2);
1731 Mat b = new Mat(3, 1, CvType.CV_32F) {
1737 assertTrue(Core.solve(a, b, dst));
1739 Mat res = new Mat(3, 1, CvType.CV_32F) {
1741 put(0, 0, -12, 2, 10);
1744 assertMatEqual(res, dst, EPS);
1747 public void testSolveMatMatMatInt() {
1748 Mat a = new Mat(3, 3, CvType.CV_32F) {
1751 put(1, 0, 1, -2, 2);
1755 Mat b = new Mat(3, 1, CvType.CV_32F) {
1761 assertTrue(Core.solve(a, b, dst, Core.DECOMP_QR | Core.DECOMP_NORMAL));
1763 Mat res = new Mat(3, 1, CvType.CV_32F) {
1765 put(0, 0, -12, 2, 10);
1768 assertMatEqual(res, dst, EPS);
1771 public void testSolvePolyMatMat() {
1772 Mat coeffs = new Mat(4, 1, CvType.CV_32F) {
1774 put(0, 0, -6, 11, -6, 1);
1777 Mat roots = new Mat();
1779 assertEquals(0.0, Core.solvePoly(coeffs, roots));
1781 truth = new Mat(3, 1, CvType.CV_32FC2) {
1783 put(0, 0, 1, 0, 2, 0, 3, 0);
1786 assertMatEqual(truth, roots, EPS);
1789 public void testSolvePolyMatMatInt() {
1790 Mat coeffs = new Mat(4, 1, CvType.CV_32F) {
1792 put(0, 0, -6, 11, -6, 1);
1795 Mat roots = new Mat();
1797 assertEquals(10.198039027185569, Core.solvePoly(coeffs, roots, 1));
1799 truth = new Mat(3, 1, CvType.CV_32FC2) {
1801 put(0, 0, 1, 0, -1, 2, -2, 12);
1804 assertMatEqual(truth, roots, EPS);
1807 public void testSort() {
1808 Mat submat = gray0.submat(0, gray0.rows() / 2, 0, gray0.cols() / 2);
1809 submat.setTo(new Scalar(1.0));
1811 Core.sort(gray0, dst, Core.SORT_EVERY_ROW);
1813 submat = dst.submat(0, dst.rows() / 2, dst.cols() / 2, dst.cols());
1814 assertTrue(submat.total() == Core.countNonZero(submat));
1816 Core.sort(gray0, dst, Core.SORT_EVERY_COLUMN);
1818 submat = dst.submat(dst.rows() / 2, dst.rows(), 0, dst.cols() / 2);
1820 assertTrue(submat.total() == Core.countNonZero(submat));
1823 public void testSortIdx() {
1824 Mat a = Mat.eye(3, 3, CvType.CV_8UC1);
1827 Core.sortIdx(a, b, Core.SORT_EVERY_ROW | Core.SORT_ASCENDING);
1829 truth = new Mat(3, 3, CvType.CV_32SC1) {
1836 assertMatEqual(truth, b);
1839 public void testSplit() {
1840 Mat m = getMat(CvType.CV_8UC3, 1, 2, 3);
1841 ArrayList<Mat> cois = new ArrayList<Mat>();
1843 Core.split(m, cois);
1845 assertMatEqual(gray1, cois.get(0));
1846 assertMatEqual(gray2, cois.get(1));
1847 assertMatEqual(gray3, cois.get(2));
1850 public void testSqrt() {
1851 Core.sqrt(gray9_32f, dst);
1853 assertMatEqual(gray3_32f, dst, EPS);
1855 Mat rgba144 = new Mat(matSize, matSize, CvType.CV_32FC4, Scalar.all(144));
1856 Mat rgba12 = new Mat(matSize, matSize, CvType.CV_32FC4, Scalar.all(12));
1858 Core.sqrt(rgba144, dst);
1860 assertMatEqual(rgba12, dst, EPS);
1863 public void testSubtractMatMatMat() {
1864 Core.subtract(gray128, gray1, dst);
1866 assertMatEqual(gray127, dst);
1869 public void testSubtractMatMatMatMat() {
1870 Mat mask = makeMask(gray1.clone());
1871 dst = gray128.clone();
1873 Core.subtract(gray128, gray1, dst, mask);
1875 assertMatEqual(makeMask(gray127, 128), dst);
1878 public void testSubtractMatMatMatMatInt() {
1879 Core.subtract(gray3, gray2, dst, gray1, CvType.CV_32F);
1881 assertMatEqual(gray1_32f, dst, EPS);
1884 public void testSumElems() {
1885 Mat src = new Mat(4, 4, CvType.CV_8U, new Scalar(10));
1887 Scalar res1 = Core.sumElems(src);
1889 assertScalarEqual(new Scalar(160), res1, EPS);
1892 public void testSVBackSubst() {
1893 Mat w = new Mat(2, 2, CvType.CV_32FC1, new Scalar(2));
1894 Mat u = new Mat(2, 2, CvType.CV_32FC1, new Scalar(4));
1895 Mat vt = new Mat(2, 2, CvType.CV_32FC1, new Scalar(2));
1896 Mat rhs = new Mat(2, 2, CvType.CV_32FC1, new Scalar(1));
1898 Core.SVBackSubst(w, u, vt, rhs, dst);
1900 Mat truth = new Mat(2, 2, CvType.CV_32FC1, new Scalar(16));
1901 assertMatEqual(truth, dst, EPS);
1904 public void testSVDecompMatMatMatMat() {
1905 Mat src = new Mat(1, 4, CvType.CV_32FC1) {
1907 put(0, 0, 1, 4, 8, 6);
1914 Core.SVDecomp(src, w, u, vt);
1916 Mat truthW = new Mat(1, 1, CvType.CV_32FC1, new Scalar(10.816654));
1917 Mat truthU = new Mat(1, 1, CvType.CV_32FC1, new Scalar(1));
1918 Mat truthVT = new Mat(1, 4, CvType.CV_32FC1) {
1920 put(0, 0, 0.09245003, 0.36980012, 0.73960024, 0.5547002);
1923 assertMatEqual(truthW, w, EPS);
1924 assertMatEqual(truthU, u, EPS);
1925 assertMatEqual(truthVT, vt, EPS);
1928 public void testSVDecompMatMatMatMatInt() {
1929 Mat src = new Mat(1, 4, CvType.CV_32FC1) {
1931 put(0, 0, 1, 4, 8, 6);
1938 Core.SVDecomp(src, w, u, vt, Core.SVD_NO_UV);
1940 Mat truthW = new Mat(1, 1, CvType.CV_32FC1, new Scalar(10.816654));
1941 assertMatEqual(truthW, w, EPS);
1942 assertTrue(u.empty());
1943 assertTrue(vt.empty());
1946 public void testTrace() {
1947 Scalar s = Core.trace(gray1);
1949 assertEquals(new Scalar(matSize), s);
1952 public void testTransform() {
1953 Mat src = new Mat(2, 2, CvType.CV_32F, new Scalar(55));
1954 Mat m = Mat.eye(2, 2, CvType.CV_32FC1);
1956 Core.transform(src, dst, m);
1958 truth = new Mat(2, 2, CvType.CV_32FC2, new Scalar(55, 1));
1959 assertMatEqual(truth, dst, EPS);
1962 public void testTranspose() {
1963 gray0.submat(0, gray0.rows() / 2, 0, gray0.cols()).setTo(new Scalar(1));
1964 Mat destination = getMat(CvType.CV_8U, 0);
1966 Core.transpose(gray0, destination);
1968 Mat subdst = destination.submat(0, destination.rows(), 0, destination.cols() / 2);
1969 assertTrue(subdst.total() == Core.countNonZero(subdst));
1972 public void testVconcat() {
1973 List<Mat> mats = Arrays.asList(Mat.eye(3, 3, CvType.CV_8U), Mat.zeros(2, 3, CvType.CV_8U));
1975 Core.vconcat(mats, dst);
1977 assertMatEqual(Mat.eye(5, 3, CvType.CV_8U), dst);
1981 public void testCopyMakeBorderMatMatIntIntIntIntInt() {
1982 Mat src = new Mat(2, 2, CvType.CV_32F, new Scalar(1));
1985 Core.copyMakeBorder(src, dst, border, border, border, border, Core.BORDER_REPLICATE);
1987 truth = new Mat(6, 6, CvType.CV_32F, new Scalar(1));
1988 assertMatEqual(truth, dst, EPS);
1991 public void testCopyMakeBorderMatMatIntIntIntIntIntScalar() {
1992 Mat src = new Mat(2, 2, CvType.CV_32F, new Scalar(1));
1994 Scalar value = new Scalar(0);
1997 Core.copyMakeBorder(src, dst, border, border, border, border, Core.BORDER_REPLICATE, value);
1998 // TODO_: write better test (use Core.BORDER_CONSTANT)
2000 truth = new Mat(6, 6, CvType.CV_32F, new Scalar(1));
2001 assertMatEqual(truth, dst, EPS);
2004 public void testBorderInterpolate() {
2005 float val1 = Core.borderInterpolate(100, 150, Core.BORDER_REFLECT_101);
2006 assertEquals(100f, val1);
2008 float val2 = Core.borderInterpolate(-5, 10, Core.BORDER_WRAP);
2009 assertEquals(5f, val2);