test_cudawarping.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. def create_affine_transform_matrix(size,angle):
  7. return np.array([[np.cos(angle), -np.sin(angle), size[1]/2], [np.sin(angle), np.cos(angle), 0]])
  8. def create_perspective_transform_matrix(size,angle):
  9. return np.vstack([create_affine_transform_matrix(size,angle),[0, 0, 1]])
  10. class cudawarping_test(NewOpenCVTests):
  11. def setUp(self):
  12. super(cudawarping_test, self).setUp()
  13. if not cv.cuda.getCudaEnabledDeviceCount():
  14. self.skipTest("No CUDA-capable device is detected")
  15. def test_resize(self):
  16. dstSz = (256,256)
  17. interp = cv.INTER_NEAREST
  18. npMat = (np.random.random((128,128,3))*255).astype(np.uint8)
  19. cuMat = cv.cuda_GpuMat(npMat)
  20. cuMatDst = cv.cuda_GpuMat(dstSz,cuMat.type())
  21. self.assertTrue(np.allclose(cv.cuda.resize(cuMat,dstSz,interpolation=interp).download(),
  22. cv.resize(npMat,dstSz,interpolation=interp)))
  23. cv.cuda.resize(cuMat,dstSz,cuMatDst,interpolation=interp)
  24. self.assertTrue(np.allclose(cuMatDst.download(),cv.resize(npMat,dstSz,interpolation=interp)))
  25. def test_warp(self):
  26. npMat = (np.random.random((128,128,3))*255).astype(np.uint8)
  27. size = npMat.shape[:2]
  28. M1 = create_affine_transform_matrix(size,np.pi/2)
  29. cuMat = cv.cuda_GpuMat(npMat)
  30. cuMatDst = cv.cuda_GpuMat(size,cuMat.type())
  31. borderType = cv.BORDER_REFLECT101
  32. self.assertTrue(np.allclose(cv.cuda.warpAffine(cuMat,M1,size,borderMode=borderType).download(),
  33. cv.warpAffine(npMat,M1,size, borderMode=borderType)))
  34. cv.cuda.warpAffine(cuMat,M1,size,cuMatDst,borderMode=borderType)
  35. self.assertTrue(np.allclose(cuMatDst.download(),cv.warpAffine(npMat,M1,size,borderMode=borderType)))
  36. interpolation = cv.INTER_NEAREST
  37. flags = interpolation | cv.WARP_INVERSE_MAP
  38. dst_gold = cv.warpAffine(npMat, M1, size, flags = flags)
  39. cuMaps = cv.cuda.buildWarpAffineMaps(M1,True,size)
  40. dst = cv.remap(npMat, cuMaps[0].download(), cuMaps[1].download(),interpolation)
  41. self.assertTrue(np.allclose(dst,dst_gold))
  42. xmap = cv.cuda_GpuMat(size,cv.CV_32FC1)
  43. ymap = cv.cuda_GpuMat(size,cv.CV_32FC1)
  44. cv.cuda.buildWarpAffineMaps(M1,True,size,xmap,ymap)
  45. dst = cv.remap(npMat, xmap.download(), ymap.download(),interpolation)
  46. self.assertTrue(np.allclose(dst,dst_gold))
  47. M2 = create_perspective_transform_matrix(size,np.pi/2)
  48. np.allclose(cv.cuda.warpPerspective(cuMat,M2,size,borderMode=borderType).download(),
  49. cv.warpPerspective(npMat,M2,size,borderMode=borderType))
  50. cv.cuda.warpPerspective(cuMat,M2,size,cuMatDst,borderMode=borderType)
  51. self.assertTrue(np.allclose(cuMatDst.download(),cv.warpPerspective(npMat,M2,size,borderMode=borderType)))
  52. dst_gold = cv.warpPerspective(npMat, M2, size, flags = flags)
  53. cuMaps = cv.cuda.buildWarpPerspectiveMaps(M2,True,size)
  54. dst = cv.remap(npMat, cuMaps[0].download(), cuMaps[1].download(),interpolation)
  55. self.assertTrue(np.allclose(dst,dst_gold))
  56. cv.cuda.buildWarpPerspectiveMaps(M2,True,size,xmap,ymap)
  57. dst = cv.remap(npMat, xmap.download(), ymap.download(),interpolation)
  58. self.assertTrue(np.allclose(dst,dst_gold))
  59. if __name__ == '__main__':
  60. NewOpenCVTests.bootstrap()