1
+ #!/usr/bin/env python
2
+ import os
3
+ import cv2 as cv
4
+ import numpy as np
5
+
6
+ from tests_common import NewOpenCVTests , unittest
7
+
8
+ class cudaarithm_test (NewOpenCVTests ):
9
+ def setUp (self ):
10
+ super (cudaarithm_test , self ).setUp ()
11
+ if not cv .cuda .getCudaEnabledDeviceCount ():
12
+ self .skipTest ("No CUDA-capable device is detected" )
13
+
14
+ def test_cudaarithm (self ):
15
+ npMat = (np .random .random ((128 , 128 , 3 )) * 255 ).astype (np .uint8 )
16
+
17
+ cuMat = cv .cuda_GpuMat (npMat )
18
+ cuMatDst = cv .cuda_GpuMat (cuMat .size (),cuMat .type ())
19
+ cuMatB = cv .cuda_GpuMat (cuMat .size (),cv .CV_8UC1 )
20
+ cuMatG = cv .cuda_GpuMat (cuMat .size (),cv .CV_8UC1 )
21
+ cuMatR = cv .cuda_GpuMat (cuMat .size (),cv .CV_8UC1 )
22
+
23
+ self .assertTrue (np .allclose (cv .cuda .merge (cv .cuda .split (cuMat )),npMat ))
24
+
25
+ cv .cuda .split (cuMat ,[cuMatB ,cuMatG ,cuMatR ])
26
+ cv .cuda .merge ([cuMatB ,cuMatG ,cuMatR ],cuMatDst )
27
+ self .assertTrue (np .allclose (cuMatDst .download (),npMat ))
28
+
29
+ shift = (np .random .random ((cuMat .channels (),)) * 8 ).astype (np .uint8 ).tolist ()
30
+ self .assertTrue (np .allclose (cv .cuda .rshift (cuMat ,shift ).download (),npMat >> shift ))
31
+ cv .cuda .rshift (cuMat ,shift ,cuMatDst )
32
+ self .assertTrue (np .allclose (cuMatDst .download (),npMat >> shift ))
33
+
34
+ self .assertTrue (np .allclose (cv .cuda .lshift (cuMat ,shift ).download (),(npMat << shift ).astype ('uint8' )))
35
+ cv .cuda .lshift (cuMat ,shift ,cuMatDst )
36
+ self .assertTrue (np .allclose (cuMatDst .download (),(npMat << shift ).astype ('uint8' )))
37
+
38
+ def test_arithmetic (self ):
39
+ npMat1 = np .random .random ((128 , 128 , 3 )) - 0.5
40
+ npMat2 = np .random .random ((128 , 128 , 3 )) - 0.5
41
+
42
+ cuMat1 = cv .cuda_GpuMat ()
43
+ cuMat2 = cv .cuda_GpuMat ()
44
+ cuMat1 .upload (npMat1 )
45
+ cuMat2 .upload (npMat2 )
46
+ cuMatDst = cv .cuda_GpuMat (cuMat1 .size (),cuMat1 .type ())
47
+
48
+ self .assertTrue (np .allclose (cv .cuda .add (cuMat1 , cuMat2 ).download (),
49
+ cv .add (npMat1 , npMat2 )))
50
+
51
+ cv .cuda .add (cuMat1 , cuMat2 , cuMatDst )
52
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .add (npMat1 , npMat2 )))
53
+
54
+ self .assertTrue (np .allclose (cv .cuda .subtract (cuMat1 , cuMat2 ).download (),
55
+ cv .subtract (npMat1 , npMat2 )))
56
+
57
+ cv .cuda .subtract (cuMat1 , cuMat2 , cuMatDst )
58
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .subtract (npMat1 , npMat2 )))
59
+
60
+ self .assertTrue (np .allclose (cv .cuda .multiply (cuMat1 , cuMat2 ).download (),
61
+ cv .multiply (npMat1 , npMat2 )))
62
+
63
+ cv .cuda .multiply (cuMat1 , cuMat2 , cuMatDst )
64
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .multiply (npMat1 , npMat2 )))
65
+
66
+ self .assertTrue (np .allclose (cv .cuda .divide (cuMat1 , cuMat2 ).download (),
67
+ cv .divide (npMat1 , npMat2 )))
68
+
69
+ cv .cuda .divide (cuMat1 , cuMat2 , cuMatDst )
70
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .divide (npMat1 , npMat2 )))
71
+
72
+ self .assertTrue (np .allclose (cv .cuda .absdiff (cuMat1 , cuMat2 ).download (),
73
+ cv .absdiff (npMat1 , npMat2 )))
74
+
75
+ cv .cuda .absdiff (cuMat1 , cuMat2 , cuMatDst )
76
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .absdiff (npMat1 , npMat2 )))
77
+
78
+ self .assertTrue (np .allclose (cv .cuda .compare (cuMat1 , cuMat2 , cv .CMP_GE ).download (),
79
+ cv .compare (npMat1 , npMat2 , cv .CMP_GE )))
80
+
81
+ cuMatDst1 = cv .cuda_GpuMat (cuMat1 .size (),cv .CV_8UC3 )
82
+ cv .cuda .compare (cuMat1 , cuMat2 , cv .CMP_GE , cuMatDst1 )
83
+ self .assertTrue (np .allclose (cuMatDst1 .download (),cv .compare (npMat1 , npMat2 , cv .CMP_GE )))
84
+
85
+ self .assertTrue (np .allclose (cv .cuda .abs (cuMat1 ).download (),
86
+ np .abs (npMat1 )))
87
+
88
+ cv .cuda .abs (cuMat1 , cuMatDst )
89
+ self .assertTrue (np .allclose (cuMatDst .download (),np .abs (npMat1 )))
90
+
91
+ self .assertTrue (np .allclose (cv .cuda .sqrt (cv .cuda .sqr (cuMat1 )).download (),
92
+ cv .cuda .abs (cuMat1 ).download ()))
93
+
94
+ cv .cuda .sqr (cuMat1 , cuMatDst )
95
+ cv .cuda .sqrt (cuMatDst , cuMatDst )
96
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .cuda .abs (cuMat1 ).download ()))
97
+
98
+ self .assertTrue (np .allclose (cv .cuda .log (cv .cuda .exp (cuMat1 )).download (),
99
+ npMat1 ))
100
+
101
+ cv .cuda .exp (cuMat1 , cuMatDst )
102
+ cv .cuda .log (cuMatDst , cuMatDst )
103
+ self .assertTrue (np .allclose (cuMatDst .download (),npMat1 ))
104
+
105
+ self .assertTrue (np .allclose (cv .cuda .pow (cuMat1 , 2 ).download (),
106
+ cv .pow (npMat1 , 2 )))
107
+
108
+ cv .cuda .pow (cuMat1 , 2 , cuMatDst )
109
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .pow (npMat1 , 2 )))
110
+
111
+ def test_logical (self ):
112
+ npMat1 = (np .random .random ((128 , 128 )) * 255 ).astype (np .uint8 )
113
+ npMat2 = (np .random .random ((128 , 128 )) * 255 ).astype (np .uint8 )
114
+
115
+ cuMat1 = cv .cuda_GpuMat ()
116
+ cuMat2 = cv .cuda_GpuMat ()
117
+ cuMat1 .upload (npMat1 )
118
+ cuMat2 .upload (npMat2 )
119
+ cuMatDst = cv .cuda_GpuMat (cuMat1 .size (),cuMat1 .type ())
120
+
121
+ self .assertTrue (np .allclose (cv .cuda .bitwise_or (cuMat1 , cuMat2 ).download (),
122
+ cv .bitwise_or (npMat1 , npMat2 )))
123
+
124
+ cv .cuda .bitwise_or (cuMat1 , cuMat2 , cuMatDst )
125
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .bitwise_or (npMat1 , npMat2 )))
126
+
127
+ self .assertTrue (np .allclose (cv .cuda .bitwise_and (cuMat1 , cuMat2 ).download (),
128
+ cv .bitwise_and (npMat1 , npMat2 )))
129
+
130
+ cv .cuda .bitwise_and (cuMat1 , cuMat2 , cuMatDst )
131
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .bitwise_and (npMat1 , npMat2 )))
132
+
133
+ self .assertTrue (np .allclose (cv .cuda .bitwise_xor (cuMat1 , cuMat2 ).download (),
134
+ cv .bitwise_xor (npMat1 , npMat2 )))
135
+
136
+ cv .cuda .bitwise_xor (cuMat1 , cuMat2 , cuMatDst )
137
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .bitwise_xor (npMat1 , npMat2 )))
138
+
139
+ self .assertTrue (np .allclose (cv .cuda .bitwise_not (cuMat1 ).download (),
140
+ cv .bitwise_not (npMat1 )))
141
+
142
+ cv .cuda .bitwise_not (cuMat1 , cuMatDst )
143
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .bitwise_not (npMat1 )))
144
+
145
+ self .assertTrue (np .allclose (cv .cuda .min (cuMat1 , cuMat2 ).download (),
146
+ cv .min (npMat1 , npMat2 )))
147
+
148
+ cv .cuda .min (cuMat1 , cuMat2 , cuMatDst )
149
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .min (npMat1 , npMat2 )))
150
+
151
+ self .assertTrue (np .allclose (cv .cuda .max (cuMat1 , cuMat2 ).download (),
152
+ cv .max (npMat1 , npMat2 )))
153
+
154
+ cv .cuda .max (cuMat1 , cuMat2 , cuMatDst )
155
+ self .assertTrue (np .allclose (cuMatDst .download (),cv .max (npMat1 , npMat2 )))
156
+
157
+ def test_convolution (self ):
158
+ npMat = (np .random .random ((128 , 128 )) * 255 ).astype (np .float32 )
159
+ npDims = np .array (npMat .shape )
160
+ kernel = (np .random .random ((3 , 3 )) * 1 ).astype (np .float32 )
161
+ kernelDims = np .array (kernel .shape )
162
+ iS = (kernelDims / 2 ).astype (int )
163
+ iE = npDims - kernelDims + iS
164
+
165
+ cuMat = cv .cuda_GpuMat (npMat )
166
+ cuKernel = cv .cuda_GpuMat (kernel )
167
+ cuMatDst = cv .cuda_GpuMat (tuple (npDims - kernelDims + 1 ), cuMat .type ())
168
+ conv = cv .cuda .createConvolution ()
169
+
170
+ self .assertTrue (np .allclose (conv .convolve (cuMat ,cuKernel ,ccorr = True ).download (),
171
+ cv .filter2D (npMat ,- 1 ,kernel ,anchor = (- 1 ,- 1 ))[iS [0 ]:iE [0 ]+ 1 ,iS [1 ]:iE [1 ]+ 1 ]))
172
+
173
+ conv .convolve (cuMat ,cuKernel ,cuMatDst ,True )
174
+ self .assertTrue (np .allclose (cuMatDst .download (),
175
+ cv .filter2D (npMat ,- 1 ,kernel ,anchor = (- 1 ,- 1 ))[iS [0 ]:iE [0 ]+ 1 ,iS [1 ]:iE [1 ]+ 1 ]))
176
+
177
+ if __name__ == '__main__' :
178
+ NewOpenCVTests .bootstrap ()
0 commit comments