From 4c66614e07319b66537b6327e2dcf871c5aa6829 Mon Sep 17 00:00:00 2001 From: Vladislav Vinogradov Date: Wed, 7 May 2014 13:15:19 +0400 Subject: [PATCH] fix cv::subtract function: call dst.create(...) before using it --- modules/core/src/arithm.cpp | 6 +++++- modules/core/test/test_arithm.cpp | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/modules/core/src/arithm.cpp b/modules/core/src/arithm.cpp index 0517a5f..f0ef920 100644 --- a/modules/core/src/arithm.cpp +++ b/modules/core/src/arithm.cpp @@ -1562,8 +1562,12 @@ void cv::subtract( InputArray src1, InputArray src2, OutputArray dst, if (dtype == -1 && dst.fixedType()) dtype = dst.depth(); - if (!dst.fixedType() || dtype == dst.depth()) + dtype = CV_MAKE_TYPE(CV_MAT_DEPTH(dtype), src1.channels()); + + if (!dst.fixedType() || dtype == dst.type()) { + dst.create(src1.size(), dtype); + if (dtype == CV_16S) { Mat _dst = dst.getMat(); diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index a240941..1687285 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -1579,3 +1579,13 @@ TEST_P(Mul1, One) } INSTANTIATE_TEST_CASE_P(Arithm, Mul1, testing::Values(Size(2, 2), Size(1, 1))); + +TEST(Subtract8u8u16s, EmptyOutputMat) +{ + cv::Mat src1 = cv::Mat::zeros(16, 16, CV_8UC1); + cv::Mat src2 = cv::Mat::zeros(16, 16, CV_8UC1); + cv::Mat dst; + cv::subtract(src1, src2, dst, cv::noArray(), CV_16S); + ASSERT_FALSE(dst.empty()); + ASSERT_EQ(0, cv::countNonZero(dst)); +} -- 2.7.4