test_houghcircles.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/python
  2. '''
  3. This example illustrates how to use cv.HoughCircles() function.
  4. '''
  5. # Python 2/3 compatibility
  6. from __future__ import print_function
  7. import cv2 as cv
  8. import numpy as np
  9. import sys
  10. from numpy import pi, sin, cos
  11. from tests_common import NewOpenCVTests
  12. def circleApproximation(circle):
  13. nPoints = 30
  14. dPhi = 2*pi / nPoints
  15. contour = []
  16. for i in range(nPoints):
  17. contour.append(([circle[0] + circle[2]*cos(i*dPhi),
  18. circle[1] + circle[2]*sin(i*dPhi)]))
  19. return np.array(contour).astype(int)
  20. def convContoursIntersectiponRate(c1, c2):
  21. s1 = cv.contourArea(c1)
  22. s2 = cv.contourArea(c2)
  23. s, _ = cv.intersectConvexConvex(c1, c2)
  24. return 2*s/(s1+s2)
  25. class houghcircles_test(NewOpenCVTests):
  26. def test_houghcircles(self):
  27. fn = "samples/data/board.jpg"
  28. src = self.get_sample(fn, 1)
  29. img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
  30. img = cv.medianBlur(img, 5)
  31. circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30)[0]
  32. testCircles = [[38, 181, 17.6],
  33. [99.7, 166, 13.12],
  34. [142.7, 160, 13.52],
  35. [223.6, 110, 8.62],
  36. [79.1, 206.7, 8.62],
  37. [47.5, 351.6, 11.64],
  38. [189.5, 354.4, 11.64],
  39. [189.8, 298.9, 10.64],
  40. [189.5, 252.4, 14.62],
  41. [252.5, 393.4, 15.62],
  42. [602.9, 467.5, 11.42],
  43. [222, 210.4, 9.12],
  44. [263.1, 216.7, 9.12],
  45. [359.8, 222.6, 9.12],
  46. [518.9, 120.9, 9.12],
  47. [413.8, 113.4, 9.12],
  48. [489, 127.2, 9.12],
  49. [448.4, 121.3, 9.12],
  50. [384.6, 128.9, 8.62]]
  51. matches_counter = 0
  52. for i in range(len(testCircles)):
  53. for j in range(len(circles)):
  54. tstCircle = circleApproximation(testCircles[i])
  55. circle = circleApproximation(circles[j])
  56. if convContoursIntersectiponRate(tstCircle, circle) > 0.6:
  57. matches_counter += 1
  58. self.assertGreater(float(matches_counter) / len(testCircles), .5)
  59. self.assertLess(float(len(circles) - matches_counter) / len(circles), .75)
  60. def test_houghcircles_alt(self):
  61. fn = "samples/data/board.jpg"
  62. src = self.get_sample(fn, 1)
  63. img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
  64. img = cv.medianBlur(img, 5)
  65. circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT_ALT, 1, 10, np.array([]), 300, 0.9, 1, 30)
  66. self.assertEqual(circles.shape, (1, 18, 3))
  67. circles = circles[0]
  68. testCircles = [[38, 181, 17.6],
  69. [99.7, 166, 13.12],
  70. [142.7, 160, 13.52],
  71. [223.6, 110, 8.62],
  72. [79.1, 206.7, 8.62],
  73. [47.5, 351.6, 11.64],
  74. [189.5, 354.4, 11.64],
  75. [189.8, 298.9, 10.64],
  76. [189.5, 252.4, 14.62],
  77. [252.5, 393.4, 15.62],
  78. [602.9, 467.5, 11.42],
  79. [222, 210.4, 9.12],
  80. [263.1, 216.7, 9.12],
  81. [359.8, 222.6, 9.12],
  82. [518.9, 120.9, 9.12],
  83. [413.8, 113.4, 9.12],
  84. [489, 127.2, 9.12],
  85. [448.4, 121.3, 9.12],
  86. [384.6, 128.9, 8.62]]
  87. matches_counter = 0
  88. for i in range(len(testCircles)):
  89. for j in range(len(circles)):
  90. tstCircle = circleApproximation(testCircles[i])
  91. circle = circleApproximation(circles[j])
  92. if convContoursIntersectiponRate(tstCircle, circle) > 0.6:
  93. matches_counter += 1
  94. self.assertGreater(float(matches_counter) / len(testCircles), .5)
  95. self.assertLess(float(len(circles) - matches_counter) / len(circles), .75)
  96. if __name__ == '__main__':
  97. NewOpenCVTests.bootstrap()