CLAHE Python bindings
[profile/ivi/opencv.git] / modules / java / android_test / src / org / opencv / test / core / CoreTest.java
1 package org.opencv.test.core;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
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
23 public class CoreTest extends OpenCVTestCase {
24
25     public void testAbsdiff() {
26         Core.absdiff(gray128, gray255, dst);
27
28         assertMatEqual(gray127, dst);
29     }
30
31     public void testAddMatMatMat() {
32         Core.add(gray128, gray128, dst);
33
34         assertMatEqual(gray255, dst);
35     }
36
37     public void testAddMatMatMatMatInt() {
38         Core.add(gray0, gray1, dst, gray1, CvType.CV_32F);
39
40         assertEquals(CvType.CV_32F, dst.depth());
41         assertMatEqual(gray1_32f, dst, EPS);
42     }
43
44     public void testAddWeightedMatDoubleMatDoubleDoubleMat() {
45         Core.addWeighted(gray1, 120.0, gray127, 1.0, 10.0, dst);
46
47         assertMatEqual(gray255, dst);
48     }
49
50     public void testAddWeightedMatDoubleMatDoubleDoubleMatInt() {
51         Core.addWeighted(gray1, 126.0, gray127, 1.0, 2.0, dst, CvType.CV_32F);
52
53         assertEquals(CvType.CV_32F, dst.depth());
54         assertMatEqual(gray255_32f, dst, EPS);
55     }
56
57     public void testBitwise_andMatMatMat() {
58         Core.bitwise_and(gray127, gray3, dst);
59
60         assertMatEqual(gray3, dst);
61     }
62
63     public void testBitwise_andMatMatMatMat() {
64         Core.bitwise_and(gray3, gray1, dst, gray255);
65
66         assertMatEqual(gray1, dst);
67     }
68
69     public void testBitwise_notMatMat() {
70         Core.bitwise_not(gray255, dst);
71
72         assertMatEqual(gray0, dst);
73     }
74
75     public void testBitwise_notMatMatMat() {
76         Core.bitwise_not(gray0, dst, gray1);
77
78         assertMatEqual(gray255, dst);
79     }
80
81     public void testBitwise_orMatMatMat() {
82         Core.bitwise_or(gray1, gray2, dst);
83
84         assertMatEqual(gray3, dst);
85     }
86
87     public void testBitwise_orMatMatMatMat() {
88         Core.bitwise_or(gray127, gray3, dst, gray255);
89
90         assertMatEqual(gray127, dst);
91     }
92
93     public void testBitwise_xorMatMatMat() {
94         Core.bitwise_xor(gray3, gray2, dst);
95
96         assertMatEqual(gray1, dst);
97     }
98
99     public void testBitwise_xorMatMatMatMat() {
100         Core.bitwise_or(gray127, gray128, dst, gray255);
101
102         assertMatEqual(gray255, dst);
103     }
104
105     public void testCalcCovarMatrixMatMatMatInt() {
106         Mat covar = new Mat(matSize, matSize, CvType.CV_64FC1);
107         Mat mean = new Mat(1, matSize, CvType.CV_64FC1);
108
109         Core.calcCovarMatrix(gray0_32f, covar, mean, Core.COVAR_ROWS | Core.COVAR_NORMAL);
110
111         assertMatEqual(gray0_64f, covar, EPS);
112         assertMatEqual(gray0_64f_1d, mean, EPS);
113     }
114
115     public void testCalcCovarMatrixMatMatMatIntInt() {
116         Mat covar = new Mat(matSize, matSize, CvType.CV_32F);
117         Mat mean = new Mat(1, matSize, CvType.CV_32F);
118
119         Core.calcCovarMatrix(gray0_32f, covar, mean, Core.COVAR_ROWS | Core.COVAR_NORMAL, CvType.CV_32F);
120
121         assertMatEqual(gray0_32f, covar, EPS);
122         assertMatEqual(gray0_32f_1d, mean, EPS);
123     }
124
125     public void testCartToPolarMatMatMatMat() {
126         Mat x = new Mat(1, 3, CvType.CV_32F) {
127             {
128                 put(0, 0, 3.0, 6.0, 5, 0);
129             }
130         };
131         Mat y = new Mat(1, 3, CvType.CV_32F) {
132             {
133                 put(0, 0, 4.0, 8.0, 12.0);
134             }
135         };
136         Mat dst_angle = new Mat();
137
138         Core.cartToPolar(x, y, dst, dst_angle);
139
140         Mat expected_magnitude = new Mat(1, 3, CvType.CV_32F) {
141             {
142                 put(0, 0, 5.0, 10.0, 13.0);
143             }
144         };
145         Mat expected_angle = new Mat(1, 3, CvType.CV_32F) {
146             {
147                 put(0, 0, atan2rad(4,3), atan2rad(8,6), atan2rad(12,5));
148             }
149         };
150         assertMatEqual(expected_magnitude, dst, EPS);
151         assertMatEqual(expected_angle, dst_angle, EPS);
152     }
153
154     public void testCartToPolarMatMatMatMatBoolean() {
155         Mat x = new Mat(1, 3, CvType.CV_32F) {
156             {
157                 put(0, 0, 3.0, 6.0, 5, 0);
158             }
159         };
160         Mat y = new Mat(1, 3, CvType.CV_32F) {
161             {
162                 put(0, 0, 4.0, 8.0, 12.0);
163             }
164         };
165         Mat dst_angle = new Mat();
166
167         Core.cartToPolar(x, y, dst, dst_angle, true);
168
169         Mat expected_magnitude = new Mat(1, 3, CvType.CV_32F) {
170             {
171                 put(0, 0, 5.0, 10.0, 13.0);
172             }
173         };
174         Mat expected_angle = new Mat(1, 3, CvType.CV_32F) {
175             {
176                 put(0, 0, atan2deg(4,3), atan2deg(8,6), atan2deg(12,5));
177             }
178         };
179         assertMatEqual(expected_magnitude, dst, EPS);
180         assertMatEqual(expected_angle, dst_angle, EPS * 180/Math.PI);
181     }
182
183
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);
187
188         assertTrue(Core.checkRange(grayRnd_32f));
189         assertTrue(Core.checkRange(new Mat()));
190         assertFalse(Core.checkRange(outOfRange));
191     }
192
193
194     public void testCheckRangeMatBooleanPointDoubleDouble() {
195         Mat inRange = new Mat(2, 3, CvType.CV_64F) {
196             {
197                 put(0, 0, 14, 48, 76, 33, 5, 99);
198             }
199         };
200
201         assertTrue(Core.checkRange(inRange, true, 5, 100));
202
203         Mat outOfRange = new Mat(2, 3, CvType.CV_64F) {
204             {
205                 put(0, 0, -4, 0, 6, 33, 4, 109);
206             }
207         };
208
209         assertFalse(Core.checkRange(outOfRange, true, 5, 100));
210     }
211
212     public void testCircleMatPointIntScalar() {
213         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
214         int radius = Math.min(gray0.cols() / 4, gray0.rows() / 4);
215         Scalar color = new Scalar(128);
216
217         Core.circle(gray0, center, radius, color);
218
219         assertTrue(0 != Core.countNonZero(gray0));
220     }
221
222     public void testCircleMatPointIntScalarInt() {
223         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
224         int radius = Math.min(gray0.cols() / 4, gray0.rows() / 4);
225         Scalar color = new Scalar(128);
226
227         Core.circle(gray0, center, radius, color, Core.FILLED);
228
229         assertTrue(0 != Core.countNonZero(gray0));
230     }
231
232     public void testCircleMatPointIntScalarIntIntInt() {
233         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
234         Point center2 = new Point(gray0.cols(), gray0.rows());
235         int radius = Math.min(gray0.cols() / 4, gray0.rows() / 4);
236         Scalar color128 = new Scalar(128);
237         Scalar color0 = new Scalar(0);
238
239         Core.circle(gray0, center2, radius * 2, color128, 2, Core.LINE_4, 1/*
240                                                                             * Number
241                                                                             * of
242                                                                             * fractional
243                                                                             * bits
244                                                                             */);
245         assertFalse(0 == Core.countNonZero(gray0));
246
247         Core.circle(gray0, center, radius, color0, 2, Core.LINE_4, 0);
248
249         assertTrue(0 == Core.countNonZero(gray0));
250     }
251
252     public void testClipLine() {
253         Rect r = new Rect(10, 10, 10, 10);
254         Point pt1 = new Point(5.0, 15.0);
255         Point pt2 = new Point(25.0, 15.0);
256
257         assertTrue(Core.clipLine(r, pt1, pt2));
258
259         Point pt1Clipped = new Point(10.0, 15.0);
260         Point pt2Clipped = new Point(19.0, 15.0);
261         assertEquals(pt1Clipped, pt1);
262         assertEquals(pt2Clipped, pt2);
263
264         pt1 = new Point(5.0, 5.0);
265         pt2 = new Point(25.0, 5.0);
266         pt1Clipped = new Point(5.0, 5.0);
267         pt2Clipped = new Point(25.0, 5.0);
268
269         assertFalse(Core.clipLine(r, pt1, pt2));
270
271         assertEquals(pt1Clipped, pt1);
272         assertEquals(pt2Clipped, pt2);
273     }
274
275     public void testCompare() {
276         Core.compare(gray0, gray0, dst, Core.CMP_EQ);
277
278         assertMatEqual(dst, gray255);
279
280         Core.compare(gray0, gray1, dst, Core.CMP_EQ);
281
282         assertMatEqual(dst, gray0);
283
284         grayRnd.put(0, 0, 0, 0);
285
286         Core.compare(gray0, grayRnd, dst, Core.CMP_GE);
287
288         int expected = (int) (grayRnd.total() - Core.countNonZero(grayRnd));
289         assertEquals(expected, Core.countNonZero(dst));
290     }
291
292     public void testCompleteSymmMat() {
293         Core.completeSymm(grayRnd_32f);
294
295         assertMatEqual(grayRnd_32f, grayRnd_32f.t(), EPS);
296     }
297
298     public void testCompleteSymmMatBoolean() {
299         Mat grayRnd_32f2 = grayRnd_32f.clone();
300
301         Core.completeSymm(grayRnd_32f, true);
302
303         assertMatEqual(grayRnd_32f, grayRnd_32f.t(), EPS);
304         Core.completeSymm(grayRnd_32f2, false);
305         assertMatNotEqual(grayRnd_32f2, grayRnd_32f, EPS);
306     }
307
308     public void testConvertScaleAbsMatMat() {
309         Core.convertScaleAbs(gray0, dst);
310
311         assertMatEqual(gray0, dst, EPS);
312
313         Core.convertScaleAbs(gray_16u_256, dst);
314
315         assertMatEqual(gray255, dst, EPS);
316     }
317
318     public void testConvertScaleAbsMatMatDoubleDouble() {
319         Core.convertScaleAbs(gray_16u_256, dst, 2, -513);
320
321         assertMatEqual(gray1, dst);
322     }
323
324     public void testCountNonZero() {
325         assertEquals(0, Core.countNonZero(gray0));
326
327         gray0.put(0, 0, 255);
328         gray0.put(gray0.rows() - 1, gray0.cols() - 1, 255);
329
330         assertEquals(2, Core.countNonZero(gray0));
331     }
332
333     public void testCubeRoot() {
334         float res = Core.cubeRoot(-27.0f);
335
336         assertEquals(-3.0f, res);
337     }
338
339     public void testDctMatMat() {
340         Mat in = new Mat(1, 4, CvType.CV_32F) {
341             {
342                 put(0, 0, 135.22211, 50.811096, 102.27016, 207.6682);
343             }
344         };
345         Mat dst1 = new Mat();
346         Mat dst2 = new Mat();
347
348         Core.dct(gray0_32f_1d, dst1);
349         Core.dct(in, dst2);
350
351         truth = new Mat(1, 4, CvType.CV_32F) {
352             {
353                 put(0, 0, 247.98576, -61.252407, 94.904533, 14.013477);
354             }
355         };
356         assertMatEqual(gray0_32f_1d, dst1, EPS);
357         assertMatEqual(truth, dst2, EPS);
358     }
359
360     public void testDctMatMatInt() {
361         Mat in = new Mat(1, 4, CvType.CV_32F) {
362             {
363                 put(0, 0, 247.98576, -61.252407, 94.904533, 14.013477);
364             }
365         };
366         Mat dst1 = new Mat();
367         Mat dst2 = new Mat();
368
369         Core.dct(gray0_32f_1d, dst1, Core.DCT_INVERSE);
370         Core.dct(in, dst2, Core.DCT_INVERSE);
371
372         truth = new Mat(1, 4, CvType.CV_32F) {
373             {
374                 put(0, 0, 135.22211, 50.811096, 102.27016, 207.6682);
375             }
376         };
377         assertMatEqual(gray0_32f_1d, dst1, EPS);
378         assertMatEqual(truth, dst2, EPS);
379     }
380
381     public void testDeterminant() {
382         Mat mat = new Mat(2, 2, CvType.CV_32F) {
383             {
384                 put(0, 0, 4.0);
385                 put(0, 1, 2.0);
386                 put(1, 0, 4.0);
387                 put(1, 1, 4.0);
388             }
389         };
390
391         double det = Core.determinant(mat);
392
393         assertEquals(8.0, det);
394     }
395
396     public void testDftMatMat() {
397         Core.dft(gray0_32f_1d, dst);
398
399         assertMatEqual(gray0_32f_1d, dst, EPS);
400     }
401
402     public void testDftMatMatIntInt() {
403         Mat src1 = new Mat(2, 4, CvType.CV_32F) {
404             {
405                 put(0, 0, 1, 2, 3, 4);
406                 put(1, 0, 1, 1, 1, 1);
407             }
408         };
409         Mat src2 = new Mat(2, 4, CvType.CV_32F) {
410             {
411                 put(0, 0, 1, 2, 3, 4);
412                 put(1, 0, 0, 0, 0, 0);
413             }
414         };
415         Mat dst1 = new Mat();
416         Mat dst2 = new Mat();
417
418         Core.dft(src1, dst1, Core.DFT_REAL_OUTPUT, 1);
419         Core.dft(src2, dst2, Core.DFT_REAL_OUTPUT, 0);
420
421         assertMatEqual(dst2, dst1, EPS);
422     }
423
424     public void testDivideDoubleMatMat() {
425         Core.divide(4.0, gray2, dst);
426
427         assertMatEqual(gray2, dst);
428
429         Core.divide(4.0, gray0, dst);
430
431         assertMatEqual(gray0, dst);
432     }
433
434     public void testDivideDoubleMatMatInt() {
435         Core.divide(9.0, gray3, dst, CvType.CV_32F);
436
437         assertMatEqual(gray3_32f, dst, EPS);
438     }
439
440     public void testDivideMatMatMat() {
441         Core.divide(gray9, gray3, dst);
442
443         assertMatEqual(gray3, dst);
444     }
445
446     public void testDivideMatMatMatDouble() {
447         Core.divide(gray1, gray2, dst, 6.0);
448
449         assertMatEqual(gray3, dst);
450     }
451
452     public void testDivideMatMatMatDoubleInt() {
453         Core.divide(gray1, gray2, dst, 6.0, CvType.CV_32F);
454
455         assertMatEqual(gray3_32f, dst, EPS);
456     }
457
458     public void testEigen() {
459         Mat src = new Mat(3, 3, CvType.CV_32FC1, new Scalar(2.0));
460         Mat eigenVals = new Mat();
461         Mat eigenVecs = new Mat();
462
463         Core.eigen(src, true, eigenVals, eigenVecs);
464
465         Mat expectedEigenVals = new Mat(3, 1, CvType.CV_32FC1) {
466             {
467                 put(0, 0, 6, 0, 0);
468             }
469         };
470         Mat expectedEigenVecs = new Mat(3, 3, CvType.CV_32FC1) {
471             {
472                 put(0, 0, 0.57735026, 0.57735026, 0.57735032);
473                 put(1, 0, 0.70710677, -0.70710677, 0);
474                 put(2, 0, -0.40824831, -0.40824831, 0.81649661);
475             }
476         };
477         assertMatEqual(eigenVals, expectedEigenVals, EPS);
478         assertMatEqual(eigenVecs, expectedEigenVecs, EPS);
479     }
480
481     public void testEllipse2Poly() {
482         Point center = new Point(4, 4);
483         Size axes = new Size(2, 2);
484         int angle = 30;
485         int arcStart = 30;
486         int arcEnd = 60;
487         int delta = 2;
488         MatOfPoint pts = new MatOfPoint();
489
490         Core.ellipse2Poly(center, axes, angle, arcStart, arcEnd, delta, pts);
491
492         Point truth[] = {
493                 new Point(5, 6),
494                 new Point(5, 6),
495                 new Point(5, 6),
496                 new Point(5, 6),
497                 new Point(5, 6),
498                 new Point(5, 6),
499                 new Point(5, 6),
500                 new Point(5, 6),
501                 new Point(4, 6),
502                 new Point(4, 6),
503                 new Point(4, 6),
504                 new Point(4, 6),
505                 new Point(4, 6),
506                 new Point(4, 6),
507                 new Point(4, 6),
508                 new Point(4, 6)
509         };
510         assertArrayPointsEquals(truth, pts.toArray(), EPS);
511     }
512
513     public void testEllipseMatPointSizeDoubleDoubleDoubleScalar() {
514         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
515         Size axes = new Size(2, 2);
516         double angle = 30, startAngle = 60, endAngle = 90;
517
518         Core.ellipse(gray0, center, axes, angle, startAngle, endAngle, colorWhite);
519
520         assertTrue(0 != Core.countNonZero(gray0));
521     }
522
523     public void testEllipseMatPointSizeDoubleDoubleDoubleScalarInt() {
524         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
525         Size axes = new Size(2, 2);
526         double angle = 30, startAngle = 60, endAngle = 90;
527
528         Core.ellipse(gray0, center, axes, angle, startAngle, endAngle, colorWhite, Core.FILLED);
529
530         assertTrue(0 != Core.countNonZero(gray0));
531     }
532
533     public void testEllipseMatPointSizeDoubleDoubleDoubleScalarIntIntInt() {
534         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
535         Size axes = new Size(2, 2);
536         Point center2 = new Point(gray0.cols(), gray0.rows());
537         Size axes2 = new Size(4, 4);
538         double angle = 30, startAngle = 0, endAngle = 30;
539
540         Core.ellipse(gray0, center, axes, angle, startAngle, endAngle, colorWhite, Core.FILLED, Core.LINE_4, 0);
541
542         assertTrue(0 != Core.countNonZero(gray0));
543
544         Core.ellipse(gray0, center2, axes2, angle, startAngle, endAngle, colorBlack, Core.FILLED, Core.LINE_4, 1);
545
546         assertEquals(0, Core.countNonZero(gray0));
547     }
548
549     public void testEllipseMatRotatedRectScalar() {
550         int matSize = 10;
551         Mat gray0 = Mat.zeros(matSize, matSize, CvType.CV_8U);
552         Point center = new Point(matSize / 2, matSize / 2);
553         Size size = new Size(matSize / 4, matSize / 2);
554         RotatedRect box = new RotatedRect(center, size, 45);
555
556         Core.ellipse(gray0, box, new Scalar(1));
557
558         final byte[] truth = new byte[] {
559                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
560                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
561                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
562                 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
563                 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
564                 0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
565                 0, 0, 0, 1, 0, 1, 1, 0, 0, 0,
566                 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
567                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
568                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
569
570         assertMatEqual(new Mat(matSize, matSize, CvType.CV_8U) {
571             {
572                 put(0, 0, truth);
573             }
574         }, gray0);
575     }
576
577     public void testEllipseMatRotatedRectScalarInt() {
578         Point center = new Point(matSize / 2, matSize / 2);
579         Size size = new Size(matSize / 4, matSize / 2);
580         RotatedRect box = new RotatedRect(center, size, 45);
581
582         Core.ellipse(gray0, box, new Scalar(1), Core.FILLED);
583         Core.ellipse(gray0, box, new Scalar(0));
584
585         assertTrue(0 < Core.countNonZero(gray0));
586     }
587
588     public void testEllipseMatRotatedRectScalarIntInt() {
589         Point center = new Point(matSize / 2, matSize / 2);
590         Size size = new Size(2, matSize * 2 / 3);
591         RotatedRect box = new RotatedRect(center, size, 20);
592
593         Core.ellipse(gray0, box, new Scalar(9), 1, Core.LINE_AA);
594         Core.ellipse(gray0, box, new Scalar(0), 1, Core.LINE_4);
595
596         assertTrue(0 < Core.countNonZero(gray0));
597     }
598
599     public void testExp() {
600         Core.exp(gray0_32f, dst);
601
602         assertMatEqual(gray1_32f, dst, EPS);
603     }
604
605     public void testExtractChannel() {
606         Core.extractChannel(rgba128, dst, 0);
607
608         assertMatEqual(gray128, dst);
609     }
610
611     public void testFastAtan2() {
612         double eps = 0.3;
613
614         float res = Core.fastAtan2(50, 50);
615
616         assertEquals(45, res, eps);
617
618         float res2 = Core.fastAtan2(80, 20);
619
620         assertEquals(Math.atan2(80, 20) * 180 / Math.PI, res2, eps);
621     }
622
623     public void testFillConvexPolyMatListOfPointScalar() {
624         MatOfPoint polyline = new MatOfPoint(new Point[]{new Point(1, 1), new Point(5, 0), new Point(6, 8), new Point(0, 9)});
625
626         Core.fillConvexPoly(gray0, polyline, new Scalar(150));
627
628         assertTrue(0 < Core.countNonZero(gray0));
629         assertTrue(gray0.total() > Core.countNonZero(gray0));
630     }
631
632     public void testFillConvexPolyMatListOfPointScalarIntInt() {
633         MatOfPoint polyline1 = new MatOfPoint(new Point(2, 1), new Point(5, 1), new Point(5, 7), new Point(2, 7));
634         MatOfPoint polyline2 = new MatOfPoint(new Point(4, 2), new Point(10, 2), new Point(10, 14), new Point(4, 14));
635
636         // current implementation of fixed-point version of fillConvexPoly
637         // requires image to be at least 2-pixel wider in each direction than
638         // contour
639         Core.fillConvexPoly(gray0, polyline1, colorWhite, Core.LINE_8, 0);
640
641         assertTrue(0 < Core.countNonZero(gray0));
642         assertTrue(gray0.total() > Core.countNonZero(gray0));
643
644         Core.fillConvexPoly(gray0, polyline2, colorBlack, Core.LINE_8, 1);
645
646         assertEquals("see http://code.opencv.org/issues/1284", 0, Core.countNonZero(gray0));
647     }
648
649     public void testFillPolyMatListOfListOfPointScalar() {
650         int matSize = 10;
651         Mat gray0 = Mat.zeros(matSize, matSize, CvType.CV_8U);
652         MatOfPoint polyline = new MatOfPoint(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4));
653         List<MatOfPoint> polylines = new ArrayList<MatOfPoint>();
654         polylines.add(polyline);
655
656         Core.fillPoly(gray0, polylines, new Scalar(1));
657
658         final byte[] truth = new byte[] {
659                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
660                 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
661                 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
662                 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
663                 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
664                 0, 1, 1, 0, 0, 0, 1, 1, 0, 0,
665                 0, 1, 1, 0, 0, 0, 1, 1, 0, 0,
666                 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
667                 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
668                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
669
670         assertMatEqual(new Mat(gray0.size(), CvType.CV_8U) {
671             {
672                 put(0, 0, truth);
673             }
674         }, gray0);
675     }
676
677     public void testFillPolyMatListOfListOfPointScalarIntIntPoint() {
678         MatOfPoint polyline1 = new MatOfPoint(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4));
679         MatOfPoint polyline2 = new MatOfPoint(new Point(0, 3), new Point(0, 7), new Point(3, 0), new Point(6, 7), new Point(6, 3));
680
681         List<MatOfPoint> polylines1 = new ArrayList<MatOfPoint>();
682         polylines1.add(polyline1);
683
684         List<MatOfPoint> polylines2 = new ArrayList<MatOfPoint>();
685         polylines2.add(polyline2);
686
687         Core.fillPoly(gray0, polylines1, new Scalar(1), Core.LINE_8, 0, new Point(0, 0));
688
689         assertTrue(0 < Core.countNonZero(gray0));
690
691         Core.fillPoly(gray0, polylines2, new Scalar(0), Core.LINE_8, 0, new Point(1, 1));
692
693         assertEquals(0, Core.countNonZero(gray0));
694     }
695
696     public void testFlip() {
697         Mat src = new Mat(2, 2, CvType.CV_32F) {
698             {
699                 put(0, 0, 1.0);
700                 put(0, 1, 2.0);
701                 put(1, 0, 3.0);
702                 put(1, 1, 4.0);
703             }
704         };
705         Mat dst1 = new Mat();
706         Mat dst2 = new Mat();
707
708         Core.flip(src, dst1, 0);
709         Core.flip(src, dst2, 1);
710
711         Mat dst_f1 = new Mat(2, 2, CvType.CV_32F) {
712             {
713                 put(0, 0, 3.0);
714                 put(0, 1, 4.0);
715                 put(1, 0, 1.0);
716                 put(1, 1, 2.0);
717             }
718         };
719         Mat dst_f2 = new Mat(2, 2, CvType.CV_32F) {
720             {
721                 put(0, 0, 2.0);
722                 put(0, 1, 1.0);
723                 put(1, 0, 4.0);
724                 put(1, 1, 3.0);
725             }
726         };
727         assertMatEqual(dst_f1, dst1, EPS);
728         assertMatEqual(dst_f2, dst2, EPS);
729     }
730
731     public void testGemmMatMatDoubleMatDoubleMat() {
732         Mat m1 = new Mat(2, 2, CvType.CV_32FC1) {
733             {
734                 put(0, 0, 1.0, 0.0);
735                 put(1, 0, 1.0, 0.0);
736             }
737         };
738         Mat m2 = new Mat(2, 2, CvType.CV_32FC1) {
739             {
740                 put(0, 0, 1.0, 0.0);
741                 put(1, 0, 1.0, 0.0);
742             }
743         };
744         Mat dmatrix = new Mat(2, 2, CvType.CV_32FC1) {
745             {
746                 put(0, 0, 0.001, 0.001);
747                 put(1, 0, 0.001, 0.001);
748             }
749         };
750
751         Core.gemm(m1, m2, 1.0, dmatrix, 1.0, dst);
752
753         Mat expected = new Mat(2, 2, CvType.CV_32FC1) {
754             {
755                 put(0, 0, 1.001, 0.001);
756                 put(1, 0, 1.001, 0.001);
757             }
758         };
759         assertMatEqual(expected, dst, EPS);
760     }
761
762     public void testGemmMatMatDoubleMatDoubleMatInt() {
763         Mat m1 = new Mat(2, 2, CvType.CV_32FC1) {
764             {
765                 put(0, 0, 1.0, 0.0);
766                 put(1, 0, 1.0, 0.0);
767             }
768         };
769         Mat m2 = new Mat(2, 2, CvType.CV_32FC1) {
770             {
771                 put(0, 0, 1.0, 0.0);
772                 put(1, 0, 1.0, 0.0);
773             }
774         };
775         Mat dmatrix = new Mat(2, 2, CvType.CV_32FC1) {
776             {
777                 put(0, 0, 0.001, 0.001);
778                 put(1, 0, 0.001, 0.001);
779             }
780         };
781
782         Core.gemm(m1, m2, 1.0, dmatrix, 1.0, dst, Core.GEMM_1_T);
783
784         Mat expected = new Mat(2, 2, CvType.CV_32FC1) {
785             {
786                 put(0, 0, 2.001, 0.001);
787                 put(1, 0, 0.001, 0.001);
788             }
789         };
790         assertMatEqual(expected, dst, EPS);
791     }
792
793     public void testGetCPUTickCount() {
794         long cpuCountStart = 0, actualTickCount;
795
796         cpuCountStart = Core.getCPUTickCount();
797         Core.sumElems(gray255);
798         actualTickCount = Core.getCPUTickCount();
799
800         long expectedTickCount = actualTickCount - cpuCountStart;
801         assertTrue(expectedTickCount > 0);
802     }
803
804     public void testGetNumberOfCPUs() {
805         int cpus = Core.getNumberOfCPUs();
806
807         assertTrue(Runtime.getRuntime().availableProcessors() <= cpus);
808     }
809
810     public void testGetOptimalDFTSize() {
811         assertEquals(1, Core.getOptimalDFTSize(0));
812         assertEquals(135, Core.getOptimalDFTSize(133));
813         assertEquals(15, Core.getOptimalDFTSize(13));
814     }
815
816     public void testGetTextSize() {
817         String text = "Android all the way";
818         double fontScale = 2;
819         int thickness = 3;
820         int baseLine[] = new int[1];
821
822         Core.getTextSize(text, Core.FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, null);
823         Size res = Core.getTextSize(text, Core.FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, baseLine);
824
825         assertEquals(543.0, res.width);
826         assertEquals(44.0, res.height);
827         assertEquals(20, baseLine[0]);
828     }
829
830     public void testGetTickCount() {
831         long startCount, endCount, count;
832
833         startCount = Core.getTickCount();
834         Core.divide(gray2, gray1, dst);
835         endCount = Core.getTickCount();
836
837         count = endCount - startCount;
838         assertTrue(count > 0);
839     }
840
841     public void testGetTickFrequency() {
842         double freq1 = Core.getTickFrequency();
843         Core.divide(gray2, gray1, dst);
844         double freq2 = Core.getTickFrequency();
845
846         assertTrue(0 < freq1);
847         assertEquals(freq1, freq2);
848     }
849
850     public void testHconcat() {
851         List<Mat> mats = Arrays.asList(Mat.eye(3, 3, CvType.CV_8U), Mat.zeros(3, 2, CvType.CV_8U));
852
853         Core.hconcat(mats, dst);
854
855         assertMatEqual(Mat.eye(3, 5, CvType.CV_8U), dst);
856     }
857
858     public void testIdctMatMat() {
859         Mat in = new Mat(1, 8, CvType.CV_32F) {
860             {
861                 put(0, 0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0);
862             }
863         };
864
865         Core.idct(in, dst);
866
867         truth = new Mat(1, 8, CvType.CV_32F) {
868             {
869                 put(0, 0, 3.3769724, -1.6215782, 2.3608727, 0.20730907, -0.86502546, 0.028082132, -0.7673766, 0.10917115);
870             }
871         };
872         assertMatEqual(truth, dst, EPS);
873     }
874
875     public void testIdctMatMatInt() {
876         Mat in = new Mat(2, 8, CvType.CV_32F) {
877             {
878                 put(0, 0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0);
879                 put(1, 0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0);
880             }
881         };
882
883         Core.idct(in, dst, Core.DCT_ROWS);
884
885         truth = new Mat(2, 8, CvType.CV_32F) {
886             {
887                 put(0, 0, 3.3769724, -1.6215782, 2.3608727, 0.20730907, -0.86502546, 0.028082132, -0.7673766, 0.10917115);
888                 put(1, 0, 3.3769724, -1.6215782, 2.3608727, 0.20730907, -0.86502546, 0.028082132, -0.7673766, 0.10917115);
889             }
890         };
891         assertMatEqual(truth, dst, EPS);
892     }
893
894     public void testIdftMatMat() {
895         Mat in = new Mat(1, 4, CvType.CV_32F) {
896             {
897                 put(0, 0, 1.0, 2.0, 3.0, 4.0);
898             }
899         };
900
901         Core.idft(in, dst);
902
903         truth = new Mat(1, 4, CvType.CV_32F) {
904             {
905                 put(0, 0, 9, -9, 1, 3);
906             }
907         };
908         assertMatEqual(truth, dst, EPS);
909     }
910
911     public void testIdftMatMatIntInt() {
912         Mat in = new Mat(2, 4, CvType.CV_32F) {
913             {
914                 put(0, 0, 1.0, 2.0, 3.0, 4.0);
915                 put(1, 0, 1.0, 2.0, 3.0, 4.0);
916             }
917         };
918         Mat dst = new Mat();
919
920         Core.idft(in, dst, Core.DFT_REAL_OUTPUT, 1);
921
922         truth = new Mat(2, 4, CvType.CV_32F) {
923             {
924                 put(0, 0, 18, -18, 2, 6);
925                 put(1, 0, 0, 0, 0, 0);
926             }
927         };
928         assertMatEqual(truth, dst, EPS);
929     }
930
931     public void testInRange() {
932         gray0.put(1, 1, 100, 150, 200);
933
934         Core.inRange(gray0, new Scalar(120), new Scalar(160), dst);
935
936         byte vals[] = new byte[3];
937         dst.get(1, 1, vals);
938
939         assertEquals(0, vals[0]);
940         assertEquals(-1, vals[1]);
941         assertEquals(0, vals[2]);
942         assertEquals(1, Core.countNonZero(dst));
943     }
944
945     public void testInsertChannel() {
946         Core.insertChannel(gray0, rgba128, 0);
947         Core.insertChannel(gray0, rgba128, 1);
948         Core.insertChannel(gray0, rgba128, 2);
949         Core.insertChannel(gray0, rgba128, 3);
950
951         assertMatEqual(rgba0, rgba128);
952     }
953
954     public void testInvertMatMat() {
955         Mat src = new Mat(2, 2, CvType.CV_32F) {
956             {
957                 put(0, 0, 1.0);
958                 put(0, 1, 2.0);
959                 put(1, 0, 1.5);
960                 put(1, 1, 4.0);
961             }
962         };
963
964         Core.invert(src, dst);
965
966         truth = new Mat(2, 2, CvType.CV_32F) {
967             {
968                 put(0, 0, 4.0);
969                 put(0, 1, -2.0);
970                 put(1, 0, -1.5);
971                 put(1, 1, 1.0);
972             }
973         };
974         assertMatEqual(truth, dst, EPS);
975     }
976
977     public void testInvertMatMatInt() {
978         Mat src = Mat.eye(3, 3, CvType.CV_32FC1);
979         src.put(0, 2, 1);
980
981         double cond = Core.invert(src, dst, Core.DECOMP_SVD);
982
983         truth = Mat.eye(3, 3, CvType.CV_32FC1);
984         truth.put(0, 2, -1);
985         assertMatEqual(truth, dst, EPS);
986         assertEquals(0.3819660544395447, cond, EPS);
987     }
988
989     public void testKmeansMatIntMatTermCriteriaIntInt() {
990         Mat data = new Mat(4, 5, CvType.CV_32FC1) {
991             {
992                 put(0, 0, 1, 2, 3, 4, 5);
993                 put(1, 0, 2, 3, 4, 5, 6);
994                 put(2, 0, 5, 4, 3, 2, 1);
995                 put(3, 0, 6, 5, 4, 3, 2);
996             }
997         };
998         TermCriteria criteria = new TermCriteria(TermCriteria.EPS, 0, EPS);
999         Mat labels = new Mat();
1000
1001         Core.kmeans(data, 2, labels, criteria, 1, Core.KMEANS_PP_CENTERS);
1002
1003         int[] first_center = new int[1];
1004         labels.get(0, 0, first_center);
1005         final int c1 = first_center[0];
1006         Mat expected_labels = new Mat(4, 1, CvType.CV_32S) {
1007             {
1008                 put(0, 0, c1, c1, 1 - c1, 1 - c1);
1009             }
1010         };
1011         assertMatEqual(expected_labels, labels);
1012     }
1013
1014     public void testKmeansMatIntMatTermCriteriaIntIntMat() {
1015         Mat data = new Mat(4, 5, CvType.CV_32FC1) {
1016             {
1017                 put(0, 0, 1, 2, 3, 4, 5);
1018                 put(1, 0, 2, 3, 4, 5, 6);
1019                 put(2, 0, 5, 4, 3, 2, 1);
1020                 put(3, 0, 6, 5, 4, 3, 2);
1021             }
1022         };
1023         TermCriteria criteria = new TermCriteria(TermCriteria.EPS, 0, EPS);
1024         Mat labels = new Mat();
1025         Mat centers = new Mat();
1026
1027         Core.kmeans(data, 2, labels, criteria, 6, Core.KMEANS_RANDOM_CENTERS, centers);
1028
1029         int[] first_center = new int[1];
1030         labels.get(0, 0, first_center);
1031         final int c1 = first_center[0];
1032         Mat expected_labels = new Mat(4, 1, CvType.CV_32S) {
1033             {
1034                 put(0, 0, c1, c1, 1 - c1, 1 - c1);
1035             }
1036         };
1037         Mat expected_centers = new Mat(2, 5, CvType.CV_32FC1) {
1038             {
1039                 put(c1, 0, 1.5, 2.5, 3.5, 4.5, 5.5);
1040                 put(1 - c1, 0, 5.5, 4.5, 3.5, 2.5, 1.5);
1041             }
1042         };
1043         assertMatEqual(expected_labels, labels);
1044         assertMatEqual(expected_centers, centers, EPS);
1045     }
1046
1047     public void testLineMatPointPointScalar() {
1048         int nPoints = Math.min(gray0.cols(), gray0.rows());
1049         Point point1 = new Point(0, 0);
1050         Point point2 = new Point(nPoints, nPoints);
1051         Scalar color = new Scalar(255);
1052
1053         Core.line(gray0, point1, point2, color);
1054
1055         assertTrue(nPoints == Core.countNonZero(gray0));
1056     }
1057
1058     public void testLineMatPointPointScalarInt() {
1059         int nPoints = Math.min(gray0.cols(), gray0.rows());
1060         Point point1 = new Point(0, 0);
1061         Point point2 = new Point(nPoints, nPoints);
1062
1063         Core.line(gray0, point1, point2, colorWhite, 0);
1064
1065         assertTrue(nPoints == Core.countNonZero(gray0));
1066     }
1067
1068     public void testLineMatPointPointScalarIntIntInt() {
1069         int nPoints = Math.min(gray0.cols(), gray0.rows());
1070         Point point1 = new Point(3, 4);
1071         Point point2 = new Point(nPoints, nPoints);
1072         Point point1_4 = new Point(3 * 4, 4 * 4);
1073         Point point2_4 = new Point(nPoints * 4, nPoints * 4);
1074
1075         Core.line(gray0, point2, point1, colorWhite, 2, Core.LINE_8, 0);
1076
1077         assertFalse(0 == Core.countNonZero(gray0));
1078
1079         Core.line(gray0, point2_4, point1_4, colorBlack, 2, Core.LINE_8, 2);
1080
1081         assertEquals(0, Core.countNonZero(gray0));
1082     }
1083
1084     public void testLog() {
1085         Mat in = new Mat(1, 4, CvType.CV_32FC1) {
1086             {
1087                 put(0, 0, 1.0, 10.0, 100.0, 1000.0);
1088             }
1089         };
1090
1091         Core.log(in, dst);
1092
1093         Mat expected = new Mat(1, 4, CvType.CV_32FC1) {
1094             {
1095                 put(0, 0, 0, 2.3025851, 4.6051702, 6.9077554);
1096             }
1097         };
1098         assertMatEqual(expected, dst, EPS);
1099     }
1100
1101     public void testLUTMatMatMat() {
1102         Mat lut = new Mat(1, 256, CvType.CV_8UC1);
1103         lut.setTo(new Scalar(0));
1104
1105         Core.LUT(grayRnd, lut, dst);
1106
1107         assertMatEqual(gray0, dst);
1108
1109         lut.setTo(new Scalar(255));
1110
1111         Core.LUT(grayRnd, lut, dst);
1112
1113         assertMatEqual(gray255, dst);
1114     }
1115
1116     public void testLUTMatMatMatInt() {
1117         Mat lut = new Mat(1, 256, CvType.CV_8UC1);
1118         // TODO: ban this overload
1119         try
1120         {
1121             Core.LUT(grayRnd, lut, dst, 1);
1122             fail("Last parameter for LUT was not supported");
1123         } catch (CvException e) {
1124             // expected
1125         }
1126     }
1127
1128     public void testMagnitude() {
1129         Mat x = new Mat(1, 4, CvType.CV_32F);
1130         Mat y = new Mat(1, 4, CvType.CV_32F);
1131         x.put(0, 0, 3.0, 5.0, 9.0, 6.0);
1132         y.put(0, 0, 4.0, 12.0, 40.0, 8.0);
1133
1134         Core.magnitude(x, y, dst);
1135
1136         Mat out = new Mat(1, 4, CvType.CV_32F);
1137         out.put(0, 0, 5.0, 13.0, 41.0, 10.0);
1138         assertMatEqual(out, dst, EPS);
1139
1140         Core.magnitude(gray0_32f, gray255_32f, dst);
1141
1142         assertMatEqual(gray255_32f, dst, EPS);
1143     }
1144
1145     public void testMahalanobis() {
1146         Mat covar = new Mat(matSize, matSize, CvType.CV_32F);
1147         Mat mean = new Mat(1, matSize, CvType.CV_32F);
1148         Core.calcCovarMatrix(grayRnd_32f, covar, mean, Core.COVAR_ROWS | Core.COVAR_NORMAL, CvType.CV_32F);
1149         covar = covar.inv();
1150         Mat line1 = grayRnd_32f.row(0);
1151         Mat line2 = grayRnd_32f.row(1);
1152
1153         double d = Core.Mahalanobis(line1, line1, covar);
1154
1155         assertEquals(0.0, d);
1156
1157         d = Core.Mahalanobis(line1, line2, covar);
1158
1159         assertTrue(d > 0.0);
1160     }
1161
1162     public void testMax() {
1163         Core.max(gray0, gray255, dst);
1164
1165         assertMatEqual(gray255, dst);
1166
1167         Mat x = new Mat(1, 1, CvType.CV_32F);
1168         Mat y = new Mat(1, 1, CvType.CV_32F);
1169         x.put(0, 0, 23.0);
1170         y.put(0, 0, 4.0);
1171
1172         Core.max(x, y, dst);
1173
1174         Mat truth = new Mat(1, 1, CvType.CV_32F);
1175         truth.put(0, 0, 23.0);
1176         assertMatEqual(truth, dst, EPS);
1177     }
1178
1179     public void testMeanMat() {
1180         Scalar mean = Core.mean(makeMask(gray128));
1181
1182         assertScalarEqual(new Scalar(64), mean, EPS);
1183     }
1184
1185     public void testMeanMatMat() {
1186         Mat mask1 = makeMask(gray1.clone());
1187         Mat mask2 = makeMask(gray0, 1);
1188
1189         Scalar mean1 = Core.mean(grayRnd, mask1);
1190         Scalar mean2 = Core.mean(grayRnd, mask2);
1191         Scalar mean = Core.mean(grayRnd, gray1);
1192
1193         assertScalarEqual(mean, new Scalar(0.5 * (mean1.val[0] + mean2.val[0])), EPS);
1194     }
1195
1196     public void testMeanStdDevMatMatMat() {
1197         MatOfDouble mean   = new MatOfDouble();
1198         MatOfDouble stddev = new MatOfDouble();
1199
1200         Core.meanStdDev(rgbLena, mean, stddev);
1201
1202         double expectedMean[] = new double[]
1203             {105.3989906311035, 99.56269836425781, 179.7303047180176};
1204         double expectedDev[] = new double[]
1205             {33.74205485167219, 52.8734582803278, 49.01569488056406};
1206
1207         assertArrayEquals(expectedMean, mean.toArray(), EPS);
1208         assertArrayEquals(expectedDev, stddev.toArray(), EPS);
1209     }
1210
1211     public void testMeanStdDevMatMatMatMat() {
1212         Mat submat = grayRnd.submat(0, grayRnd.rows() / 2, 0, grayRnd.cols() / 2);
1213         submat.setTo(new Scalar(33));
1214         Mat mask = gray0.clone();
1215         submat = mask.submat(0, mask.rows() / 2, 0, mask.cols() / 2);
1216         submat.setTo(new Scalar(1));
1217         MatOfDouble mean   = new MatOfDouble();
1218         MatOfDouble stddev = new MatOfDouble();
1219
1220         Core.meanStdDev(grayRnd, mean, stddev, mask);
1221
1222         double expectedMean[] = new double[] {33d};
1223         double expectedDev[]  = new double[] {0d};
1224
1225         assertArrayEquals(expectedMean, mean.toArray(), EPS);
1226         assertArrayEquals(expectedDev, stddev.toArray(), EPS);
1227     }
1228
1229     public void testMerge() {
1230         Mat src1 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(1));
1231         Mat src2 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(2));
1232         Mat src3 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(3));
1233         List<Mat> listMat = Arrays.asList(src1, src2, src3);
1234
1235         Core.merge(listMat, dst);
1236
1237         truth = new Mat(2, 2, CvType.CV_32FC3, new Scalar(1, 2, 3));
1238         assertMatEqual(truth, dst, EPS);
1239     }
1240
1241     public void testMin() {
1242         Core.min(gray0, gray255, dst);
1243
1244         assertMatEqual(gray0, dst);
1245     }
1246
1247     public void testMinMaxLocMat() {
1248         double minVal = 1;
1249         double maxVal = 10;
1250         Point minLoc = new Point(gray3.cols() / 4, gray3.rows() / 2);
1251         Point maxLoc = new Point(gray3.cols() / 2, gray3.rows() / 4);
1252         gray3.put((int) minLoc.y, (int) minLoc.x, minVal);
1253         gray3.put((int) maxLoc.y, (int) maxLoc.x, maxVal);
1254
1255         Core.MinMaxLocResult mmres = Core.minMaxLoc(gray3);
1256
1257         assertEquals(minVal, mmres.minVal);
1258         assertEquals(maxVal, mmres.maxVal);
1259         assertPointEquals(minLoc, mmres.minLoc, EPS);
1260         assertPointEquals(maxLoc, mmres.maxLoc, EPS);
1261     }
1262
1263     public void testMinMaxLocMatMat() {
1264         Mat src = new Mat(4, 4, CvType.CV_8U) {
1265             {
1266                 put(0, 0, 2, 4, 27, 3);
1267                 put(1, 0, 0, 8, 7, 130);
1268                 put(2, 0, 13, 4, 13, 4);
1269                 put(3, 0, 6, 4, 2, 13);
1270             }
1271         };
1272         Mat mask = new Mat(4, 4, CvType.CV_8U, new Scalar(0));
1273         mask.submat(1, 3, 1, 4).setTo(new Scalar(1));
1274
1275         MinMaxLocResult res = Core.minMaxLoc(src, mask);
1276
1277         assertEquals(4.0, res.minVal);
1278         assertEquals(130.0, res.maxVal);
1279         assertPointEquals(new Point(1, 2), res.minLoc, EPS);
1280         assertPointEquals(new Point(3, 1), res.maxLoc, EPS);
1281     }
1282
1283     public void testMixChannels() {
1284         rgba0.setTo(new Scalar(10, 20, 30, 40));
1285         List<Mat> src = Arrays.asList(rgba0);
1286         List<Mat> dst = Arrays.asList(gray3, gray2, gray1, gray0, getMat(CvType.CV_8UC3, 0, 0, 0));
1287         MatOfInt fromTo = new MatOfInt(
1288                 3, 0,
1289                 3, 1,
1290                 2, 2,
1291                 0, 3,
1292                 2, 4,
1293                 1, 5,
1294                 0, 6
1295         );
1296
1297         Core.mixChannels(src, dst, fromTo);
1298
1299         assertMatEqual(getMat(CvType.CV_8U, 40), dst.get(0));
1300         assertMatEqual(getMat(CvType.CV_8U, 40), dst.get(1));
1301         assertMatEqual(getMat(CvType.CV_8U, 30), dst.get(2));
1302         assertMatEqual(getMat(CvType.CV_8U, 10), dst.get(3));
1303         assertMatEqual(getMat(CvType.CV_8UC3, 30, 20, 10), dst.get(4));
1304     }
1305
1306     public void testMulSpectrumsMatMatMatInt() {
1307         Mat src1 = new Mat(1, 4, CvType.CV_32F) {
1308             {
1309                 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1310             }
1311         };
1312         Mat src2 = new Mat(1, 4, CvType.CV_32F) {
1313             {
1314                 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1315             }
1316         };
1317
1318         Core.mulSpectrums(src1, src2, dst, Core.DFT_ROWS);
1319
1320         Mat expected = new Mat(1, 4, CvType.CV_32F) {
1321             {
1322                 put(0, 0, 1, -5, 12, 16);
1323             }
1324         };
1325         assertMatEqual(expected, dst, EPS);
1326     }
1327
1328     public void testMulSpectrumsMatMatMatIntBoolean() {
1329         Mat src1 = new Mat(1, 4, CvType.CV_32F) {
1330             {
1331                 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1332             }
1333         };
1334         Mat src2 = new Mat(1, 4, CvType.CV_32F) {
1335             {
1336                 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1337             }
1338         };
1339
1340         Core.mulSpectrums(src1, src2, dst, Core.DFT_ROWS, true);
1341
1342         Mat expected = new Mat(1, 4, CvType.CV_32F) {
1343             {
1344                 put(0, 0, 1, 13, 0, 16);
1345             }
1346         };
1347         assertMatEqual(expected, dst, EPS);
1348     }
1349
1350     public void testMultiplyMatMatMat() {
1351         Core.multiply(gray0, gray255, dst);
1352
1353         assertMatEqual(gray0, dst);
1354     }
1355
1356     public void testMultiplyMatMatMatDouble() {
1357         Core.multiply(gray1, gray1, dst, 2.0);
1358
1359         assertMatEqual(gray2, dst);
1360
1361     }
1362
1363     public void testMultiplyMatMatMatDoubleInt() {
1364         Core.multiply(gray1, gray2, dst, 1.5, CvType.CV_32F);
1365
1366         assertMatEqual(gray3_32f, dst, EPS);
1367     }
1368
1369     public void testMulTransposedMatMatBoolean() {
1370         Core.mulTransposed(grayE_32f, dst, true);
1371
1372         assertMatEqual(grayE_32f, dst, EPS);
1373     }
1374
1375     public void testMulTransposedMatMatBooleanMatDouble() {
1376         Core.mulTransposed(grayE_32f, dst, true, gray0_32f, 2);
1377
1378         truth = gray0_32f;
1379         truth.diag().setTo(new Scalar(2));
1380         assertMatEqual(truth, dst, EPS);
1381     }
1382
1383     public void testMulTransposedMatMatBooleanMatDoubleInt() {
1384         Mat a = getMat(CvType.CV_32F, 1);
1385
1386         Core.mulTransposed(a, dst, true, gray0_32f, 3, CvType.CV_64F);
1387
1388         assertMatEqual(getMat(CvType.CV_64F, 3 * a.rows()), dst, EPS);
1389     }
1390
1391     public void testNormalizeMatMat() {
1392         Mat m = gray0.clone();
1393         m.diag().setTo(new Scalar(2));
1394
1395         Core.normalize(m, dst);
1396
1397         assertMatEqual(gray0, dst);
1398     }
1399
1400     public void testNormalizeMatMatDoubleDoubleInt() {
1401         Mat src = new Mat(1, 4, CvType.CV_32F) {
1402             {
1403                 put(0, 0, 1.0, 2.0, 3.0, 4.0);
1404             }
1405         };
1406
1407         Core.normalize(src, dst, 1.0, 2.0, Core.NORM_INF);
1408
1409         Mat expected = new Mat(1, 4, CvType.CV_32F) {
1410             {
1411                 put(0, 0, 0.25, 0.5, 0.75, 1);
1412             }
1413         };
1414         assertMatEqual(expected, dst, EPS);
1415     }
1416
1417     public void testNormalizeMatMatDoubleDoubleIntInt() {
1418         Mat src = new Mat(1, 5, CvType.CV_32F) {
1419             {
1420                 put(0, 0, 0, 1, 2, 3, 4);
1421             }
1422         };
1423
1424         Core.normalize(src, dst, 1, 2, Core.NORM_MINMAX, CvType.CV_64F);
1425
1426         Mat expected = new Mat(1, 5, CvType.CV_64F) {
1427             {
1428                 put(0, 0, 1, 1.25, 1.5, 1.75, 2);
1429             }
1430         };
1431         assertMatEqual(expected, dst, EPS);
1432     }
1433
1434     public void testNormalizeMatMatDoubleDoubleIntIntMat() {
1435         Mat src = new Mat(1, 5, CvType.CV_32F) {
1436             {
1437                 put(0, 0, 0, 1, 2, 3, 4);
1438             }
1439         };
1440         Mat mask = new Mat(1, 5, CvType.CV_8U) {
1441             {
1442                 put(0, 0, 1, 0, 0, 0, 1);
1443             }
1444         };
1445         dst = src.clone();
1446
1447         Core.normalize(src, dst, 1, 2, Core.NORM_MINMAX, CvType.CV_32F, mask);
1448
1449         Mat expected = new Mat(1, 5, CvType.CV_32F) {
1450             {
1451                 put(0, 0, 1, 1, 2, 3, 2);
1452             }
1453         };
1454         assertMatEqual(expected, dst, EPS);
1455     }
1456
1457     public void testNormMat() {
1458         double n = Core.norm(gray1);
1459
1460         assertEquals(10., n);
1461     }
1462
1463     public void testNormMatInt() {
1464         double n = Core.norm(gray127, Core.NORM_INF);
1465
1466         assertEquals(127., n);
1467     }
1468
1469     public void testNormMatIntMat() {
1470         double n = Core.norm(gray3, Core.NORM_L1, gray0);
1471
1472         assertEquals(0.0, n);
1473     }
1474
1475     public void testNormMatMat() {
1476         double n = Core.norm(gray0, gray1);
1477
1478         assertEquals(10.0, n);
1479     }
1480
1481     public void testNormMatMatInt() {
1482         double n = Core.norm(gray127, gray1, Core.NORM_INF);
1483
1484         assertEquals(126.0, n);
1485     }
1486
1487     public void testNormMatMatIntMat() {
1488         double n = Core.norm(gray3, gray0, Core.NORM_L1, makeMask(gray0.clone(), 1));
1489
1490         assertEquals(150.0, n);
1491     }
1492
1493     public void testPCABackProject() {
1494         Mat mean = new Mat(1, 4, CvType.CV_32F) {
1495             {
1496                 put(0, 0, 2, 4, 4, 8);
1497             }
1498         };
1499         Mat vectors = new Mat(1, 4, CvType.CV_32F, new Scalar(0)) {
1500             {
1501                 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1502             }
1503         };
1504         Mat data = new Mat(3, 1, CvType.CV_32F) {
1505             {
1506                 put(0, 0, -5, 0, -10);
1507             }
1508         };
1509         Mat result = new Mat();
1510
1511         Core.PCABackProject(data, mean, vectors, result);
1512
1513         Mat truth = new Mat(3, 4, CvType.CV_32F) {
1514             {
1515                 put(0, 0, 1, 2, 2, 4);
1516                 put(1, 0, 2, 4, 4, 8);
1517                 put(2, 0, 0, 0, 0, 0);
1518             }
1519         };
1520         assertMatEqual(truth, result, EPS);
1521     }
1522
1523     public void testPCAComputeMatMatMat() {
1524         Mat data = new Mat(3, 4, CvType.CV_32F) {
1525             {
1526                 put(0, 0, 1, 2, 2, 4);
1527                 put(1, 0, 2, 4, 4, 8);
1528                 put(2, 0, 3, 6, 6, 12);
1529             }
1530         };
1531         Mat mean = new Mat();
1532         Mat vectors = new Mat();
1533
1534         Core.PCACompute(data, mean, vectors);
1535
1536         Mat mean_truth = new Mat(1, 4, CvType.CV_32F) {
1537             {
1538                 put(0, 0, 2, 4, 4, 8);
1539             }
1540         };
1541         Mat vectors_truth = new Mat(3, 4, CvType.CV_32F, new Scalar(0)) {
1542             {
1543                 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1544             }
1545         };
1546         assertMatEqual(mean_truth, mean, EPS);
1547         assertMatEqual(vectors_truth, vectors, EPS);
1548     }
1549
1550     public void testPCAComputeMatMatMatInt() {
1551         Mat data = new Mat(3, 4, CvType.CV_32F) {
1552             {
1553                 put(0, 0, 1, 2, 2, 4);
1554                 put(1, 0, 2, 4, 4, 8);
1555                 put(2, 0, 3, 6, 6, 12);
1556             }
1557         };
1558         Mat mean = new Mat();
1559         Mat vectors = new Mat();
1560
1561         Core.PCACompute(data, mean, vectors, 1);
1562
1563         Mat mean_truth = new Mat(1, 4, CvType.CV_32F) {
1564             {
1565                 put(0, 0, 2, 4, 4, 8);
1566             }
1567         };
1568         Mat vectors_truth = new Mat(1, 4, CvType.CV_32F, new Scalar(0)) {
1569             {
1570                 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1571             }
1572         };
1573         assertMatEqual(mean_truth, mean, EPS);
1574         assertMatEqual(vectors_truth, vectors, EPS);
1575     }
1576
1577     public void testPCAProject() {
1578         Mat mean = new Mat(1, 4, CvType.CV_32F) {
1579             {
1580                 put(0, 0, 2, 4, 4, 8);
1581             }
1582         };
1583         Mat vectors = new Mat(1, 4, CvType.CV_32F, new Scalar(0)) {
1584             {
1585                 put(0, 0, 0.2, 0.4, 0.4, 0.8);
1586             }
1587         };
1588         Mat data = new Mat(3, 4, CvType.CV_32F) {
1589             {
1590                 put(0, 0, 1, 2, 2, 4);
1591                 put(1, 0, 2, 4, 4, 8);
1592                 put(2, 0, 0, 0, 0, 0);
1593             }
1594         };
1595         Mat result = new Mat();
1596
1597         Core.PCAProject(data, mean, vectors, result);
1598
1599         Mat truth = new Mat(3, 1, CvType.CV_32F) {
1600             {
1601                 put(0, 0, -5, 0, -10);
1602             }
1603         };
1604         assertMatEqual(truth, result, EPS);
1605     }
1606
1607     public void testPerspectiveTransform() {
1608         Mat src = new Mat(matSize, matSize, CvType.CV_32FC2);
1609         Core.randu(src, 0, 256);
1610         Mat transformMatrix = Mat.eye(3, 3, CvType.CV_32F);
1611
1612         Core.perspectiveTransform(src, dst, transformMatrix);
1613         assertMatEqual(src, dst, EPS);
1614     }
1615
1616     public void testPerspectiveTransform3D() {
1617         Mat src = new Mat(matSize, matSize, CvType.CV_32FC3);
1618         Core.randu(src, 0, 256);
1619         Mat transformMatrix = Mat.eye(4, 4, CvType.CV_32F);
1620
1621         Core.perspectiveTransform(src, dst, transformMatrix);
1622
1623         assertMatEqual(src, dst, EPS);
1624     }
1625
1626     private static double atan2deg(double y, double x)
1627     {
1628         double res = Math.atan2(y, x);
1629         if (res < 0)
1630             res = Math.PI * 2 + res;
1631         return res * 180 / Math.PI;
1632     }
1633
1634     private static double atan2rad(double y, double x)
1635     {
1636         double res = Math.atan2(y, x);
1637         if (res < 0)
1638             res = Math.PI * 2 + res;
1639         return res;
1640     }
1641
1642     public void testPhaseMatMatMat() {
1643         Mat x = new Mat(1, 4, CvType.CV_32F) {
1644             {
1645                 put(0, 0, 10.0, 10.0, 20.0, 5.0);
1646             }
1647         };
1648         Mat y = new Mat(1, 4, CvType.CV_32F) {
1649             {
1650                 put(0, 0, 20.0, 15.0, 20.0, 20.0);
1651             }
1652         };
1653         Mat gold = new Mat(1, 4, CvType.CV_32F) {
1654             {
1655                 put(0, 0, atan2rad(20, 10), atan2rad(15, 10), atan2rad(20, 20), atan2rad(20, 5));
1656             }
1657         };
1658
1659         Core.phase(x, y, dst);
1660
1661         assertMatEqual(gold, dst, EPS);
1662     }
1663
1664     public void testPhaseMatMatMatBoolean() {
1665         Mat x = new Mat(1, 4, CvType.CV_32F) {
1666             {
1667                 put(0, 0, 10.0, 10.0, 20.0, 5.0);
1668             }
1669         };
1670         Mat y = new Mat(1, 4, CvType.CV_32F) {
1671             {
1672                 put(0, 0, 20.0, 15.0, 20.0, 20.0);
1673             }
1674         };
1675         Mat gold = new Mat(1, 4, CvType.CV_32F) {
1676             {
1677                 put(0, 0, atan2deg(20, 10), atan2deg(15, 10), atan2deg(20, 20), atan2deg(20, 5));
1678             }
1679         };
1680
1681         Core.phase(x, y, dst, true);
1682
1683         assertMatEqual(gold, dst, EPS * 180 / Math.PI);
1684     }
1685
1686     public void testPolarToCartMatMatMatMat() {
1687         Mat magnitude = new Mat(1, 3, CvType.CV_32F) {
1688             {
1689                 put(0, 0, 5.0, 10.0, 13.0);
1690             }
1691         };
1692         Mat angle = new Mat(1, 3, CvType.CV_32F) {
1693             {
1694                 put(0, 0, 0.92729962, 0.92729962, 1.1759995);
1695             }
1696         };
1697         Mat xCoordinate = new Mat();
1698         Mat yCoordinate = new Mat();
1699
1700         Core.polarToCart(magnitude, angle, xCoordinate, yCoordinate);
1701
1702         Mat x = new Mat(1, 3, CvType.CV_32F) {
1703             {
1704                 put(0, 0, 3.0, 6.0, 5, 0);
1705             }
1706         };
1707         Mat y = new Mat(1, 3, CvType.CV_32F) {
1708             {
1709                 put(0, 0, 4.0, 8.0, 12.0);
1710             }
1711         };
1712         assertMatEqual(x, xCoordinate, EPS);
1713         assertMatEqual(y, yCoordinate, EPS);
1714     }
1715
1716     public void testPolarToCartMatMatMatMatBoolean() {
1717         Mat magnitude = new Mat(1, 3, CvType.CV_32F) {
1718             {
1719                 put(0, 0, 5.0, 10.0, 13.0);
1720             }
1721         };
1722         Mat angle = new Mat(1, 3, CvType.CV_32F) {
1723             {
1724                 put(0, 0, 0.92729962, 0.92729962, 1.1759995);
1725             }
1726         };
1727         Mat xCoordinate = new Mat();
1728         Mat yCoordinate = new Mat();
1729
1730         Core.polarToCart(magnitude, angle, xCoordinate, yCoordinate, true);
1731
1732         Mat x = new Mat(1, 3, CvType.CV_32F) {
1733             {
1734                 put(0, 0, 4.9993458, 9.9986916, 12.997262);
1735             }
1736         };
1737         Mat y = new Mat(1, 3, CvType.CV_32F) {
1738             {
1739                 put(0, 0, 0.080918625, 0.16183725, 0.26680708);
1740             }
1741         };
1742         assertMatEqual(x, xCoordinate, EPS);
1743         assertMatEqual(y, yCoordinate, EPS);
1744     }
1745
1746     public void testPolylinesMatListOfListOfPointBooleanScalar() {
1747         Mat img = gray0;
1748         List<MatOfPoint> polyline = new ArrayList<MatOfPoint>();
1749         polyline.add(new MatOfPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
1750
1751         Core.polylines(img, polyline, true, new Scalar(100));
1752
1753         assertEquals(22, Core.countNonZero(img));
1754
1755         Core.polylines(img, polyline, false, new Scalar(0));
1756
1757         assertEquals(4, Core.countNonZero(img));
1758     }
1759
1760     public void testPolylinesMatListOfListOfPointBooleanScalarInt() {
1761         Mat img = gray0;
1762         List<MatOfPoint> polyline = new ArrayList<MatOfPoint>();
1763         polyline.add(new MatOfPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
1764
1765         Core.polylines(img, polyline, true, new Scalar(100), 2);
1766
1767         assertEquals(62, Core.countNonZero(img));
1768     }
1769
1770     public void testPolylinesMatListOfListOfPointBooleanScalarIntIntInt() {
1771         Mat img = gray0;
1772         List<MatOfPoint> polyline1 = new ArrayList<MatOfPoint>();
1773         polyline1.add(new MatOfPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
1774         List<MatOfPoint> polyline2 = new ArrayList<MatOfPoint>();
1775         polyline2.add(new MatOfPoint(new Point(2, 2), new Point(14, 2), new Point(14, 12), new Point(2, 12)));
1776
1777         Core.polylines(img, polyline1, true, new Scalar(100), 2, Core.LINE_8, 0);
1778
1779         assertTrue(Core.countNonZero(img) > 0);
1780
1781         Core.polylines(img, polyline2, true, new Scalar(0), 2, Core.LINE_8, 1);
1782
1783         assertEquals(0, Core.countNonZero(img));
1784     }
1785
1786     public void testPow() {
1787         Core.pow(gray2, 7, dst);
1788
1789         assertMatEqual(gray128, dst);
1790     }
1791
1792     public void testPutTextMatStringPointIntDoubleScalar() {
1793         String text = "Hello World";
1794         Size labelSize = new Size(175, 22);
1795         Mat img = new Mat(20 + (int) labelSize.height, 20 + (int) labelSize.width, CvType.CV_8U, colorBlack);
1796         Point origin = new Point(10, labelSize.height + 10);
1797
1798         Core.putText(img, text, origin, Core.FONT_HERSHEY_SIMPLEX, 1.0, colorWhite);
1799
1800         assertTrue(Core.countNonZero(img) > 0);
1801         // check that border is not corrupted
1802         Core.rectangle(img, new Point(11, 11), new Point(labelSize.width + 10, labelSize.height + 10), colorBlack, Core.FILLED);
1803         assertEquals(0, Core.countNonZero(img));
1804     }
1805
1806     public void testPutTextMatStringPointIntDoubleScalarInt() {
1807         String text = "Hello World";
1808         Size labelSize = new Size(176, 22);
1809         Mat img = new Mat(20 + (int) labelSize.height, 20 + (int) labelSize.width, CvType.CV_8U, colorBlack);
1810         Point origin = new Point(10, labelSize.height + 10);
1811
1812         Core.putText(img, text, origin, Core.FONT_HERSHEY_SIMPLEX, 1.0, colorWhite, 2);
1813
1814         assertTrue(Core.countNonZero(img) > 0);
1815         // check that border is not corrupted
1816         Core.rectangle(img, new Point(10, 10), new Point(labelSize.width + 10 + 1, labelSize.height + 10 + 1), colorBlack, Core.FILLED);
1817         assertEquals(0, Core.countNonZero(img));
1818     }
1819
1820     public void testPutTextMatStringPointIntDoubleScalarIntIntBoolean() {
1821         String text = "Hello World";
1822         Size labelSize = new Size(175, 22);
1823
1824         Mat img = new Mat(20 + (int) labelSize.height, 20 + (int) labelSize.width, CvType.CV_8U, colorBlack);
1825         Point origin = new Point(10, 10);
1826
1827         Core.putText(img, text, origin, Core.FONT_HERSHEY_SIMPLEX, 1.0, colorWhite, 1, Core.LINE_8, true);
1828
1829         assertTrue(Core.countNonZero(img) > 0);
1830         // check that border is not corrupted
1831         Core.rectangle(img, new Point(10, 10), new Point(labelSize.width + 9, labelSize.height + 9), colorBlack, Core.FILLED);
1832         assertEquals(0, Core.countNonZero(img));
1833     }
1834
1835     public void testRandn() {
1836         Core.randn(gray0, 100, 23);
1837
1838         assertEquals(100., Core.mean(gray0).val[0], 23 / 2);
1839     }
1840
1841     public void testRandShuffleMat() {
1842         Mat original = new Mat(1, 5, CvType.CV_32F) {
1843             {
1844                 put(0, 0, 7, 5, 2, 8, 1);
1845             }
1846         };
1847         Mat shuffled = original.clone();
1848
1849         Core.randShuffle(shuffled);
1850
1851         assertMatNotEqual(original, shuffled, EPS);
1852         Mat dst1 = new Mat();
1853         Mat dst2 = new Mat();
1854         Core.sort(original, dst1, Core.SORT_ASCENDING);
1855         Core.sort(shuffled, dst2, Core.SORT_ASCENDING);
1856         assertMatEqual(dst1, dst2, EPS);
1857     }
1858
1859     public void testRandShuffleMatDouble() {
1860         Mat original = new Mat(1, 5, CvType.CV_32F) {
1861             {
1862                 put(0, 0, 7, 5, 2, 8, 1);
1863             }
1864         };
1865         Mat shuffled = original.clone();
1866
1867         Core.randShuffle(shuffled, 10);
1868
1869         assertMatNotEqual(original, shuffled, EPS);
1870         Mat dst1 = new Mat();
1871         Mat dst2 = new Mat();
1872         Core.sort(original, dst1, Core.SORT_ASCENDING);
1873         Core.sort(shuffled, dst2, Core.SORT_ASCENDING);
1874         assertMatEqual(dst1, dst2, EPS);
1875     }
1876
1877     public void testRandu() {
1878         Core.randu(gray0, 3, 23);
1879         fail("Not yet implemented");
1880         //assertTrue(Core.checkRange(gray0, true, null, 3, 23));
1881     }
1882
1883     public void testRectangleMatPointPointScalar() {
1884         Point bottomRight = new Point(gray0.cols() / 2, gray0.rows() / 2);
1885         Point topLeft = new Point(0, 0);
1886         Scalar color = new Scalar(128);
1887
1888         Core.rectangle(gray0, bottomRight, topLeft, color);
1889
1890         assertTrue(0 != Core.countNonZero(gray0));
1891     }
1892
1893     public void testRectangleMatPointPointScalarInt() {
1894         Point bottomRight = new Point(gray0.cols(), gray0.rows());
1895         Point topLeft = new Point(0, 0);
1896         Scalar color = new Scalar(128);
1897
1898         Core.rectangle(gray0, bottomRight, topLeft, color, 2);
1899         Core.rectangle(gray0, bottomRight, topLeft, colorBlack);
1900
1901         assertTrue(0 != Core.countNonZero(gray0));
1902     }
1903
1904     public void testRectangleMatPointPointScalarIntInt() {
1905         Point bottomRight = new Point(gray0.cols() / 2, gray0.rows() / 2);
1906         Point topLeft = new Point(0, 0);
1907         Scalar color = new Scalar(128);
1908
1909         Core.rectangle(gray0, bottomRight, topLeft, color, 2, Core.LINE_AA, 0);
1910         Core.rectangle(gray0, bottomRight, topLeft, colorBlack, 2, Core.LINE_4, 0);
1911
1912         assertTrue(0 != Core.countNonZero(gray0));
1913     }
1914
1915     public void testRectangleMatPointPointScalarIntIntInt() {
1916         Point bottomRight1 = new Point(gray0.cols(), gray0.rows());
1917         Point bottomRight2 = new Point(gray0.cols() / 2, gray0.rows() / 2);
1918         Point topLeft = new Point(0, 0);
1919         Scalar color = new Scalar(128);
1920
1921         Core.rectangle(gray0, bottomRight1, topLeft, color, 2, Core.LINE_8, 1);
1922
1923         assertTrue(0 != Core.countNonZero(gray0));
1924
1925         Core.rectangle(gray0, bottomRight2, topLeft, colorBlack, 2, Core.LINE_8, 0);
1926
1927         assertEquals(0, Core.countNonZero(gray0));
1928     }
1929
1930     public void testReduceMatMatIntInt() {
1931         Mat src = new Mat(2, 2, CvType.CV_32F) {
1932             {
1933                 put(0, 0, 1, 0);
1934                 put(1, 0, 3, 0);
1935             }
1936         };
1937
1938         Core.reduce(src, dst, 0, Core.REDUCE_AVG);
1939
1940         Mat out = new Mat(1, 2, CvType.CV_32F) {
1941             {
1942                 put(0, 0, 2, 0);
1943             }
1944         };
1945         assertMatEqual(out, dst, EPS);
1946     }
1947
1948     public void testReduceMatMatIntIntInt() {
1949         Mat src = new Mat(2, 2, CvType.CV_32F) {
1950             {
1951                 put(0, 0, 1, 0);
1952                 put(1, 0, 2, 3);
1953             }
1954         };
1955
1956         Core.reduce(src, dst, 1, Core.REDUCE_SUM, CvType.CV_64F);
1957
1958         Mat out = new Mat(2, 1, CvType.CV_64F) {
1959             {
1960                 put(0, 0, 1, 5);
1961             }
1962         };
1963         assertMatEqual(out, dst, EPS);
1964     }
1965
1966     public void testRepeat() {
1967         Mat src = new Mat(1, 2, CvType.CV_32F, new Scalar(0));
1968
1969         Core.repeat(src, matSize, matSize / 2, dst);
1970
1971         assertMatEqual(gray0_32f, dst, EPS);
1972     }
1973
1974     public void testScaleAdd() {
1975         Core.scaleAdd(gray3, 2.0, gray3, dst);
1976
1977         assertMatEqual(gray9, dst);
1978     }
1979
1980     public void testSetIdentityMat() {
1981         Core.setIdentity(gray0_32f);
1982
1983         assertMatEqual(grayE_32f, gray0_32f, EPS);
1984     }
1985
1986     public void testSetIdentityMatScalar() {
1987         Mat m = gray0_32f;
1988
1989         Core.setIdentity(m, new Scalar(5));
1990
1991         truth = new Mat(m.size(), m.type(), new Scalar(0));
1992         truth.diag().setTo(new Scalar(5));
1993         assertMatEqual(truth, m, EPS);
1994     }
1995
1996     public void testSolveCubic() {
1997         Mat coeffs = new Mat(1, 4, CvType.CV_32F) {
1998             {
1999                 put(0, 0, 1, 6, 11, 6);
2000             }
2001         };
2002
2003         assertEquals(3, Core.solveCubic(coeffs, dst));
2004
2005         Mat roots = new Mat(3, 1, CvType.CV_32F) {
2006             {
2007                 put(0, 0, -3, -1, -2);
2008             }
2009         };
2010         assertMatEqual(roots, dst, EPS);
2011     }
2012
2013     public void testSolveMatMatMat() {
2014         Mat a = new Mat(3, 3, CvType.CV_32F) {
2015             {
2016                 put(0, 0, 1, 1, 1);
2017                 put(1, 0, 1, -2, 2);
2018                 put(2, 0, 1, 2, 1);
2019             }
2020         };
2021         Mat b = new Mat(3, 1, CvType.CV_32F) {
2022             {
2023                 put(0, 0, 0, 4, 2);
2024             }
2025         };
2026
2027         assertTrue(Core.solve(a, b, dst));
2028
2029         Mat res = new Mat(3, 1, CvType.CV_32F) {
2030             {
2031                 put(0, 0, -12, 2, 10);
2032             }
2033         };
2034         assertMatEqual(res, dst, EPS);
2035     }
2036
2037     public void testSolveMatMatMatInt() {
2038         Mat a = new Mat(3, 3, CvType.CV_32F) {
2039             {
2040                 put(0, 0, 1, 1, 1);
2041                 put(1, 0, 1, -2, 2);
2042                 put(2, 0, 1, 2, 1);
2043             }
2044         };
2045         Mat b = new Mat(3, 1, CvType.CV_32F) {
2046             {
2047                 put(0, 0, 0, 4, 2);
2048             }
2049         };
2050
2051         assertTrue(Core.solve(a, b, dst, Core.DECOMP_QR | Core.DECOMP_NORMAL));
2052
2053         Mat res = new Mat(3, 1, CvType.CV_32F) {
2054             {
2055                 put(0, 0, -12, 2, 10);
2056             }
2057         };
2058         assertMatEqual(res, dst, EPS);
2059     }
2060
2061     public void testSolvePolyMatMat() {
2062         Mat coeffs = new Mat(4, 1, CvType.CV_32F) {
2063             {
2064                 put(0, 0, -6, 11, -6, 1);
2065             }
2066         };
2067         Mat roots = new Mat();
2068
2069         assertEquals(0.0, Core.solvePoly(coeffs, roots));
2070
2071         truth = new Mat(3, 1, CvType.CV_32FC2) {
2072             {
2073                 put(0, 0, 1, 0, 2, 0, 3, 0);
2074             }
2075         };
2076         assertMatEqual(truth, roots, EPS);
2077     }
2078
2079     public void testSolvePolyMatMatInt() {
2080         Mat coeffs = new Mat(4, 1, CvType.CV_32F) {
2081             {
2082                 put(0, 0, -6, 11, -6, 1);
2083             }
2084         };
2085         Mat roots = new Mat();
2086
2087         assertEquals(10.198039027185569, Core.solvePoly(coeffs, roots, 1));
2088
2089         truth = new Mat(3, 1, CvType.CV_32FC2) {
2090             {
2091                 put(0, 0, 1, 0, -1, 2, -2, 12);
2092             }
2093         };
2094         assertMatEqual(truth, roots, EPS);
2095     }
2096
2097     public void testSort() {
2098         Mat submat = gray0.submat(0, gray0.rows() / 2, 0, gray0.cols() / 2);
2099         submat.setTo(new Scalar(1.0));
2100
2101         Core.sort(gray0, dst, Core.SORT_EVERY_ROW);
2102
2103         submat = dst.submat(0, dst.rows() / 2, dst.cols() / 2, dst.cols());
2104         assertTrue(submat.total() == Core.countNonZero(submat));
2105
2106         Core.sort(gray0, dst, Core.SORT_EVERY_COLUMN);
2107
2108         submat = dst.submat(dst.rows() / 2, dst.rows(), 0, dst.cols() / 2);
2109
2110         assertTrue(submat.total() == Core.countNonZero(submat));
2111     }
2112
2113     public void testSortIdx() {
2114         Mat a = Mat.eye(3, 3, CvType.CV_8UC1);
2115         Mat b = new Mat();
2116
2117         Core.sortIdx(a, b, Core.SORT_EVERY_ROW | Core.SORT_ASCENDING);
2118
2119         truth = new Mat(3, 3, CvType.CV_32SC1) {
2120             {
2121                 put(0, 0, 1, 2, 0);
2122                 put(1, 0, 0, 2, 1);
2123                 put(2, 0, 0, 1, 2);
2124             }
2125         };
2126         assertMatEqual(truth, b);
2127     }
2128
2129     public void testSplit() {
2130         Mat m = getMat(CvType.CV_8UC3, 1, 2, 3);
2131         ArrayList<Mat> cois = new ArrayList<Mat>();
2132
2133         Core.split(m, cois);
2134
2135         assertMatEqual(gray1, cois.get(0));
2136         assertMatEqual(gray2, cois.get(1));
2137         assertMatEqual(gray3, cois.get(2));
2138     }
2139
2140     public void testSqrt() {
2141         Core.sqrt(gray9_32f, dst);
2142
2143         assertMatEqual(gray3_32f, dst, EPS);
2144
2145         Mat rgba144 = new Mat(matSize, matSize, CvType.CV_32FC4, Scalar.all(144));
2146         Mat rgba12 = new Mat(matSize, matSize, CvType.CV_32FC4, Scalar.all(12));
2147
2148         Core.sqrt(rgba144, dst);
2149
2150         assertMatEqual(rgba12, dst, EPS);
2151     }
2152
2153     public void testSubtractMatMatMat() {
2154         Core.subtract(gray128, gray1, dst);
2155
2156         assertMatEqual(gray127, dst);
2157     }
2158
2159     public void testSubtractMatMatMatMat() {
2160         Mat mask = makeMask(gray1.clone());
2161         dst = gray128.clone();
2162
2163         Core.subtract(gray128, gray1, dst, mask);
2164
2165         assertMatEqual(makeMask(gray127, 128), dst);
2166     }
2167
2168     public void testSubtractMatMatMatMatInt() {
2169         Core.subtract(gray3, gray2, dst, gray1, CvType.CV_32F);
2170
2171         assertMatEqual(gray1_32f, dst, EPS);
2172     }
2173
2174     public void testSumElems() {
2175         Mat src = new Mat(4, 4, CvType.CV_8U, new Scalar(10));
2176
2177         Scalar res1 = Core.sumElems(src);
2178
2179         assertScalarEqual(new Scalar(160), res1, EPS);
2180     }
2181
2182     public void testSVBackSubst() {
2183         Mat w = new Mat(2, 2, CvType.CV_32FC1, new Scalar(2));
2184         Mat u = new Mat(2, 2, CvType.CV_32FC1, new Scalar(4));
2185         Mat vt = new Mat(2, 2, CvType.CV_32FC1, new Scalar(2));
2186         Mat rhs = new Mat(2, 2, CvType.CV_32FC1, new Scalar(1));
2187
2188         Core.SVBackSubst(w, u, vt, rhs, dst);
2189
2190         Mat truth = new Mat(2, 2, CvType.CV_32FC1, new Scalar(16));
2191         assertMatEqual(truth, dst, EPS);
2192     }
2193
2194     public void testSVDecompMatMatMatMat() {
2195         Mat src = new Mat(1, 4, CvType.CV_32FC1) {
2196             {
2197                 put(0, 0, 1, 4, 8, 6);
2198             }
2199         };
2200         Mat w = new Mat();
2201         Mat u = new Mat();
2202         Mat vt = new Mat();
2203
2204         Core.SVDecomp(src, w, u, vt);
2205
2206         Mat truthW = new Mat(1, 1, CvType.CV_32FC1, new Scalar(10.816654));
2207         Mat truthU = new Mat(1, 1, CvType.CV_32FC1, new Scalar(1));
2208         Mat truthVT = new Mat(1, 4, CvType.CV_32FC1) {
2209             {
2210                 put(0, 0, 0.09245003, 0.36980012, 0.73960024, 0.5547002);
2211             }
2212         };
2213         assertMatEqual(truthW, w, EPS);
2214         assertMatEqual(truthU, u, EPS);
2215         assertMatEqual(truthVT, vt, EPS);
2216     }
2217
2218     public void testSVDecompMatMatMatMatInt() {
2219         Mat src = new Mat(1, 4, CvType.CV_32FC1) {
2220             {
2221                 put(0, 0, 1, 4, 8, 6);
2222             }
2223         };
2224         Mat w = new Mat();
2225         Mat u = new Mat();
2226         Mat vt = new Mat();
2227
2228         Core.SVDecomp(src, w, u, vt, Core.SVD_NO_UV);
2229
2230         Mat truthW = new Mat(1, 1, CvType.CV_32FC1, new Scalar(10.816654));
2231         assertMatEqual(truthW, w, EPS);
2232         assertTrue(u.empty());
2233         assertTrue(vt.empty());
2234     }
2235
2236     public void testTrace() {
2237         Scalar s = Core.trace(gray1);
2238
2239         assertEquals(new Scalar(matSize), s);
2240     }
2241
2242     public void testTransform() {
2243         Mat src = new Mat(2, 2, CvType.CV_32F, new Scalar(55));
2244         Mat m = Mat.eye(2, 2, CvType.CV_32FC1);
2245
2246         Core.transform(src, dst, m);
2247
2248         truth = new Mat(2, 2, CvType.CV_32FC2, new Scalar(55, 1));
2249         assertMatEqual(truth, dst, EPS);
2250     }
2251
2252     public void testTranspose() {
2253         gray0.submat(0, gray0.rows() / 2, 0, gray0.cols()).setTo(new Scalar(1));
2254         Mat destination = getMat(CvType.CV_8U, 0);
2255
2256         Core.transpose(gray0, destination);
2257
2258         Mat subdst = destination.submat(0, destination.rows(), 0, destination.cols() / 2);
2259         assertTrue(subdst.total() == Core.countNonZero(subdst));
2260     }
2261
2262     public void testVconcat() {
2263         List<Mat> mats = Arrays.asList(Mat.eye(3, 3, CvType.CV_8U), Mat.zeros(2, 3, CvType.CV_8U));
2264
2265         Core.vconcat(mats, dst);
2266
2267         assertMatEqual(Mat.eye(5, 3, CvType.CV_8U), dst);
2268
2269     }
2270
2271 }