test_cuda_xfeatures2d.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 xfeatures2d_test(NewOpenCVTests):
  7. def setUp(self):
  8. super(xfeatures2d_test, self).setUp()
  9. if not cv.cuda.getCudaEnabledDeviceCount():
  10. self.skipTest("No CUDA-capable device is detected")
  11. @unittest.skipIf('OPENCV_TEST_DATA_PATH' not in os.environ,
  12. "OPENCV_TEST_DATA_PATH is not defined")
  13. def test_surf(self):
  14. img_path = os.environ['OPENCV_TEST_DATA_PATH'] + "/gpu/features2d/aloe.png"
  15. hessianThreshold = 100
  16. nOctaves = 3
  17. nOctaveLayers = 2
  18. extended = False
  19. keypointsRatio = 0.05
  20. upright = False
  21. npMat = cv.cvtColor(cv.imread(img_path),cv.COLOR_BGR2GRAY)
  22. cuMat = cv.cuda_GpuMat(npMat)
  23. try:
  24. cuSurf = cv.cuda_SURF_CUDA.create(hessianThreshold,nOctaves,nOctaveLayers,extended,keypointsRatio,upright)
  25. surf = cv.xfeatures2d_SURF.create(hessianThreshold,nOctaves,nOctaveLayers,extended,upright)
  26. except cv.error as e:
  27. self.assertEqual(e.code, cv.Error.StsNotImplemented)
  28. self.skipTest("OPENCV_ENABLE_NONFREE is not enabled in this build.")
  29. cuKeypoints = cuSurf.detect(cuMat,cv.cuda_GpuMat())
  30. keypointsHost = cuSurf.downloadKeypoints(cuKeypoints)
  31. keypoints = surf.detect(npMat)
  32. self.assertTrue(len(keypointsHost) == len(keypoints))
  33. cuKeypoints, cuDescriptors = cuSurf.detectWithDescriptors(cuMat,cv.cuda_GpuMat(),cuKeypoints,useProvidedKeypoints=True)
  34. keypointsHost = cuSurf.downloadKeypoints(cuKeypoints)
  35. descriptorsHost = cuDescriptors.download()
  36. keypoints, descriptors = surf.compute(npMat,keypoints)
  37. self.assertTrue(len(keypointsHost) == len(keypoints) and descriptorsHost.shape == descriptors.shape)
  38. if __name__ == '__main__':
  39. NewOpenCVTests.bootstrap()