mirror of
https://github.com/opencv/opencv.git
synced 2026-11-24 10:18:08 -06:00
core: SIMD fast path for findNonZero on 8-bit inputs
This commit is contained in:
@@ -136,4 +136,26 @@ PERF_TEST_P(Size_MatType, hasNonZero, testing::Combine( testing::Values( TYPICAL
|
||||
SANITY_CHECK_NOTHING();
|
||||
}
|
||||
|
||||
typedef tuple<Size, double> Size_Density_t;
|
||||
typedef perf::TestBaseWithParam<Size_Density_t> Size_Density;
|
||||
|
||||
PERF_TEST_P(Size_Density, findNonZero, testing::Combine(
|
||||
testing::Values(TYPICAL_MAT_SIZES),
|
||||
testing::Values(0.0, 0.001, 0.01, 0.1, 0.5)))
|
||||
{
|
||||
Size sz = get<0>(GetParam());
|
||||
double density = get<1>(GetParam());
|
||||
|
||||
Mat r(sz, CV_32FC1);
|
||||
randu(r, 0, 1);
|
||||
Mat src = r < density; // CV_8UC1: 255 where r < density, 0 elsewhere
|
||||
std::vector<Point> idx;
|
||||
|
||||
declare.in(src);
|
||||
|
||||
TEST_CYCLE() findNonZero(src, idx);
|
||||
|
||||
SANITY_CHECK_NOTHING();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -167,7 +167,20 @@ void findNonZero(InputArray _src, OutputArray _idx)
|
||||
const uchar* ptr8 = src.ptr(i);
|
||||
if( depth == CV_8U || depth == CV_8S || depth == CV_Bool || depth == CV_8F_E4M3FNUZ )
|
||||
{
|
||||
for( j = 0; j < cols; j++ )
|
||||
j = 0;
|
||||
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
||||
// skip whole SIMD blocks of zeros; only scan blocks that contain a non-zero byte
|
||||
const int vlanes = VTraits<v_uint8>::vlanes();
|
||||
const v_uint8 vzero = vx_setzero_u8();
|
||||
for( ; j <= cols - vlanes; j += vlanes )
|
||||
{
|
||||
if( !v_check_any(v_ne(vx_load(ptr8 + j), vzero)) )
|
||||
continue;
|
||||
for( int t = j; t < j + vlanes; t++ )
|
||||
if( ptr8[t] != 0 ) buf[k++] = t;
|
||||
}
|
||||
#endif
|
||||
for( ; j < cols; j++ )
|
||||
if( ptr8[j] != 0 ) buf[k++] = j;
|
||||
}
|
||||
else if( depth == CV_16U || depth == CV_16S )
|
||||
|
||||
@@ -2414,6 +2414,32 @@ TEST(Core_FindNonZero, fp8)
|
||||
}
|
||||
}
|
||||
|
||||
// widths around and above the SIMD block size, so both the vector loop and the scalar tail run
|
||||
TEST(Core_FindNonZero, wide_rows_8u)
|
||||
{
|
||||
RNG& rng = theRNG();
|
||||
for (int cols : { 15, 16, 17, 63, 64, 65, 127, 1000 })
|
||||
{
|
||||
for (double density : { 0.0, 0.01, 0.3 })
|
||||
{
|
||||
Mat r(3, cols + 5, CV_32FC1);
|
||||
rng.fill(r, RNG::UNIFORM, 0, 1);
|
||||
Mat full = r < density;
|
||||
Mat src = full.colRange(2, 2 + cols); // ROI: rows are not continuous
|
||||
|
||||
std::vector<Point> expected;
|
||||
for (int y = 0; y < src.rows; y++)
|
||||
for (int x = 0; x < src.cols; x++)
|
||||
if (src.at<uchar>(y, x) != 0)
|
||||
expected.push_back(Point(x, y));
|
||||
|
||||
std::vector<Point> pts;
|
||||
findNonZero(src, pts);
|
||||
EXPECT_EQ(expected, pts) << "cols=" << cols << " density=" << density;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Core_BoolVector, support)
|
||||
{
|
||||
std::vector<bool> test;
|
||||
|
||||
Reference in New Issue
Block a user