From 2f929376ec9046fb01b9eb271dead01e418ab29b Mon Sep 17 00:00:00 2001 From: Vitaly Tuzov Date: Mon, 10 Sep 2018 20:05:45 +0300 Subject: [PATCH] Fixed meanStdDev() implementation for the case input matrix has more than 4 channels --- modules/core/src/mean.cpp | 14 +++++++------- modules/core/test/test_arithm.cpp | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/modules/core/src/mean.cpp b/modules/core/src/mean.cpp index 30959f3bf9..e17875c08b 100644 --- a/modules/core/src/mean.cpp +++ b/modules/core/src/mean.cpp @@ -306,14 +306,14 @@ static int sumsqr_(const T* src0, const uchar* mask, ST* sum, SQT* sqsum, int le if( !mask ) { SumSqr_SIMD vop; - int i = vop(src0, mask, sum, sqsum, len, cn), k = cn % 4; - src += i * cn; + int x = vop(src0, mask, sum, sqsum, len, cn), k = cn % 4; + src = src0 + x * cn; if( k == 1 ) { ST s0 = sum[0]; SQT sq0 = sqsum[0]; - for( ; i < len; i++, src += cn ) + for(int i = x; i < len; i++, src += cn ) { T v = src[0]; s0 += v; sq0 += (SQT)v*v; @@ -325,7 +325,7 @@ static int sumsqr_(const T* src0, const uchar* mask, ST* sum, SQT* sqsum, int le { ST s0 = sum[0], s1 = sum[1]; SQT sq0 = sqsum[0], sq1 = sqsum[1]; - for( ; i < len; i++, src += cn ) + for(int i = x; i < len; i++, src += cn ) { T v0 = src[0], v1 = src[1]; s0 += v0; sq0 += (SQT)v0*v0; @@ -338,7 +338,7 @@ static int sumsqr_(const T* src0, const uchar* mask, ST* sum, SQT* sqsum, int le { ST s0 = sum[0], s1 = sum[1], s2 = sum[2]; SQT sq0 = sqsum[0], sq1 = sqsum[1], sq2 = sqsum[2]; - for( ; i < len; i++, src += cn ) + for(int i = x; i < len; i++, src += cn ) { T v0 = src[0], v1 = src[1], v2 = src[2]; s0 += v0; sq0 += (SQT)v0*v0; @@ -351,10 +351,10 @@ static int sumsqr_(const T* src0, const uchar* mask, ST* sum, SQT* sqsum, int le for( ; k < cn; k += 4 ) { - src = src0 + k; + src = src0 + x * cn + k; ST s0 = sum[k], s1 = sum[k+1], s2 = sum[k+2], s3 = sum[k+3]; SQT sq0 = sqsum[k], sq1 = sqsum[k+1], sq2 = sqsum[k+2], sq3 = sqsum[k+3]; - for( ; i < len; i++, src += cn ) + for(int i = x; i < len; i++, src += cn ) { T v0, v1; v0 = src[0], v1 = src[1]; diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index 049c86dd0d..8b84436140 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -2184,4 +2184,21 @@ TEST(Core_ConvertTo, regression_12121) } } +TEST(Core_MeanStdDev, regression_multichannel) +{ + { + uchar buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, + 3, 4, 5, 6, 7, 8, 9, 10 }; + double ref_buf[] = { 2., 3., 4., 5., 6., 7., 8., 9., + 1., 1., 1., 1., 1., 1., 1., 1. }; + Mat src(1, 2, CV_MAKETYPE(CV_8U, 8), buf); + Mat ref_m(8, 1, CV_64FC1, ref_buf); + Mat ref_sd(8, 1, CV_64FC1, ref_buf + 8); + Mat dst_m, dst_sd; + meanStdDev(src, dst_m, dst_sd); + EXPECT_EQ(0, cv::norm(dst_m, ref_m, NORM_L1)); + EXPECT_EQ(0, cv::norm(dst_sd, ref_sd, NORM_L1)); + } +} + }} // namespace