@@ -22,6 +22,48 @@ Image2BlobParams::Image2BlobParams(const Scalar& scalefactor_, const Size& size_
22
22
datalayout(datalayout_), paddingmode(mode_)
23
23
{}
24
24
25
+ void getVector (InputArrayOfArrays images_, std::vector<Mat>& images) {
26
+ images_.getMatVector (images);
27
+ }
28
+
29
+ void getVector (InputArrayOfArrays images_, std::vector<UMat>& images) {
30
+ images_.getUMatVector (images);
31
+ }
32
+
33
+ void getMat (UMat& blob, InputArray blob_, AccessFlag flag) {
34
+ if (blob_.kind () == _InputArray::UMAT)
35
+ blob = blob_.getUMat ();
36
+ else if (blob_.kind () == _InputArray::MAT) {
37
+ blob = blob_.getUMat ();
38
+ }
39
+ }
40
+
41
+ void getMat (Mat& blob, InputArray blob_, AccessFlag flag) {
42
+ if (blob_.kind () == _InputArray::UMAT)
43
+ blob = blob_.getMat ();
44
+ else if (blob_.kind () == _InputArray::MAT) {
45
+ blob = blob_.getMat ();
46
+ }
47
+ }
48
+
49
+ void getChannelFromBlob (Mat& m, InputArray blob, int i, int j, int rows, int cols, int type) {
50
+ m = Mat (rows, cols, type, blob.getMat ().ptr (i, j));
51
+ }
52
+
53
+ void getChannelFromBlob (UMat& m, InputArray blob, int i, int j, int rows, int cols, int type) {
54
+ UMat ublob = blob.getUMat ();
55
+ int offset = (i * ublob.step .p [0 ] + j * ublob.step .p [1 ]) / ublob.elemSize ();
56
+ int length = 1 ;
57
+ for (int i = 0 ; i < ublob.dims ; ++i) {
58
+ length *= ublob.size [i];
59
+ }
60
+
61
+ const int newShape[1 ] { length };
62
+ UMat reshaped = ublob.reshape (1 , 1 , newShape);
63
+ UMat roi = reshaped (Rect (0 , offset, 1 , rows * cols));
64
+ m = roi.reshape (CV_MAT_CN (type), rows);
65
+ }
66
+
25
67
Mat blobFromImage (InputArray image, const double scalefactor, const Size& size,
26
68
const Scalar& mean, bool swapRB, bool crop, int ddepth)
27
69
{
@@ -35,8 +77,13 @@ void blobFromImage(InputArray image, OutputArray blob, double scalefactor,
35
77
const Size& size, const Scalar& mean, bool swapRB, bool crop, int ddepth)
36
78
{
37
79
CV_TRACE_FUNCTION ();
38
- std::vector<Mat> images (1 , image.getMat ());
39
- blobFromImages (images, blob, scalefactor, size, mean, swapRB, crop, ddepth);
80
+ if (image.kind () == _InputArray::UMAT) {
81
+ std::vector<UMat> images (1 , image.getUMat ());
82
+ blobFromImages (images, blob, scalefactor, size, mean, swapRB, crop, ddepth);
83
+ } else {
84
+ std::vector<Mat> images (1 , image.getMat ());
85
+ blobFromImages (images, blob, scalefactor, size, mean, swapRB, crop, ddepth);
86
+ }
40
87
}
41
88
42
89
Mat blobFromImages (InputArrayOfArrays images, double scalefactor, Size size,
@@ -52,9 +99,9 @@ void blobFromImages(InputArrayOfArrays images_, OutputArray blob_, double scalef
52
99
Size size, const Scalar& mean_, bool swapRB, bool crop, int ddepth)
53
100
{
54
101
CV_TRACE_FUNCTION ();
55
- if (images_.kind () != _InputArray::STD_VECTOR_MAT && images_.kind () != _InputArray::STD_ARRAY_MAT &&
102
+ if (images_.kind () != _InputArray::STD_VECTOR_UMAT && images_. kind () != _InputArray:: STD_VECTOR_MAT && images_.kind () != _InputArray::STD_ARRAY_MAT &&
56
103
images_.kind () != _InputArray::STD_VECTOR_VECTOR) {
57
- String error_message = " The data is expected as vectors of vectors or vectors of matrices ." ;
104
+ String error_message = " The data is expected as vectors of vectors, vectors of Mats or vectors of UMats ." ;
58
105
CV_Error (Error::StsBadArg, error_message);
59
106
}
60
107
Image2BlobParams param (Scalar::all (scalefactor), size, mean_, swapRB, ddepth);
@@ -71,13 +118,6 @@ Mat blobFromImageWithParams(InputArray image, const Image2BlobParams& param)
71
118
return blob;
72
119
}
73
120
74
- void blobFromImageWithParams (InputArray image, OutputArray blob, const Image2BlobParams& param)
75
- {
76
- CV_TRACE_FUNCTION ();
77
- std::vector<Mat> images (1 , image.getMat ());
78
- blobFromImagesWithParams (images, blob, param);
79
- }
80
-
81
121
Mat blobFromImagesWithParams (InputArrayOfArrays images, const Image2BlobParams& param)
82
122
{
83
123
CV_TRACE_FUNCTION ();
@@ -86,19 +126,22 @@ Mat blobFromImagesWithParams(InputArrayOfArrays images, const Image2BlobParams&
86
126
return blob;
87
127
}
88
128
89
- void blobFromImagesWithParams (InputArrayOfArrays images_, OutputArray blob_, const Image2BlobParams& param)
129
+ template <class Tmat >
130
+ void blobFromImagesWithParamsImpl (InputArrayOfArrays images_, Tmat& blob_, const Image2BlobParams& param)
90
131
{
91
132
CV_TRACE_FUNCTION ();
92
- if (images_.kind () != _InputArray::STD_VECTOR_MAT && images_.kind () != _InputArray::STD_ARRAY_MAT &&
93
- images_.kind () != _InputArray::STD_VECTOR_VECTOR) {
94
- String error_message = " The data is expected as vectors of vectors or vectors of matrices." ;
133
+ if (!std::is_same<Tmat, UMat>::value && !std::is_same<Tmat, Mat>::value) {
134
+ String error_message = " The template parameter is expected to be either a cv::Mat or a cv::UMat" ;
95
135
CV_Error (Error::StsBadArg, error_message);
96
136
}
137
+
97
138
CV_CheckType (param.ddepth , param.ddepth == CV_32F || param.ddepth == CV_8U,
98
139
" Blob depth should be CV_32F or CV_8U" );
99
140
Size size = param.size ;
100
- std::vector<Mat> images;
101
- images_.getMatVector (images);
141
+
142
+ std::vector<Tmat> images;
143
+ getVector (images_, images);
144
+
102
145
CV_Assert (!images.empty ());
103
146
104
147
if (param.ddepth == CV_8U)
@@ -164,12 +207,12 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
164
207
if (images[i].depth () == CV_8U && param.ddepth == CV_32F)
165
208
images[i].convertTo (images[i], CV_32F);
166
209
167
- images[i] -= mean;
210
+ subtract ( images[i], mean, images[i]) ;
168
211
multiply (images[i], scalefactor, images[i]);
169
212
}
170
213
171
214
size_t nimages = images.size ();
172
- Mat image0 = images[0 ];
215
+ Tmat image0 = images[0 ];
173
216
CV_Assert (image0.dims == 2 );
174
217
175
218
if (param.datalayout == DNN_LAYOUT_NCHW)
@@ -178,21 +221,22 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
178
221
{
179
222
int sz[] = { (int )nimages, nch, image0.rows , image0.cols };
180
223
blob_.create (4 , sz, param.ddepth );
181
- Mat blob = blob_.getMat ();
182
- Mat ch[4 ];
224
+ std::vector<Tmat> ch (4 );
183
225
184
226
for (size_t i = 0 ; i < nimages; i++)
185
227
{
186
- const Mat & image = images[i];
228
+ const Tmat & image = images[i];
187
229
CV_Assert (image.depth () == blob_.depth ());
188
230
nch = image.channels ();
189
231
CV_Assert (image.dims == 2 && (nch == 3 || nch == 4 ));
190
232
CV_Assert (image.size () == image0.size ());
191
233
192
- for (int j = 0 ; j < nch; j++)
193
- ch[j] = Mat (image.rows , image.cols , param.ddepth , blob.ptr ((int )i, j));
234
+ for (int j = 0 ; j < nch; j++) {
235
+ getChannelFromBlob (ch[j], blob_, i, j ,image.rows , image.cols , param.ddepth );
236
+ }
194
237
if (param.swapRB )
195
238
std::swap (ch[0 ], ch[2 ]);
239
+
196
240
split (image, ch);
197
241
}
198
242
}
@@ -201,11 +245,12 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
201
245
CV_Assert (nch == 1 );
202
246
int sz[] = { (int )nimages, 1 , image0.rows , image0.cols };
203
247
blob_.create (4 , sz, param.ddepth );
204
- Mat blob = blob_.getMat ();
248
+ Mat blob;
249
+ getMat (blob, blob_, ACCESS_RW);
205
250
206
251
for (size_t i = 0 ; i < nimages; i++)
207
252
{
208
- const Mat & image = images[i];
253
+ const Tmat & image = images[i];
209
254
CV_Assert (image.depth () == blob_.depth ());
210
255
nch = image.channels ();
211
256
CV_Assert (image.dims == 2 && (nch == 1 ));
@@ -219,11 +264,12 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
219
264
{
220
265
int sz[] = { (int )nimages, image0.rows , image0.cols , nch};
221
266
blob_.create (4 , sz, param.ddepth );
222
- Mat blob = blob_.getMat ();
267
+ Mat blob;
268
+ getMat (blob, blob_, ACCESS_RW);
223
269
int subMatType = CV_MAKETYPE (param.ddepth , nch);
224
270
for (size_t i = 0 ; i < nimages; i++)
225
271
{
226
- const Mat & image = images[i];
272
+ const Tmat & image = images[i];
227
273
CV_Assert (image.depth () == blob_.depth ());
228
274
CV_Assert (image.channels () == image0.channels ());
229
275
CV_Assert (image.size () == image0.size ());
@@ -243,6 +289,72 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
243
289
{
244
290
CV_Error (Error::StsUnsupportedFormat, " Unsupported data layout in blobFromImagesWithParams function." );
245
291
}
292
+ CV_Assert (blob_.total ());
293
+ }
294
+
295
+ void blobFromImagesWithParams (InputArrayOfArrays images, OutputArray blob, const Image2BlobParams& param) {
296
+ CV_TRACE_FUNCTION ();
297
+
298
+ if (images.kind () == _InputArray::STD_VECTOR_UMAT) {
299
+ if (blob.kind () == _InputArray::UMAT) {
300
+ UMat& u = blob.getUMatRef ();
301
+ blobFromImagesWithParamsImpl<cv::UMat>(images, u, param);
302
+ return ;
303
+ } else if (blob.kind () == _InputArray::MAT) {
304
+ UMat u = blob.getMatRef ().getUMat (ACCESS_WRITE);
305
+ blobFromImagesWithParamsImpl<cv::UMat>(images, u, param);
306
+ u.copyTo (blob);
307
+ return ;
308
+ }
309
+ } else if (images.kind () == _InputArray::STD_VECTOR_MAT) {
310
+ if (blob.kind () == _InputArray::UMAT) {
311
+ Mat m = blob.getUMatRef ().getMat (ACCESS_WRITE);
312
+ blobFromImagesWithParamsImpl<cv::Mat>(images, m, param);
313
+ m.copyTo (blob);
314
+ return ;
315
+ } else if (blob.kind () == _InputArray::MAT) {
316
+ Mat& m = blob.getMatRef ();
317
+ blobFromImagesWithParamsImpl<cv::Mat>(images, m, param);
318
+ return ;
319
+ }
320
+ }
321
+
322
+ CV_Error (Error::StsBadArg, " Images are expected to be a vector of either a Mat or UMat and Blob is expected to be either a Mat or UMat" );
323
+ }
324
+
325
+ void blobFromImageWithParams (InputArray image, OutputArray blob, const Image2BlobParams& param)
326
+ {
327
+ CV_TRACE_FUNCTION ();
328
+
329
+ if (image.kind () == _InputArray::UMAT) {
330
+ if (blob.kind () == _InputArray::UMAT) {
331
+ UMat& u = blob.getUMatRef ();
332
+ std::vector<UMat> images (1 , image.getUMat ());
333
+ blobFromImagesWithParamsImpl<cv::UMat>(images, u, param);
334
+ return ;
335
+ } else if (blob.kind () == _InputArray::MAT) {
336
+ UMat u = blob.getMatRef ().getUMat (ACCESS_RW);
337
+ std::vector<UMat> images (1 , image.getUMat ());
338
+ blobFromImagesWithParamsImpl<cv::UMat>(images, u, param);
339
+ u.copyTo (blob);
340
+ return ;
341
+ }
342
+ } else if (image.kind () == _InputArray::MAT) {
343
+ if (blob.kind () == _InputArray::UMAT) {
344
+ Mat m = blob.getUMatRef ().getMat (ACCESS_RW);
345
+ std::vector<Mat> images (1 , image.getMat ());
346
+ blobFromImagesWithParamsImpl<cv::Mat>(images, m, param);
347
+ m.copyTo (blob);
348
+ return ;
349
+ } else if (blob.kind () == _InputArray::MAT) {
350
+ Mat& m = blob.getMatRef ();
351
+ std::vector<Mat> images (1 , image.getMat ());
352
+ blobFromImagesWithParamsImpl<cv::Mat>(images, m, param);
353
+ return ;
354
+ }
355
+ }
356
+
357
+ CV_Error (Error::StsBadArg, " Image an Blob are expected to be either a Mat or UMat" );
246
358
}
247
359
248
360
void imagesFromBlob (const cv::Mat& blob_, OutputArrayOfArrays images_)
0 commit comments