imgproc: parallelize and vectorize cv::warpPolar forward map build (#30997)

In cv::warpPolar(), the forward path (no WARP_INVERSE_MAP) builds both remap tables, mapx and mapy, in a serial scalar loop over the output rows before calling remap(). Every output row is independent, so this loop can be threaded and vectorized.
This commit is contained in:
Pratham Kumar
2026-11-30 18:33:20 +05:30
committed by GitHub
parent 1e611f6fc0
commit 6bcac1ae69
2 changed files with 78 additions and 14 deletions

View File

@@ -366,4 +366,36 @@ PERF_TEST_P( TestWarpPerspectiveSmallSrc, WarpPerspective_SmallSrc,
SANITY_CHECK_NOTHING();
}
CV_ENUM(PolarMode, WARP_POLAR_LINEAR, WARP_POLAR_LOG)
typedef TestBaseWithParam< tuple<MatType, Size, InterType, PolarMode, bool> > TestWarpPolar;
PERF_TEST_P( TestWarpPolar, WarpPolar,
Combine(
Values(CV_8UC1, CV_8UC3),
Values( szVGA, sz720p, sz1080p ),
InterType::all(),
PolarMode::all(),
testing::Bool()
)
)
{
const int dataType = get<0>(GetParam());
const Size sz = get<1>(GetParam());
const int interType = get<2>(GetParam());
const int polarMode = get<3>(GetParam());
const bool inverse = get<4>(GetParam());
Mat src(sz, dataType), dst(sz, dataType);
cvtest::fillGradient(src);
declare.in(src).out(dst);
const Point2f center(sz.width * 0.5f, sz.height * 0.5f);
const double maxRadius = std::min(sz.width, sz.height) * 0.5;
const int flags = interType | polarMode | (inverse ? WARP_INVERSE_MAP : 0);
TEST_CYCLE() warpPolar( src, dst, sz, center, maxRadius, flags );
SANITY_CHECK_NOTHING();
}
} // namespace

View File

@@ -3425,7 +3425,7 @@ void cv::warpPolar(InputArray _src, OutputArray _dst, Size dsize,
{
CV_Assert(!dsize.empty());
double Kangle = CV_2PI / dsize.height;
int phi, rho;
int rho;
// precalculate scaled rho
Mat rhos = Mat(1, dsize.width, CV_32F);
@@ -3444,23 +3444,55 @@ void cv::warpPolar(InputArray _src, OutputArray _dst, Size dsize,
bufRhos[rho] = (float)(rho * Kmag);
}
for (phi = 0; phi < dsize.height; phi++)
auto buildPolarMapRows = [&](const Range& range)
{
double KKy = Kangle * phi;
double cp = std::cos(KKy);
double sp = std::sin(KKy);
float* mx = (float*)(mapx.data + phi*mapx.step);
float* my = (float*)(mapy.data + phi*mapy.step);
for (rho = 0; rho < dsize.width; rho++)
for (int row = range.start; row < range.end; row++)
{
double x = bufRhos[rho] * cp + center.x;
double y = bufRhos[rho] * sp + center.y;
double KKy = Kangle * row;
double cp = std::cos(KKy);
double sp = std::sin(KKy);
float* mx = (float*)(mapx.data + row*mapx.step);
float* my = (float*)(mapy.data + row*mapy.step);
int col = 0;
#if (CV_SIMD_64F || CV_SIMD_SCALABLE_64F)
{
const v_float64 vcp = vx_setall_f64(cp), vsp = vx_setall_f64(sp);
const v_float64 vcx = vx_setall_f64((double)center.x), vcy = vx_setall_f64((double)center.y);
const int vl = VTraits<v_float32>::vlanes();
for (; col <= dsize.width - vl; col += vl)
{
const v_float32 r = vx_load(bufRhos + col);
const v_float64 rlo = v_cvt_f64(r);
const v_float64 rhi = v_cvt_f64_high(r);
const v_float64 xlo = v_add(v_mul(rlo, vcp), vcx);
const v_float64 xhi = v_add(v_mul(rhi, vcp), vcx);
const v_float64 ylo = v_add(v_mul(rlo, vsp), vcy);
const v_float64 yhi = v_add(v_mul(rhi, vsp), vcy);
v_store(mx + col, v_cvt_f32(xlo, xhi));
v_store(my + col, v_cvt_f32(ylo, yhi));
}
}
#endif
for (; col < dsize.width; col++)
{
double x = bufRhos[col] * cp + center.x;
double y = bufRhos[col] * sp + center.y;
mx[rho] = (float)x;
my[rho] = (float)y;
mx[col] = (float)x;
my[col] = (float)y;
}
}
}
#if (CV_SIMD_64F || CV_SIMD_SCALABLE_64F)
vx_cleanup();
#endif
};
const double polarStripes = std::min((double)dsize.height,
(double)dsize.width * (double)dsize.height / 32768.0);
if (polarStripes < 2.0)
buildPolarMapRows(Range(0, dsize.height));
else
parallel_for_(Range(0, dsize.height), buildPolarMapRows, polarStripes);
remap(_src, _dst, mapx, mapy, flags & cv::INTER_MAX, (flags & cv::WARP_FILL_OUTLIERS) ? cv::BORDER_CONSTANT : cv::BORDER_TRANSPARENT);
}
else