1 package org.opencv.test;
3 import java.io.BufferedReader;
5 import java.io.FileOutputStream;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.nio.channels.FileChannel;
9 import java.nio.charset.Charset;
10 import java.util.List;
12 import junit.framework.TestCase;
14 import org.opencv.core.Core;
15 import org.opencv.core.CvType;
16 import org.opencv.core.Mat;
17 import org.opencv.core.Point;
18 import org.opencv.core.Point3;
19 import org.opencv.core.Rect;
20 import org.opencv.core.Scalar;
21 import org.opencv.core.Size;
22 import org.opencv.core.DMatch;
23 import org.opencv.core.KeyPoint;
24 import org.opencv.imgcodecs.Imgcodecs;
26 import android.util.Log;
28 public class OpenCVTestCase extends TestCase {
29 //change to 'true' to unblock fail on fail("Not yet implemented")
30 public static final boolean passNYI = true;
32 protected static boolean isTestCaseEnabled = true;
34 protected static final int matSize = 10;
35 protected static final double EPS = 0.001;
36 protected static final double weakEPS = 0.5;
38 private static final String TAG = "OpenCVTestCase";
43 protected Scalar colorBlack;
44 protected Scalar colorWhite;
46 // Naming notation: <channels info>_[depth]_[dimensions]_value
47 // examples: gray0 - single channel 8U 2d Mat filled with 0
48 // grayRnd - single channel 8U 2d Mat filled with random numbers
51 // TODO: OpenCVTestCase refactorings
53 // - create methods gray0() and create src1 explicitly
54 // - create some masks
55 // - use truth member everywhere - remove truth from base class - each test
56 // fixture should use own truth filed
63 protected Mat gray127;
64 protected Mat gray128;
65 protected Mat gray255;
66 protected Mat grayRnd;
68 protected Mat gray_16u_256;
69 protected Mat gray_16s_1024;
71 protected Mat gray0_32f;
72 protected Mat gray1_32f;
73 protected Mat gray3_32f;
74 protected Mat gray9_32f;
75 protected Mat gray255_32f;
76 protected Mat grayE_32f;
77 protected Mat grayRnd_32f;
79 protected Mat gray0_32f_1d;
81 protected Mat gray0_64f;
82 protected Mat gray0_64f_1d;
85 protected Mat rgba128;
87 protected Mat rgbLena;
88 protected Mat grayChess;
94 protected void setUp() throws Exception {
98 assertTrue(dst.empty());
101 colorBlack = new Scalar(0);
102 colorWhite = new Scalar(255, 255, 255);
104 gray0 = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(0));
105 gray1 = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(1));
106 gray2 = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(2));
107 gray3 = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(3));
108 gray9 = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(9));
109 gray127 = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(127));
110 gray128 = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(128));
111 gray255 = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
113 grayRnd = new Mat(matSize, matSize, CvType.CV_8U);
114 Core.randu(grayRnd, 0, 256);
116 gray_16u_256 = new Mat(matSize, matSize, CvType.CV_16U, new Scalar(256));
117 gray_16s_1024 = new Mat(matSize, matSize, CvType.CV_16S, new Scalar(1024));
119 gray0_32f = new Mat(matSize, matSize, CvType.CV_32F, new Scalar(0.0));
120 gray1_32f = new Mat(matSize, matSize, CvType.CV_32F, new Scalar(1.0));
121 gray3_32f = new Mat(matSize, matSize, CvType.CV_32F, new Scalar(3.0));
122 gray9_32f = new Mat(matSize, matSize, CvType.CV_32F, new Scalar(9.0));
123 gray255_32f = new Mat(matSize, matSize, CvType.CV_32F, new Scalar(255.0));
124 grayE_32f = new Mat(matSize, matSize, CvType.CV_32F);
125 grayE_32f = Mat.eye(matSize, matSize, CvType.CV_32FC1);
126 grayRnd_32f = new Mat(matSize, matSize, CvType.CV_32F);
127 Core.randu(grayRnd_32f, 0, 256);
129 gray0_64f = new Mat(matSize, matSize, CvType.CV_64F, new Scalar(0.0));
131 gray0_32f_1d = new Mat(1, matSize, CvType.CV_32F, new Scalar(0.0));
132 gray0_64f_1d = new Mat(1, matSize, CvType.CV_64F, new Scalar(0.0));
134 rgba0 = new Mat(matSize, matSize, CvType.CV_8UC4, Scalar.all(0));
135 rgba128 = new Mat(matSize, matSize, CvType.CV_8UC4, Scalar.all(128));
137 rgbLena = Imgcodecs.imread(OpenCVTestRunner.LENA_PATH);
138 grayChess = Imgcodecs.imread(OpenCVTestRunner.CHESS_PATH, 0);
140 v1 = new Mat(1, 3, CvType.CV_32F);
141 v1.put(0, 0, 1.0, 3.0, 2.0);
142 v2 = new Mat(1, 3, CvType.CV_32F);
143 v2.put(0, 0, 2.0, 1.0, 3.0);
147 protected void tearDown() throws Exception {
157 gray_16u_256.release();
158 gray_16s_1024.release();
164 gray255_32f.release();
167 grayRnd_32f.release();
168 gray0_32f_1d.release();
170 gray0_64f_1d.release();
182 protected void runTest() throws Throwable {
183 // Do nothing if the precondition does not hold.
184 if (isTestCaseEnabled) {
187 Log.e(TAG, "Test case \"" + this.getClass().getName() + "\" disabled!");
191 protected Mat getMat(int type, double... vals)
193 return new Mat(matSize, matSize, type, new Scalar(vals));
196 protected Mat makeMask(Mat m, double... vals)
198 m.submat(0, m.rows(), 0, m.cols() / 2).setTo(new Scalar(vals));
202 public static void fail(String msg) {
203 if(msg == "Not yet implemented" && passNYI)
208 public static <E extends Number> void assertListEquals(List<E> list1, List<E> list2) {
209 if (list1.size() != list2.size()) {
210 throw new UnsupportedOperationException();
213 if (!list1.isEmpty())
215 if (list1.get(0) instanceof Float || list1.get(0) instanceof Double)
216 throw new UnsupportedOperationException();
219 for (int i = 0; i < list1.size(); i++)
220 assertEquals(list1.get(i), list2.get(i));
223 public static <E extends Number> void assertListEquals(List<E> list1, List<E> list2, double epsilon) {
224 if (list1.size() != list2.size()) {
225 throw new UnsupportedOperationException();
228 for (int i = 0; i < list1.size(); i++)
229 assertTrue(Math.abs(list1.get(i).doubleValue() - list2.get(i).doubleValue()) <= epsilon);
232 public static <E extends Number> void assertArrayEquals(E[] ar1, E[] ar2, double epsilon) {
233 if (ar1.length != ar2.length) {
234 fail("Arrays have different sizes.");
237 for (int i = 0; i < ar1.length; i++)
238 assertEquals(ar1[i].doubleValue(), ar2[i].doubleValue(), epsilon);
239 //assertTrue(Math.abs(ar1[i].doubleValue() - ar2[i].doubleValue()) <= epsilon);
242 public static void assertArrayEquals(double[] ar1, double[] ar2, double epsilon) {
243 if (ar1.length != ar2.length) {
244 fail("Arrays have different sizes.");
247 for (int i = 0; i < ar1.length; i++)
248 assertEquals(ar1[i], ar2[i], epsilon);
249 //assertTrue(Math.abs(ar1[i].doubleValue() - ar2[i].doubleValue()) <= epsilon);
252 public static void assertListMatEquals(List<Mat> list1, List<Mat> list2, double epsilon) {
253 if (list1.size() != list2.size()) {
254 throw new UnsupportedOperationException();
257 for (int i = 0; i < list1.size(); i++)
258 assertMatEqual(list1.get(i), list2.get(i), epsilon);
261 public static void assertListPointEquals(List<Point> list1, List<Point> list2, double epsilon) {
262 if (list1.size() != list2.size()) {
263 throw new UnsupportedOperationException();
266 for (int i = 0; i < list1.size(); i++)
267 assertPointEquals(list1.get(i), list2.get(i), epsilon);
270 public static void assertArrayPointsEquals(Point[] vp1, Point[] vp2, double epsilon) {
271 if (vp1.length != vp2.length) {
272 fail("Arrays have different sizes.");
275 for (int i = 0; i < vp1.length; i++)
276 assertPointEquals(vp1[i], vp2[i], epsilon);
278 public static void assertListPoint3Equals(List<Point3> list1, List<Point3> list2, double epsilon) {
279 if (list1.size() != list2.size()) {
280 throw new UnsupportedOperationException();
283 for (int i = 0; i < list1.size(); i++)
284 assertPoint3Equals(list1.get(i), list2.get(i), epsilon);
287 public static void assertListRectEquals(List<Rect> list1, List<Rect> list2) {
288 if (list1.size() != list2.size()) {
289 throw new UnsupportedOperationException();
292 for (int i = 0; i < list1.size(); i++)
293 assertRectEquals(list1.get(i), list2.get(i));
296 public static void assertRectEquals(Rect expected, Rect actual) {
297 String msg = "expected:<" + expected + "> but was:<" + actual + ">";
298 assertEquals(msg, expected.x, actual.x);
299 assertEquals(msg, expected.y, actual.y);
300 assertEquals(msg, expected.width, actual.width);
301 assertEquals(msg, expected.height, actual.height);
304 public static void assertMatEqual(Mat m1, Mat m2) {
305 compareMats(m1, m2, true);
308 public static void assertMatNotEqual(Mat m1, Mat m2) {
309 compareMats(m1, m2, false);
312 public static void assertMatEqual(Mat expected, Mat actual, double eps) {
313 compareMats(expected, actual, eps, true);
316 public static void assertMatNotEqual(Mat expected, Mat actual, double eps) {
317 compareMats(expected, actual, eps, false);
320 public static void assertKeyPointEqual(KeyPoint expected, KeyPoint actual, double eps) {
321 String msg = "expected:<" + expected + "> but was:<" + actual + ">";
322 assertTrue(msg, Math.hypot(expected.pt.x - actual.pt.x, expected.pt.y - actual.pt.y) < eps);
323 assertTrue(msg, Math.abs(expected.size - actual.size) < eps);
324 assertTrue(msg, Math.abs(expected.angle - actual.angle) < eps);
325 assertTrue(msg, Math.abs(expected.response - actual.response) < eps);
326 assertEquals(msg, expected.octave, actual.octave);
327 assertEquals(msg, expected.class_id, actual.class_id);
330 public static void assertListKeyPointEquals(List<KeyPoint> expected, List<KeyPoint> actual, double epsilon) {
331 assertEquals(expected.size(), actual.size());
332 for (int i = 0; i < expected.size(); i++)
333 assertKeyPointEqual(expected.get(i), actual.get(i), epsilon);
336 public static void assertDMatchEqual(DMatch expected, DMatch actual, double eps) {
337 String msg = "expected:<" + expected + "> but was:<" + actual + ">";
338 assertEquals(msg, expected.queryIdx, actual.queryIdx);
339 assertEquals(msg, expected.trainIdx, actual.trainIdx);
340 assertEquals(msg, expected.imgIdx, actual.imgIdx);
341 assertTrue(msg, Math.abs(expected.distance - actual.distance) < eps);
344 public static void assertScalarEqual(Scalar expected, Scalar actual, double eps) {
345 String msg = "expected:<" + expected + "> but was:<" + actual + ">";
346 assertTrue(msg, Math.abs(expected.val[0] - actual.val[0]) < eps);
347 assertTrue(msg, Math.abs(expected.val[1] - actual.val[1]) < eps);
348 assertTrue(msg, Math.abs(expected.val[2] - actual.val[2]) < eps);
349 assertTrue(msg, Math.abs(expected.val[3] - actual.val[3]) < eps);
352 public static void assertArrayDMatchEquals(DMatch[] expected, DMatch[] actual, double epsilon) {
353 assertEquals(expected.length, actual.length);
354 for (int i = 0; i < expected.length; i++)
355 assertDMatchEqual(expected[i], actual[i], epsilon);
358 public static void assertListDMatchEquals(List<DMatch> expected, List<DMatch> actual, double epsilon) {
359 DMatch expectedArray[] = expected.toArray(new DMatch[0]);
360 DMatch actualArray[] = actual.toArray(new DMatch[0]);
361 assertArrayDMatchEquals(expectedArray, actualArray, epsilon);
364 public static void assertPointEquals(Point expected, Point actual, double eps) {
365 String msg = "expected:<" + expected + "> but was:<" + actual + ">";
366 assertEquals(msg, expected.x, actual.x, eps);
367 assertEquals(msg, expected.y, actual.y, eps);
370 public static void assertSizeEquals(Size expected, Size actual, double eps) {
371 String msg = "expected:<" + expected + "> but was:<" + actual + ">";
372 assertEquals(msg, expected.width, actual.width, eps);
373 assertEquals(msg, expected.height, actual.height, eps);
376 public static void assertPoint3Equals(Point3 expected, Point3 actual, double eps) {
377 String msg = "expected:<" + expected + "> but was:<" + actual + ">";
378 assertEquals(msg, expected.x, actual.x, eps);
379 assertEquals(msg, expected.y, actual.y, eps);
380 assertEquals(msg, expected.z, actual.z, eps);
383 static private void compareMats(Mat expected, Mat actual, boolean isEqualityMeasured) {
384 if (expected.type() != actual.type() || expected.cols() != actual.cols() || expected.rows() != actual.rows()) {
385 throw new UnsupportedOperationException("Can not compare " + expected + " and " + actual);
388 if (expected.depth() == CvType.CV_32F || expected.depth() == CvType.CV_64F) {
389 if (isEqualityMeasured)
390 throw new UnsupportedOperationException(
391 "Floating-point Mats must not be checked for exact match. Use assertMatEqual(Mat expected, Mat actual, double eps) instead.");
393 throw new UnsupportedOperationException(
394 "Floating-point Mats must not be checked for exact match. Use assertMatNotEqual(Mat expected, Mat actual, double eps) instead.");
397 Mat diff = new Mat();
398 Core.absdiff(expected, actual, diff);
399 Mat reshaped = diff.reshape(1);
400 int mistakes = Core.countNonZero(reshaped);
405 if (isEqualityMeasured)
406 assertTrue("Mats are different in " + mistakes + " points", 0 == mistakes);
408 assertFalse("Mats are equal", 0 == mistakes);
411 static private void compareMats(Mat expected, Mat actual, double eps, boolean isEqualityMeasured) {
412 if (expected.type() != actual.type() || expected.cols() != actual.cols() || expected.rows() != actual.rows()) {
413 throw new UnsupportedOperationException("Can not compare " + expected + " and " + actual);
416 Mat diff = new Mat();
417 Core.absdiff(expected, actual, diff);
418 double maxDiff = Core.norm(diff, Core.NORM_INF);
420 if (isEqualityMeasured)
421 assertTrue("Max difference between expected and actiual Mats is "+ maxDiff + ", that bigger than " + eps,
422 Core.checkRange(diff, true, 0.0, eps));
424 assertFalse("Max difference between expected and actiual Mats is "+ maxDiff + ", that less than " + eps,
425 Core.checkRange(diff, true, 0.0, eps));
428 protected static String readFile(String path) {
430 BufferedReader br = new BufferedReader(new FileReader(path));
432 StringBuffer result = new StringBuffer();
433 while ((line = br.readLine()) != null) {
437 return result.toString();
438 } catch (IOException e) {
439 OpenCVTestRunner.Log("Failed to read file \"" + path
440 + "\". Exception is thrown: " + e);
445 protected static void writeFile(String path, String content) {
446 FileOutputStream stream = null;
448 stream = new FileOutputStream(new File(path));
449 FileChannel fc = stream.getChannel();
450 fc.write(Charset.defaultCharset().encode(content));
451 } catch (IOException e) {
452 OpenCVTestRunner.Log("Failed to write file \"" + path
453 + "\". Exception is thrown: " + e);
458 } catch (IOException e) {
459 OpenCVTestRunner.Log("Exception is thrown: " + e);