test_lk_homography.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. '''
  3. Lucas-Kanade homography tracker test
  4. ===============================
  5. Uses goodFeaturesToTrack for track initialization and back-tracking for match verification
  6. between frames. Finds homography between reference and current views.
  7. '''
  8. # Python 2/3 compatibility
  9. from __future__ import print_function
  10. import numpy as np
  11. import cv2 as cv
  12. #local modules
  13. from tst_scene_render import TestSceneRender
  14. from tests_common import NewOpenCVTests, isPointInRect
  15. lk_params = dict( winSize = (19, 19),
  16. maxLevel = 2,
  17. criteria = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 0.03))
  18. feature_params = dict( maxCorners = 1000,
  19. qualityLevel = 0.01,
  20. minDistance = 8,
  21. blockSize = 19 )
  22. def checkedTrace(img0, img1, p0, back_threshold = 1.0):
  23. p1, _st, _err = cv.calcOpticalFlowPyrLK(img0, img1, p0, None, **lk_params)
  24. p0r, _st, _err = cv.calcOpticalFlowPyrLK(img1, img0, p1, None, **lk_params)
  25. d = abs(p0-p0r).reshape(-1, 2).max(-1)
  26. status = d < back_threshold
  27. return p1, status
  28. class lk_homography_test(NewOpenCVTests):
  29. render = None
  30. framesCounter = 0
  31. frame = frame0 = None
  32. p0 = None
  33. p1 = None
  34. gray0 = gray1 = None
  35. numFeaturesInRectOnStart = 0
  36. def test_lk_homography(self):
  37. self.render = TestSceneRender(self.get_sample('samples/data/graf1.png'),
  38. self.get_sample('samples/data/box.png'), noise = 0.1, speed = 1.0)
  39. frame = self.render.getNextFrame()
  40. frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
  41. self.frame0 = frame.copy()
  42. self.p0 = cv.goodFeaturesToTrack(frame_gray, **feature_params)
  43. isForegroundHomographyFound = False
  44. if self.p0 is not None:
  45. self.p1 = self.p0
  46. self.gray0 = frame_gray
  47. self.gray1 = frame_gray
  48. currRect = self.render.getCurrentRect()
  49. for (x,y) in self.p0[:,0]:
  50. if isPointInRect((x,y), currRect):
  51. self.numFeaturesInRectOnStart += 1
  52. while self.framesCounter < 200:
  53. self.framesCounter += 1
  54. frame = self.render.getNextFrame()
  55. frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
  56. if self.p0 is not None:
  57. p2, trace_status = checkedTrace(self.gray1, frame_gray, self.p1)
  58. self.p1 = p2[trace_status].copy()
  59. self.p0 = self.p0[trace_status].copy()
  60. self.gray1 = frame_gray
  61. if len(self.p0) < 4:
  62. self.p0 = None
  63. continue
  64. _H, status = cv.findHomography(self.p0, self.p1, cv.RANSAC, 5.0)
  65. goodPointsInRect = 0
  66. goodPointsOutsideRect = 0
  67. for (_x0, _y0), (x1, y1), good in zip(self.p0[:,0], self.p1[:,0], status[:,0]):
  68. if good:
  69. if isPointInRect((x1,y1), self.render.getCurrentRect()):
  70. goodPointsInRect += 1
  71. else: goodPointsOutsideRect += 1
  72. if goodPointsOutsideRect < goodPointsInRect:
  73. isForegroundHomographyFound = True
  74. self.assertGreater(float(goodPointsInRect) / (self.numFeaturesInRectOnStart + 1), 0.6)
  75. else:
  76. self.p0 = cv.goodFeaturesToTrack(frame_gray, **feature_params)
  77. self.assertEqual(isForegroundHomographyFound, True)
  78. if __name__ == '__main__':
  79. NewOpenCVTests.bootstrap()