test_legacy.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import numpy as np
  4. import cv2 as cv
  5. from tests_common import NewOpenCVTests
  6. class Hackathon244Tests(NewOpenCVTests):
  7. def test_int_array(self):
  8. a = np.array([-1, 2, -3, 4, -5])
  9. absa0 = np.abs(a)
  10. self.assertTrue(cv.norm(a, cv.NORM_L1) == 15)
  11. absa1 = cv.absdiff(a, 0)
  12. self.assertEqual(cv.norm(absa1, absa0, cv.NORM_INF), 0)
  13. def test_imencode(self):
  14. a = np.zeros((480, 640), dtype=np.uint8)
  15. flag, ajpg = cv.imencode("img_q90.jpg", a, [cv.IMWRITE_JPEG_QUALITY, 90])
  16. self.assertEqual(flag, True)
  17. self.assertEqual(ajpg.dtype, np.uint8)
  18. self.assertTrue(isinstance(ajpg, np.ndarray), "imencode returned buffer of wrong type: {}".format(type(ajpg)))
  19. self.assertEqual(len(ajpg.shape), 1, "imencode returned buffer with wrong shape: {}".format(ajpg.shape))
  20. self.assertGreaterEqual(len(ajpg), 1, "imencode length of the returned buffer should be at least 1")
  21. self.assertLessEqual(
  22. len(ajpg), a.size,
  23. "imencode length of the returned buffer shouldn't exceed number of elements in original image"
  24. )
  25. def test_projectPoints(self):
  26. objpt = np.float64([[1,2,3]])
  27. imgpt0, jac0 = cv.projectPoints(objpt, np.zeros(3), np.zeros(3), np.eye(3), np.float64([]))
  28. imgpt1, jac1 = cv.projectPoints(objpt, np.zeros(3), np.zeros(3), np.eye(3), None)
  29. self.assertEqual(imgpt0.shape, (objpt.shape[0], 1, 2))
  30. self.assertEqual(imgpt1.shape, imgpt0.shape)
  31. self.assertEqual(jac0.shape, jac1.shape)
  32. self.assertEqual(jac0.shape[0], 2*objpt.shape[0])
  33. def test_estimateAffine3D(self):
  34. pattern_size = (11, 8)
  35. pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)
  36. pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
  37. pattern_points *= 10
  38. (retval, out, inliers) = cv.estimateAffine3D(pattern_points, pattern_points)
  39. self.assertEqual(retval, 1)
  40. if cv.norm(out[2,:]) < 1e-3:
  41. out[2,2]=1
  42. self.assertLess(cv.norm(out, np.float64([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])), 1e-3)
  43. self.assertEqual(cv.countNonZero(inliers), pattern_size[0]*pattern_size[1])
  44. def test_fast(self):
  45. fd = cv.FastFeatureDetector_create(30, True)
  46. img = self.get_sample("samples/data/right02.jpg", 0)
  47. img = cv.medianBlur(img, 3)
  48. keypoints = fd.detect(img)
  49. self.assertTrue(600 <= len(keypoints) <= 700)
  50. for kpt in keypoints:
  51. self.assertNotEqual(kpt.response, 0)
  52. def check_close_angles(self, a, b, angle_delta):
  53. self.assertTrue(abs(a - b) <= angle_delta or
  54. abs(360 - abs(a - b)) <= angle_delta)
  55. def check_close_pairs(self, a, b, delta):
  56. self.assertLessEqual(abs(a[0] - b[0]), delta)
  57. self.assertLessEqual(abs(a[1] - b[1]), delta)
  58. def check_close_boxes(self, a, b, delta, angle_delta):
  59. self.check_close_pairs(a[0], b[0], delta)
  60. self.check_close_pairs(a[1], b[1], delta)
  61. self.check_close_angles(a[2], b[2], angle_delta)
  62. def test_geometry(self):
  63. npt = 100
  64. np.random.seed(244)
  65. a = np.random.randn(npt,2).astype('float32')*50 + 150
  66. be = cv.fitEllipse(a)
  67. br = cv.minAreaRect(a)
  68. mc, mr = cv.minEnclosingCircle(a)
  69. be0 = ((150.2511749267578, 150.77322387695312), (158.024658203125, 197.57696533203125), 37.57804489135742)
  70. br0 = ((161.2974090576172, 154.41793823242188), (207.7177734375, 199.2301483154297), 80.83544921875)
  71. mc0, mr0 = (160.41790771484375, 144.55152893066406), 136.713500977
  72. self.check_close_boxes(be, be0, 5, 15)
  73. self.check_close_boxes(br, br0, 5, 15)
  74. self.check_close_pairs(mc, mc0, 5)
  75. self.assertLessEqual(abs(mr - mr0), 5)
  76. if __name__ == '__main__':
  77. NewOpenCVTests.bootstrap()