fixed problem with Nx2 numpy arrays in geometrical functions (#2783)
[profile/ivi/opencv.git] / modules / python / test / test2.py
1 #/usr/bin/env python
2
3 import unittest
4 import random
5 import time
6 import math
7 import sys
8 import array
9 import urllib
10 import tarfile
11 import hashlib
12 import os
13 import getopt
14 import operator
15 import functools
16 import numpy as np
17 import cv2
18 import cv2.cv as cv
19
20 class NewOpenCVTests(unittest.TestCase):
21
22     def get_sample(self, filename, iscolor = cv.CV_LOAD_IMAGE_COLOR):
23         if not filename in self.image_cache:
24             filedata = urllib.urlopen("https://raw.github.com/Itseez/opencv/master/" + filename).read()
25             self.image_cache[filename] = cv2.imdecode(np.fromstring(filedata, dtype=np.uint8), iscolor)
26         return self.image_cache[filename]
27
28     def setUp(self):
29         self.image_cache = {}
30
31     def hashimg(self, im):
32         """ Compute a hash for an image, useful for image comparisons """
33         return hashlib.md5(im.tostring()).digest()
34
35 # Tests to run first; check the handful of basic operations that the later tests rely on
36
37 class Hackathon244Tests(NewOpenCVTests):
38     
39     def test_int_array(self):
40         a = np.array([-1, 2, -3, 4, -5])
41         absa0 = np.abs(a)
42         self.assert_(cv2.norm(a, cv2.NORM_L1) == 15)
43         absa1 = cv2.absdiff(a, 0)
44         self.assertEqual(cv2.norm(absa1, absa0, cv2.NORM_INF), 0)
45         
46     def test_imencode(self):
47         a = np.zeros((480, 640), dtype=np.uint8)
48         flag, ajpg = cv2.imencode("img_q90.jpg", a, [cv2.IMWRITE_JPEG_QUALITY, 90])
49         self.assertEqual(flag, True)
50         self.assertEqual(ajpg.dtype, np.uint8)
51         self.assertGreater(ajpg.shape[0], 1)
52         self.assertEqual(ajpg.shape[1], 1)
53     
54     def test_projectPoints(self):
55         objpt = np.float64([[1,2,3]])
56         imgpt0, jac0 = cv2.projectPoints(objpt, np.zeros(3), np.zeros(3), np.eye(3), np.float64([]))
57         imgpt1, jac1 = cv2.projectPoints(objpt, np.zeros(3), np.zeros(3), np.eye(3), None)
58         self.assertEqual(imgpt0.shape, (objpt.shape[0], 1, 2))
59         self.assertEqual(imgpt1.shape, imgpt0.shape)
60         self.assertEqual(jac0.shape, jac1.shape)
61         self.assertEqual(jac0.shape[0], 2*objpt.shape[0])
62         
63     def test_estimateAffine3D(self):
64         pattern_size = (11, 8)
65         pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)
66         pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
67         pattern_points *= 10
68         (retval, out, inliers) = cv2.estimateAffine3D(pattern_points, pattern_points)
69         self.assertEqual(retval, 1)
70         if cv2.norm(out[2,:]) < 1e-3:
71             out[2,2]=1
72         self.assertLess(cv2.norm(out, np.float64([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])), 1e-3)
73         self.assertEqual(cv2.countNonZero(inliers), pattern_size[0]*pattern_size[1])
74         
75     def test_fast(self):
76         fd = cv2.FastFeatureDetector(30, True)
77         img = self.get_sample("samples/cpp/right02.jpg", 0)
78         img = cv2.medianBlur(img, 3)
79         imgc = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
80         keypoints = fd.detect(img)
81         self.assert_(600 <= len(keypoints) <= 700)
82         for kpt in keypoints:
83             self.assertNotEqual(kpt.response, 0)
84
85     def check_close_angles(self, a, b, angle_delta):
86         self.assert_(abs(a - b) <= angle_delta or
87                      abs(360 - abs(a - b)) <= angle_delta)
88
89     def check_close_pairs(self, a, b, delta):
90         self.assertLessEqual(abs(a[0] - b[0]), delta)
91         self.assertLessEqual(abs(a[1] - b[1]), delta)
92
93     def check_close_boxes(self, a, b, delta, angle_delta):
94         self.check_close_pairs(a[0], b[0], delta)
95         self.check_close_pairs(a[1], b[1], delta)
96         self.check_close_angles(a[2], b[2], angle_delta)
97
98     def test_geometry(self):
99         npt = 100
100         np.random.seed(244)
101         a = np.random.randn(npt,2).astype('float32')*50 + 150
102
103         img = np.zeros((300, 300, 3), dtype='uint8')
104         be = cv2.fitEllipse(a)
105         br = cv2.minAreaRect(a)
106         mc, mr = cv2.minEnclosingCircle(a)
107         
108         be0 = ((150.2511749267578, 150.77322387695312), (158.024658203125, 197.57696533203125), 37.57804489135742)
109         br0 = ((161.2974090576172, 154.41793823242188), (199.2301483154297, 207.7177734375), -9.164555549621582)
110         mc0, mr0 = (160.41790771484375, 144.55152893066406), 136.713500977
111         
112         self.check_close_boxes(be, be0, 5, 15)
113         self.check_close_boxes(br, br0, 5, 15)
114         self.check_close_pairs(mc, mc0, 5)
115         self.assertLessEqual(abs(mr - mr0), 5)
116
117 if __name__ == '__main__':
118     print "testing", cv2.__version__
119     random.seed(0)
120     unittest.main()