Skip to content

Commit f23b6ba

Browse files
committed
Fixed multidimensional count non-zero IPP implementation
1 parent 0e60b26 commit f23b6ba

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

modules/core/src/stat.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,23 +1333,23 @@ static bool ipp_countNonZero( Mat &src, int &res )
13331333
{
13341334
IppStatus status;
13351335
const Mat *arrays[] = {&src, NULL};
1336-
uchar *ptrs[1] = {NULL};
1337-
NAryMatIterator it(arrays, ptrs);
1336+
Mat planes[1];
1337+
NAryMatIterator it(arrays, planes, 1);
13381338
IppiSize size = {(int)it.size*src.channels(), 1};
1339-
1339+
res = 0;
13401340
for (size_t i = 0; i < it.nplanes; i++, ++it)
13411341
{
13421342
if(depth == CV_8U)
1343-
status = CV_INSTRUMENT_FUN_IPP(ippiCountInRange_8u_C1R, (const Ipp8u *)src.ptr(), (int)src.step, size, &count, 0, 0);
1343+
status = CV_INSTRUMENT_FUN_IPP(ippiCountInRange_8u_C1R, it.planes->ptr<Ipp8u>(), (int)it.planes->step, size, &count, 0, 0);
13441344
else if(depth == CV_32F)
1345-
status = CV_INSTRUMENT_FUN_IPP(ippiCountInRange_32f_C1R, (const Ipp32f *)src.ptr(), (int)src.step, size, &count, 0, 0);
1345+
status = CV_INSTRUMENT_FUN_IPP(ippiCountInRange_32f_C1R, it.planes->ptr<Ipp32f>(), (int)it.planes->step, size, &count, 0, 0);
13461346
else
13471347
return false;
13481348

1349-
if(status < 0)
1349+
if(status < 0 || (int)it.planes->total()*src.channels() < count)
13501350
return false;
13511351

1352-
res += (size.width*size.height - count);
1352+
res += (int)it.planes->total()*src.channels() - count;
13531353
}
13541354
}
13551355

modules/core/test/test_countnonzero.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,29 @@ void CV_CountNonZeroTest::run(int)
250250
}
251251

252252
TEST (Core_CountNonZero, accuracy) { CV_CountNonZeroTest test; test.safe_run(); }
253+
254+
255+
typedef testing::TestWithParam<std::tr1::tuple<int, int> > CountNonZeroND;
256+
257+
TEST_P (CountNonZeroND, ndim)
258+
{
259+
const int dims = std::tr1::get<0>(GetParam());
260+
const int type = std::tr1::get<1>(GetParam());
261+
const int ONE_SIZE = 5;
262+
263+
vector<int> sizes(dims);
264+
fill(sizes.begin(), sizes.end(), ONE_SIZE);
265+
266+
Mat data(sizes, CV_MAKETYPE(type, 1));
267+
data = 0;
268+
EXPECT_EQ(0, cv::countNonZero(data));
269+
data = Scalar::all(1);
270+
EXPECT_EQ(pow(ONE_SIZE, dims), cv::countNonZero(data));
271+
}
272+
273+
INSTANTIATE_TEST_CASE_P(Core, CountNonZeroND,
274+
testing::Combine(
275+
testing::Range(2, 9),
276+
testing::Values(CV_8U, CV_8S, CV_32F)
277+
)
278+
);

0 commit comments

Comments
 (0)