@@ -70,25 +70,8 @@ static String toString(const T &v)
70
70
71
71
Mat blobFromImage (const Mat& image_, double scalefactor, bool swapRB)
72
72
{
73
- Mat image;
74
- if (image_.depth () == CV_8U)
75
- {
76
- image_.convertTo (image, CV_32F, scalefactor);
77
- }
78
- else
79
- image = image_;
80
- CV_Assert (image.dims == 2 && image.depth () == CV_32F);
81
- int nch = image.channels ();
82
- CV_Assert (nch == 3 || nch == 4 );
83
- int sz[] = { 1 , 3 , image.rows , image.cols };
84
- Mat blob (4 , sz, CV_32F);
85
- Mat ch[4 ];
86
- for ( int j = 0 ; j < 3 ; j++ )
87
- ch[j] = Mat (image.rows , image.cols , CV_32F, blob.ptr (0 , j));
88
- if (swapRB)
89
- std::swap (ch[0 ], ch[2 ]);
90
- split (image, ch);
91
- return blob;
73
+ std::vector<Mat> images (1 , image_);
74
+ return blobFromImages (images, scalefactor, swapRB);
92
75
}
93
76
94
77
Mat blobFromImages (const std::vector<Mat>& images, double scalefactor, bool swapRB)
@@ -98,30 +81,57 @@ Mat blobFromImages(const std::vector<Mat>& images, double scalefactor, bool swap
98
81
return Mat ();
99
82
Mat image0 = images[0 ];
100
83
int nch = image0.channels ();
101
- CV_Assert (image0.dims == 2 && (nch == 3 || nch == 4 ));
102
- int sz[] = { (int )nimages, 3 , image0.rows , image0.cols };
103
- Mat blob (4 , sz, CV_32F), image;
104
- Mat ch[4 ];
105
-
106
- for ( i = 0 ; i < nimages; i++ )
84
+ CV_Assert (image0.dims == 2 );
85
+ Mat blob, image;
86
+ if (nch == 3 || nch == 4 )
107
87
{
108
- Mat image_ = images[i];
109
- if (image_.depth () == CV_8U)
88
+ int sz[] = { (int )nimages, 3 , image0.rows , image0.cols };
89
+ blob = Mat (4 , sz, CV_32F);
90
+ Mat ch[4 ];
91
+
92
+ for ( i = 0 ; i < nimages; i++ )
110
93
{
111
- image_.convertTo (image, CV_32F, scalefactor);
94
+ Mat image_ = images[i];
95
+ if (image_.depth () == CV_8U)
96
+ {
97
+ image_.convertTo (image, CV_32F, scalefactor);
98
+ }
99
+ else
100
+ image = image_;
101
+ CV_Assert (image.depth () == CV_32F);
102
+ nch = image.channels ();
103
+ CV_Assert (image.dims == 2 && (nch == 3 || nch == 4 ));
104
+ CV_Assert (image.size () == image0.size ());
105
+
106
+ for ( int j = 0 ; j < 3 ; j++ )
107
+ ch[j] = Mat (image.rows , image.cols , CV_32F, blob.ptr ((int )i, j));
108
+ if (swapRB)
109
+ std::swap (ch[0 ], ch[2 ]);
110
+ split (image, ch);
112
111
}
113
- else
114
- image = image_;
115
- CV_Assert (image.depth () == CV_32F);
116
- nch = image.channels ();
117
- CV_Assert (image.dims == 2 && (nch == 3 || nch == 4 ));
118
- CV_Assert (image.size () == image0.size ());
119
-
120
- for ( int j = 0 ; j < 3 ; j++ )
121
- ch[j] = Mat (image.rows , image.cols , CV_32F, blob.ptr ((int )i, j));
122
- if (swapRB)
123
- std::swap (ch[0 ], ch[2 ]);
124
- split (image, ch);
112
+ }
113
+ else
114
+ {
115
+ CV_Assert (nch == 1 );
116
+ int sz[] = { (int )nimages, 1 , image0.rows , image0.cols };
117
+ blob = Mat (4 , sz, CV_32F);
118
+
119
+ for ( i = 0 ; i < nimages; i++ )
120
+ {
121
+ Mat image_ = images[i];
122
+ if (image_.depth () == CV_8U)
123
+ {
124
+ image_.convertTo (image, CV_32F, scalefactor);
125
+ }
126
+ else
127
+ image = image_;
128
+ CV_Assert (image.depth () == CV_32F);
129
+ nch = image.channels ();
130
+ CV_Assert (image.dims == 2 && (nch == 1 ));
131
+ CV_Assert (image.size () == image0.size ());
132
+
133
+ image.copyTo (Mat (image.rows , image.cols , CV_32F, blob.ptr ((int )i, 0 )));
134
+ }
125
135
}
126
136
return blob;
127
137
}
0 commit comments