Skip to content

Commit b675235

Browse files
Pandinosaurusvpisarev
authored andcommitted
dnn : Added an imagesFromBlob method to the dnn module (opencv#10607)
* Added the imagesFromBlob method to the dnn module. * Rewritten imagesFromBlob based on first dkurt comments * Updated code with getPlane() * Modify comment of imagesFromBlob() in dnn module * modified comments, removed useless assertions & added OutputArrayOfArray * replaced tabs with whitespaces & put vectorOfChannels instantiation outside the loop * Changed pre-commit.sample to pre-commit in .git/hooks/ * Added a test for imagesFromBlob in test_misc.cpp (dnn) * Changed nbOfImages, robustified test with cv::randu, modified assertion
1 parent 5a791e6 commit b675235

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

modules/dnn/include/opencv2/dnn/dnn.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,15 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
735735
double scalefactor=1.0, Size size = Size(),
736736
const Scalar& mean = Scalar(), bool swapRB=true, bool crop=true);
737737

738+
/** @brief Parse a 4D blob and output the images it contains as 2D arrays through a simpler data structure
739+
* (std::vector<cv::Mat>).
740+
* @param[in] blob_ 4 dimensional array (images, channels, height, width) in floating point precision (CV_32F) from
741+
* which you would like to extract the images.
742+
* @param[out] images_ array of 2D Mat containing the images extracted from the blob in floating point precision
743+
* (CV_32F). They are non normalized neither mean added. The number of returned images equals the first dimension
744+
* of the blob (batch size). Every image has a number of channels equals to the second dimension of the blob (depth).
745+
*/
746+
CV_EXPORTS_W void imagesFromBlob(const cv::Mat& blob_, OutputArrayOfArrays images_);
738747

739748
/** @brief Convert all weights of Caffe network to half precision floating point.
740749
* @param src Path to origin model from Caffe framework contains single

modules/dnn/src/dnn.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,31 @@ void blobFromImages(InputArrayOfArrays images_, OutputArray blob_, double scalef
192192
}
193193
}
194194

195+
void imagesFromBlob(const cv::Mat& blob_, OutputArrayOfArrays images_)
196+
{
197+
CV_TRACE_FUNCTION();
198+
199+
//A blob is a 4 dimensional matrix in floating point precision
200+
//blob_[0] = batchSize = nbOfImages
201+
//blob_[1] = nbOfChannels
202+
//blob_[2] = height
203+
//blob_[3] = width
204+
CV_Assert(blob_.depth() == CV_32F);
205+
CV_Assert(blob_.dims == 4);
206+
207+
images_.create(cv::Size(1, blob_.size[0]), blob_.depth());
208+
209+
std::vector<Mat> vectorOfChannels(blob_.size[1]);
210+
for (int n = 0; n < blob_.size[0]; ++n)
211+
{
212+
for (int c = 0; c < blob_.size[1]; ++c)
213+
{
214+
vectorOfChannels[c] = getPlane(blob_, n, c);
215+
}
216+
cv::merge(vectorOfChannels, images_.getMatRef(n));
217+
}
218+
}
219+
195220
class OpenCLBackendWrapper : public BackendWrapper
196221
{
197222
public:

modules/dnn/test/test_misc.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,25 @@ TEST(blobFromImage, allocated)
3636
ASSERT_EQ(blobData, blob.data);
3737
}
3838

39+
TEST(imagesFromBlob, Regression)
40+
{
41+
int nbOfImages = 8;
42+
43+
std::vector<cv::Mat> inputImgs(nbOfImages);
44+
for (int i = 0; i < nbOfImages; i++)
45+
{
46+
inputImgs[i] = cv::Mat::ones(100, 100, CV_32FC3);
47+
cv::randu(inputImgs[i], cv::Scalar::all(0), cv::Scalar::all(1));
48+
}
49+
50+
cv::Mat blob = cv::dnn::blobFromImages(inputImgs, 1., cv::Size(), cv::Scalar(), false, false);
51+
std::vector<cv::Mat> outputImgs;
52+
cv::dnn::imagesFromBlob(blob, outputImgs);
53+
54+
for (int i = 0; i < nbOfImages; i++)
55+
{
56+
ASSERT_EQ(cv::countNonZero(inputImgs[i] != outputImgs[i]), 0);
57+
}
58+
}
59+
3960
}} // namespace

0 commit comments

Comments
 (0)