Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/python/test/test_cuda.py
16337 views
1
#!/usr/bin/env python
2
3
'''
4
CUDA-accelerated Computer Vision functions
5
'''
6
7
# Python 2/3 compatibility
8
from __future__ import print_function
9
10
import numpy as np
11
import cv2 as cv
12
13
from tests_common import NewOpenCVTests
14
15
class cuda_test(NewOpenCVTests):
16
def setUp(self):
17
super(cuda_test, self).setUp()
18
if not cv.cuda.getCudaEnabledDeviceCount():
19
self.skipTest("No CUDA-capable device is detected")
20
21
def test_cuda_upload_download(self):
22
npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
23
cuMat = cv.cuda_GpuMat()
24
cuMat.upload(npMat)
25
26
self.assertTrue(np.allclose(cuMat.download(), npMat))
27
28
def test_cudaarithm_arithmetic(self):
29
npMat1 = np.random.random((128, 128, 3)) - 0.5
30
npMat2 = np.random.random((128, 128, 3)) - 0.5
31
32
cuMat1 = cv.cuda_GpuMat()
33
cuMat2 = cv.cuda_GpuMat()
34
cuMat1.upload(npMat1)
35
cuMat2.upload(npMat2)
36
37
self.assertTrue(np.allclose(cv.cuda.add(cuMat1, cuMat2).download(),
38
cv.add(npMat1, npMat2)))
39
40
self.assertTrue(np.allclose(cv.cuda.subtract(cuMat1, cuMat2).download(),
41
cv.subtract(npMat1, npMat2)))
42
43
self.assertTrue(np.allclose(cv.cuda.multiply(cuMat1, cuMat2).download(),
44
cv.multiply(npMat1, npMat2)))
45
46
self.assertTrue(np.allclose(cv.cuda.divide(cuMat1, cuMat2).download(),
47
cv.divide(npMat1, npMat2)))
48
49
self.assertTrue(np.allclose(cv.cuda.absdiff(cuMat1, cuMat2).download(),
50
cv.absdiff(npMat1, npMat2)))
51
52
self.assertTrue(np.allclose(cv.cuda.compare(cuMat1, cuMat2, cv.CMP_GE).download(),
53
cv.compare(npMat1, npMat2, cv.CMP_GE)))
54
55
self.assertTrue(np.allclose(cv.cuda.abs(cuMat1).download(),
56
np.abs(npMat1)))
57
58
self.assertTrue(np.allclose(cv.cuda.sqrt(cv.cuda.sqr(cuMat1)).download(),
59
cv.cuda.abs(cuMat1).download()))
60
61
62
self.assertTrue(np.allclose(cv.cuda.log(cv.cuda.exp(cuMat1)).download(),
63
npMat1))
64
65
self.assertTrue(np.allclose(cv.cuda.pow(cuMat1, 2).download(),
66
cv.pow(npMat1, 2)))
67
68
def test_cudaarithm_logical(self):
69
npMat1 = (np.random.random((128, 128)) * 255).astype(np.uint8)
70
npMat2 = (np.random.random((128, 128)) * 255).astype(np.uint8)
71
72
cuMat1 = cv.cuda_GpuMat()
73
cuMat2 = cv.cuda_GpuMat()
74
cuMat1.upload(npMat1)
75
cuMat2.upload(npMat2)
76
77
self.assertTrue(np.allclose(cv.cuda.bitwise_or(cuMat1, cuMat2).download(),
78
cv.bitwise_or(npMat1, npMat2)))
79
80
self.assertTrue(np.allclose(cv.cuda.bitwise_and(cuMat1, cuMat2).download(),
81
cv.bitwise_and(npMat1, npMat2)))
82
83
self.assertTrue(np.allclose(cv.cuda.bitwise_xor(cuMat1, cuMat2).download(),
84
cv.bitwise_xor(npMat1, npMat2)))
85
86
self.assertTrue(np.allclose(cv.cuda.bitwise_not(cuMat1).download(),
87
cv.bitwise_not(npMat1)))
88
89
self.assertTrue(np.allclose(cv.cuda.min(cuMat1, cuMat2).download(),
90
cv.min(npMat1, npMat2)))
91
92
self.assertTrue(np.allclose(cv.cuda.max(cuMat1, cuMat2).download(),
93
cv.max(npMat1, npMat2)))
94
95
def test_cudabgsegm_existence(self):
96
#Test at least the existence of wrapped functions for now
97
98
bgsub = cv.cuda.createBackgroundSubtractorMOG()
99
bgsub = cv.cuda.createBackgroundSubtractorMOG2()
100
101
self.assertTrue(True) #It is sufficient that no exceptions have been there
102
103
def test_cudacodec_existence(self):
104
#Test at least the existence of wrapped functions for now
105
106
try:
107
writer = cv.cudacodec.createVideoWriter("tmp", (128, 128), 30)
108
reader = cv.cudacodec.createVideoReader("tmp")
109
except cv.error as e:
110
self.assertEqual(e.code, cv.Error.StsNotImplemented)
111
self.skipTest("NVCUVENC is not installed")
112
113
self.assertTrue(True) #It is sufficient that no exceptions have been there
114
115
def test_cudafeatures2d(self):
116
npMat1 = self.get_sample("samples/data/right01.jpg")
117
npMat2 = self.get_sample("samples/data/right02.jpg")
118
119
cuMat1 = cv.cuda_GpuMat()
120
cuMat2 = cv.cuda_GpuMat()
121
cuMat1.upload(npMat1)
122
cuMat2.upload(npMat2)
123
124
cuMat1 = cv.cuda.cvtColor(cuMat1, cv.COLOR_RGB2GRAY)
125
cuMat2 = cv.cuda.cvtColor(cuMat2, cv.COLOR_RGB2GRAY)
126
127
fast = cv.cuda_FastFeatureDetector.create()
128
kps = fast.detectAsync(cuMat1)
129
130
orb = cv.cuda_ORB.create()
131
kps1, descs1 = orb.detectAndComputeAsync(cuMat1, None)
132
kps2, descs2 = orb.detectAndComputeAsync(cuMat2, None)
133
134
bf = cv.cuda_DescriptorMatcher.createBFMatcher(cv.NORM_HAMMING)
135
matches = bf.match(descs1, descs2)
136
self.assertGreater(len(matches), 0)
137
matches = bf.knnMatch(descs1, descs2, 2)
138
self.assertGreater(len(matches), 0)
139
matches = bf.radiusMatch(descs1, descs2, 0.1)
140
self.assertGreater(len(matches), 0)
141
142
self.assertTrue(True) #It is sufficient that no exceptions have been there
143
144
def test_cudafilters_existence(self):
145
#Test at least the existence of wrapped functions for now
146
147
filter = cv.cuda.createBoxFilter(cv.CV_8UC1, -1, (3, 3))
148
filter = cv.cuda.createLinearFilter(cv.CV_8UC4, -1, np.eye(3))
149
filter = cv.cuda.createLaplacianFilter(cv.CV_16UC1, -1, ksize=3)
150
filter = cv.cuda.createSeparableLinearFilter(cv.CV_8UC1, -1, np.eye(3), np.eye(3))
151
filter = cv.cuda.createDerivFilter(cv.CV_8UC1, -1, 1, 1, 3)
152
filter = cv.cuda.createSobelFilter(cv.CV_8UC1, -1, 1, 1)
153
filter = cv.cuda.createScharrFilter(cv.CV_8UC1, -1, 1, 0)
154
filter = cv.cuda.createGaussianFilter(cv.CV_8UC1, -1, (3, 3), 16)
155
filter = cv.cuda.createMorphologyFilter(cv.MORPH_DILATE, cv.CV_32FC1, np.eye(3))
156
filter = cv.cuda.createBoxMaxFilter(cv.CV_8UC1, (3, 3))
157
filter = cv.cuda.createBoxMinFilter(cv.CV_8UC1, (3, 3))
158
filter = cv.cuda.createRowSumFilter(cv.CV_8UC1, cv.CV_32FC1, 3)
159
filter = cv.cuda.createColumnSumFilter(cv.CV_8UC1, cv.CV_32FC1, 3)
160
filter = cv.cuda.createMedianFilter(cv.CV_8UC1, 3)
161
162
self.assertTrue(True) #It is sufficient that no exceptions have been there
163
164
def test_cudafilters_laplacian(self):
165
npMat = (np.random.random((128, 128)) * 255).astype(np.uint16)
166
cuMat = cv.cuda_GpuMat()
167
cuMat.upload(npMat)
168
169
self.assertTrue(np.allclose(cv.cuda.createLaplacianFilter(cv.CV_16UC1, -1, ksize=3).apply(cuMat).download(),
170
cv.Laplacian(npMat, cv.CV_16UC1, ksize=3)))
171
172
def test_cudaimgproc(self):
173
npC1 = (np.random.random((128, 128)) * 255).astype(np.uint8)
174
npC3 = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
175
npC4 = (np.random.random((128, 128, 4)) * 255).astype(np.uint8)
176
cuC1 = cv.cuda_GpuMat()
177
cuC3 = cv.cuda_GpuMat()
178
cuC4 = cv.cuda_GpuMat()
179
cuC1.upload(npC1)
180
cuC3.upload(npC3)
181
cuC4.upload(npC4)
182
183
cv.cuda.cvtColor(cuC3, cv.COLOR_RGB2HSV)
184
cv.cuda.demosaicing(cuC1, cv.cuda.COLOR_BayerGR2BGR_MHT)
185
cv.cuda.gammaCorrection(cuC3)
186
cv.cuda.alphaComp(cuC4, cuC4, cv.cuda.ALPHA_XOR)
187
cv.cuda.calcHist(cuC1)
188
cv.cuda.equalizeHist(cuC1)
189
cv.cuda.evenLevels(3, 0, 255)
190
cv.cuda.meanShiftFiltering(cuC4, 10, 5)
191
cv.cuda.meanShiftProc(cuC4, 10, 5)
192
cv.cuda.bilateralFilter(cuC3, 3, 16, 3)
193
cv.cuda.blendLinear
194
195
cv.cuda.meanShiftSegmentation(cuC4, 10, 5, 5).download()
196
197
clahe = cv.cuda.createCLAHE()
198
clahe.apply(cuC1, cv.cuda_Stream.Null());
199
200
histLevels = cv.cuda.histEven(cuC3, 20, 0, 255)
201
cv.cuda.histRange(cuC1, histLevels)
202
203
detector = cv.cuda.createCannyEdgeDetector(0, 100)
204
detector.detect(cuC1)
205
206
detector = cv.cuda.createHoughLinesDetector(3, np.pi / 180, 20)
207
detector.detect(cuC1)
208
209
detector = cv.cuda.createHoughSegmentDetector(3, np.pi / 180, 20, 5)
210
detector.detect(cuC1)
211
212
detector = cv.cuda.createHoughCirclesDetector(3, 20, 10, 10, 20, 100)
213
detector.detect(cuC1)
214
215
detector = cv.cuda.createGeneralizedHoughBallard()
216
#BUG: detect accept only Mat!
217
#Even if generate_gpumat_decls is set to True, it only wraps overload CUDA functions.
218
#The problem is that Mat and GpuMat are not fully compatible to enable system-wide overloading
219
#detector.detect(cuC1, cuC1, cuC1)
220
221
detector = cv.cuda.createGeneralizedHoughGuil()
222
#BUG: same as above..
223
#detector.detect(cuC1, cuC1, cuC1)
224
225
detector = cv.cuda.createHarrisCorner(cv.CV_8UC1, 15, 5, 1)
226
detector.compute(cuC1)
227
228
detector = cv.cuda.createMinEigenValCorner(cv.CV_8UC1, 15, 5, 1)
229
detector.compute(cuC1)
230
231
detector = cv.cuda.createGoodFeaturesToTrackDetector(cv.CV_8UC1)
232
detector.detect(cuC1)
233
234
matcher = cv.cuda.createTemplateMatching(cv.CV_8UC1, cv.TM_CCOEFF_NORMED)
235
matcher.match(cuC3, cuC3)
236
237
self.assertTrue(True) #It is sufficient that no exceptions have been there
238
239
def test_cudaimgproc_cvtColor(self):
240
npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
241
cuMat = cv.cuda_GpuMat()
242
cuMat.upload(npMat)
243
244
self.assertTrue(np.allclose(cv.cuda.cvtColor(cuMat, cv.COLOR_BGR2HSV).download(),
245
cv.cvtColor(npMat, cv.COLOR_BGR2HSV)))
246
247
if __name__ == '__main__':
248
NewOpenCVTests.bootstrap()
249
250