Fixed java tests build
[profile/ivi/opencv.git] / modules / java / android_test / src / org / opencv / test / imgproc / ImgprocTest.java
1 package org.opencv.test.imgproc;
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.CvType;
9 import org.opencv.core.Mat;
10 import org.opencv.core.MatOfFloat;
11 import org.opencv.core.MatOfInt;
12 import org.opencv.core.MatOfInt4;
13 import org.opencv.core.MatOfPoint;
14 import org.opencv.core.MatOfPoint2f;
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.imgproc.Imgproc;
22 import org.opencv.test.OpenCVTestCase;
23
24 public class ImgprocTest extends OpenCVTestCase {
25
26     Point anchorPoint;
27     private int imgprocSz;
28     Size size;
29
30     @Override
31     protected void setUp() throws Exception {
32         super.setUp();
33
34         imgprocSz = 2;
35         anchorPoint = new Point(2, 2);
36         size = new Size(3, 3);
37     }
38
39     public void testAccumulateMatMat() {
40         Mat src = getMat(CvType.CV_64F, 2);
41         Mat dst = getMat(CvType.CV_64F, 0);
42         Mat dst2 = src.clone();
43
44         Imgproc.accumulate(src, dst);
45         Imgproc.accumulate(src, dst2);
46
47         assertMatEqual(src, dst, EPS);
48         assertMatEqual(getMat(CvType.CV_64F, 4), dst2, EPS);
49     }
50
51     public void testAccumulateMatMatMat() {
52         Mat src = getMat(CvType.CV_64F, 2);
53         Mat mask = makeMask(getMat(CvType.CV_8U, 1));
54         Mat dst = getMat(CvType.CV_64F, 0);
55         Mat dst2 = src.clone();
56
57         Imgproc.accumulate(src, dst, mask);
58         Imgproc.accumulate(src, dst2, mask);
59
60         assertMatEqual(makeMask(getMat(CvType.CV_64F, 2)), dst, EPS);
61         assertMatEqual(makeMask(getMat(CvType.CV_64F, 4), 2), dst2, EPS);
62     }
63
64     public void testAccumulateProductMatMatMat() {
65         Mat src = getMat(CvType.CV_64F, 2);
66         Mat dst = getMat(CvType.CV_64F, 0);
67         Mat dst2 = src.clone();
68
69         Imgproc.accumulateProduct(src, src, dst);
70         Imgproc.accumulateProduct(src, dst, dst2);
71
72         assertMatEqual(getMat(CvType.CV_64F, 4), dst, EPS);
73         assertMatEqual(getMat(CvType.CV_64F, 10), dst2, EPS);
74     }
75
76     public void testAccumulateProductMatMatMatMat() {
77         Mat src = getMat(CvType.CV_64F, 2);
78         Mat mask = makeMask(getMat(CvType.CV_8U, 1));
79         Mat dst = getMat(CvType.CV_64F, 0);
80         Mat dst2 = src.clone();
81
82         Imgproc.accumulateProduct(src, src, dst, mask);
83         Imgproc.accumulateProduct(src, dst, dst2, mask);
84
85         assertMatEqual(makeMask(getMat(CvType.CV_64F, 4)), dst, EPS);
86         assertMatEqual(makeMask(getMat(CvType.CV_64F, 10), 2), dst2, EPS);
87     }
88
89     public void testAccumulateSquareMatMat() {
90         Mat src = getMat(CvType.CV_64F, 2);
91         Mat dst = getMat(CvType.CV_64F, 0);
92         Mat dst2 = src.clone();
93
94         Imgproc.accumulateSquare(src, dst);
95         Imgproc.accumulateSquare(src, dst2);
96
97         assertMatEqual(getMat(CvType.CV_64F, 4), dst, EPS);
98         assertMatEqual(getMat(CvType.CV_64F, 6), dst2, EPS);
99     }
100
101     public void testAccumulateSquareMatMatMat() {
102         Mat src = getMat(CvType.CV_64F, 2);
103         Mat mask = makeMask(getMat(CvType.CV_8U, 1));
104         Mat dst = getMat(CvType.CV_64F, 0);
105         Mat dst2 = src.clone();
106
107         Imgproc.accumulateSquare(src, dst, mask);
108         Imgproc.accumulateSquare(src, dst2, mask);
109
110         assertMatEqual(makeMask(getMat(CvType.CV_64F, 4)), dst, EPS);
111         assertMatEqual(makeMask(getMat(CvType.CV_64F, 6), 2), dst2, EPS);
112     }
113
114     public void testAccumulateWeightedMatMatDouble() {
115         Mat src = getMat(CvType.CV_64F, 2);
116         Mat dst = getMat(CvType.CV_64F, 4);
117         Mat dst2 = src.clone();
118
119         Imgproc.accumulateWeighted(src, dst, 0.5);
120         Imgproc.accumulateWeighted(src, dst2, 2);
121
122         assertMatEqual(getMat(CvType.CV_64F, 3), dst, EPS);
123         assertMatEqual(getMat(CvType.CV_64F, 2), dst2, EPS);
124     }
125
126     public void testAccumulateWeightedMatMatDoubleMat() {
127         Mat src = getMat(CvType.CV_64F, 2);
128         Mat mask = makeMask(getMat(CvType.CV_8U, 1));
129         Mat dst = getMat(CvType.CV_64F, 4);
130         Mat dst2 = src.clone();
131
132         Imgproc.accumulateWeighted(src, dst, 0.5, mask);
133         Imgproc.accumulateWeighted(src, dst2, 2, mask);
134
135         assertMatEqual(makeMask(getMat(CvType.CV_64F, 3), 4), dst, EPS);
136         assertMatEqual(getMat(CvType.CV_64F, 2), dst2, EPS);
137     }
138
139     public void testAdaptiveThreshold() {
140         Mat src = makeMask(getMat(CvType.CV_8U, 50), 20);
141         Mat dst = new Mat();
142
143         Imgproc.adaptiveThreshold(src, dst, 1, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, 0);
144
145         assertEquals(src.rows(), Core.countNonZero(dst));
146     }
147
148     public void testApproxPolyDP() {
149         MatOfPoint2f curve = new MatOfPoint2f(new Point(1, 3), new Point(2, 4), new Point(3, 5), new Point(4, 4), new Point(5, 3));
150
151         MatOfPoint2f approxCurve = new MatOfPoint2f();
152
153         Imgproc.approxPolyDP(curve, approxCurve, EPS, true);
154
155         List<Point> approxCurveGold =  new ArrayList<Point>(3);
156         approxCurveGold.add(new Point(1, 3));
157         approxCurveGold.add(new Point(3, 5));
158         approxCurveGold.add(new Point(5, 3));
159
160         assertListPointEquals(approxCurve.toList(), approxCurveGold, EPS);
161     }
162
163     public void testArcLength() {
164         MatOfPoint2f curve = new MatOfPoint2f(new Point(1, 3), new Point(2, 4), new Point(3, 5), new Point(4, 4), new Point(5, 3));
165
166         double arcLength = Imgproc.arcLength(curve, false);
167
168         assertEquals(5.656854152679443, arcLength);
169     }
170
171     public void testBilateralFilterMatMatIntDoubleDouble() {
172         Imgproc.bilateralFilter(gray255, dst, 5, 10, 5);
173
174         assertMatEqual(gray255, dst);
175         // TODO_: write better test
176     }
177
178     public void testBilateralFilterMatMatIntDoubleDoubleInt() {
179         Imgproc.bilateralFilter(gray255, dst, 5, 10, 5, Core.BORDER_REFLECT);
180
181         assertMatEqual(gray255, dst);
182         // TODO_: write better test
183     }
184
185     public void testBlurMatMatSize() {
186         Imgproc.blur(gray0, dst, size);
187         assertMatEqual(gray0, dst);
188
189         Imgproc.blur(gray255, dst, size);
190         assertMatEqual(gray255, dst);
191         // TODO_: write better test
192     }
193
194     public void testBlurMatMatSizePoint() {
195         Imgproc.blur(gray0, dst, size, anchorPoint);
196         assertMatEqual(gray0, dst);
197         // TODO_: write better test
198     }
199
200     public void testBlurMatMatSizePointInt() {
201         Imgproc.blur(gray0, dst, size, anchorPoint, Core.BORDER_REFLECT);
202         assertMatEqual(gray0, dst);
203         // TODO_: write better test
204     }
205
206     public void testBoundingRect() {
207         MatOfPoint points = new MatOfPoint(new Point(0, 0), new Point(0, 4), new Point(4, 0), new Point(4, 4));
208         Point p1 = new Point(1, 1);
209         Point p2 = new Point(-5, -2);
210
211         Rect bbox = Imgproc.boundingRect(points);
212
213         assertTrue(bbox.contains(p1));
214         assertFalse(bbox.contains(p2));
215     }
216
217     public void testBoxFilterMatMatIntSize() {
218         Size size = new Size(3, 3);
219         Imgproc.boxFilter(gray0, dst, 8, size);
220         assertMatEqual(gray0, dst);
221         // TODO_: write better test
222     }
223
224     public void testBoxFilterMatMatIntSizePointBoolean() {
225         Imgproc.boxFilter(gray255, dst, 8, size, anchorPoint, false);
226         assertMatEqual(gray255, dst);
227         // TODO_: write better test
228     }
229
230     public void testBoxFilterMatMatIntSizePointBooleanInt() {
231         Imgproc.boxFilter(gray255, dst, 8, size, anchorPoint, false, Core.BORDER_REFLECT);
232         assertMatEqual(gray255, dst);
233         // TODO_: write better test
234     }
235
236     public void testCalcBackProject() {
237         List<Mat> images = Arrays.asList(grayChess);
238         MatOfInt channels = new MatOfInt(0);
239         MatOfInt histSize = new MatOfInt(10);
240         MatOfFloat ranges = new MatOfFloat(0f, 256f);
241
242         Mat hist = new Mat();
243         Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges);
244         Core.normalize(hist, hist);
245
246         Imgproc.calcBackProject(images, channels, hist, dst, ranges, 255);
247
248         assertEquals(grayChess.size(), dst.size());
249         assertEquals(grayChess.depth(), dst.depth());
250         assertFalse(0 == Core.countNonZero(dst));
251     }
252
253     public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat() {
254         List<Mat> images = Arrays.asList(gray128);
255         MatOfInt channels = new MatOfInt(0);
256         MatOfInt histSize = new MatOfInt(10);
257         MatOfFloat ranges = new MatOfFloat(0f, 256f);
258         Mat hist = new Mat();
259
260         Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges);
261
262         truth = new Mat(10, 1, CvType.CV_32F, Scalar.all(0)) {
263             {
264                 put(5, 0, 100);
265             }
266         };
267         assertMatEqual(truth, hist, EPS);
268     }
269
270     public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat2D() {
271         List<Mat> images = Arrays.asList(gray255, gray128);
272         MatOfInt channels = new MatOfInt(0, 1);
273         MatOfInt histSize = new MatOfInt(10, 10);
274         MatOfFloat ranges = new MatOfFloat(0f, 256f, 0f, 256f);
275         Mat hist = new Mat();
276
277         Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges);
278
279         truth = new Mat(10, 10, CvType.CV_32F, Scalar.all(0)) {
280             {
281                 put(9, 5, 100);
282             }
283         };
284         assertMatEqual(truth, hist, EPS);
285     }
286
287     public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat3D() {
288         List<Mat> images = Arrays.asList(rgbLena);
289
290         Mat hist3D = new Mat();
291         List<Mat> histList = Arrays.asList( new Mat[] {new Mat(), new Mat(), new Mat()} );
292
293         MatOfInt histSize = new MatOfInt(10);
294         MatOfFloat ranges = new MatOfFloat(0f, 256f);
295
296         for(int i=0; i<rgbLena.channels(); i++)
297         {
298             Imgproc.calcHist(images, new MatOfInt(i), new Mat(), histList.get(i), histSize, ranges);
299
300             assertEquals(10, histList.get(i).checkVector(1));
301         }
302
303         Core.merge(histList, hist3D);
304
305         assertEquals(CvType.CV_32FC3, hist3D.type());
306         assertEquals(10, hist3D.checkVector(3));
307
308         Mat truth = new Mat(10, 1, CvType.CV_32FC3);
309         truth.put(0, 0,
310                  0, 24870, 0,
311                  1863, 31926, 1,
312                  56682, 37677, 2260,
313                  77278, 44751, 32436,
314                  69397, 41343, 18526,
315                  27180, 40407, 18658,
316                  21101, 15993, 32042,
317                  8343, 18585, 47786,
318                  300, 6567, 80988,
319                  0, 25, 29447
320                 );
321
322         assertMatEqual(truth, hist3D, EPS);
323     }
324
325     public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloatBoolean() {
326         List<Mat> images = Arrays.asList(gray255, gray128);
327         MatOfInt channels = new MatOfInt(0, 1);
328         MatOfInt histSize = new MatOfInt(10, 10);
329         MatOfFloat ranges = new MatOfFloat(0f, 256f, 0f, 256f);
330         Mat hist = new Mat();
331
332         Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges, true);
333
334         truth = new Mat(10, 10, CvType.CV_32F, Scalar.all(0)) {
335             {
336                 put(9, 5, 100);
337             }
338         };
339         assertMatEqual(truth, hist, EPS);
340     }
341
342     public void testCannyMatMatDoubleDouble() {
343         Imgproc.Canny(gray255, dst, 5, 10);
344         assertMatEqual(gray0, dst);
345         // TODO_: write better test
346     }
347
348     public void testCannyMatMatDoubleDoubleIntBoolean() {
349         Imgproc.Canny(gray0, dst, 5, 10, 5, true);
350         assertMatEqual(gray0, dst);
351         // TODO_: write better test
352     }
353
354     public void testCompareHist() {
355         Mat H1 = new Mat(3, 1, CvType.CV_32F);
356         Mat H2 = new Mat(3, 1, CvType.CV_32F);
357         H1.put(0, 0, 1, 2, 3);
358         H2.put(0, 0, 4, 5, 6);
359
360         double distance = Imgproc.compareHist(H1, H2, Imgproc.CV_COMP_CORREL);
361
362         assertEquals(1., distance);
363     }
364
365     public void testContourAreaMat() {
366         Mat contour = new Mat(1, 4, CvType.CV_32FC2);
367         contour.put(0, 0, 0, 0, 10, 0, 10, 10, 5, 4);
368
369         double area = Imgproc.contourArea(contour);
370
371         assertEquals(45., area);
372     }
373
374     public void testContourAreaMatBoolean() {
375         Mat contour = new Mat(1, 4, CvType.CV_32FC2);
376         contour.put(0, 0, 0, 0, 10, 0, 10, 10, 5, 4);
377
378         double area = Imgproc.contourArea(contour, true);
379
380         assertEquals(45., area);
381         // TODO_: write better test
382     }
383
384     public void testConvertMapsMatMatMatMatInt() {
385         Mat map1 = new Mat(1, 4, CvType.CV_32FC1, new Scalar(1));
386         Mat map2 = new Mat(1, 4, CvType.CV_32FC1, new Scalar(2));
387         Mat dstmap1 = new Mat(1, 4, CvType.CV_16SC2);
388         Mat dstmap2 = new Mat(1, 4, CvType.CV_16UC1);
389
390         Imgproc.convertMaps(map1, map2, dstmap1, dstmap2, CvType.CV_16SC2);
391
392         Mat truthMap1 = new Mat(1, 4, CvType.CV_16SC2);
393         truthMap1.put(0, 0, 1, 2, 1, 2, 1, 2, 1, 2);
394         assertMatEqual(truthMap1, dstmap1);
395         Mat truthMap2 = new Mat(1, 4, CvType.CV_16UC1, new Scalar(0));
396         assertMatEqual(truthMap2, dstmap2);
397     }
398
399     public void testConvertMapsMatMatMatMatIntBoolean() {
400         Mat map1 = new Mat(1, 3, CvType.CV_32FC1, new Scalar(2));
401         Mat map2 = new Mat(1, 3, CvType.CV_32FC1, new Scalar(4));
402         Mat dstmap1 = new Mat(1, 3, CvType.CV_16SC2);
403         Mat dstmap2 = new Mat(1, 3, CvType.CV_16UC1);
404
405         Imgproc.convertMaps(map1, map2, dstmap1, dstmap2, CvType.CV_16SC2, false);
406         // TODO_: write better test (last param == true)
407
408         Mat truthMap1 = new Mat(1, 3, CvType.CV_16SC2);
409         truthMap1.put(0, 0, 2, 4, 2, 4, 2, 4);
410         assertMatEqual(truthMap1, dstmap1);
411         Mat truthMap2 = new Mat(1, 3, CvType.CV_16UC1, new Scalar(0));
412         assertMatEqual(truthMap2, dstmap2);
413     }
414
415     public void testConvexHullMatMat() {
416         MatOfPoint points = new MatOfPoint(
417                 new Point(20, 0),
418                 new Point(40, 0),
419                 new Point(30, 20),
420                 new Point(0,  20),
421                 new Point(20, 10),
422                 new Point(30, 10)
423         );
424
425         MatOfInt hull = new MatOfInt();
426
427         Imgproc.convexHull(points, hull);
428
429         MatOfInt expHull = new MatOfInt(
430                 1, 2, 3, 0
431         );
432         assertMatEqual(expHull, hull, EPS);
433     }
434
435     public void testConvexHullMatMatBooleanBoolean() {
436         MatOfPoint points = new MatOfPoint(
437                 new Point(2, 0),
438                 new Point(4, 0),
439                 new Point(3, 2),
440                 new Point(0, 2),
441                 new Point(2, 1),
442                 new Point(3, 1)
443         );
444
445         MatOfInt hull = new MatOfInt();
446
447         Imgproc.convexHull(points, hull, true);
448
449         MatOfInt expHull = new MatOfInt(
450                 3, 2, 1, 0
451         );
452         assertMatEqual(expHull, hull, EPS);
453     }
454
455     public void testConvexityDefects() {
456         MatOfPoint points = new MatOfPoint(
457                 new Point(20, 0),
458                 new Point(40, 0),
459                 new Point(30, 20),
460                 new Point(0,  20),
461                 new Point(20, 10),
462                 new Point(30, 10)
463         );
464
465         MatOfInt hull = new MatOfInt();
466         Imgproc.convexHull(points, hull);
467
468         MatOfInt4 convexityDefects = new MatOfInt4();
469         Imgproc.convexityDefects(points, hull, convexityDefects);
470
471         assertMatEqual(new MatOfInt4(3, 0, 5, 3620), convexityDefects);
472     }
473
474     public void testCornerEigenValsAndVecsMatMatIntInt() {
475         fail("Not yet implemented");
476         // TODO: write better test
477         Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32FC1);
478         src.put(0, 0, 1, 2);
479         src.put(1, 0, 4, 2);
480
481         int blockSize = 3;
482         int ksize = 5;
483
484         // TODO: eigen vals and vectors returned = 0 for most src matrices
485         Imgproc.cornerEigenValsAndVecs(src, dst, blockSize, ksize);
486         truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32FC(6), new Scalar(0));
487         assertMatEqual(truth, dst, EPS);
488     }
489
490     public void testCornerEigenValsAndVecsMatMatIntIntInt() {
491         fail("Not yet implemented");
492         // TODO: write better test
493         Mat src = new Mat(4, 4, CvType.CV_32FC1, new Scalar(128));
494
495         int blockSize = 3;
496         int ksize = 5;
497
498         truth = new Mat(4, 4, CvType.CV_32FC(6), new Scalar(0));
499
500         Imgproc.cornerEigenValsAndVecs(src, dst, blockSize, ksize, Core.BORDER_REFLECT);
501         assertMatEqual(truth, dst, EPS);
502     }
503
504     public void testCornerHarrisMatMatIntIntDouble() {
505         fail("Not yet implemented");
506         // TODO: write better test
507
508         truth = new Mat(matSize, matSize, CvType.CV_32FC1, new Scalar(0));
509         int blockSize = 5;
510         int ksize = 7;
511         double k = 0.1;
512         Imgproc.cornerHarris(gray128, dst, blockSize, ksize, k);
513         assertMatEqual(truth, dst, EPS);
514     }
515
516     public void testCornerHarrisMatMatIntIntDoubleInt() {
517         fail("Not yet implemented");
518         // TODO: write better test
519
520         truth = new Mat(matSize, matSize, CvType.CV_32FC1, new Scalar(0));
521         int blockSize = 5;
522         int ksize = 7;
523         double k = 0.1;
524         Imgproc.cornerHarris(gray255, dst, blockSize, ksize, k, Core.BORDER_REFLECT);
525         assertMatEqual(truth, dst, EPS);
526     }
527
528     public void testCornerMinEigenValMatMatInt() {
529         fail("Not yet implemented");
530         // TODO: write better test
531
532         Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32FC1);
533         src.put(0, 0, 1, 2);
534         src.put(1, 0, 2, 1);
535         int blockSize = 5;
536
537         Imgproc.cornerMinEigenVal(src, dst, blockSize);
538
539         truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32FC1, new Scalar(0));
540         assertMatEqual(truth, dst, EPS);
541
542         Imgproc.cornerMinEigenVal(gray255, dst, blockSize);
543
544         truth = new Mat(matSize, matSize, CvType.CV_32FC1, new Scalar(0));
545         assertMatEqual(truth, dst, EPS);
546     }
547
548     public void testCornerMinEigenValMatMatIntInt() {
549         Mat src = Mat.eye(3, 3, CvType.CV_32FC1);
550         int blockSize = 3;
551         int ksize = 5;
552
553         Imgproc.cornerMinEigenVal(src, dst, blockSize, ksize);
554
555         truth = new Mat(3, 3, CvType.CV_32FC1) {
556             {
557                 put(0, 0, 1. / 18, 1. / 36, 1. / 18);
558                 put(1, 0, 1. / 36, 1. / 18, 1. / 36);
559                 put(2, 0, 1. / 18, 1. / 36, 1. / 18);
560             }
561         };
562         assertMatEqual(truth, dst, EPS);
563     }
564
565     public void testCornerMinEigenValMatMatIntIntInt() {
566         Mat src = Mat.eye(3, 3, CvType.CV_32FC1);
567         int blockSize = 3;
568         int ksize = 5;
569
570         Imgproc.cornerMinEigenVal(src, dst, blockSize, ksize, Core.BORDER_REFLECT);
571
572         truth = new Mat(3, 3, CvType.CV_32FC1) {
573             {
574                 put(0, 0, 0.68055558, 0.92708349, 0.5868057);
575                 put(1, 0, 0.92708343, 0.92708343, 0.92708343);
576                 put(2, 0, 0.58680564, 0.92708343, 0.68055564);
577             }
578         };
579         assertMatEqual(truth, dst, EPS);
580     }
581
582     public void testCornerSubPix() {
583         Mat img = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(128));
584         Point truthPosition = new Point(img.cols() / 2, img.rows() / 2);
585
586         Rect r = new Rect(new Point(0, 0), truthPosition);
587         Imgproc.rectangle(img, r.tl(), r.br(), new Scalar(0), Core.FILLED);
588         MatOfPoint2f corners = new MatOfPoint2f(new Point(truthPosition.x + 1, truthPosition.y + 1));
589         Size winSize = new Size(2, 2);
590         Size zeroZone = new Size(-1, -1);
591         TermCriteria criteria = new TermCriteria(TermCriteria.EPS, 0, 0.01);
592
593         Imgproc.cornerSubPix(img, corners, winSize, zeroZone, criteria);
594
595         assertPointEquals(truthPosition, corners.toList().get(0), weakEPS);
596     }
597
598     public void testCvtColorMatMatInt() {
599         fail("Not yet implemented");
600     }
601
602     public void testCvtColorMatMatIntInt() {
603         fail("Not yet implemented");
604     }
605
606     public void testDilateMatMatMat() {
607         Mat kernel = new Mat();
608
609         Imgproc.dilate(gray255, dst, kernel);
610
611         assertMatEqual(gray255, dst);
612
613         Imgproc.dilate(gray1, dst, kernel);
614
615         assertMatEqual(gray1, dst);
616         // TODO_: write better test
617     }
618
619     public void testDilateMatMatMatPoint() {
620         fail("Not yet implemented");
621     }
622
623     public void testDilateMatMatMatPointInt() {
624         fail("Not yet implemented");
625     }
626
627     public void testDilateMatMatMatPointIntInt() {
628         fail("Not yet implemented");
629     }
630
631     public void testDilateMatMatMatPointIntIntScalar() {
632         fail("Not yet implemented");
633     }
634
635     public void testDistanceTransformWithLabels() {
636         Mat dstLables = getMat(CvType.CV_32SC1, 0);
637         Mat labels = new Mat();
638
639         Imgproc.distanceTransformWithLabels(gray128, dst, labels, Imgproc.CV_DIST_L2, 3);
640
641         assertMatEqual(dstLables, labels);
642         assertMatEqual(getMat(CvType.CV_32FC1, 8192), dst, EPS);
643     }
644
645     public void testDrawContoursMatListOfMatIntScalar() {
646         Imgproc.rectangle(gray0, new Point(1, 2), new Point(7, 8), new Scalar(100));
647         List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
648         Imgproc.findContours(gray0, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
649
650         Imgproc.drawContours(gray0, contours, -1, new Scalar(0));
651
652         assertEquals(0, Core.countNonZero(gray0));
653     }
654
655     public void testDrawContoursMatListOfMatIntScalarInt() {
656         Imgproc.rectangle(gray0, new Point(1, 2), new Point(7, 8), new Scalar(100));
657         List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
658         Imgproc.findContours(gray0, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
659
660         Imgproc.drawContours(gray0, contours, -1, new Scalar(0), Core.FILLED);
661
662         assertEquals(0, Core.countNonZero(gray0));
663     }
664
665
666     public void testDrawContoursMatListOfMatIntScalarIntIntMatIntPoint() {
667         fail("Not yet implemented");
668     }
669
670     public void testEqualizeHist() {
671         Imgproc.equalizeHist(gray0, dst);
672         assertMatEqual(gray0, dst);
673
674         Imgproc.equalizeHist(gray255, dst);
675         assertMatEqual(gray255, dst);
676         // TODO_: write better test
677     }
678
679     public void testErodeMatMatMat() {
680         Mat kernel = new Mat();
681
682         Imgproc.erode(gray128, dst, kernel);
683
684         assertMatEqual(gray128, dst);
685     }
686
687     public void testErodeMatMatMatPointInt() {
688         Mat src = new Mat(3, 3, CvType.CV_8U) {
689             {
690                 put(0, 0, 15, 9, 10);
691                 put(1, 0, 10, 8, 12);
692                 put(2, 0, 12, 20, 25);
693             }
694         };
695         Mat kernel = new Mat();
696
697         Imgproc.erode(src, dst, kernel, anchorPoint, 10);
698
699         truth = new Mat(3, 3, CvType.CV_8U, new Scalar(8));
700         assertMatEqual(truth, dst);
701     }
702
703     public void testErodeMatMatMatPointIntIntScalar() {
704         Mat src = new Mat(3, 3, CvType.CV_8U) {
705             {
706                 put(0, 0, 15, 9, 10);
707                 put(1, 0, 10, 8, 12);
708                 put(2, 0, 12, 20, 25);
709             }
710         };
711         Mat kernel = new Mat();
712         Scalar sc = new Scalar(3, 3);
713
714         Imgproc.erode(src, dst, kernel, anchorPoint, 10, Core.BORDER_REFLECT, sc);
715
716         truth = new Mat(3, 3, CvType.CV_8U, new Scalar(8));
717         assertMatEqual(truth, dst);
718     }
719
720     public void testFilter2DMatMatIntMat() {
721         Mat src = Mat.eye(4, 4, CvType.CV_32F);
722         Mat kernel = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(1));
723
724         Imgproc.filter2D(src, dst, -1, kernel);
725
726         truth = new Mat(4, 4, CvType.CV_32F) {
727             {
728                 put(0, 0, 2, 2, 1, 0);
729                 put(1, 0, 2, 2, 1, 0);
730                 put(2, 0, 1, 1, 2, 1);
731                 put(3, 0, 0, 0, 1, 2);
732             }
733         };
734         assertMatEqual(truth, dst, EPS);
735     }
736
737     public void testFilter2DMatMatIntMatPointDouble() {
738         fail("Not yet implemented");
739     }
740
741     public void testFilter2DMatMatIntMatPointDoubleInt() {
742         Mat kernel = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(0));
743         Point point = new Point(0, 0);
744
745         Imgproc.filter2D(gray128, dst, -1, kernel, point, 2, Core.BORDER_CONSTANT);
746
747         assertMatEqual(gray2, dst);
748     }
749
750     public void testFindContoursMatListOfMatMatIntInt() {
751         Mat img = new Mat(50, 50, CvType.CV_8UC1, new Scalar(0));
752         List<MatOfPoint> contours = new ArrayList<MatOfPoint>(5);
753         Mat hierarchy = new Mat();
754
755         Imgproc.findContours(img, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
756
757         // no contours on empty image
758         assertEquals(contours.size(), 0);
759         assertEquals(contours.size(), hierarchy.total());
760
761         Imgproc.rectangle(img, new Point(10, 20), new Point(20, 30), new Scalar(100), 3, Imgproc.LINE_AA, 0);
762         Imgproc.rectangle(img, new Point(30, 35), new Point(40, 45), new Scalar(200));
763
764         Imgproc.findContours(img, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
765
766         // two contours of two rectangles
767         assertEquals(contours.size(), 2);
768         assertEquals(contours.size(), hierarchy.total());
769     }
770
771     public void testFindContoursMatListOfMatMatIntIntPoint() {
772         Mat img = new Mat(50, 50, CvType.CV_8UC1, new Scalar(0));
773         Mat img2 = img.submat(5, 50, 3, 50);
774         List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
775         List<MatOfPoint> contours2 = new ArrayList<MatOfPoint>();
776         Mat hierarchy = new Mat();
777
778         Imgproc.rectangle(img, new Point(10, 20), new Point(20, 30), new Scalar(100), 3, Imgproc.LINE_AA, 0);
779         Imgproc.rectangle(img, new Point(30, 35), new Point(40, 45), new Scalar(200));
780
781         Imgproc.findContours(img, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
782         Imgproc.findContours(img2, contours2, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(3, 5));
783
784         assertEquals(contours.size(), contours2.size());
785         assertMatEqual(contours.get(0), contours2.get(0));
786         /*
787         Log.d("findContours", "hierarchy=" + hierarchy);
788         int iBuff[] = new int[ (int) (hierarchy.total() * hierarchy.channels()) ]; // [ Contour0 (next sibling num, previous sibling num, 1st child num, parent num), Contour1(...), ...
789         hierarchy.get(0, 0, iBuff);
790         Log.d("findContours", Arrays.toString(iBuff));
791         */
792     }
793
794     public void testFitEllipse() {
795         MatOfPoint2f points = new MatOfPoint2f(new Point(0, 0), new Point(-1, 1), new Point(1, 1), new Point(1, -1), new Point(-1, -1));
796         RotatedRect rrect = new RotatedRect();
797
798         rrect = Imgproc.fitEllipse(points);
799
800         assertPointEquals(new Point(0, 0), rrect.center, EPS);
801         assertEquals(2.828, rrect.size.width, EPS);
802         assertEquals(2.828, rrect.size.height, EPS);
803     }
804
805     public void testFitLine() {
806         Mat points = new Mat(1, 4, CvType.CV_32FC2);
807         points.put(0, 0, 0, 0, 2, 3, 3, 4, 5, 8);
808
809         Mat linePoints = new Mat(4, 1, CvType.CV_32FC1);
810         linePoints.put(0, 0, 0.53196341, 0.84676737, 2.496531, 3.7467217);
811
812         Imgproc.fitLine(points, dst, Imgproc.CV_DIST_L12, 0, 0.01, 0.01);
813
814         assertMatEqual(linePoints, dst, EPS);
815     }
816
817     public void testFloodFillMatMatPointScalar() {
818         Mat mask = new Mat(matSize + 2, matSize + 2, CvType.CV_8U, new Scalar(0));
819         Mat img = gray0;
820         Imgproc.circle(mask, new Point(matSize / 2 + 1, matSize / 2 + 1), 3, new Scalar(2));
821
822         int retval = Imgproc.floodFill(img, mask, new Point(matSize / 2, matSize / 2), new Scalar(1));
823
824         assertEquals(Core.countNonZero(img), retval);
825         Imgproc.circle(mask, new Point(matSize / 2 + 1, matSize / 2 + 1), 3, new Scalar(0));
826         assertEquals(retval + 4 * (matSize + 1), Core.countNonZero(mask));
827         assertMatEqual(mask.submat(1, matSize + 1, 1, matSize + 1), img);
828     }
829
830     public void testFloodFillMatMatPointScalar_WithoutMask() {
831         Mat img = gray0;
832         Imgproc.circle(img, new Point(matSize / 2, matSize / 2), 3, new Scalar(2));
833
834         // TODO: ideally we should pass null instead of "new Mat()"
835         int retval = Imgproc.floodFill(img, new Mat(), new Point(matSize / 2, matSize / 2), new Scalar(1));
836
837         Imgproc.circle(img, new Point(matSize / 2, matSize / 2), 3, new Scalar(0));
838         assertEquals(Core.countNonZero(img), retval);
839     }
840
841     public void testFloodFillMatMatPointScalarRect() {
842         fail("Not yet implemented");
843     }
844
845     public void testFloodFillMatMatPointScalarRectScalar() {
846         fail("Not yet implemented");
847     }
848
849     public void testFloodFillMatMatPointScalarRectScalarScalar() {
850         fail("Not yet implemented");
851     }
852
853     public void testFloodFillMatMatPointScalarRectScalarScalarInt() {
854         fail("Not yet implemented");
855     }
856
857     public void testGaussianBlurMatMatSizeDouble() {
858         Imgproc.GaussianBlur(gray0, dst, size, 1);
859         assertMatEqual(gray0, dst);
860
861         Imgproc.GaussianBlur(gray2, dst, size, 1);
862         assertMatEqual(gray2, dst);
863     }
864
865     public void testGaussianBlurMatMatSizeDoubleDouble() {
866         Imgproc.GaussianBlur(gray2, dst, size, 0, 0);
867
868         assertMatEqual(gray2, dst);
869         // TODO_: write better test
870     }
871
872     public void testGaussianBlurMatMatSizeDoubleDoubleInt() {
873         Imgproc.GaussianBlur(gray2, dst, size, 1, 3, Core.BORDER_REFLECT);
874
875         assertMatEqual(gray2, dst);
876         // TODO_: write better test
877     }
878
879     public void testGetAffineTransform() {
880         MatOfPoint2f src = new MatOfPoint2f(new Point(2, 3), new Point(3, 1), new Point(1, 4));
881         MatOfPoint2f dst = new MatOfPoint2f(new Point(3, 3), new Point(7, 4), new Point(5, 6));
882
883         Mat transform = Imgproc.getAffineTransform(src, dst);
884
885         Mat truth = new Mat(2, 3, CvType.CV_64FC1) {
886             {
887                 put(0, 0, -8, -6, 37);
888                 put(1, 0, -7, -4, 29);
889             }
890         };
891         assertMatEqual(truth, transform, EPS);
892     }
893
894     public void testGetDefaultNewCameraMatrixMat() {
895         Mat mtx = Imgproc.getDefaultNewCameraMatrix(gray0);
896
897         assertFalse(mtx.empty());
898         assertEquals(0, Core.countNonZero(mtx));
899     }
900
901     public void testGetDefaultNewCameraMatrixMatSizeBoolean() {
902         Mat mtx = Imgproc.getDefaultNewCameraMatrix(gray0, size, true);
903
904         assertFalse(mtx.empty());
905         assertFalse(0 == Core.countNonZero(mtx));
906         // TODO_: write better test
907     }
908
909     public void testGetDerivKernelsMatMatIntIntInt() {
910         Mat kx = new Mat(imgprocSz, imgprocSz, CvType.CV_32F);
911         Mat ky = new Mat(imgprocSz, imgprocSz, CvType.CV_32F);
912         Mat expKx = new Mat(3, 1, CvType.CV_32F);
913         Mat expKy = new Mat(3, 1, CvType.CV_32F);
914         kx.put(0, 0, 1, 1);
915         kx.put(1, 0, 1, 1);
916         ky.put(0, 0, 2, 2);
917         ky.put(1, 0, 2, 2);
918         expKx.put(0, 0, 1, -2, 1);
919         expKy.put(0, 0, 1, -2, 1);
920
921         Imgproc.getDerivKernels(kx, ky, 2, 2, 3);
922
923         assertMatEqual(expKx, kx, EPS);
924         assertMatEqual(expKy, ky, EPS);
925     }
926
927     public void testGetDerivKernelsMatMatIntIntIntBooleanInt() {
928         Mat kx = new Mat(imgprocSz, imgprocSz, CvType.CV_32F);
929         Mat ky = new Mat(imgprocSz, imgprocSz, CvType.CV_32F);
930         Mat expKx = new Mat(3, 1, CvType.CV_32F);
931         Mat expKy = new Mat(3, 1, CvType.CV_32F);
932         kx.put(0, 0, 1, 1);
933         kx.put(1, 0, 1, 1);
934         ky.put(0, 0, 2, 2);
935         ky.put(1, 0, 2, 2);
936         expKx.put(0, 0, 1, -2, 1);
937         expKy.put(0, 0, 1, -2, 1);
938
939         Imgproc.getDerivKernels(kx, ky, 2, 2, 3, true, CvType.CV_32F);
940
941         assertMatEqual(expKx, kx, EPS);
942         assertMatEqual(expKy, ky, EPS);
943         // TODO_: write better test
944     }
945
946     public void testGetGaussianKernelIntDouble() {
947         dst = Imgproc.getGaussianKernel(1, 0.5);
948
949         truth = new Mat(1, 1, CvType.CV_64FC1, new Scalar(1));
950         assertMatEqual(truth, dst, EPS);
951     }
952
953     public void testGetGaussianKernelIntDoubleInt() {
954         dst = Imgproc.getGaussianKernel(3, 0.8, CvType.CV_32F);
955
956         truth = new Mat(3, 1, CvType.CV_32F);
957         truth.put(0, 0, 0.23899426, 0.52201146, 0.23899426);
958         assertMatEqual(truth, dst, EPS);
959     }
960
961     public void testGetPerspectiveTransform() {
962         fail("Not yet implemented");
963     }
964
965     public void testGetRectSubPixMatSizePointMat() {
966         Size size = new Size(3, 3);
967         Point center = new Point(gray255.cols() / 2, gray255.rows() / 2);
968
969         Imgproc.getRectSubPix(gray255, size, center, dst);
970
971         truth = new Mat(3, 3, CvType.CV_8U, new Scalar(255));
972         assertMatEqual(truth, dst);
973     }
974
975     public void testGetRectSubPixMatSizePointMatInt() {
976         Mat src = new Mat(10, 10, CvType.CV_32F, new Scalar(2));
977         Size patchSize = new Size(5, 5);
978         Point center = new Point(src.cols() / 2, src.rows() / 2);
979
980         Imgproc.getRectSubPix(src, patchSize, center, dst);
981
982         truth = new Mat(5, 5, CvType.CV_32F, new Scalar(2));
983         assertMatEqual(truth, dst, EPS);
984     }
985
986     public void testGetRotationMatrix2D() {
987         Point center = new Point(0, 0);
988
989         dst = Imgproc.getRotationMatrix2D(center, 0, 1);
990
991         truth = new Mat(2, 3, CvType.CV_64F) {
992             {
993                 put(0, 0, 1, 0, 0);
994                 put(1, 0, 0, 1, 0);
995             }
996         };
997
998         assertMatEqual(truth, dst, EPS);
999     }
1000
1001     public void testGetStructuringElementIntSize() {
1002         dst = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, size);
1003
1004         truth = new Mat(3, 3, CvType.CV_8UC1, new Scalar(1));
1005         assertMatEqual(truth, dst);
1006     }
1007
1008     public void testGetStructuringElementIntSizePoint() {
1009         dst = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, size, anchorPoint);
1010
1011         truth = new Mat(3, 3, CvType.CV_8UC1) {
1012             {
1013                 put(0, 0, 0, 0, 1);
1014                 put(1, 0, 0, 0, 1);
1015                 put(2, 0, 1, 1, 1);
1016             }
1017         };
1018         assertMatEqual(truth, dst);
1019     }
1020
1021     public void testGoodFeaturesToTrackMatListOfPointIntDoubleDouble() {
1022         Mat src = gray0;
1023         Imgproc.rectangle(src, new Point(2, 2), new Point(8, 8), new Scalar(100), -1);
1024         MatOfPoint lp = new MatOfPoint();
1025
1026         Imgproc.goodFeaturesToTrack(src, lp, 100, 0.01, 3);
1027
1028         assertEquals(4, lp.total());
1029     }
1030
1031     public void testGoodFeaturesToTrackMatListOfPointIntDoubleDoubleMatIntBooleanDouble() {
1032         Mat src = gray0;
1033         Imgproc.rectangle(src, new Point(2, 2), new Point(8, 8), new Scalar(100), -1);
1034         MatOfPoint lp = new MatOfPoint();
1035
1036         Imgproc.goodFeaturesToTrack(src, lp, 100, 0.01, 3, gray1, 4, true, 0);
1037
1038         assertEquals(4, lp.total());
1039     }
1040
1041     public void testGrabCutMatMatRectMatMatInt() {
1042         fail("Not yet implemented");
1043     }
1044
1045     public void testGrabCutMatMatRectMatMatIntInt() {
1046         fail("Not yet implemented");
1047     }
1048
1049     public void testHoughCirclesMatMatIntDoubleDouble() {
1050         int sz = 512;
1051         Mat img = new Mat(sz, sz, CvType.CV_8U, new Scalar(128));
1052         Mat circles = new Mat();
1053
1054         Imgproc.HoughCircles(img, circles, Imgproc.CV_HOUGH_GRADIENT, 2, img.rows() / 4);
1055
1056         assertEquals(0, circles.cols());
1057     }
1058
1059     public void testHoughCirclesMatMatIntDoubleDouble1() {
1060         int sz = 512;
1061         Mat img = new Mat(sz, sz, CvType.CV_8U, new Scalar(128));
1062         Mat circles = new Mat();
1063
1064         Point center = new Point(img.cols() / 2, img.rows() / 2);
1065         int radius = Math.min(img.cols() / 4, img.rows() / 4);
1066         Imgproc.circle(img, center, radius, colorBlack, 3);
1067
1068         Imgproc.HoughCircles(img, circles, Imgproc.CV_HOUGH_GRADIENT, 2, img.rows() / 4);
1069
1070         assertEquals(1, circles.cols());
1071     }
1072
1073     public void testHoughCirclesMatMatIntDoubleDoubleDoubleDoubleIntInt() {
1074         fail("Not yet implemented");
1075     }
1076
1077     public void testHoughLinesMatMatDoubleDoubleInt() {
1078         int sz = 512;
1079         Mat img = new Mat(sz, sz, CvType.CV_8U, new Scalar(0));
1080         Point point1 = new Point(50, 50);
1081         Point point2 = new Point(img.cols() / 2, img.rows() / 2);
1082         Imgproc.line(img, point1, point2, colorWhite, 1);
1083         Mat lines = new Mat();
1084
1085         Imgproc.HoughLines(img, lines, 1, 3.1415926/180, 100);
1086
1087         assertEquals(1, lines.cols());
1088
1089         /*
1090         Log.d("HoughLines", "lines=" + lines);
1091         int num = (int)lines.total();
1092         int buff[] = new int[num*4]; //[ (x1, y1, x2, y2), (...), ...]
1093         lines.get(0, 0, buff);
1094         Log.d("HoughLines", "lines=" + Arrays.toString(buff));
1095         */
1096     }
1097
1098     public void testHoughLinesMatMatDoubleDoubleIntDouble() {
1099         fail("Not yet implemented");
1100     }
1101
1102     public void testHoughLinesMatMatDoubleDoubleIntDoubleDouble() {
1103         fail("Not yet implemented");
1104     }
1105
1106     public void testHoughLinesPMatMatDoubleDoubleInt() {
1107         int sz = 512;
1108         Mat img = new Mat(sz, sz, CvType.CV_8U, new Scalar(0));
1109         Point point1 = new Point(0, 0);
1110         Point point2 = new Point(sz, sz);
1111         Point point3 = new Point(sz, 0);
1112         Point point4 = new Point(2*sz/3, sz/3);
1113         Imgproc.line(img, point1, point2, Scalar.all(255), 1);
1114         Imgproc.line(img, point3, point4, Scalar.all(255), 1);
1115         Mat lines = new Mat();
1116
1117         Imgproc.HoughLinesP(img, lines, 1, 3.1415926/180, 100);
1118
1119         assertEquals(2, lines.cols());
1120
1121         /*
1122         Log.d("HoughLinesP", "lines=" + lines);
1123         int num = (int)lines.cols();
1124         int buff[] = new int[num*4]; // CV_32SC4 as [ (x1, y1, x2, y2), (...), ...]
1125         lines.get(0, 0, buff);
1126         Log.d("HoughLinesP", "lines=" + Arrays.toString(buff));
1127         */
1128     }
1129
1130     public void testHoughLinesPMatMatDoubleDoubleIntDouble() {
1131         fail("Not yet implemented");
1132     }
1133
1134     public void testHoughLinesPMatMatDoubleDoubleIntDoubleDouble() {
1135         fail("Not yet implemented");
1136     }
1137
1138     public void testHuMoments() {
1139         fail("Not yet implemented");
1140     }
1141
1142     public void testInitUndistortRectifyMap() {
1143         fail("Not yet implemented");
1144         Mat cameraMatrix = new Mat(3, 3, CvType.CV_32F);
1145         cameraMatrix.put(0, 0, 1, 0, 1);
1146         cameraMatrix.put(1, 0, 0, 1, 1);
1147         cameraMatrix.put(2, 0, 0, 0, 1);
1148
1149         Mat R = new Mat(3, 3, CvType.CV_32F, new Scalar(2));
1150         Mat newCameraMatrix = new Mat(3, 3, CvType.CV_32F, new Scalar(3));
1151
1152         Mat distCoeffs = new Mat();
1153         Mat map1 = new Mat();
1154         Mat map2 = new Mat();
1155
1156         // TODO: complete this test
1157         Imgproc.initUndistortRectifyMap(cameraMatrix, distCoeffs, R, newCameraMatrix, size, CvType.CV_32F, map1, map2);
1158     }
1159
1160     public void testInitWideAngleProjMapMatMatSizeIntIntMatMat() {
1161         fail("Not yet implemented");
1162         Mat cameraMatrix = new Mat(3, 3, CvType.CV_32F);
1163         Mat distCoeffs = new Mat(1, 4, CvType.CV_32F);
1164         // Size imageSize = new Size(2, 2);
1165
1166         cameraMatrix.put(0, 0, 1, 0, 1);
1167         cameraMatrix.put(1, 0, 0, 1, 2);
1168         cameraMatrix.put(2, 0, 0, 0, 1);
1169
1170         distCoeffs.put(0, 0, 1, 3, 2, 4);
1171         truth = new Mat(3, 3, CvType.CV_32F);
1172         truth.put(0, 0, 0, 0, 0);
1173         truth.put(1, 0, 0, 0, 0);
1174         truth.put(2, 0, 0, 3, 0);
1175         // TODO: No documentation for this function
1176         // Imgproc.initWideAngleProjMap(cameraMatrix, distCoeffs, imageSize,
1177         // 5, m1type, truthput1, truthput2);
1178     }
1179
1180     public void testInitWideAngleProjMapMatMatSizeIntIntMatMatInt() {
1181         fail("Not yet implemented");
1182     }
1183
1184     public void testInitWideAngleProjMapMatMatSizeIntIntMatMatIntDouble() {
1185         fail("Not yet implemented");
1186     }
1187
1188     public void testIntegral2MatMatMat() {
1189         Mat src = new Mat(3, 3, CvType.CV_32F, new Scalar(3));
1190         Mat expSum = new Mat(4, 4, CvType.CV_64F);
1191         Mat expSqsum = new Mat(4, 4, CvType.CV_64F);
1192         Mat sum = new Mat();
1193         Mat sqsum = new Mat();
1194
1195         expSum.put(0, 0, 0, 0, 0, 0);
1196         expSum.put(1, 0, 0, 3, 6, 9);
1197         expSum.put(2, 0, 0, 6, 12, 18);
1198         expSum.put(3, 0, 0, 9, 18, 27);
1199
1200         expSqsum.put(0, 0, 0, 0, 0, 0);
1201         expSqsum.put(1, 0, 0, 9, 18, 27);
1202         expSqsum.put(2, 0, 0, 18, 36, 54);
1203         expSqsum.put(3, 0, 0, 27, 54, 81);
1204
1205         Imgproc.integral2(src, sum, sqsum);
1206
1207         assertMatEqual(expSum, sum, EPS);
1208         assertMatEqual(expSqsum, sqsum, EPS);
1209     }
1210
1211     public void testIntegral2MatMatMatInt() {
1212         Mat src = new Mat(3, 3, CvType.CV_32F, new Scalar(3));
1213         Mat expSum = new Mat(4, 4, CvType.CV_64F);
1214         Mat expSqsum = new Mat(4, 4, CvType.CV_64F);
1215         Mat sum = new Mat();
1216         Mat sqsum = new Mat();
1217
1218         expSum.put(0, 0, 0, 0, 0, 0);
1219         expSum.put(1, 0, 0, 3, 6, 9);
1220         expSum.put(2, 0, 0, 6, 12, 18);
1221         expSum.put(3, 0, 0, 9, 18, 27);
1222
1223         expSqsum.put(0, 0, 0, 0, 0, 0);
1224         expSqsum.put(1, 0, 0, 9, 18, 27);
1225         expSqsum.put(2, 0, 0, 18, 36, 54);
1226         expSqsum.put(3, 0, 0, 27, 54, 81);
1227
1228         Imgproc.integral2(src, sum, sqsum, CvType.CV_64F, CvType.CV_64F);
1229
1230         assertMatEqual(expSum, sum, EPS);
1231         assertMatEqual(expSqsum, sqsum, EPS);
1232     }
1233
1234     public void testIntegral3MatMatMatMat() {
1235         Mat src = new Mat(1, 1, CvType.CV_32F, new Scalar(1));
1236         Mat expSum = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
1237         Mat expSqsum = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
1238         Mat expTilted = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
1239         Mat sum = new Mat();
1240         Mat sqsum = new Mat();
1241         Mat tilted = new Mat();
1242
1243         expSum.put(0, 0, 0, 0);
1244         expSum.put(1, 0, 0, 1);
1245
1246         expSqsum.put(0, 0, 0, 0);
1247         expSqsum.put(1, 0, 0, 1);
1248
1249         expTilted.put(0, 0, 0, 0);
1250         expTilted.put(1, 0, 0, 1);
1251
1252         Imgproc.integral3(src, sum, sqsum, tilted);
1253
1254         assertMatEqual(expSum, sum, EPS);
1255         assertMatEqual(expSqsum, sqsum, EPS);
1256         assertMatEqual(expTilted, tilted, EPS);
1257     }
1258
1259     public void testIntegral3MatMatMatMatInt() {
1260         Mat src = new Mat(1, 1, CvType.CV_32F, new Scalar(1));
1261         Mat expSum = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
1262         Mat expSqsum = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
1263         Mat expTilted = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
1264         Mat sum = new Mat();
1265         Mat sqsum = new Mat();
1266         Mat tilted = new Mat();
1267
1268         expSum.put(0, 0, 0, 0);
1269         expSum.put(1, 0, 0, 1);
1270
1271         expSqsum.put(0, 0, 0, 0);
1272         expSqsum.put(1, 0, 0, 1);
1273
1274         expTilted.put(0, 0, 0, 0);
1275         expTilted.put(1, 0, 0, 1);
1276
1277         Imgproc.integral3(src, sum, sqsum, tilted, CvType.CV_64F, CvType.CV_64F);
1278
1279         assertMatEqual(expSum, sum, EPS);
1280         assertMatEqual(expSqsum, sqsum, EPS);
1281         assertMatEqual(expTilted, tilted, EPS);
1282     }
1283
1284     public void testIntegralMatMat() {
1285         Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(2));
1286
1287         Imgproc.integral(src, dst);
1288
1289         truth = new Mat(3, 3, CvType.CV_64F) {
1290             {
1291                 put(0, 0, 0, 0, 0);
1292                 put(1, 0, 0, 2, 4);
1293                 put(2, 0, 0, 4, 8);
1294             }
1295         };
1296         assertMatEqual(truth, dst, EPS);
1297     }
1298
1299     public void testIntegralMatMatInt() {
1300         Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(2));
1301
1302         Imgproc.integral(src, dst, CvType.CV_64F);
1303
1304         truth = new Mat(3, 3, CvType.CV_64F) {
1305             {
1306                 put(0, 0, 0, 0, 0);
1307                 put(1, 0, 0, 2, 4);
1308                 put(2, 0, 0, 4, 8);
1309             }
1310         };
1311         assertMatEqual(truth, dst, EPS);
1312     }
1313
1314     public void testInvertAffineTransform() {
1315         Mat src = new Mat(2, 3, CvType.CV_64F, new Scalar(1));
1316
1317         Imgproc.invertAffineTransform(src, dst);
1318
1319         truth = new Mat(2, 3, CvType.CV_64F, new Scalar(0));
1320         assertMatEqual(truth, dst, EPS);
1321     }
1322
1323     public void testIsContourConvex() {
1324         MatOfPoint contour1 = new MatOfPoint(new Point(0, 0), new Point(10, 0), new Point(10, 10), new Point(5, 4));
1325
1326         assertFalse(Imgproc.isContourConvex(contour1));
1327
1328         MatOfPoint contour2 = new MatOfPoint(new Point(0, 0), new Point(10, 0), new Point(10, 10), new Point(5, 6));
1329
1330         assertTrue(Imgproc.isContourConvex(contour2));
1331     }
1332
1333     public void testLaplacianMatMatInt() {
1334         Imgproc.Laplacian(gray0, dst, CvType.CV_8U);
1335
1336         assertMatEqual(gray0, dst);
1337     }
1338
1339     public void testLaplacianMatMatIntIntDoubleDouble() {
1340         Mat src = Mat.eye(imgprocSz, imgprocSz, CvType.CV_32F);
1341
1342         Imgproc.Laplacian(src, dst, CvType.CV_32F, 1, 2, EPS);
1343
1344         truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F) {
1345             {
1346                 put(0, 0, -7.9990001, 8.0009995);
1347                 put(1, 0, 8.0009995, -7.9990001);
1348             }
1349         };
1350         assertMatEqual(truth, dst, EPS);
1351     }
1352
1353     public void testLaplacianMatMatIntIntDoubleDoubleInt() {
1354         Mat src = new Mat(3, 3, CvType.CV_32F, new Scalar(2));
1355
1356         Imgproc.Laplacian(src, dst, CvType.CV_32F, 1, 2, EPS, Core.BORDER_REFLECT);
1357
1358         truth = new Mat(3, 3, CvType.CV_32F, new Scalar(0.00099945068));
1359         assertMatEqual(truth, dst, EPS);
1360     }
1361
1362     public void testMatchShapes() {
1363         Mat contour1 = new Mat(1, 4, CvType.CV_32FC2);
1364         Mat contour2 = new Mat(1, 4, CvType.CV_32FC2);
1365         contour1.put(0, 0, 1, 1, 5, 1, 4, 3, 6, 2);
1366         contour2.put(0, 0, 1, 1, 6, 1, 4, 1, 2, 5);
1367
1368         double distance = Imgproc.matchShapes(contour1, contour2, Imgproc.CV_CONTOURS_MATCH_I1, 1);
1369
1370         assertEquals(2.81109697365334, distance, EPS);
1371     }
1372
1373     public void testMatchTemplate() {
1374         Mat image = new Mat(imgprocSz, imgprocSz, CvType.CV_8U);
1375         Mat templ = new Mat(imgprocSz, imgprocSz, CvType.CV_8U);
1376         image.put(0, 0, 1, 2, 3, 4);
1377         templ.put(0, 0, 5, 6, 7, 8);
1378
1379         Imgproc.matchTemplate(image, templ, dst, Imgproc.TM_CCORR);
1380
1381         truth = new Mat(1, 1, CvType.CV_32F, new Scalar(70));
1382         assertMatEqual(truth, dst, EPS);
1383
1384         Imgproc.matchTemplate(gray255, gray0, dst, Imgproc.TM_CCORR);
1385
1386         truth = new Mat(1, 1, CvType.CV_32F, new Scalar(0));
1387         assertMatEqual(truth, dst, EPS);
1388     }
1389
1390     public void testMedianBlur() {
1391         Imgproc.medianBlur(gray255, dst, 5);
1392         assertMatEqual(gray255, dst);
1393
1394         Imgproc.medianBlur(gray2, dst, 3);
1395         assertMatEqual(gray2, dst);
1396         // TODO_: write better test
1397     }
1398
1399     public void testMinAreaRect() {
1400         MatOfPoint2f points = new MatOfPoint2f(new Point(1, 1), new Point(5, 1), new Point(4, 3), new Point(6, 2));
1401
1402         RotatedRect rrect = Imgproc.minAreaRect(points);
1403
1404         assertEquals(new Size(2, 5), rrect.size);
1405         assertEquals(-90., rrect.angle);
1406         assertEquals(new Point(3.5, 2), rrect.center);
1407     }
1408
1409     public void testMinEnclosingCircle() {
1410         MatOfPoint2f points = new MatOfPoint2f(new Point(0, 0), new Point(-1, 0), new Point(0, -1), new Point(1, 0), new Point(0, 1));
1411         Point actualCenter = new Point();
1412         float[] radius = new float[1];
1413
1414         Imgproc.minEnclosingCircle(points, actualCenter, radius);
1415
1416         assertEquals(new Point(0, 0), actualCenter);
1417         assertEquals(1.03f, radius[0], EPS);
1418     }
1419
1420     public void testMomentsMat() {
1421         fail("Not yet implemented");
1422     }
1423
1424     public void testMomentsMatBoolean() {
1425         fail("Not yet implemented");
1426     }
1427
1428     public void testMorphologyExMatMatIntMat() {
1429         Imgproc.morphologyEx(gray255, dst, Imgproc.MORPH_GRADIENT, gray0);
1430
1431         assertMatEqual(gray0, dst);
1432         // TODO_: write better test
1433     }
1434
1435     public void testMorphologyExMatMatIntMatPointInt() {
1436         Mat src = Mat.eye(imgprocSz, imgprocSz, CvType.CV_8U);
1437
1438         Mat kernel = new Mat(imgprocSz, imgprocSz, CvType.CV_8U, new Scalar(0));
1439         Point point = new Point(0, 0);
1440
1441         Imgproc.morphologyEx(src, dst, Imgproc.MORPH_CLOSE, kernel, point, 10);
1442
1443         truth = Mat.eye(imgprocSz, imgprocSz, CvType.CV_8U);
1444         assertMatEqual(truth, dst);
1445         // TODO_: write better test
1446     }
1447
1448
1449     public void testMorphologyExMatMatIntMatPointIntIntScalar() {
1450         Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_8U);
1451         src.put(0, 0, 2, 1);
1452         src.put(1, 0, 2, 1);
1453
1454         Mat kernel = new Mat(imgprocSz, imgprocSz, CvType.CV_8U, new Scalar(1));
1455         Point point = new Point(1, 1);
1456         Scalar sc = new Scalar(3, 3);
1457
1458         Imgproc.morphologyEx(src, dst, Imgproc.MORPH_TOPHAT, kernel, point, 10, Core.BORDER_REFLECT, sc);
1459         truth = new Mat(imgprocSz, imgprocSz, CvType.CV_8U) {
1460             {
1461                 put(0, 0, 1, 0);
1462                 put(1, 0, 1, 0);
1463             }
1464         };
1465         assertMatEqual(truth, dst);
1466         // TODO_: write better test
1467     }
1468
1469     public void testPointPolygonTest() {
1470         MatOfPoint2f contour = new MatOfPoint2f(new Point(0, 0), new Point(1, 3), new Point(3, 4), new Point(4, 3), new Point(2, 1));
1471         double sign1 = Imgproc.pointPolygonTest(contour, new Point(2, 2), false);
1472         assertEquals(1.0, sign1);
1473
1474         double sign2 = Imgproc.pointPolygonTest(contour, new Point(4, 4), true);
1475         assertEquals(-Math.sqrt(0.5), sign2);
1476     }
1477
1478     public void testPreCornerDetectMatMatInt() {
1479         Mat src = new Mat(4, 4, CvType.CV_32F, new Scalar(1));
1480         int ksize = 3;
1481
1482         Imgproc.preCornerDetect(src, dst, ksize);
1483
1484         truth = new Mat(4, 4, CvType.CV_32F, new Scalar(0));
1485         assertMatEqual(truth, dst, EPS);
1486     }
1487
1488     public void testPreCornerDetectMatMatIntInt() {
1489         Mat src = new Mat(4, 4, CvType.CV_32F, new Scalar(1));
1490         int ksize = 3;
1491
1492         Imgproc.preCornerDetect(src, dst, ksize, Core.BORDER_REFLECT);
1493
1494         truth = new Mat(4, 4, CvType.CV_32F, new Scalar(0));
1495         assertMatEqual(truth, dst, EPS);
1496         // TODO_: write better test
1497     }
1498
1499     public void testPyrDownMatMat() {
1500         Mat src = new Mat(4, 4, CvType.CV_32F) {
1501             {
1502                 put(0, 0, 2, 1, 4, 2);
1503                 put(1, 0, 3, 2, 6, 8);
1504                 put(2, 0, 4, 6, 8, 10);
1505                 put(3, 0, 12, 32, 6, 18);
1506             }
1507         };
1508
1509         Imgproc.pyrDown(src, dst);
1510
1511         truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F) {
1512             {
1513                 put(0, 0, 2.78125, 4.609375);
1514                 put(1, 0, 8.546875, 8.8515625);
1515             }
1516         };
1517         assertMatEqual(truth, dst, EPS);
1518     }
1519
1520     public void testPyrDownMatMatSize() {
1521         Mat src = new Mat(4, 4, CvType.CV_32F) {
1522             {
1523                 put(0, 0, 2, 1, 4, 2);
1524                 put(1, 0, 3, 2, 6, 8);
1525                 put(2, 0, 4, 6, 8, 10);
1526                 put(3, 0, 12, 32, 6, 18);
1527             }
1528         };
1529         Size dstSize = new Size(2, 2);
1530
1531         Imgproc.pyrDown(src, dst, dstSize);
1532
1533         truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F) {
1534             {
1535                 put(0, 0, 2.78125, 4.609375);
1536                 put(1, 0, 8.546875, 8.8515625);
1537             }
1538         };
1539         assertMatEqual(truth, dst, EPS);
1540         // TODO_: write better test
1541     }
1542
1543     public void testPyrMeanShiftFilteringMatMatDoubleDouble() {
1544         Mat src = new Mat(matSize, matSize, CvType.CV_8UC3, new Scalar(0));
1545
1546         Imgproc.pyrMeanShiftFiltering(src, dst, 10, 50);
1547
1548         assertMatEqual(src, dst);
1549         // TODO_: write better test
1550     }
1551
1552     public void testPyrMeanShiftFilteringMatMatDoubleDoubleInt() {
1553         fail("Not yet implemented");
1554     }
1555
1556     public void testPyrMeanShiftFilteringMatMatDoubleDoubleIntTermCriteria() {
1557         fail("Not yet implemented");
1558     }
1559
1560     public void testPyrUpMatMat() {
1561         Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F);
1562         src.put(0, 0, 2, 1);
1563         src.put(1, 0, 3, 2);
1564
1565         Imgproc.pyrUp(src, dst);
1566
1567         truth = new Mat(4, 4, CvType.CV_32F) {
1568             {
1569                 put(0, 0, 2,     1.75,  1.375, 1.25);
1570                 put(1, 0, 2.25,  2,     1.625, 1.5);
1571                 put(2, 0, 2.625, 2.375, 2,     1.875);
1572                 put(3, 0, 2.75,  2.5,   2.125, 2);
1573             }
1574         };
1575         assertMatEqual(truth, dst, EPS);
1576     }
1577
1578     public void testPyrUpMatMatSize() {
1579         fail("Not yet implemented");
1580     }
1581
1582     public void testRemapMatMatMatMatInt() {
1583         fail("Not yet implemented");
1584         // this test does something weird
1585         Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(2));
1586         Mat map1 = new Mat(1, 3, CvType.CV_32FC1);
1587         Mat map2 = new Mat(1, 3, CvType.CV_32FC1);
1588
1589         map1.put(0, 0, 3, 6, 5);
1590         map2.put(0, 0, 4, 8, 12);
1591
1592         Imgproc.remap(src, dst, map1, map2, Imgproc.INTER_LINEAR);
1593
1594         truth = new Mat(1, 3, CvType.CV_32F, new Scalar(0));
1595         assertMatEqual(truth, dst, EPS);
1596     }
1597
1598     public void testRemapMatMatMatMatIntIntScalar() {
1599         fail("Not yet implemented");
1600         // this test does something weird
1601         Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(2));
1602         Mat map1 = new Mat(1, 3, CvType.CV_32FC1);
1603         Mat map2 = new Mat(1, 3, CvType.CV_32FC1);
1604
1605         Scalar sc = new Scalar(0);
1606
1607         map1.put(0, 0, 3, 6, 5, 0);
1608         map2.put(0, 0, 4, 8, 12);
1609
1610         truth = new Mat(1, 3, CvType.CV_32F, new Scalar(2));
1611
1612         Imgproc.remap(src, dst, map1, map2, Imgproc.INTER_LINEAR, Core.BORDER_REFLECT, sc);
1613         assertMatEqual(truth, dst, EPS);
1614     }
1615
1616     public void testResizeMatMatSize() {
1617         Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_8UC1, new Scalar(1));
1618         Size dsize = new Size(1, 1);
1619
1620         Imgproc.resize(src, dst, dsize);
1621
1622         truth = new Mat(1, 1, CvType.CV_8UC1, new Scalar(1));
1623         assertMatEqual(truth, dst);
1624     }
1625
1626     public void testResizeMatMatSizeDoubleDoubleInt() {
1627         Imgproc.resize(gray255, dst, new Size(2, 2), 0, 0, Imgproc.INTER_AREA);
1628
1629         truth = new Mat(2, 2, CvType.CV_8UC1, new Scalar(255));
1630         assertMatEqual(truth, dst);
1631         // TODO_: write better test
1632     }
1633
1634     public void testScharrMatMatIntIntInt() {
1635         Mat src = Mat.eye(imgprocSz, imgprocSz, CvType.CV_32F);
1636
1637         Imgproc.Scharr(src, dst, CvType.CV_32F, 1, 0);
1638
1639         truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(0));
1640         assertMatEqual(truth, dst, EPS);
1641     }
1642
1643     public void testScharrMatMatIntIntIntDoubleDouble() {
1644         Mat src = Mat.eye(imgprocSz, imgprocSz, CvType.CV_32F);
1645
1646         Imgproc.Scharr(src, dst, CvType.CV_32F, 1, 0, 1.5, 0.001);
1647
1648         truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(0.001));
1649         assertMatEqual(truth, dst, EPS);
1650     }
1651
1652     public void testScharrMatMatIntIntIntDoubleDoubleInt() {
1653         Mat src = Mat.eye(3, 3, CvType.CV_32F);
1654
1655         Imgproc.Scharr(src, dst, CvType.CV_32F, 1, 0, 1.5, 0, Core.BORDER_REFLECT);
1656
1657         truth = new Mat(3, 3, CvType.CV_32F) {
1658             {
1659                 put(0, 0, -15, -19.5, -4.5);
1660                 put(1, 0, 10.5, 0, -10.5);
1661                 put(2, 0, 4.5, 19.5, 15);
1662             }
1663         };
1664         assertMatEqual(truth, dst, EPS);
1665     }
1666
1667     public void testSepFilter2DMatMatIntMatMat() {
1668         Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(2));
1669         Mat kernelX = new Mat(1, 3, CvType.CV_32FC1);
1670         Mat kernelY = new Mat(1, 3, CvType.CV_32FC1);
1671         kernelX.put(0, 0, 4, 3, 7);
1672         kernelY.put(0, 0, 9, 4, 2);
1673
1674         Imgproc.sepFilter2D(src, dst, CvType.CV_32F, kernelX, kernelY);
1675
1676         truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(420));
1677         assertMatEqual(truth, dst, EPS);
1678     }
1679
1680     public void testSepFilter2DMatMatIntMatMatPointDouble() {
1681         Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32FC1, new Scalar(2));
1682         Mat kernelX = new Mat(1, 3, CvType.CV_32FC1);
1683         kernelX.put(0, 0, 2, 2, 2);
1684         Mat kernelY = new Mat(1, 3, CvType.CV_32FC1);
1685         kernelY.put(0, 0, 1, 1, 1);
1686
1687         Imgproc.sepFilter2D(src, dst, CvType.CV_32F, kernelX, kernelY, anchorPoint, weakEPS);
1688
1689         truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(36 + weakEPS));
1690         assertMatEqual(truth, dst, EPS);
1691     }
1692
1693     public void testSepFilter2DMatMatIntMatMatPointDoubleInt() {
1694         Mat kernelX = new Mat(1, 3, CvType.CV_32FC1);
1695         kernelX.put(0, 0, 2, 2, 2);
1696
1697         Mat kernelY = new Mat(1, 3, CvType.CV_32FC1);
1698         kernelY.put(0, 0, 1, 1, 1);
1699
1700         Imgproc.sepFilter2D(gray0, dst, CvType.CV_32F, kernelX, kernelY, anchorPoint, weakEPS, Core.BORDER_REFLECT);
1701
1702         truth = new Mat(10, 10, CvType.CV_32F, new Scalar(weakEPS));
1703         assertMatEqual(truth, dst, EPS);
1704         // TODO_: write better test
1705     }
1706
1707     public void testSobelMatMatIntIntInt() {
1708         Imgproc.Sobel(gray255, dst, CvType.CV_8U, 1, 0);
1709
1710         assertMatEqual(gray0, dst);
1711     }
1712
1713     public void testSobelMatMatIntIntIntIntDoubleDouble() {
1714         Imgproc.Sobel(gray255, dst, CvType.CV_8U, 1, 0, 3, 2, 0.001);
1715         assertMatEqual(gray0, dst);
1716         // TODO_: write better test
1717     }
1718
1719     public void testSobelMatMatIntIntIntIntDoubleDoubleInt() {
1720         Mat src = new Mat(3, 3, CvType.CV_32F) {
1721             {
1722                 put(0, 0, 2, 0, 1);
1723                 put(1, 0, 6, 4, 3);
1724                 put(2, 0, 1, 0, 2);
1725             }
1726         };
1727
1728         Imgproc.Sobel(src, dst, CvType.CV_32F, 1, 0, 3, 2, 0, Core.BORDER_REPLICATE);
1729
1730         truth = new Mat(3, 3, CvType.CV_32F) {
1731             {
1732                 put(0, 0, -16, -12, 4);
1733                 put(1, 0, -14, -12, 2);
1734                 put(2, 0, -10, 0, 10);
1735             }
1736         };
1737         assertMatEqual(truth, dst, EPS);
1738     }
1739
1740     public void testThreshold() {
1741         Imgproc.threshold(makeMask(gray0.clone(), 10), dst, 5, 255, Imgproc.THRESH_TRUNC);
1742         assertMatEqual(makeMask(gray0.clone(), 5), dst);
1743
1744         Imgproc.threshold(makeMask(gray2.clone(), 10), dst, 1, 255, Imgproc.THRESH_BINARY);
1745         assertMatEqual(gray255, dst);
1746
1747         Imgproc.threshold(makeMask(gray2.clone(), 10), dst, 3, 255, Imgproc.THRESH_BINARY_INV);
1748         assertMatEqual(makeMask(gray255.clone(), 0), dst);
1749     }
1750
1751     public void testUndistortMatMatMatMat() {
1752         Mat src = new Mat(3, 3, CvType.CV_32F, new Scalar(3));
1753         Mat cameraMatrix = new Mat(3, 3, CvType.CV_32F) {
1754             {
1755                 put(0, 0, 1, 0, 1);
1756                 put(1, 0, 0, 1, 2);
1757                 put(2, 0, 0, 0, 1);
1758             }
1759         };
1760         Mat distCoeffs = new Mat(1, 4, CvType.CV_32F) {
1761             {
1762                 put(0, 0, 1, 3, 2, 4);
1763             }
1764         };
1765
1766         Imgproc.undistort(src, dst, cameraMatrix, distCoeffs);
1767
1768         truth = new Mat(3, 3, CvType.CV_32F) {
1769             {
1770                 put(0, 0, 0, 0, 0);
1771                 put(1, 0, 0, 0, 0);
1772                 put(2, 0, 0, 3, 0);
1773             }
1774         };
1775         assertMatEqual(truth, dst, EPS);
1776     }
1777
1778     public void testUndistortMatMatMatMatMat() {
1779         Mat src = new Mat(3, 3, CvType.CV_32F, new Scalar(3));
1780         Mat cameraMatrix = new Mat(3, 3, CvType.CV_32F) {
1781             {
1782                 put(0, 0, 1, 0, 1);
1783                 put(1, 0, 0, 1, 2);
1784                 put(2, 0, 0, 0, 1);
1785             }
1786         };
1787         Mat distCoeffs = new Mat(1, 4, CvType.CV_32F) {
1788             {
1789                 put(0, 0, 2, 1, 4, 5);
1790             }
1791         };
1792         Mat newCameraMatrix = new Mat(3, 3, CvType.CV_32F, new Scalar(1));
1793
1794         Imgproc.undistort(src, dst, cameraMatrix, distCoeffs, newCameraMatrix);
1795
1796         truth = new Mat(3, 3, CvType.CV_32F, new Scalar(3));
1797         assertMatEqual(truth, dst, EPS);
1798     }
1799
1800     //undistortPoints(List<Point> src, List<Point> dst, Mat cameraMatrix, Mat distCoeffs)
1801     public void testUndistortPointsListOfPointListOfPointMatMat() {
1802         MatOfPoint2f src = new MatOfPoint2f(new Point(1, 2), new Point(3, 4), new Point(-1, -1));
1803         MatOfPoint2f dst = new MatOfPoint2f();
1804         Mat cameraMatrix = Mat.eye(3, 3, CvType.CV_64FC1);
1805         Mat distCoeffs = new Mat(8, 1, CvType.CV_64FC1, new Scalar(0));
1806
1807         Imgproc.undistortPoints(src, dst, cameraMatrix, distCoeffs);
1808
1809         assertEquals(src.size(), dst.size());
1810         for(int i=0; i<src.toList().size(); i++) {
1811             //Log.d("UndistortPoints", "s="+src.get(i)+", d="+dst.get(i));
1812             assertTrue(src.toList().get(i).equals(dst.toList().get(i)));
1813         }
1814     }
1815
1816
1817     public void testWarpAffineMatMatMatSize() {
1818         Mat src = new Mat(3, 3, CvType.CV_32F) {
1819             {
1820                 put(0, 0, 2, 0, 1);
1821                 put(1, 0, 6, 4, 3);
1822                 put(2, 0, 1, 0, 2);
1823             }
1824         };
1825         Mat M = new Mat(2, 3, CvType.CV_32F) {
1826             {
1827                 put(0, 0, 1, 0, 1);
1828                 put(1, 0, 0, 1, 1);
1829             }
1830         };
1831
1832         Imgproc.warpAffine(src, dst, M, new Size(3, 3));
1833
1834         truth = new Mat(3, 3, CvType.CV_32F) {
1835             {
1836                 put(0, 0, 0, 0, 0);
1837                 put(1, 0, 0, 2, 0);
1838                 put(2, 0, 0, 6, 4);
1839             }
1840         };
1841         assertMatEqual(truth, dst, EPS);
1842     }
1843
1844     public void testWarpAffineMatMatMatSizeInt() {
1845         Mat src = new Mat(3, 3, CvType.CV_32F) {
1846             {
1847                 put(0, 0, 2, 4, 1);
1848                 put(1, 0, 6, 4, 3);
1849                 put(2, 0, 0, 2, 2);
1850             }
1851         };
1852         Mat M = new Mat(2, 3, CvType.CV_32F) {
1853             {
1854                 put(0, 0, 1, 0, 0);
1855                 put(1, 0, 0, 0, 1);
1856             }
1857         };
1858
1859         Imgproc.warpAffine(src, dst, M, new Size(2, 2), Imgproc.WARP_INVERSE_MAP);
1860
1861         truth = new Mat(2, 2, CvType.CV_32F) {
1862             {
1863                 put(0, 0, 6, 4);
1864                 put(1, 0, 6, 4);
1865             }
1866         };
1867         assertMatEqual(truth, dst, EPS);
1868     }
1869
1870     public void testWarpAffineMatMatMatSizeIntInt() {
1871         fail("Not yet implemented");
1872     }
1873
1874     public void testWarpAffineMatMatMatSizeIntIntScalar() {
1875         fail("Not yet implemented");
1876     }
1877
1878     public void testWarpPerspectiveMatMatMatSize() {
1879         Mat src = new Mat(3, 3, CvType.CV_32F) {
1880             {
1881                 put(0, 0, 2, 4, 1);
1882                 put(1, 0, 0, 4, 5);
1883                 put(2, 0, 1, 2, 2);
1884             }
1885         };
1886         Mat M = new Mat(3, 3, CvType.CV_32F) {
1887             {
1888                 put(0, 0, 1, 0, 1);
1889                 put(1, 0, 0, 1, 1);
1890                 put(2, 0, 0, 0, 1);
1891             }
1892         };
1893
1894         Imgproc.warpPerspective(src, dst, M, new Size(3, 3));
1895
1896         truth = new Mat(3, 3, CvType.CV_32F) {
1897             {
1898                 put(0, 0, 0, 0, 0);
1899                 put(1, 0, 0, 2, 4);
1900                 put(2, 0, 0, 0, 4);
1901             }
1902         };
1903         assertMatEqual(truth, dst, EPS);
1904     }
1905
1906     public void testWarpPerspectiveMatMatMatSizeInt() {
1907         fail("Not yet implemented");
1908     }
1909
1910     public void testWarpPerspectiveMatMatMatSizeIntInt() {
1911         fail("Not yet implemented");
1912     }
1913
1914     public void testWarpPerspectiveMatMatMatSizeIntIntScalar() {
1915         fail("Not yet implemented");
1916     }
1917
1918     public void testWatershed() {
1919         Mat image = Mat.eye(4, 4, CvType.CV_8UC(3));
1920         Mat markers = new Mat(4, 4, CvType.CV_32SC1, new Scalar(0));
1921
1922         Imgproc.watershed(image, markers);
1923
1924         truth = new Mat(4, 4, CvType.CV_32SC1) {
1925             {
1926                 put(0, 0, -1, -1, -1, -1);
1927                 put(1, 0, -1, 0, 0, -1);
1928                 put(2, 0, -1, 0, 0, -1);
1929                 put(3, 0, -1, -1, -1, -1);
1930             }
1931         };
1932         assertMatEqual(truth, markers);
1933     }
1934
1935     public void testGetTextSize() {
1936         String text = "Android all the way";
1937         double fontScale = 2;
1938         int thickness = 3;
1939         int baseLine[] = new int[1];
1940
1941         Imgproc.getTextSize(text, Core.FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, null);
1942         Size res = Imgproc.getTextSize(text, Core.FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, baseLine);
1943
1944         assertEquals(543.0, res.width);
1945         assertEquals(44.0, res.height);
1946         assertEquals(20, baseLine[0]);
1947     }
1948
1949     public void testCircleMatPointIntScalar() {
1950         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
1951         int radius = Math.min(gray0.cols() / 4, gray0.rows() / 4);
1952         Scalar color = new Scalar(128);
1953
1954         Imgproc.circle(gray0, center, radius, color);
1955
1956         assertTrue(0 != Core.countNonZero(gray0));
1957     }
1958
1959     public void testCircleMatPointIntScalarInt() {
1960         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
1961         int radius = Math.min(gray0.cols() / 4, gray0.rows() / 4);
1962         Scalar color = new Scalar(128);
1963
1964         Imgproc.circle(gray0, center, radius, color, Core.FILLED);
1965
1966         assertTrue(0 != Core.countNonZero(gray0));
1967     }
1968
1969     public void testCircleMatPointIntScalarIntIntInt() {
1970         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
1971         Point center2 = new Point(gray0.cols(), gray0.rows());
1972         int radius = Math.min(gray0.cols() / 4, gray0.rows() / 4);
1973         Scalar color128 = new Scalar(128);
1974         Scalar color0 = new Scalar(0);
1975
1976         Imgproc.circle(gray0, center2, radius * 2, color128, 2, Imgproc.LINE_4, 1/*
1977                                                                             * Number
1978                                                                             * of
1979                                                                             * fractional
1980                                                                             * bits
1981                                                                             */);
1982         assertFalse(0 == Core.countNonZero(gray0));
1983
1984         Imgproc.circle(gray0, center, radius, color0, 2, Imgproc.LINE_4, 0);
1985
1986         assertTrue(0 == Core.countNonZero(gray0));
1987     }
1988
1989     public void testClipLine() {
1990         Rect r = new Rect(10, 10, 10, 10);
1991         Point pt1 = new Point(5.0, 15.0);
1992         Point pt2 = new Point(25.0, 15.0);
1993
1994         assertTrue(Imgproc.clipLine(r, pt1, pt2));
1995
1996         Point pt1Clipped = new Point(10.0, 15.0);
1997         Point pt2Clipped = new Point(19.0, 15.0);
1998         assertEquals(pt1Clipped, pt1);
1999         assertEquals(pt2Clipped, pt2);
2000
2001         pt1 = new Point(5.0, 5.0);
2002         pt2 = new Point(25.0, 5.0);
2003         pt1Clipped = new Point(5.0, 5.0);
2004         pt2Clipped = new Point(25.0, 5.0);
2005
2006         assertFalse(Imgproc.clipLine(r, pt1, pt2));
2007
2008         assertEquals(pt1Clipped, pt1);
2009         assertEquals(pt2Clipped, pt2);
2010     }
2011
2012     public void testEllipse2Poly() {
2013         Point center = new Point(4, 4);
2014         Size axes = new Size(2, 2);
2015         int angle = 30;
2016         int arcStart = 30;
2017         int arcEnd = 60;
2018         int delta = 2;
2019         MatOfPoint pts = new MatOfPoint();
2020
2021         Imgproc.ellipse2Poly(center, axes, angle, arcStart, arcEnd, delta, pts);
2022
2023         Point truth[] = {
2024                 new Point(5, 6),
2025                 new Point(4, 6)
2026         };
2027         assertArrayPointsEquals(truth, pts.toArray(), EPS);
2028     }
2029
2030     public void testEllipseMatPointSizeDoubleDoubleDoubleScalar() {
2031         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
2032         Size axes = new Size(2, 2);
2033         double angle = 30, startAngle = 60, endAngle = 90;
2034
2035         Imgproc.ellipse(gray0, center, axes, angle, startAngle, endAngle, colorWhite);
2036
2037         assertTrue(0 != Core.countNonZero(gray0));
2038     }
2039
2040     public void testEllipseMatPointSizeDoubleDoubleDoubleScalarInt() {
2041         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
2042         Size axes = new Size(2, 2);
2043         double angle = 30, startAngle = 60, endAngle = 90;
2044
2045         Imgproc.ellipse(gray0, center, axes, angle, startAngle, endAngle, colorWhite, Core.FILLED);
2046
2047         assertTrue(0 != Core.countNonZero(gray0));
2048     }
2049
2050     public void testEllipseMatPointSizeDoubleDoubleDoubleScalarIntIntInt() {
2051         Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
2052         Size axes = new Size(2, 2);
2053         Point center2 = new Point(gray0.cols(), gray0.rows());
2054         Size axes2 = new Size(4, 4);
2055         double angle = 30, startAngle = 0, endAngle = 30;
2056
2057         Imgproc.ellipse(gray0, center, axes, angle, startAngle, endAngle, colorWhite, Core.FILLED, Imgproc.LINE_4, 0);
2058
2059         assertTrue(0 != Core.countNonZero(gray0));
2060
2061         Imgproc.ellipse(gray0, center2, axes2, angle, startAngle, endAngle, colorBlack, Core.FILLED, Imgproc.LINE_4, 1);
2062
2063         assertEquals(0, Core.countNonZero(gray0));
2064     }
2065
2066     public void testEllipseMatRotatedRectScalar() {
2067         int matSize = 10;
2068         Mat gray0 = Mat.zeros(matSize, matSize, CvType.CV_8U);
2069         Point center = new Point(matSize / 2, matSize / 2);
2070         Size size = new Size(matSize / 4, matSize / 2);
2071         RotatedRect box = new RotatedRect(center, size, 45);
2072
2073         Imgproc.ellipse(gray0, box, new Scalar(1));
2074
2075         final byte[] truth = new byte[] {
2076                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2077                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2078                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2079                 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
2080                 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
2081                 0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
2082                 0, 0, 0, 1, 0, 1, 1, 0, 0, 0,
2083                 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
2084                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2085                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2086
2087         assertMatEqual(new Mat(matSize, matSize, CvType.CV_8U) {
2088             {
2089                 put(0, 0, truth);
2090             }
2091         }, gray0);
2092     }
2093
2094     public void testEllipseMatRotatedRectScalarInt() {
2095         Point center = new Point(matSize / 2, matSize / 2);
2096         Size size = new Size(matSize / 4, matSize / 2);
2097         RotatedRect box = new RotatedRect(center, size, 45);
2098
2099         Imgproc.ellipse(gray0, box, new Scalar(1), Core.FILLED);
2100         Imgproc.ellipse(gray0, box, new Scalar(0));
2101
2102         assertTrue(0 < Core.countNonZero(gray0));
2103     }
2104
2105     public void testEllipseMatRotatedRectScalarIntInt() {
2106         Point center = new Point(matSize / 2, matSize / 2);
2107         Size size = new Size(2, matSize * 2 / 3);
2108         RotatedRect box = new RotatedRect(center, size, 20);
2109
2110         Imgproc.ellipse(gray0, box, new Scalar(9), 1, Imgproc.LINE_AA);
2111         Imgproc.ellipse(gray0, box, new Scalar(0), 1, Imgproc.LINE_4);
2112
2113         assertTrue(0 < Core.countNonZero(gray0));
2114     }
2115
2116     public void testPolylinesMatListOfListOfPointBooleanScalar() {
2117         Mat img = gray0;
2118         List<MatOfPoint> polyline = new ArrayList<MatOfPoint>();
2119         polyline.add(new MatOfPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
2120
2121         Imgproc.polylines(img, polyline, true, new Scalar(100));
2122
2123         assertEquals(22, Core.countNonZero(img));
2124
2125         Imgproc.polylines(img, polyline, false, new Scalar(0));
2126
2127         assertEquals(4, Core.countNonZero(img));
2128     }
2129
2130     public void testPolylinesMatListOfListOfPointBooleanScalarInt() {
2131         Mat img = gray0;
2132         List<MatOfPoint> polyline = new ArrayList<MatOfPoint>();
2133         polyline.add(new MatOfPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
2134
2135         Imgproc.polylines(img, polyline, true, new Scalar(100), 2);
2136
2137         assertEquals(62, Core.countNonZero(img));
2138     }
2139
2140     public void testPolylinesMatListOfListOfPointBooleanScalarIntIntInt() {
2141         Mat img = gray0;
2142         List<MatOfPoint> polyline1 = new ArrayList<MatOfPoint>();
2143         polyline1.add(new MatOfPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
2144         List<MatOfPoint> polyline2 = new ArrayList<MatOfPoint>();
2145         polyline2.add(new MatOfPoint(new Point(2, 2), new Point(14, 2), new Point(14, 12), new Point(2, 12)));
2146
2147         Imgproc.polylines(img, polyline1, true, new Scalar(100), 2, Imgproc.LINE_8, 0);
2148
2149         assertTrue(Core.countNonZero(img) > 0);
2150
2151         Imgproc.polylines(img, polyline2, true, new Scalar(0), 2, Imgproc.LINE_8, 1);
2152
2153         assertEquals(0, Core.countNonZero(img));
2154     }
2155
2156     public void testPutTextMatStringPointIntDoubleScalar() {
2157         String text = "Hello World";
2158         Size labelSize = new Size(175, 22);
2159         Mat img = new Mat(20 + (int) labelSize.height, 20 + (int) labelSize.width, CvType.CV_8U, colorBlack);
2160         Point origin = new Point(10, labelSize.height + 10);
2161
2162         Imgproc.putText(img, text, origin, Core.FONT_HERSHEY_SIMPLEX, 1.0, colorWhite);
2163
2164         assertTrue(Core.countNonZero(img) > 0);
2165         // check that border is not corrupted
2166         Imgproc.rectangle(img, new Point(11, 11), new Point(labelSize.width + 10, labelSize.height + 10), colorBlack, Core.FILLED);
2167         assertEquals(0, Core.countNonZero(img));
2168     }
2169
2170     public void testPutTextMatStringPointIntDoubleScalarInt() {
2171         String text = "Hello World";
2172         Size labelSize = new Size(176, 22);
2173         Mat img = new Mat(20 + (int) labelSize.height, 20 + (int) labelSize.width, CvType.CV_8U, colorBlack);
2174         Point origin = new Point(10, labelSize.height + 10);
2175
2176         Imgproc.putText(img, text, origin, Core.FONT_HERSHEY_SIMPLEX, 1.0, colorWhite, 2);
2177
2178         assertTrue(Core.countNonZero(img) > 0);
2179         // check that border is not corrupted
2180         Imgproc.rectangle(img, new Point(10, 10), new Point(labelSize.width + 10 + 1, labelSize.height + 10 + 1), colorBlack, Core.FILLED);
2181         assertEquals(0, Core.countNonZero(img));
2182     }
2183
2184     public void testPutTextMatStringPointIntDoubleScalarIntIntBoolean() {
2185         String text = "Hello World";
2186         Size labelSize = new Size(175, 22);
2187
2188         Mat img = new Mat(20 + (int) labelSize.height, 20 + (int) labelSize.width, CvType.CV_8U, colorBlack);
2189         Point origin = new Point(10, 10);
2190
2191         Imgproc.putText(img, text, origin, Core.FONT_HERSHEY_SIMPLEX, 1.0, colorWhite, 1, Imgproc.LINE_8, true);
2192
2193         assertTrue(Core.countNonZero(img) > 0);
2194         // check that border is not corrupted
2195         Imgproc.rectangle(img, new Point(10, 10), new Point(labelSize.width + 9, labelSize.height + 9), colorBlack, Core.FILLED);
2196         assertEquals(0, Core.countNonZero(img));
2197     }
2198 }