dnn: handle 4-channel images in blobFromImage (#9944)
authorVladislav Sovrasov <sovrasov.vlad@gmail.com>
Fri, 27 Oct 2017 11:06:53 +0000 (14:06 +0300)
committerVadim Pisarevsky <vadim.pisarevsky@gmail.com>
Fri, 27 Oct 2017 11:06:53 +0000 (14:06 +0300)
modules/dnn/include/opencv2/dnn/dnn.hpp
modules/dnn/src/dnn.cpp
modules/dnn/test/test_misc.cpp [new file with mode: 0644]

index ff222d7..4d10d49 100644 (file)
@@ -688,7 +688,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
     CV_EXPORTS_W Mat readTorchBlob(const String &filename, bool isBinary = true);
     /** @brief Creates 4-dimensional blob from image. Optionally resizes and crops @p image from center,
      *  subtract @p mean values, scales values by @p scalefactor, swap Blue and Red channels.
-     *  @param image input image (with 1- or 3-channels).
+     *  @param image input image (with 1-, 3- or 4-channels).
      *  @param size spatial size for output image
      *  @param mean scalar with mean values which are subtracted from channels. Values are intended
      *  to be in (mean-R, mean-G, mean-B) order if @p image has BGR ordering and @p swapRB is true.
@@ -706,7 +706,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
     /** @brief Creates 4-dimensional blob from series of images. Optionally resizes and
      *  crops @p images from center, subtract @p mean values, scales values by @p scalefactor,
      *  swap Blue and Red channels.
-     *  @param images input images (all with 1- or 3-channels).
+     *  @param images input images (all with 1-, 3- or 4-channels).
      *  @param size spatial size for output image
      *  @param mean scalar with mean values which are subtracted from channels. Values are intended
      *  to be in (mean-R, mean-G, mean-B) order if @p image has BGR ordering and @p swapRB is true.
index c8ef9bc..bec4f0a 100644 (file)
@@ -136,7 +136,7 @@ Mat blobFromImages(const std::vector<Mat>& images_, double scalefactor, Size siz
     Mat blob, image;
     if (nch == 3 || nch == 4)
     {
-        int sz[] = { (int)nimages, 3, image0.rows, image0.cols };
+        int sz[] = { (int)nimages, nch, image0.rows, image0.cols };
         blob = Mat(4, sz, CV_32F);
         Mat ch[4];
 
@@ -148,7 +148,7 @@ Mat blobFromImages(const std::vector<Mat>& images_, double scalefactor, Size siz
             CV_Assert(image.dims == 2 && (nch == 3 || nch == 4));
             CV_Assert(image.size() == image0.size());
 
-            for( int j = 0; j < 3; j++ )
+            for( int j = 0; j < nch; j++ )
                 ch[j] = Mat(image.rows, image.cols, CV_32F, blob.ptr((int)i, j));
             if(swapRB)
                 std::swap(ch[0], ch[2]);
diff --git a/modules/dnn/test/test_misc.cpp b/modules/dnn/test/test_misc.cpp
new file mode 100644 (file)
index 0000000..2eee54c
--- /dev/null
@@ -0,0 +1,30 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+//
+// Copyright (C) 2017, Intel Corporation, all rights reserved.
+// Third party copyrights are property of their respective owners.
+
+#include "test_precomp.hpp"
+
+namespace cvtest
+{
+
+TEST(blobFromImage_4ch, Regression)
+{
+    Mat ch[4];
+    for(int i = 0; i < 4; i++)
+        ch[i] = Mat::ones(10, 10, CV_8U)*i;
+
+    Mat img;
+    merge(ch, 4, img);
+    Mat blob = dnn::blobFromImage(img, 1., Size(), Scalar(), false, false);
+
+    for(int i = 0; i < 4; i++)
+    {
+        ch[i] = Mat(img.rows, img.cols, CV_32F, blob.ptr(0, i));
+        ASSERT_DOUBLE_EQ(cvtest::norm(ch[i], cv::NORM_INF), i);
+    }
+}
+
+}