fitline.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. '''
  3. Robust line fitting.
  4. ==================
  5. Example of using cv.fitLine function for fitting line
  6. to points in presence of outliers.
  7. Usage
  8. -----
  9. fitline.py
  10. Switch through different M-estimator functions and see,
  11. how well the robust functions fit the line even
  12. in case of ~50% of outliers.
  13. Keys
  14. ----
  15. SPACE - generate random points
  16. f - change distance function
  17. ESC - exit
  18. '''
  19. # Python 2/3 compatibility
  20. from __future__ import print_function
  21. import sys
  22. PY3 = sys.version_info[0] == 3
  23. import numpy as np
  24. import cv2 as cv
  25. # built-in modules
  26. import itertools as it
  27. # local modules
  28. from common import draw_str
  29. w, h = 512, 256
  30. def toint(p):
  31. return tuple(map(int, p))
  32. def sample_line(p1, p2, n, noise=0.0):
  33. p1 = np.float32(p1)
  34. t = np.random.rand(n,1)
  35. return p1 + (p2-p1)*t + np.random.normal(size=(n, 2))*noise
  36. dist_func_names = it.cycle('DIST_L2 DIST_L1 DIST_L12 DIST_FAIR DIST_WELSCH DIST_HUBER'.split())
  37. if PY3:
  38. cur_func_name = next(dist_func_names)
  39. else:
  40. cur_func_name = dist_func_names.next()
  41. def update(_=None):
  42. noise = cv.getTrackbarPos('noise', 'fit line')
  43. n = cv.getTrackbarPos('point n', 'fit line')
  44. r = cv.getTrackbarPos('outlier %', 'fit line') / 100.0
  45. outn = int(n*r)
  46. p0, p1 = (90, 80), (w-90, h-80)
  47. img = np.zeros((h, w, 3), np.uint8)
  48. cv.line(img, toint(p0), toint(p1), (0, 255, 0))
  49. if n > 0:
  50. line_points = sample_line(p0, p1, n-outn, noise)
  51. outliers = np.random.rand(outn, 2) * (w, h)
  52. points = np.vstack([line_points, outliers])
  53. for p in line_points:
  54. cv.circle(img, toint(p), 2, (255, 255, 255), -1)
  55. for p in outliers:
  56. cv.circle(img, toint(p), 2, (64, 64, 255), -1)
  57. func = getattr(cv, cur_func_name)
  58. vx, vy, cx, cy = cv.fitLine(np.float32(points), func, 0, 0.01, 0.01)
  59. cv.line(img, (int(cx-vx*w), int(cy-vy*w)), (int(cx+vx*w), int(cy+vy*w)), (0, 0, 255))
  60. draw_str(img, (20, 20), cur_func_name)
  61. cv.imshow('fit line', img)
  62. def main():
  63. cv.namedWindow('fit line')
  64. cv.createTrackbar('noise', 'fit line', 3, 50, update)
  65. cv.createTrackbar('point n', 'fit line', 100, 500, update)
  66. cv.createTrackbar('outlier %', 'fit line', 30, 100, update)
  67. while True:
  68. update()
  69. ch = cv.waitKey(0)
  70. if ch == ord('f'):
  71. global cur_func_name
  72. if PY3:
  73. cur_func_name = next(dist_func_names)
  74. else:
  75. cur_func_name = dist_func_names.next()
  76. if ch == 27:
  77. break
  78. print('Done')
  79. if __name__ == '__main__':
  80. print(__doc__)
  81. main()
  82. cv.destroyAllWindows()