Skip to content

Commit 78fabfe

Browse files
arrybnvpisarev
authored andcommitted
Changed blobFromImages() function, so it can handle 1-channel images (#1185)
1 parent 1ca1a12 commit 78fabfe

File tree

1 file changed

+50
-40
lines changed

1 file changed

+50
-40
lines changed

modules/dnn/src/dnn.cpp

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,8 @@ static String toString(const T &v)
7070

7171
Mat blobFromImage(const Mat& image_, double scalefactor, bool swapRB)
7272
{
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);
9275
}
9376

9477
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
9881
return Mat();
9982
Mat image0 = images[0];
10083
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)
10787
{
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++ )
11093
{
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);
112111
}
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+
}
125135
}
126136
return blob;
127137
}

0 commit comments

Comments
 (0)