test_cudabgsegm.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python
  2. import os
  3. import cv2 as cv
  4. import numpy as np
  5. from tests_common import NewOpenCVTests, unittest
  6. class cudabgsegm_test(NewOpenCVTests):
  7. def setUp(self):
  8. super(cudabgsegm_test, self).setUp()
  9. if not cv.cuda.getCudaEnabledDeviceCount():
  10. self.skipTest("No CUDA-capable device is detected")
  11. def test_cudabgsegm(self):
  12. lr = 0.05
  13. sz = (128,128,1)
  14. npMat = (np.random.random(sz) * 255).astype(np.uint8)
  15. cuMat = cv.cuda_GpuMat(npMat)
  16. cuMatBg = cv.cuda_GpuMat(cuMat.size(),cuMat.type())
  17. cuMatFg = cv.cuda_GpuMat(cuMat.size(),cuMat.type())
  18. mog = cv.cuda.createBackgroundSubtractorMOG()
  19. mog.apply(cuMat, lr, cv.cuda.Stream_Null(), cuMatFg)
  20. mog.getBackgroundImage(cv.cuda.Stream_Null(),cuMatBg)
  21. self.assertTrue(sz[:2] == cuMatFg.size() == cuMatBg.size())
  22. self.assertTrue(sz[2] == cuMatFg.channels() == cuMatBg.channels())
  23. self.assertTrue(cv.CV_8UC1 == cuMatFg.type() == cuMatBg.type())
  24. mog = cv.cuda.createBackgroundSubtractorMOG()
  25. self.assertTrue(np.allclose(cuMatFg.download(),mog.apply(cuMat, lr, cv.cuda.Stream_Null()).download()))
  26. self.assertTrue(np.allclose(cuMatBg.download(),mog.getBackgroundImage(cv.cuda.Stream_Null()).download()))
  27. mog2 = cv.cuda.createBackgroundSubtractorMOG2()
  28. mog2.apply(cuMat, lr, cv.cuda.Stream_Null(), cuMatFg)
  29. mog2.getBackgroundImage(cv.cuda.Stream_Null(),cuMatBg)
  30. self.assertTrue(sz[:2] == cuMatFg.size() == cuMatBg.size())
  31. self.assertTrue(sz[2] == cuMatFg.channels() == cuMatBg.channels())
  32. self.assertTrue(cv.CV_8UC1 == cuMatFg.type() == cuMatBg.type())
  33. mog2 = cv.cuda.createBackgroundSubtractorMOG2()
  34. self.assertTrue(np.allclose(cuMatFg.download(),mog2.apply(cuMat, lr, cv.cuda.Stream_Null()).download()))
  35. self.assertTrue(np.allclose(cuMatBg.download(),mog2.getBackgroundImage(cv.cuda.Stream_Null()).download()))
  36. if __name__ == '__main__':
  37. NewOpenCVTests.bootstrap()