test_composition.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import unittest
  2. import os
  3. import sys
  4. import numpy as np
  5. import cv2 as cv
  6. sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
  7. '..', '..')))
  8. from opencv_stitching.stitcher import Stitcher
  9. class TestImageComposition(unittest.TestCase):
  10. # visual test: look especially in the sky
  11. def test_exposure_compensation(self):
  12. img = cv.imread("s1.jpg")
  13. img = increase_brightness(img, value=25)
  14. cv.imwrite("s1_bright.jpg", img)
  15. stitcher = Stitcher(compensator="no", blender_type="no")
  16. result = stitcher.stitch(["s1_bright.jpg", "s2.jpg"])
  17. cv.imwrite("without_exposure_comp.jpg", result)
  18. stitcher = Stitcher(blender_type="no")
  19. result = stitcher.stitch(["s1_bright.jpg", "s2.jpg"])
  20. cv.imwrite("with_exposure_comp.jpg", result)
  21. def test_timelapse(self):
  22. stitcher = Stitcher(timelapse='as_is')
  23. _ = stitcher.stitch(["s1.jpg", "s2.jpg"])
  24. frame1 = cv.imread("fixed_s1.jpg")
  25. max_image_shape_derivation = 3
  26. np.testing.assert_allclose(frame1.shape[:2],
  27. (700, 1811),
  28. atol=max_image_shape_derivation)
  29. left = cv.cvtColor(frame1[:, :1300, ], cv.COLOR_BGR2GRAY)
  30. right = cv.cvtColor(frame1[:, 1300:, ], cv.COLOR_BGR2GRAY)
  31. self.assertGreater(cv.countNonZero(left), 800000)
  32. self.assertEqual(cv.countNonZero(right), 0)
  33. def increase_brightness(img, value=30):
  34. hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
  35. h, s, v = cv.split(hsv)
  36. lim = 255 - value
  37. v[v > lim] = 255
  38. v[v <= lim] += value
  39. final_hsv = cv.merge((h, s, v))
  40. img = cv.cvtColor(final_hsv, cv.COLOR_HSV2BGR)
  41. return img
  42. def starttest():
  43. unittest.main()
  44. if __name__ == "__main__":
  45. starttest()