Merge pull request #13544 from eightco:bugfix
authorLee Jaehwan <eightco89@gmail.com>
Fri, 4 Jan 2019 12:10:50 +0000 (21:10 +0900)
committerAlexander Alekhin <alexander.a.alekhin@gmail.com>
Fri, 4 Jan 2019 12:10:50 +0000 (15:10 +0300)
Fix a bug in cv :: merge when array of 3-channel mat is input (#13544)

* Mat merge function bug fix - Bug fix of merge function of 3-channel vector <Mat> of 3 or 4 matrices

* Add Core_merge test for opencv#13544

* fixups

modules/core/src/merge.cpp
modules/core/test/test_mat.cpp

index b460d21..0aeaa82 100644 (file)
@@ -305,7 +305,7 @@ void cv::merge(const Mat* mv, size_t n, OutputArray _dst)
         return;
     }
 
-    CV_IPP_RUN_FAST(ipp_merge(mv, dst, (int)n));
+    CV_IPP_RUN(allch1, ipp_merge(mv, dst, (int)n));
 
     if( !allch1 )
     {
index f585c4f..f69140b 100644 (file)
@@ -1888,6 +1888,29 @@ TEST(Core_Split, crash_12171)
     EXPECT_EQ(2, dst2.ptr<uchar>(1)[1]);
 }
 
+TEST(Core_Merge, bug_13544)
+{
+    Mat src1(2, 2, CV_8UC3, Scalar::all(1));
+    Mat src2(2, 2, CV_8UC3, Scalar::all(2));
+    Mat src3(2, 2, CV_8UC3, Scalar::all(3));
+    Mat src_arr[] = { src1, src2, src3 };
+    Mat dst;
+    merge(src_arr, 3, dst);
+    ASSERT_EQ(9, dst.channels());  // Avoid memory access out of buffer
+    EXPECT_EQ(3, (int)dst.ptr<uchar>(0)[6]);
+    EXPECT_EQ(3, (int)dst.ptr<uchar>(0)[7]);
+    EXPECT_EQ(3, (int)dst.ptr<uchar>(0)[8]);
+    EXPECT_EQ(1, (int)dst.ptr<uchar>(1)[0]);
+    EXPECT_EQ(1, (int)dst.ptr<uchar>(1)[1]);
+    EXPECT_EQ(1, (int)dst.ptr<uchar>(1)[2]);
+    EXPECT_EQ(2, (int)dst.ptr<uchar>(1)[3]);
+    EXPECT_EQ(2, (int)dst.ptr<uchar>(1)[4]);
+    EXPECT_EQ(2, (int)dst.ptr<uchar>(1)[5]);
+    EXPECT_EQ(3, (int)dst.ptr<uchar>(1)[6]);
+    EXPECT_EQ(3, (int)dst.ptr<uchar>(1)[7]);
+    EXPECT_EQ(3, (int)dst.ptr<uchar>(1)[8]);
+}
+
 struct CustomType  // like cv::Keypoint
 {
     Point2f pt;