mirror of
https://github.com/opencv/opencv.git
synced 2026-11-15 22:08:03 -06:00
vectorized and parallel DFT/DCT + matchTemplate - #30061 (Made with a help from Claude Code Fable 5.1) ## Summary This PR replaces the scalar 1D DFT/DCT kernels in `modules/core/src/dxt.cpp` with a new engine built on universal intrinsics and compiled per ISA through `ocv_add_dispatched_file`, and makes the 2D transforms, the `DFT_ROWS`/`DCT_ROWS` batches and `matchTemplate` (`crossCorr`) multi-threaded. The public API, the output formats (CCS packing of real spectra, `DFT_COMPLEX_OUTPUT` layout, DCT ordering and normalization) and the IPP/OpenCL paths are unchanged. Only floating-point rounding may differ (the new twiddle tables are more accurate than the old recurrence-generated ones, so the error goes down, not up). Measured on Apple M4 Max (16 cores, NEON), macOS 26.5.x, Xcode 26.6, Release build (`-O3`, no IPP). All numbers are the minimum over repeated runs, `5.x` vs this PR. ## Changes ### New 1D engine (`dxt.hpp`, `dxt.simd.hpp`, `dxt.cpp`) - **Split re/im layout.** The input pass gathers the data into separate `re[]`/`im[]` workspace arrays; all middle stages operate on the split arrays, so a complex multiplication is four independent vector FMAs with no lane shuffling. The output pass interleaves back (or packs the CCS / DCT layout) with the scaling fused in. - **Mixed-radix decimation-in-time with a fused first stage.** The plan chooses the first stage (radix 1/2/4/8, twiddle-free, fused with the gather and the digit-reversal permutation) from the power of two in the length, then radix-4 stages, then radix-3, radix-5 and a generic odd radix. There are no radix-2 middle stages. - **Vectorized butterflies with universal intrinsics** (`v_float32`/`v_float64`, VLA-safe): one flat loop per stage with a single clamped counter for both the data and the twiddle offsets (the back-off clamp handles spans that are not multiples of the vector width; such stages run out of place, ping-ponging between two workspace pairs, the others run in place). - **Vectorized first stage.** The radix-2/4/8 first stage reads the input columns contiguously (`v_load_deinterleave`), runs the small DFT in registers and scatters the outputs of every lane with one 4-channel interleaved store. Only odd lengths use a scalar table-driven gather. - **Vectorized edge passes**: interleave/deinterleave, the real-FFT untangling in both directions (with `v_reverse` for the mirrored half), DCT-II post-twiddle and DCT-III pre-twiddle, the Makhoul permutations. - **Inverse = conjugation trick**: the middle stages are direction-free and use forward twiddles only; the inverse conjugates on the gather and negates the imaginary scale on the output. - **Immutable plan + per-executor workspace, zero allocations per transform.** The plan (radix sequence, permutation table, per-stage split twiddle tables) is built once in `init()` and shared by all rows of a batch; every executor owns a workspace. Twiddles are generated in double precision from a two-level exact table (a few ulps, much cheaper to build than the old N-point recurrence, which matters because `cv::dft()` builds a plan on every call). - **Per-ISA dispatch**: `ocv_add_dispatched_file(dxt SSE2 AVX2 AVX512_ICL RVV)`; the kernels are reached through a function-pointer table resolved once per depth, following the pattern of `arithm.simd.hpp` / `warp_kernels.simd.hpp`. The plan is built with the vector width of the dispatched kernels (never with the baseline `VTraits`). - The legacy scalar engine, `DFTInit`, the SSE3 `DFT_VecR*` butterflies and the `noPermute`/ `factors[0] >>= 1` machinery are removed (−1400 lines). The legacy `cv_hal_dft` hook (which took the old tables) is no longer called from the 1D path; the in-tree RISC-V HAL implementation of it will be updated in a separate PR. ### Parallelism - `crossCorr` (`matchTemplate`): the correlation tiles are independent and are now distributed with `parallel_for_`; every worker creates its DFT buffers and `DFT2D` contexts once per range. (The tile loop had been sequential since the C++ rewrite in 2010, which dropped the OpenMP pragma of `icvCrossCorr`.) - 2D DFT/DCT and `DFT_ROWS`/`DCT_ROWS` batches: the row pass, the column pass and the DCT line loop distribute their lines over threads; the 1D plan is shared, each worker allocates its workspace once per range. The number of stripes is derived from the N log N cost, so a 256x256 transform stays on the calling thread and 1024x1024 gets ~20 stripes. Nested calls (the 2D DFTs inside the parallel `crossCorr` tiles) are serialized by `parallel_for_` itself. - The column pass copies columns in batches (`CopyFromColumns`/`CopyToColumns`, 4..16 adjacent columns copied row by row into contiguous buffers sized to stay within 128 KB) instead of two at a time: with power-of-two row steps the old strided copies aliased in the cache and took 60-70% of the 2D time. ### Tests - `Core_DFT.sweep_1d`, `Core_DCT.sweep_1d`: every 5-smooth length up to 2048 plus odd, prime and "awkward" lengths, both depths, C2C forward/inverse, R2C (CCS and complex output), C2R (CCS and complex input), DCT/IDCT, in-place, column vectors, round trips; compared against the naive O(N^2) references of `test_dxt.cpp` with a tolerance of `32*eps*(log2(N) + 2 + maxfactor/16)`. - `Core_DFT.sweep_rows`: `DFT_ROWS` batches sharing one plan. - `Core_DFT.nonzero_rows`: 2D transforms with `nonzero_rows` (previously uncovered). - `Core_DFT.parallel_large`: large 2D / rows / DCT transforms, multi-threaded result vs the single-threaded one. - The naive DCT reference reduces the angle exactly before `cos()` (its accuracy was limiting the comparison for n > 100 in double precision). - Perf: `perf_dft.cpp` gains 1D row/column cases for `dft` and `dct`. - Existing `opencv_test_core` (incl. `Core_DFT*`, `Core_DCT*`, `Core_MulSpectrums`, `Core_DivSpectrums`) and `opencv_test_imgproc` (phaseCorrelate, matchTemplate, filter2D) pass; the x86-64 build (SSE3 baseline + SSE4.1/AVX2/AVX512_ICL dispatch) passes the DFT/DCT tests under Rosetta 2 (AVX2/AVX-512 paths compile-checked only on this machine). ## Performance ### 1D transforms (single row, one thread), microseconds `C2C` = complex-to-complex, `R2C` = real-to-CCS, `C2R` = CCS-to-real (inverse, scaled), `32`/`64` = float/double. | n | C2C32 | C2C64 | R2C32 | C2R32 | R2C64 | DCT32 | IDCT32 | DCT64 | |---|---|---|---|---|---|---|---|---| | 256 | 0.82 → 0.55 | 0.86 → 0.74 | 0.67 → 0.53 | 0.63 → 0.52 | 0.64 → 0.56 | 0.88 → 0.56 | 0.88 → 0.56 | 0.90 → 0.66 | | 1000 | 3.84 → 1.70 | 4.08 → 2.58 | 2.86 → 1.36 | 2.88 → 1.38 | 2.77 → 1.74 | 3.95 → 1.69 | 4.02 → 1.72 | 3.97 → 2.15 | | 1024 | 3.25 → 1.65 | 3.44 → 2.40 | 2.46 → 1.23 | 2.35 → 1.27 | 2.42 → 1.59 | 3.57 → 1.57 | 3.51 → 1.59 | 3.45 → 2.02 | | 4096 | 14.9 → 6.4 | 16.4 → 11.5 | 10.7 → 4.3 | 10.2 → 4.5 | 11.0 → 6.9 | 15.4 → 6.0 | 15.3 → 6.0 | 17.0 → 9.5 | | 4725 (3³·5²·7) | 27.5 → 17.2 | 29.4 → 24.1 | 28.2 → 16.4 | 29.8 → 18.3 | 30.3 → 24.7 | – | – | – | | 65536 | 350 → 273 | 411 → 494 | 223 → 136 | 274 → 139 | 251 → 249 | 318 → 167 | 299 → 159 | 372 → 320 | Each `cv::dft()` call builds its plan anew; at n = 4096 the plan takes ~1.9 µs of the 6.4 µs (the old code spent ~4 µs of 14.9 on its tables). For n = 65536 both versions are bound by the memory traffic of the out-of-cache data, and C2C64 gets slightly slower (the split layout needs one more pass over 1 MB of data; a plan cache or larger radices could recover this later). ### 2D transforms, single thread, microseconds | size | C2C32 | R2C32 | C2R32 | R2C64 | DCT32 | IDCT32 | R2C32 rows | |---|---|---|---|---|---|---|---| | 256×256 | 297 → 140 | 162 → 75 | 165 → 77 | 178 → 126 | 259 → 166 | 290 → 174 | 72 → 29 | | 512×512 | 1660 → 569 | 765 → 306 | 723 → 317 | 896 → 551 | 1381 → 946 | 1377 → 957 | 303 → 125 | | 480×640 | 1555 → 715 | 895 → 377 | 918 → 392 | 978 → 677 | 1248 → 707 | 1262 → 695 | 389 → 147 | | 1024×1024 | 7508 → 2625 | 3781 → 1387 | 3883 → 1398 | 4964 → 3201 | 6438 → 4556 | 6805 → 4583 | 1341 → 516 | | 1080×1920 | 13141 → 5543 | 7143 → 2880 | 7269 → 2970 | 7770 → 5348 | 9735 → 5190 | 9477 → 5381 | 3116 → 1192 | | 2048×2048 | 38151 → 16822 | 18381 → 6973 | 18784 → 7294 | 25146 → 17476 | 30291 → 21631 | 30540 → 22003 | 5809 → 2242 | ### 2D transforms, default thread count (16), microseconds | size | C2C32 | R2C32 | C2R32 | R2C64 | DCT32 | IDCT32 | R2C32 rows | |---|---|---|---|---|---|---|---| | 256×256 | 309 → 138 | 157 → 75 | 161 → 77 | 174 → 126 | 265 → 167 | 293 → 170 | 71 → 29 | | 512×512 | 1660 → 162 | 785 → 155 | 788 → 160 | 866 → 256 | 1410 → 235 | 1366 → 228 | 303 → 41 | | 480×640 | 1531 → 178 | 895 → 142 | 916 → 141 | 957 → 222 | 1241 → 172 | 1248 → 173 | 393 → 46 | | 1024×1024 | 7495 → 381 | 3755 → 274 | 3767 → 269 | 4875 → 478 | 6323 → 650 | 6583 → 630 | 1343 → 94 | | 1080×1920 | 12905 → 681 | 7131 → 403 | 7240 → 382 | 7760 → 657 | 9673 → 605 | 9513 → 607 | 3124 → 143 | | 2048×2048 | 38155 → 1481 | 18324 → 826 | 18559 → 857 | 24861 → 1776 | 30120 → 2592 | 30741 → 2734 | 5890 → 274 | (5.x is single-threaded for all of these; 256×256 stays single-threaded in the new code by design.) ### matchTemplate (`opencv_perf_imgproc`, `*matchTemplate*`), milliseconds Whole suite (156 cases): 921 → 284 ms. Selected cases (8-bit image, 16×16 template unless noted): | image, template | SQDIFF | SQDIFF_NORMED | CCORR | CCORR_NORMED | CCOEFF | CCOEFF_NORMED | |---|---|---|---|---|---|---| | 128×128 | 0.15 → 0.12 | 0.14 → 0.12 | 0.09 → 0.05 | 0.13 → 0.09 | 0.11 → 0.08 | 0.13 → 0.10 | | 320×240 | 0.99 → 0.52 | 1.09 → 0.58 | 0.79 → 0.23 | 0.96 → 0.42 | 0.93 → 0.38 | 1.02 → 0.53 | | 640×480 | 3.19 → 1.17 | 3.35 → 1.22 | 2.27 → 0.29 | 3.12 → 1.04 | 2.94 → 0.90 | 3.43 → 1.38 | | 800×600 | 5.86 → 1.64 | 5.61 → 1.77 | 4.11 → 0.34 | 5.48 → 1.53 | 5.19 → 1.28 | 5.82 → 2.04 | | 1024×768 | 9.73 → 2.72 | 9.59 → 2.90 | 6.88 → 0.48 | 9.11 → 2.45 | 8.44 → 2.05 | 9.84 → 3.44 | | 1280×1024 | 14.92 → 4.33 | 15.01 → 4.58 | 10.70 → 0.64 | 14.31 → 3.97 | 13.40 → 3.33 | 15.37 → 5.58 | | 1280×1024, 1260×1000 | 13.06 → 7.90 | 13.08 → 7.74 | 10.75 → 3.58 | 12.23 → 5.03 | 11.11 → 3.94 | 12.03 → 5.05 | `TM_CCORR` is now dominated by the (still single-threaded) `integral()` and normalization passes in the other methods; those are candidates for a follow-up. ## Notes for reviewers - `modules/core/src/dxt.hpp` documents the engine layout; the plan builder (`DftPlan::build`) and the driver (`runDft`) are in `dxt.cpp`, the kernels in `dxt.simd.hpp`. - Stages whose span is smaller than the vector width run scalar (on NEON this only affects lengths of the form 2·odd; on AVX2/AVX-512 also the first radix-4 stage of 4^k lengths); handling them with narrower fixed-width vectors is a possible follow-up. - A radix-8 middle stage was implemented and measured slower on NEON than two radix-4 stages (register pressure), so it is not included. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
1594 lines
51 KiB
C++
1594 lines
51 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
#include "test_precomp.hpp"
|
|
|
|
#define CV_TEST_DXT_MUL_CONJ 8 /**< conjugate the second argument of cvMulSpectrums */
|
|
|
|
namespace opencv_test { namespace {
|
|
|
|
static Mat initDFTWave( int n, bool inv )
|
|
{
|
|
int i;
|
|
double angle = (inv ? 1 : -1)*CV_PI*2/n;
|
|
Complexd wi, w1;
|
|
Mat wave(1, n, CV_64FC2);
|
|
Complexd* w = wave.ptr<Complexd>();
|
|
|
|
w1.re = cos(angle);
|
|
w1.im = sin(angle);
|
|
w[0].re = wi.re = 1.;
|
|
w[0].im = wi.im = 0.;
|
|
|
|
for( i = 1; i < n; i++ )
|
|
{
|
|
double t = wi.re*w1.re - wi.im*w1.im;
|
|
wi.im = wi.re*w1.im + wi.im*w1.re;
|
|
wi.re = t;
|
|
w[i] = wi;
|
|
}
|
|
|
|
return wave;
|
|
}
|
|
|
|
|
|
static void DFT_1D( const Mat& _src, Mat& _dst, int flags, const Mat& _wave=Mat())
|
|
{
|
|
_dst.create(_src.size(), _src.type());
|
|
int i, j, k, n = _dst.cols + _dst.rows - 1;
|
|
Mat wave = _wave;
|
|
double scale = (flags & DFT_SCALE) ? 1./n : 1.;
|
|
size_t esz = _src.elemSize();
|
|
size_t srcstep = esz, dststep = esz;
|
|
const uchar* src0 = _src.ptr();
|
|
uchar* dst0 = _dst.ptr();
|
|
|
|
CV_Assert( _src.cols + _src.rows - 1 == n );
|
|
|
|
if( wave.empty() )
|
|
wave = initDFTWave( n, (flags & DFT_INVERSE) != 0 );
|
|
|
|
const Complexd* w = wave.ptr<Complexd>();
|
|
if( !_src.isContinuous() )
|
|
srcstep = _src.step;
|
|
if( !_dst.isContinuous() )
|
|
dststep = _dst.step;
|
|
|
|
if( _src.type() == CV_32FC2 )
|
|
{
|
|
for( i = 0; i < n; i++ )
|
|
{
|
|
Complexf* dst = (Complexf*)(dst0 + i*dststep);
|
|
Complexd sum(0,0);
|
|
int delta = i;
|
|
k = 0;
|
|
|
|
for( j = 0; j < n; j++ )
|
|
{
|
|
const Complexf* src = (const Complexf*)(src0 + j*srcstep);
|
|
sum.re += src->re*w[k].re - src->im*w[k].im;
|
|
sum.im += src->re*w[k].im + src->im*w[k].re;
|
|
k += delta;
|
|
k -= (k >= n ? n : 0);
|
|
}
|
|
|
|
dst->re = (float)(sum.re*scale);
|
|
dst->im = (float)(sum.im*scale);
|
|
}
|
|
}
|
|
else if( _src.type() == CV_64FC2 )
|
|
{
|
|
for( i = 0; i < n; i++ )
|
|
{
|
|
Complexd* dst = (Complexd*)(dst0 + i*dststep);
|
|
Complexd sum(0,0);
|
|
int delta = i;
|
|
k = 0;
|
|
|
|
for( j = 0; j < n; j++ )
|
|
{
|
|
const Complexd* src = (const Complexd*)(src0 + j*srcstep);
|
|
sum.re += src->re*w[k].re - src->im*w[k].im;
|
|
sum.im += src->re*w[k].im + src->im*w[k].re;
|
|
k += delta;
|
|
k -= (k >= n ? n : 0);
|
|
}
|
|
|
|
dst->re = sum.re*scale;
|
|
dst->im = sum.im*scale;
|
|
}
|
|
}
|
|
else
|
|
CV_Error(cv::Error::StsUnsupportedFormat, "");
|
|
}
|
|
|
|
|
|
static void DFT_2D( const Mat& src, Mat& dst, int flags )
|
|
{
|
|
const int cn = 2;
|
|
int i;
|
|
dst.create(src.size(), src.type());
|
|
Mat tmp( src.cols, src.rows, src.type());
|
|
Mat wave = initDFTWave( dst.cols, (flags & DFT_INVERSE) != 0 );
|
|
|
|
// 1. row-wise transform
|
|
for( i = 0; i < dst.rows; i++ )
|
|
{
|
|
Mat srci = src.row(i).reshape(cn, src.cols), dsti = tmp.col(i);
|
|
DFT_1D(srci, dsti, flags, wave );
|
|
}
|
|
|
|
if( (flags & DFT_ROWS) == 0 )
|
|
{
|
|
if( dst.cols != dst.rows )
|
|
wave = initDFTWave( dst.rows, (flags & DFT_INVERSE) != 0 );
|
|
|
|
// 2. column-wise transform
|
|
for( i = 0; i < dst.cols; i++ )
|
|
{
|
|
Mat srci = tmp.row(i).reshape(cn, tmp.cols), dsti = dst.col(i);
|
|
DFT_1D(srci, dsti, flags, wave );
|
|
}
|
|
}
|
|
else
|
|
cvtest::transpose(tmp, dst);
|
|
}
|
|
|
|
|
|
static Mat initDCTWave( int n, bool inv )
|
|
{
|
|
int i, k;
|
|
double angle = CV_PI*0.5/n;
|
|
Mat wave(n, n, CV_64F);
|
|
|
|
double scale = sqrt(1./n);
|
|
for( k = 0; k < n; k++ )
|
|
wave.at<double>(0, k) = scale;
|
|
scale *= sqrt(2.);
|
|
for( i = 1; i < n; i++ )
|
|
for( k = 0; k < n; k++ )
|
|
{
|
|
// reduce the angle exactly (period 4n) before the cosine to keep the reference accurate
|
|
int m = (int)(((int64)i*(2*k + 1)) % (4*n));
|
|
wave.at<double>(i, k) = scale*cos( angle*m );
|
|
}
|
|
|
|
if( inv )
|
|
cv::transpose( wave, wave );
|
|
|
|
return wave;
|
|
}
|
|
|
|
|
|
static void DCT_1D( const Mat& _src, Mat& _dst, int flags, const Mat& _wave=Mat() )
|
|
{
|
|
_dst.create( _src.size(), _src.type() );
|
|
int i, j, n = _dst.cols + _dst.rows - 1;
|
|
Mat wave = _wave;
|
|
int srcstep = 1, dststep = 1;
|
|
double* w;
|
|
|
|
CV_Assert( _src.cols + _src.rows - 1 == n);
|
|
|
|
if( wave.empty() )
|
|
wave = initDCTWave( n, (flags & DFT_INVERSE) != 0 );
|
|
w = wave.ptr<double>();
|
|
|
|
if( !_src.isContinuous() )
|
|
srcstep = (int)(_src.step/_src.elemSize());
|
|
if( !_dst.isContinuous() )
|
|
dststep = (int)(_dst.step/_dst.elemSize());
|
|
|
|
if( _src.type() == CV_32FC1 )
|
|
{
|
|
float *dst = _dst.ptr<float>();
|
|
|
|
for( i = 0; i < n; i++, dst += dststep )
|
|
{
|
|
const float* src = _src.ptr<float>();
|
|
double sum = 0;
|
|
|
|
for( j = 0; j < n; j++, src += srcstep )
|
|
sum += src[0]*w[j];
|
|
w += n;
|
|
dst[0] = (float)sum;
|
|
}
|
|
}
|
|
else if( _src.type() == CV_64FC1 )
|
|
{
|
|
double *dst = _dst.ptr<double>();
|
|
|
|
for( i = 0; i < n; i++, dst += dststep )
|
|
{
|
|
const double* src = _src.ptr<double>();
|
|
double sum = 0;
|
|
|
|
for( j = 0; j < n; j++, src += srcstep )
|
|
sum += src[0]*w[j];
|
|
w += n;
|
|
dst[0] = sum;
|
|
}
|
|
}
|
|
else
|
|
CV_Assert(0);
|
|
}
|
|
|
|
|
|
static void DCT_2D( const Mat& src, Mat& dst, int flags )
|
|
{
|
|
const int cn = 1;
|
|
int i;
|
|
dst.create( src.size(), src.type() );
|
|
Mat tmp(dst.cols, dst.rows, dst.type() );
|
|
Mat wave = initDCTWave( dst.cols, (flags & DCT_INVERSE) != 0 );
|
|
|
|
// 1. row-wise transform
|
|
for( i = 0; i < dst.rows; i++ )
|
|
{
|
|
Mat srci = src.row(i).reshape(cn, src.cols);
|
|
Mat dsti = tmp.col(i);
|
|
DCT_1D(srci, dsti, flags, wave);
|
|
}
|
|
|
|
if( (flags & DCT_ROWS) == 0 )
|
|
{
|
|
if( dst.cols != dst.rows )
|
|
wave = initDCTWave( dst.rows, (flags & DCT_INVERSE) != 0 );
|
|
|
|
// 2. column-wise transform
|
|
for( i = 0; i < dst.cols; i++ )
|
|
{
|
|
Mat srci = tmp.row(i).reshape(cn, tmp.cols);
|
|
Mat dsti = dst.col(i);
|
|
DCT_1D( srci, dsti, flags, wave );
|
|
}
|
|
}
|
|
else
|
|
cvtest::transpose( tmp, dst );
|
|
}
|
|
|
|
|
|
static void convertFromCCS( const Mat& _src0, const Mat& _src1, Mat& _dst, int flags )
|
|
{
|
|
if( _dst.rows > 1 && (_dst.cols > 1 || (flags & DFT_ROWS)) )
|
|
{
|
|
int i, count = _dst.rows, len = _dst.cols;
|
|
bool is2d = (flags & DFT_ROWS) == 0;
|
|
Mat src0row, src1row, dstrow;
|
|
for( i = 0; i < count; i++ )
|
|
{
|
|
int j = !is2d || i == 0 ? i : count - i;
|
|
src0row = _src0.row(i);
|
|
src1row = _src1.row(j);
|
|
dstrow = _dst.row(i);
|
|
convertFromCCS( src0row, src1row, dstrow, 0 );
|
|
}
|
|
|
|
if( is2d )
|
|
{
|
|
src0row = _src0.col(0);
|
|
dstrow = _dst.col(0);
|
|
convertFromCCS( src0row, src0row, dstrow, 0 );
|
|
if( (len & 1) == 0 )
|
|
{
|
|
src0row = _src0.col(_src0.cols - 1);
|
|
dstrow = _dst.col(len/2);
|
|
convertFromCCS( src0row, src0row, dstrow, 0 );
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int i, n = _dst.cols + _dst.rows - 1, n2 = (n+1) >> 1;
|
|
int cn = _src0.channels();
|
|
int srcstep = cn, dststep = 1;
|
|
|
|
if( !_dst.isContinuous() )
|
|
dststep = (int)(_dst.step/_dst.elemSize());
|
|
|
|
if( !_src0.isContinuous() )
|
|
srcstep = (int)(_src0.step/_src0.elemSize1());
|
|
|
|
if( _dst.depth() == CV_32F )
|
|
{
|
|
Complexf* dst = _dst.ptr<Complexf>();
|
|
const float* src0 = _src0.ptr<float>();
|
|
const float* src1 = _src1.ptr<float>();
|
|
int delta0, delta1;
|
|
|
|
dst->re = src0[0];
|
|
dst->im = 0;
|
|
|
|
if( (n & 1) == 0 )
|
|
{
|
|
dst[n2*dststep].re = src0[(cn == 1 ? n-1 : n2)*srcstep];
|
|
dst[n2*dststep].im = 0;
|
|
}
|
|
|
|
delta0 = srcstep;
|
|
delta1 = delta0 + (cn == 1 ? srcstep : 1);
|
|
if( cn == 1 )
|
|
srcstep *= 2;
|
|
|
|
for( i = 1; i < n2; i++, delta0 += srcstep, delta1 += srcstep )
|
|
{
|
|
float t0 = src0[delta0];
|
|
float t1 = src0[delta1];
|
|
|
|
dst[i*dststep].re = t0;
|
|
dst[i*dststep].im = t1;
|
|
|
|
t0 = src1[delta0];
|
|
t1 = -src1[delta1];
|
|
|
|
dst[(n-i)*dststep].re = t0;
|
|
dst[(n-i)*dststep].im = t1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Complexd* dst = _dst.ptr<Complexd>();
|
|
const double* src0 = _src0.ptr<double>();
|
|
const double* src1 = _src1.ptr<double>();
|
|
int delta0, delta1;
|
|
|
|
dst->re = src0[0];
|
|
dst->im = 0;
|
|
|
|
if( (n & 1) == 0 )
|
|
{
|
|
dst[n2*dststep].re = src0[(cn == 1 ? n-1 : n2)*srcstep];
|
|
dst[n2*dststep].im = 0;
|
|
}
|
|
|
|
delta0 = srcstep;
|
|
delta1 = delta0 + (cn == 1 ? srcstep : 1);
|
|
if( cn == 1 )
|
|
srcstep *= 2;
|
|
|
|
for( i = 1; i < n2; i++, delta0 += srcstep, delta1 += srcstep )
|
|
{
|
|
double t0 = src0[delta0];
|
|
double t1 = src0[delta1];
|
|
|
|
dst[i*dststep].re = t0;
|
|
dst[i*dststep].im = t1;
|
|
|
|
t0 = src1[delta0];
|
|
t1 = -src1[delta1];
|
|
|
|
dst[(n-i)*dststep].re = t0;
|
|
dst[(n-i)*dststep].im = t1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
static void fixCCS( Mat& mat, int cols, int flags )
|
|
{
|
|
int i, rows = mat.rows;
|
|
int rows2 = (flags & DFT_ROWS) ? rows : rows/2 + 1, cols2 = cols/2 + 1;
|
|
|
|
CV_Assert( cols2 == mat.cols );
|
|
|
|
if( mat.type() == CV_32FC2 )
|
|
{
|
|
for( i = 0; i < rows2; i++ )
|
|
{
|
|
Complexf* row = mat.ptr<Complexf>(i);
|
|
if( (flags & DFT_ROWS) || i == 0 || (i == rows2 - 1 && rows % 2 == 0) )
|
|
{
|
|
row[0].im = 0;
|
|
if( cols % 2 == 0 )
|
|
row[cols2-1].im = 0;
|
|
}
|
|
else
|
|
{
|
|
Complexf* row2 = mat.ptr<Complexf>(rows-i);
|
|
row2[0].re = row[0].re;
|
|
row2[0].im = -row[0].im;
|
|
|
|
if( cols % 2 == 0 )
|
|
{
|
|
row2[cols2-1].re = row[cols2-1].re;
|
|
row2[cols2-1].im = -row[cols2-1].im;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if( mat.type() == CV_64FC2 )
|
|
{
|
|
for( i = 0; i < rows2; i++ )
|
|
{
|
|
Complexd* row = mat.ptr<Complexd>(i);
|
|
if( (flags & DFT_ROWS) || i == 0 || (i == rows2 - 1 && rows % 2 == 0) )
|
|
{
|
|
row[0].im = 0;
|
|
if( cols % 2 == 0 )
|
|
row[cols2-1].im = 0;
|
|
}
|
|
else
|
|
{
|
|
Complexd* row2 = mat.ptr<Complexd>(rows-i);
|
|
row2[0].re = row[0].re;
|
|
row2[0].im = -row[0].im;
|
|
|
|
if( cols % 2 == 0 )
|
|
{
|
|
row2[cols2-1].re = row[cols2-1].re;
|
|
row2[cols2-1].im = -row[cols2-1].im;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static void mulComplex( const Mat& src1, const Mat& src2, Mat& dst, int flags )
|
|
{
|
|
dst.create(src1.rows, src1.cols, src1.type());
|
|
int i, j, depth = src1.depth(), cols = src1.cols*2;
|
|
|
|
CV_Assert( src1.size == src2.size && src1.type() == src2.type() &&
|
|
(src1.type() == CV_32FC2 || src1.type() == CV_64FC2) );
|
|
|
|
const Mat* src1_ = &src1;
|
|
Mat src1_tmp;
|
|
if (dst.data == src1.data)
|
|
{
|
|
src1_tmp = src1.clone();
|
|
src1_ = &src1_tmp;
|
|
}
|
|
const Mat* src2_ = &src2;
|
|
Mat src2_tmp;
|
|
if (dst.data == src2.data)
|
|
{
|
|
src2_tmp = src2.clone();
|
|
src2_ = &src2_tmp;
|
|
}
|
|
|
|
for( i = 0; i < dst.rows; i++ )
|
|
{
|
|
if( depth == CV_32F )
|
|
{
|
|
const float* a = src1_->ptr<float>(i);
|
|
const float* b = src2_->ptr<float>(i);
|
|
float* c = dst.ptr<float>(i);
|
|
|
|
if( !(flags & CV_TEST_DXT_MUL_CONJ) )
|
|
for( j = 0; j < cols; j += 2 )
|
|
{
|
|
double re = (double)a[j]*(double)b[j] - (double)a[j+1]*(double)b[j+1];
|
|
double im = (double)a[j+1]*(double)b[j] + (double)a[j]*(double)b[j+1];
|
|
|
|
c[j] = (float)re;
|
|
c[j+1] = (float)im;
|
|
}
|
|
else
|
|
for( j = 0; j < cols; j += 2 )
|
|
{
|
|
double re = (double)a[j]*(double)b[j] + (double)a[j+1]*(double)b[j+1];
|
|
double im = (double)a[j+1]*(double)b[j] - (double)a[j]*(double)b[j+1];
|
|
|
|
c[j] = (float)re;
|
|
c[j+1] = (float)im;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
const double* a = src1_->ptr<double>(i);
|
|
const double* b = src2_->ptr<double>(i);
|
|
double* c = dst.ptr<double>(i);
|
|
|
|
if( !(flags & CV_TEST_DXT_MUL_CONJ) )
|
|
for( j = 0; j < cols; j += 2 )
|
|
{
|
|
double re = a[j]*b[j] - a[j+1]*b[j+1];
|
|
double im = a[j+1]*b[j] + a[j]*b[j+1];
|
|
|
|
c[j] = re;
|
|
c[j+1] = im;
|
|
}
|
|
else
|
|
for( j = 0; j < cols; j += 2 )
|
|
{
|
|
double re = a[j]*b[j] + a[j+1]*b[j+1];
|
|
double im = a[j+1]*b[j] - a[j]*b[j+1];
|
|
|
|
c[j] = re;
|
|
c[j+1] = im;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class CxCore_DXTBaseTest : public cvtest::ArrayTest
|
|
{
|
|
public:
|
|
typedef cvtest::ArrayTest Base;
|
|
CxCore_DXTBaseTest( bool _allow_complex=false, bool _allow_odd=false,
|
|
bool _spectrum_mode=false );
|
|
protected:
|
|
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
|
|
int prepare_test_case( int test_case_idx );
|
|
double get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ );
|
|
int flags; // transformation flags
|
|
bool allow_complex; // whether input/output may be complex or not:
|
|
// true for DFT and MulSpectrums, false for DCT
|
|
bool allow_odd; // whether input/output may be have odd (!=1) dimensions:
|
|
// true for DFT and MulSpectrums, false for DCT
|
|
bool spectrum_mode; // (2 complex/ccs inputs, 1 complex/ccs output):
|
|
// true for MulSpectrums, false for DFT and DCT
|
|
bool inplace; // inplace operation (set for each individual test case)
|
|
bool temp_dst; // use temporary destination (for real->ccs DFT and ccs MulSpectrums)
|
|
};
|
|
|
|
|
|
CxCore_DXTBaseTest::CxCore_DXTBaseTest( bool _allow_complex, bool _allow_odd, bool _spectrum_mode )
|
|
: Base(), flags(0), allow_complex(_allow_complex), allow_odd(_allow_odd),
|
|
spectrum_mode(_spectrum_mode), inplace(false), temp_dst(false)
|
|
{
|
|
test_array[INPUT].push_back(NULL);
|
|
if( spectrum_mode )
|
|
test_array[INPUT].push_back(NULL);
|
|
test_array[OUTPUT].push_back(NULL);
|
|
test_array[REF_OUTPUT].push_back(NULL);
|
|
test_array[TEMP].push_back(NULL);
|
|
test_array[TEMP].push_back(NULL);
|
|
|
|
max_log_array_size = 9;
|
|
element_wise_relative_error = spectrum_mode;
|
|
}
|
|
|
|
|
|
void CxCore_DXTBaseTest::get_test_array_types_and_sizes( int test_case_idx,
|
|
vector<vector<Size> >& sizes,
|
|
vector<vector<int> >& types )
|
|
{
|
|
RNG& rng = ts->get_rng();
|
|
int bits = cvtest::randInt(rng);
|
|
int depth = cvtest::randInt(rng)%2 + CV_32F;
|
|
int cn = !allow_complex || !(bits & 256) ? 1 : 2;
|
|
Size size;
|
|
Base::get_test_array_types_and_sizes( test_case_idx, sizes, types );
|
|
|
|
flags = bits & (cv::DFT_INVERSE | cv::DFT_SCALE | cv::DFT_ROWS | CV_TEST_DXT_MUL_CONJ);
|
|
if( spectrum_mode )
|
|
flags &= ~cv::DFT_INVERSE;
|
|
types[TEMP][0] = types[TEMP][1] = types[INPUT][0] =
|
|
types[OUTPUT][0] = CV_MAKETYPE(depth, cn);
|
|
size = sizes[INPUT][0];
|
|
|
|
temp_dst = false;
|
|
|
|
if( flags & cv::DFT_ROWS && (bits&1024) )
|
|
{
|
|
if( bits&16 )
|
|
size.width = 1;
|
|
else
|
|
size.height = 1;
|
|
flags &= ~cv::DFT_ROWS;
|
|
}
|
|
|
|
const int P2_MIN_SIZE = 32;
|
|
if( ((bits >> 10) & 1) == 0 )
|
|
{
|
|
size.width = (size.width / P2_MIN_SIZE)*P2_MIN_SIZE;
|
|
size.width = MAX(size.width, 1);
|
|
size.height = (size.height / P2_MIN_SIZE)*P2_MIN_SIZE;
|
|
size.height = MAX(size.height, 1);
|
|
}
|
|
|
|
if( !allow_odd )
|
|
{
|
|
if( size.width > 1 && (size.width&1) != 0 )
|
|
size.width = (size.width + 1) & -2;
|
|
|
|
if( size.height > 1 && (size.height&1) != 0 && !(flags & cv::DFT_ROWS) )
|
|
size.height = (size.height + 1) & -2;
|
|
}
|
|
|
|
sizes[INPUT][0] = sizes[OUTPUT][0] = size;
|
|
sizes[TEMP][0] = sizes[TEMP][1] = cv::Size(0,0);
|
|
|
|
if( spectrum_mode )
|
|
{
|
|
if( cn == 1 )
|
|
{
|
|
types[OUTPUT][0] = CV_MAKETYPE(depth, 2);
|
|
sizes[TEMP][0] = size;
|
|
}
|
|
sizes[INPUT][0] = sizes[INPUT][1] = size;
|
|
types[INPUT][1] = types[INPUT][0];
|
|
}
|
|
else if( /*(cn == 2 && (bits&32)) ||*/ (cn == 1 && allow_complex) )
|
|
{
|
|
types[TEMP][0] = CV_MAKETYPE(depth, 2); // CV_??FC2
|
|
sizes[TEMP][0] = size;
|
|
size = cv::Size(size.width/2+1, size.height);
|
|
|
|
if( flags & cv::DFT_INVERSE )
|
|
{
|
|
if( cn == 2 )
|
|
{
|
|
types[OUTPUT][0] = depth;
|
|
sizes[INPUT][0] = size;
|
|
}
|
|
types[TEMP][1] = types[TEMP][0];
|
|
sizes[TEMP][1] = sizes[TEMP][0];
|
|
}
|
|
else
|
|
{
|
|
if( allow_complex )
|
|
types[OUTPUT][0] = CV_MAKETYPE(depth, 2);
|
|
|
|
if( cn == 2 )
|
|
{
|
|
types[INPUT][0] = depth;
|
|
types[TEMP][1] = types[TEMP][0];
|
|
sizes[TEMP][1] = size;
|
|
}
|
|
else
|
|
{
|
|
types[TEMP][1] = depth;
|
|
sizes[TEMP][1] = sizes[TEMP][0];
|
|
}
|
|
temp_dst = true;
|
|
}
|
|
}
|
|
|
|
inplace = false;
|
|
if( spectrum_mode ||
|
|
(!temp_dst && types[INPUT][0] == types[OUTPUT][0]) ||
|
|
(temp_dst && types[INPUT][0] == types[TEMP][1]) )
|
|
inplace = (bits & 64) != 0;
|
|
|
|
types[REF_OUTPUT][0] = types[OUTPUT][0];
|
|
sizes[REF_OUTPUT][0] = sizes[OUTPUT][0];
|
|
}
|
|
|
|
|
|
double CxCore_DXTBaseTest::get_success_error_level( int test_case_idx, int i, int j )
|
|
{
|
|
return Base::get_success_error_level( test_case_idx, i, j );
|
|
}
|
|
|
|
|
|
int CxCore_DXTBaseTest::prepare_test_case( int test_case_idx )
|
|
{
|
|
int code = Base::prepare_test_case( test_case_idx );
|
|
if( code > 0 )
|
|
{
|
|
int in_type = test_mat[INPUT][0].type();
|
|
int out_type = test_mat[OUTPUT][0].type();
|
|
|
|
if( CV_MAT_CN(in_type) == 2 && CV_MAT_CN(out_type) == 1 )
|
|
fixCCS( test_mat[INPUT][0], test_mat[OUTPUT][0].cols, flags );
|
|
|
|
if( inplace )
|
|
cvtest::copy( test_mat[INPUT][test_case_idx & (int)spectrum_mode],
|
|
temp_dst ? test_mat[TEMP][1] :
|
|
in_type == out_type ? test_mat[OUTPUT][0] :
|
|
test_mat[TEMP][0] );
|
|
}
|
|
|
|
return code;
|
|
}
|
|
|
|
|
|
////////////////////// FFT ////////////////////////
|
|
class CxCore_DFTTest : public CxCore_DXTBaseTest
|
|
{
|
|
public:
|
|
CxCore_DFTTest();
|
|
protected:
|
|
void run_func();
|
|
void prepare_to_validation( int test_case_idx );
|
|
double get_success_error_level( int test_case_idx, int i, int j );
|
|
};
|
|
|
|
CxCore_DFTTest::CxCore_DFTTest() : CxCore_DXTBaseTest( true, true, false )
|
|
{
|
|
}
|
|
|
|
double CxCore_DFTTest::get_success_error_level( int test_case_idx, int i, int j )
|
|
{
|
|
CV_Assert(i == OUTPUT);
|
|
CV_Assert(j == 0);
|
|
|
|
int depth = test_mat[i][j].depth();
|
|
|
|
// NOTE: non-default threshold intorduced for ARMPL integration
|
|
if (depth == CV_32F)
|
|
return 1.5e-4;
|
|
|
|
return CxCore_DXTBaseTest::get_success_error_level(test_case_idx, i, j);
|
|
}
|
|
|
|
void CxCore_DFTTest::run_func()
|
|
{
|
|
Mat& dst = temp_dst ? test_mat[TEMP][1] : test_mat[OUTPUT][0];
|
|
const Mat& src = inplace ? dst : test_mat[INPUT][0];
|
|
|
|
if(!(flags & cv::DFT_INVERSE))
|
|
cv::dft( src, dst, flags );
|
|
else
|
|
cv::idft(src, dst, flags & ~cv::DFT_INVERSE);
|
|
}
|
|
|
|
|
|
void CxCore_DFTTest::prepare_to_validation( int /*test_case_idx*/ )
|
|
{
|
|
Mat& src = test_mat[INPUT][0];
|
|
Mat& dst = test_mat[REF_OUTPUT][0];
|
|
Mat* tmp_src = &src;
|
|
Mat* tmp_dst = &dst;
|
|
int src_cn = src.channels();
|
|
int dst_cn = dst.channels();
|
|
|
|
if( src_cn != 2 || dst_cn != 2 )
|
|
{
|
|
tmp_src = &test_mat[TEMP][0];
|
|
|
|
if( !(flags & cv::DFT_INVERSE ) )
|
|
{
|
|
Mat& cvdft_dst = test_mat[TEMP][1];
|
|
convertFromCCS( cvdft_dst, cvdft_dst,
|
|
test_mat[OUTPUT][0], flags );
|
|
*tmp_src = Scalar::all(0);
|
|
cvtest::insert( src, *tmp_src, 0 );
|
|
}
|
|
else
|
|
{
|
|
convertFromCCS( src, src, *tmp_src, flags );
|
|
tmp_dst = &test_mat[TEMP][1];
|
|
}
|
|
}
|
|
|
|
if( src.rows == 1 || (src.cols == 1 && !(flags & cv::DFT_ROWS)) )
|
|
DFT_1D( *tmp_src, *tmp_dst, flags );
|
|
else
|
|
DFT_2D( *tmp_src, *tmp_dst, flags );
|
|
|
|
if( tmp_dst != &dst )
|
|
cvtest::extract( *tmp_dst, dst, 0 );
|
|
}
|
|
|
|
////////////////////// DCT ////////////////////////
|
|
class CxCore_DCTTest : public CxCore_DXTBaseTest
|
|
{
|
|
public:
|
|
CxCore_DCTTest();
|
|
protected:
|
|
void run_func();
|
|
void prepare_to_validation( int test_case_idx );
|
|
#if defined(HAVE_ARMPL)
|
|
double get_success_error_level( int test_case_idx, int i, int j ) CV_OVERRIDE;
|
|
#endif
|
|
};
|
|
|
|
|
|
CxCore_DCTTest::CxCore_DCTTest() : CxCore_DXTBaseTest( false, false, false )
|
|
{
|
|
}
|
|
|
|
#if defined(HAVE_ARMPL)
|
|
double CxCore_DCTTest::get_success_error_level(int, int i, int j)
|
|
{
|
|
CV_Assert(i == OUTPUT);
|
|
CV_Assert(j == 0);
|
|
|
|
int depth = test_mat[i][j].depth();
|
|
|
|
if (depth == CV_32F)
|
|
return 1.67e-5;
|
|
|
|
return 1e-12;
|
|
}
|
|
#endif
|
|
|
|
void CxCore_DCTTest::run_func()
|
|
{
|
|
Mat& dst = test_mat[OUTPUT][0];
|
|
const Mat& src = inplace ? dst : test_mat[INPUT][0];
|
|
|
|
if(!(flags & cv::DFT_INVERSE))
|
|
cv::dct( src, dst, flags );
|
|
else
|
|
cv::idct( src, dst, flags & ~cv::DFT_INVERSE);
|
|
}
|
|
|
|
|
|
void CxCore_DCTTest::prepare_to_validation( int /*test_case_idx*/ )
|
|
{
|
|
const Mat& src = test_mat[INPUT][0];
|
|
Mat& dst = test_mat[REF_OUTPUT][0];
|
|
|
|
if( src.rows == 1 || (src.cols == 1 && !(flags & cv::DFT_ROWS)) )
|
|
DCT_1D( src, dst, flags );
|
|
else
|
|
DCT_2D( src, dst, flags );
|
|
}
|
|
|
|
|
|
////////////////////// MulSpectrums ////////////////////////
|
|
class CxCore_MulSpectrumsTest : public CxCore_DXTBaseTest
|
|
{
|
|
public:
|
|
CxCore_MulSpectrumsTest();
|
|
protected:
|
|
void run_func();
|
|
void prepare_to_validation( int test_case_idx );
|
|
double get_success_error_level( int test_case_idx, int i, int j );
|
|
};
|
|
|
|
|
|
CxCore_MulSpectrumsTest::CxCore_MulSpectrumsTest() : CxCore_DXTBaseTest( true, true, true )
|
|
{
|
|
}
|
|
|
|
double CxCore_MulSpectrumsTest::get_success_error_level( int test_case_idx, int i, int j )
|
|
{
|
|
CV_UNUSED(test_case_idx);
|
|
CV_Assert(i == OUTPUT);
|
|
CV_Assert(j == 0);
|
|
const int elem_depth = test_mat[i][j].depth();
|
|
CV_Assert(elem_depth == CV_32F || elem_depth == CV_64F);
|
|
|
|
element_wise_relative_error = false;
|
|
double maxInputValue = 1000; // ArrayTest::get_minmax_bounds
|
|
double err = 8 * maxInputValue; // result = A*B + C*D
|
|
return (elem_depth == CV_32F ? FLT_EPSILON : DBL_EPSILON) * err;
|
|
}
|
|
|
|
void CxCore_MulSpectrumsTest::run_func()
|
|
{
|
|
Mat& dst = !test_mat[TEMP].empty() && !test_mat[TEMP][0].empty() ?
|
|
test_mat[TEMP][0] : test_mat[OUTPUT][0];
|
|
Mat *src1 = &test_mat[INPUT][0], *src2 = &test_mat[INPUT][1];
|
|
|
|
if( inplace )
|
|
{
|
|
if( ts->get_current_test_info()->test_case_idx & 1 )
|
|
src2 = &dst;
|
|
else
|
|
src1 = &dst;
|
|
}
|
|
|
|
cv::mulSpectrums( *src1, *src2, dst, flags, (flags & CV_TEST_DXT_MUL_CONJ) != 0 );
|
|
}
|
|
|
|
|
|
void CxCore_MulSpectrumsTest::prepare_to_validation( int /*test_case_idx*/ )
|
|
{
|
|
Mat* src1 = &test_mat[INPUT][0];
|
|
Mat* src2 = &test_mat[INPUT][1];
|
|
Mat& dst = test_mat[OUTPUT][0];
|
|
Mat& dst0 = test_mat[REF_OUTPUT][0];
|
|
int cn = src1->channels();
|
|
|
|
if( cn == 1 )
|
|
{
|
|
convertFromCCS( *src1, *src1, dst, flags );
|
|
convertFromCCS( *src2, *src2, dst0, flags );
|
|
src1 = &dst;
|
|
src2 = &dst0;
|
|
}
|
|
|
|
mulComplex( *src1, *src2, dst0, flags );
|
|
if( cn == 1 )
|
|
{
|
|
Mat& temp = test_mat[TEMP][0];
|
|
convertFromCCS( temp, temp, dst, flags );
|
|
}
|
|
}
|
|
|
|
////////////////////// DivSpectrums ////////////////////////
|
|
class CV_DivSpectrumsTest : public cvtest::ArrayTest
|
|
{
|
|
public:
|
|
CV_DivSpectrumsTest();
|
|
protected:
|
|
void run_func();
|
|
void get_test_array_types_and_sizes( int, vector<vector<Size> >& sizes, vector<vector<int> >& types );
|
|
void prepare_to_validation( int test_case_idx );
|
|
int flags;
|
|
};
|
|
|
|
|
|
CV_DivSpectrumsTest::CV_DivSpectrumsTest() : flags(0)
|
|
{
|
|
// Allocate test matrices.
|
|
test_array[INPUT].push_back(NULL); // first input DFT as a CCS-packed array or complex matrix.
|
|
test_array[INPUT].push_back(NULL); // second input DFT as a CCS-packed array or complex matrix.
|
|
test_array[OUTPUT].push_back(NULL); // output DFT as a complex matrix.
|
|
test_array[REF_OUTPUT].push_back(NULL); // reference output DFT as a complex matrix.
|
|
test_array[TEMP].push_back(NULL); // first input DFT converted to a complex matrix.
|
|
test_array[TEMP].push_back(NULL); // second input DFT converted to a complex matrix.
|
|
test_array[TEMP].push_back(NULL); // output DFT as a CCV-packed array.
|
|
}
|
|
|
|
void CV_DivSpectrumsTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
|
|
{
|
|
cvtest::ArrayTest::get_test_array_types_and_sizes(test_case_idx, sizes, types);
|
|
RNG& rng = ts->get_rng();
|
|
|
|
// Get the flag of the input.
|
|
const int rand_int_flags = cvtest::randInt(rng);
|
|
flags = rand_int_flags & (CV_TEST_DXT_MUL_CONJ | DFT_ROWS);
|
|
|
|
// Get input type.
|
|
const int rand_int_type = cvtest::randInt(rng);
|
|
int type;
|
|
|
|
if (rand_int_type % 4)
|
|
{
|
|
type = CV_32FC1;
|
|
}
|
|
else if (rand_int_type % 4 == 1)
|
|
{
|
|
type = CV_32FC2;
|
|
}
|
|
else if (rand_int_type % 4 == 2)
|
|
{
|
|
type = CV_64FC1;
|
|
}
|
|
else
|
|
{
|
|
type = CV_64FC2;
|
|
}
|
|
|
|
for( size_t i = 0; i < types.size(); i++ )
|
|
{
|
|
for( size_t j = 0; j < types[i].size(); j++ )
|
|
{
|
|
types[i][j] = type;
|
|
}
|
|
}
|
|
|
|
// Inputs are CCS-packed arrays. Prepare outputs and temporary inputs as complex matrices.
|
|
if( type == CV_32FC1 || type == CV_64FC1 )
|
|
{
|
|
types[OUTPUT][0] += CV_DEPTH_MAX;
|
|
types[REF_OUTPUT][0] += CV_DEPTH_MAX;
|
|
types[TEMP][0] += CV_DEPTH_MAX;
|
|
types[TEMP][1] += CV_DEPTH_MAX;
|
|
}
|
|
}
|
|
|
|
/// Helper function to convert a ccs array of depth_t into a complex matrix.
|
|
template<typename depth_t>
|
|
static void convert_from_ccs_helper( const Mat& src0, const Mat& src1, Mat& dst )
|
|
{
|
|
const int cn = src0.channels();
|
|
int srcstep = cn;
|
|
int dststep = 1;
|
|
|
|
if( !dst.isContinuous() )
|
|
dststep = (int)(dst.step/dst.elemSize());
|
|
|
|
if( !src0.isContinuous() )
|
|
srcstep = (int)(src0.step/src0.elemSize1());
|
|
|
|
Complex<depth_t> *dst_data = dst.ptr<Complex<depth_t> >();
|
|
const depth_t* src0_data = src0.ptr<depth_t>();
|
|
const depth_t* src1_data = src1.ptr<depth_t>();
|
|
dst_data->re = src0_data[0];
|
|
dst_data->im = 0;
|
|
const int n = dst.cols + dst.rows - 1;
|
|
const int n2 = (n+1) >> 1;
|
|
|
|
if( (n & 1) == 0 )
|
|
{
|
|
dst_data[n2*dststep].re = src0_data[(cn == 1 ? n-1 : n2)*srcstep];
|
|
dst_data[n2*dststep].im = 0;
|
|
}
|
|
|
|
int delta0 = srcstep;
|
|
int delta1 = delta0 + (cn == 1 ? srcstep : 1);
|
|
|
|
if( cn == 1 )
|
|
srcstep *= 2;
|
|
|
|
for( int i = 1; i < n2; i++, delta0 += srcstep, delta1 += srcstep )
|
|
{
|
|
depth_t t0 = src0_data[delta0];
|
|
depth_t t1 = src0_data[delta1];
|
|
|
|
dst_data[i*dststep].re = t0;
|
|
dst_data[i*dststep].im = t1;
|
|
|
|
t0 = src1_data[delta0];
|
|
t1 = -src1_data[delta1];
|
|
|
|
dst_data[(n-i)*dststep].re = t0;
|
|
dst_data[(n-i)*dststep].im = t1;
|
|
}
|
|
}
|
|
|
|
/// Helper function to convert a ccs array into a complex matrix.
|
|
static void convert_from_ccs( const Mat& src0, const Mat& src1, Mat& dst, const int flags )
|
|
{
|
|
if( dst.rows > 1 && (dst.cols > 1 || (flags & DFT_ROWS)) )
|
|
{
|
|
const int count = dst.rows;
|
|
const int len = dst.cols;
|
|
const bool is2d = (flags & DFT_ROWS) == 0;
|
|
for( int i = 0; i < count; i++ )
|
|
{
|
|
const int j = !is2d || i == 0 ? i : count - i;
|
|
const Mat& src0row = src0.row(i);
|
|
const Mat& src1row = src1.row(j);
|
|
Mat dstrow = dst.row(i);
|
|
convert_from_ccs( src0row, src1row, dstrow, 0 );
|
|
}
|
|
|
|
if( is2d )
|
|
{
|
|
const Mat& src0row = src0.col(0);
|
|
Mat dstrow = dst.col(0);
|
|
convert_from_ccs( src0row, src0row, dstrow, 0 );
|
|
|
|
if( (len & 1) == 0 )
|
|
{
|
|
const Mat& src0row_even = src0.col(src0.cols - 1);
|
|
Mat dstrow_even = dst.col(len/2);
|
|
convert_from_ccs( src0row_even, src0row_even, dstrow_even, 0 );
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if( dst.depth() == CV_32F )
|
|
{
|
|
convert_from_ccs_helper<float>( src0, src1, dst );
|
|
}
|
|
else
|
|
{
|
|
convert_from_ccs_helper<double>( src0, src1, dst );
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Helper function to compute complex number (nu_re + nu_im * i) / (de_re + de_im * i).
|
|
static std::pair<double, double> divide_complex_numbers( const double nu_re, const double nu_im,
|
|
const double de_re, const double de_im,
|
|
const bool conj_de )
|
|
{
|
|
if ( conj_de )
|
|
{
|
|
return divide_complex_numbers( nu_re, nu_im, de_re, -de_im, false /* conj_de */ );
|
|
}
|
|
|
|
const double result_de = de_re * de_re + de_im * de_im + DBL_EPSILON;
|
|
const double result_re = nu_re * de_re + nu_im * de_im;
|
|
const double result_im = nu_re * (-de_im) + nu_im * de_re;
|
|
return std::pair<double, double>(result_re / result_de, result_im / result_de);
|
|
}
|
|
|
|
/// Helper function to divide a DFT in src1 by a DFT in src2 with depths depth_t. The DFTs are
|
|
/// complex matrices.
|
|
template <typename depth_t>
|
|
static void div_complex_helper( const Mat& src1, const Mat& src2, Mat& dst, int flags )
|
|
{
|
|
CV_Assert( src1.size == src2.size && src1.type() == src2.type() );
|
|
dst.create( src1.rows, src1.cols, src1.type() );
|
|
const int cn = src1.channels();
|
|
int cols = src1.cols * cn;
|
|
|
|
for( int i = 0; i < dst.rows; i++ )
|
|
{
|
|
const depth_t *src1_data = src1.ptr<depth_t>(i);
|
|
const depth_t *src2_data = src2.ptr<depth_t>(i);
|
|
depth_t *dst_data = dst.ptr<depth_t>(i);
|
|
for( int j = 0; j < cols; j += 2 )
|
|
{
|
|
std::pair<double, double> result =
|
|
divide_complex_numbers( src1_data[j], src1_data[j + 1],
|
|
src2_data[j], src2_data[j + 1],
|
|
(flags & CV_TEST_DXT_MUL_CONJ) != 0 );
|
|
dst_data[j] = (depth_t)result.first;
|
|
dst_data[j + 1] = (depth_t)result.second;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Helper function to divide a DFT in src1 by a DFT in src2. The DFTs are complex matrices.
|
|
static void div_complex( const Mat& src1, const Mat& src2, Mat& dst, const int flags )
|
|
{
|
|
const int type = src1.type();
|
|
CV_Assert( type == CV_32FC2 || type == CV_64FC2 );
|
|
|
|
if ( src1.depth() == CV_32F )
|
|
{
|
|
return div_complex_helper<float>( src1, src2, dst, flags );
|
|
}
|
|
else
|
|
{
|
|
return div_complex_helper<double>( src1, src2, dst, flags );
|
|
}
|
|
}
|
|
|
|
void CV_DivSpectrumsTest::prepare_to_validation( int /* test_case_idx */ )
|
|
{
|
|
Mat &src1 = test_mat[INPUT][0];
|
|
Mat &src2 = test_mat[INPUT][1];
|
|
Mat &ref_dst = test_mat[REF_OUTPUT][0];
|
|
const int cn = src1.channels();
|
|
// Inputs are CCS-packed arrays. Convert them to complex matrices and get the expected output
|
|
// as a complex matrix.
|
|
if( cn == 1 )
|
|
{
|
|
Mat &converted_src1 = test_mat[TEMP][0];
|
|
Mat &converted_src2 = test_mat[TEMP][1];
|
|
convert_from_ccs( src1, src1, converted_src1, flags );
|
|
convert_from_ccs( src2, src2, converted_src2, flags );
|
|
div_complex( converted_src1, converted_src2, ref_dst, flags );
|
|
}
|
|
// Inputs are complex matrices. Get the expected output as a complex matrix.
|
|
else
|
|
{
|
|
div_complex( src1, src2, ref_dst, flags );
|
|
}
|
|
}
|
|
|
|
void CV_DivSpectrumsTest::run_func()
|
|
{
|
|
const Mat &src1 = test_mat[INPUT][0];
|
|
const Mat &src2 = test_mat[INPUT][1];
|
|
const int cn = src1.channels();
|
|
|
|
// Inputs are CCS-packed arrays. Get the output as a CCS-packed array and convert it to a
|
|
// complex matrix.
|
|
if ( cn == 1 )
|
|
{
|
|
Mat &dst = test_mat[TEMP][2];
|
|
cv::divSpectrums( src1, src2, dst, flags, (flags & CV_TEST_DXT_MUL_CONJ) != 0 );
|
|
Mat &converted_dst = test_mat[OUTPUT][0];
|
|
convert_from_ccs( dst, dst, converted_dst, flags );
|
|
}
|
|
// Inputs are complex matrices. Get the output as a complex matrix.
|
|
else
|
|
{
|
|
Mat &dst = test_mat[OUTPUT][0];
|
|
cv::divSpectrums( src1, src2, dst, flags, (flags & CV_TEST_DXT_MUL_CONJ) != 0 );
|
|
}
|
|
}
|
|
|
|
TEST(Core_DCT, accuracy) { CxCore_DCTTest test; test.safe_run(); }
|
|
TEST(Core_DFT, accuracy) { CxCore_DFTTest test; test.safe_run(); }
|
|
TEST(Core_MulSpectrums, accuracy) { CxCore_MulSpectrumsTest test; test.safe_run(); }
|
|
TEST(Core_DivSpectrums, accuracy) { CV_DivSpectrumsTest test; test.safe_run(); }
|
|
|
|
|
|
class Core_DFTComplexOutputTest : public cvtest::BaseTest
|
|
{
|
|
public:
|
|
Core_DFTComplexOutputTest() {}
|
|
~Core_DFTComplexOutputTest() {}
|
|
protected:
|
|
void run(int)
|
|
{
|
|
RNG& rng = theRNG();
|
|
for( int i = 0; i < 10; i++ )
|
|
{
|
|
int m = rng.uniform(2, 11);
|
|
int n = rng.uniform(2, 11);
|
|
int depth = rng.uniform(0, 2) + CV_32F;
|
|
Mat src8u(m, n, depth), src(m, n, depth), dst(m, n, CV_MAKETYPE(depth, 2));
|
|
Mat z = Mat::zeros(m, n, depth), dstz;
|
|
randu(src8u, Scalar::all(0), Scalar::all(10));
|
|
src8u.convertTo(src, src.type());
|
|
dst = Scalar::all(123);
|
|
Mat mv[] = {src, z}, srcz;
|
|
merge(mv, 2, srcz);
|
|
dft(srcz, dstz);
|
|
dft(src, dst, DFT_COMPLEX_OUTPUT);
|
|
if (cvtest::norm(dst, dstz, NORM_INF) > 1e-3)
|
|
{
|
|
cout << "actual:\n" << dst << endl << endl;
|
|
cout << "reference:\n" << dstz << endl << endl;
|
|
CV_Error(cv::Error::StsError, "");
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
TEST(Core_DFT, complex_output) { Core_DFTComplexOutputTest test; test.safe_run(); }
|
|
|
|
TEST(Core_DFT, complex_output2)
|
|
{
|
|
for( int i = 0; i < 100; i++ )
|
|
{
|
|
int type = theRNG().uniform(0, 2) ? CV_64F : CV_32F;
|
|
int m = theRNG().uniform(1, 10);
|
|
int n = theRNG().uniform(1, 10);
|
|
Mat x(m, n, type), out;
|
|
randu(x, -1., 1.);
|
|
dft(x, out, DFT_ROWS | DFT_COMPLEX_OUTPUT);
|
|
double nrm = cvtest::norm(out, NORM_INF);
|
|
double thresh = n*m*2;
|
|
if( nrm > thresh )
|
|
{
|
|
cout << "x: " << x << endl;
|
|
cout << "out: " << out << endl;
|
|
ASSERT_LT(nrm, thresh);
|
|
}
|
|
}
|
|
}
|
|
|
|
class Core_DXTReverseTest : public cvtest::BaseTest
|
|
{
|
|
public:
|
|
enum Mode
|
|
{
|
|
ModeDFT,
|
|
ModeDCT
|
|
};
|
|
Core_DXTReverseTest(Mode m) : mode(m) {}
|
|
private:
|
|
Mode mode;
|
|
protected:
|
|
void run(int)
|
|
{
|
|
for (int i = 0; i < 3; ++i)
|
|
{
|
|
if (mode == ModeDCT && i != 0)
|
|
continue;
|
|
int flags = 0;
|
|
int flags_inv = DFT_INVERSE | DFT_SCALE;
|
|
int cn_in = 0;
|
|
int cn_out = 0;
|
|
switch (i)
|
|
{
|
|
case 0: cn_in = 1; cn_out = 1; break;
|
|
case 1: cn_in = 1; cn_out = 2; flags |= DFT_COMPLEX_OUTPUT; flags_inv |= DFT_REAL_OUTPUT; break;
|
|
case 2: cn_in = 2; cn_out = 2; break;
|
|
};
|
|
for (int j = 0; j < 100; ++j)
|
|
{
|
|
RNG& rng = ts->get_rng();
|
|
int type = rng.uniform(0, 2) ? CV_64F : CV_32F;
|
|
int m = rng.uniform(1, 10);
|
|
int n = rng.uniform(1, 10);
|
|
if (mode == ModeDCT)
|
|
{
|
|
m *= 2;
|
|
n *= 2;
|
|
}
|
|
Mat one(m, n, CV_MAKETYPE(type, cn_in));
|
|
cvtest::randUni(rng, one, Scalar::all(-1.), Scalar::all(1.));
|
|
Mat out;
|
|
Mat two;
|
|
if (mode == ModeDFT)
|
|
{
|
|
cv::dft(one, out, flags);
|
|
cv::dft(out, two, flags_inv);
|
|
}
|
|
else if (mode == ModeDCT)
|
|
{
|
|
cv::dct(one, out, flags);
|
|
cv::dct(out, two, flags_inv);
|
|
}
|
|
if (out.channels() != cn_out || two.channels() != cn_in || cvtest::norm(one, two, NORM_INF) > 1e-5)
|
|
{
|
|
cout << "Test #" << j + 1 << " - "
|
|
<< "elements: " << m << " x " << n << ", "
|
|
<< "channels: "
|
|
<< one.channels() << " (" << cn_in << ")" << " -> "
|
|
<< out.channels() << " (" << cn_out << ")" << " -> "
|
|
<< two.channels() << " (" << cn_in << ")"
|
|
<< endl;
|
|
cout << "signal:\n" << one << endl << endl;
|
|
cout << "spectrum:\n" << out << endl << endl;
|
|
cout << "inverse:\n" << two << endl << endl;
|
|
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
TEST(Core_DFT, reverse) { Core_DXTReverseTest test(Core_DXTReverseTest::ModeDFT); test.safe_run(); }
|
|
TEST(Core_DCT, reverse) { Core_DXTReverseTest test(Core_DXTReverseTest::ModeDCT); test.safe_run(); }
|
|
|
|
//////////////////////// 1D size sweep against the naive references ////////////////////////
|
|
// Covers every 5-smooth length up to 2048 plus small, odd, prime and "awkward" lengths, so that
|
|
// all radices (2/4/8 first stage, 3, 5, generic odd), all stage modes and all packing variants
|
|
// (CCS even/odd, DFT_COMPLEX_OUTPUT, DFT_REAL_OUTPUT, DCT/IDCT rows and columns) are exercised.
|
|
// Comparisons are tolerance-based: |out - ref| <= c*(log2(n)+2)*eps(T)*max|ref|.
|
|
|
|
static std::vector<int> dxtSweepSizes()
|
|
{
|
|
std::vector<int> sizes;
|
|
for (int n = 1; n <= 2048; n++)
|
|
{
|
|
int m = n;
|
|
while (m % 2 == 0) m /= 2;
|
|
while (m % 3 == 0) m /= 3;
|
|
while (m % 5 == 0) m /= 5;
|
|
if (m == 1)
|
|
sizes.push_back(n);
|
|
}
|
|
static const int extra[] = { 7, 11, 13, 14, 21, 22, 26, 49, 77, 98, 121, 143, 154, 242, 338, 1001, 2018 };
|
|
for (size_t i = 0; i < sizeof(extra)/sizeof(extra[0]); i++)
|
|
sizes.push_back(extra[i]);
|
|
return sizes;
|
|
}
|
|
|
|
static double dxtSweepThresh(int depth, int n)
|
|
{
|
|
double eps = depth == CV_32F ? FLT_EPSILON : DBL_EPSILON;
|
|
// a generic odd-radix stage accumulates O(radix) terms per output, so the largest prime
|
|
// factor enters the error bound linearly
|
|
int m = n, maxf = 1;
|
|
for (int f = 2; f*f <= m; f++)
|
|
while (m % f == 0) { m /= f; maxf = f; }
|
|
maxf = std::max(maxf, m);
|
|
return 32*eps*(std::log((double)n)/std::log(2.) + 2 + maxf/16.);
|
|
}
|
|
|
|
// max |a - b| relative to max |b|
|
|
static double dxtRelDiff(const Mat& a, const Mat& b)
|
|
{
|
|
double maxb = cvtest::norm(b, NORM_INF);
|
|
return cvtest::norm(a, b, NORM_INF)/std::max(maxb, 1e-300);
|
|
}
|
|
|
|
static Mat dxtRandom(RNG& rng, int rows, int cols, int type)
|
|
{
|
|
Mat m(rows, cols, type);
|
|
cvtest::randUni(rng, m, Scalar::all(-1.), Scalar::all(1.));
|
|
return m;
|
|
}
|
|
|
|
TEST(Core_DFT, sweep_1d)
|
|
{
|
|
RNG& rng = cvtest::TS::ptr()->get_rng();
|
|
std::vector<int> sizes = dxtSweepSizes();
|
|
for (size_t si = 0; si < sizes.size(); si++)
|
|
{
|
|
int n = sizes[si];
|
|
for (int depth = CV_32F; depth <= CV_64F; depth += CV_64F - CV_32F)
|
|
{
|
|
double thresh = dxtSweepThresh(depth, n);
|
|
for (int sc = 0; sc < 2; sc++)
|
|
{
|
|
int scale_flag = sc ? DFT_SCALE : 0;
|
|
SCOPED_TRACE(cv::format("n=%d depth=%d scale=%d", n, depth, sc));
|
|
|
|
// complex -> complex, forward and inverse, and the round trip
|
|
Mat x = dxtRandom(rng, 1, n, CV_MAKETYPE(depth, 2)), ref, out, back;
|
|
DFT_1D(x, ref, scale_flag);
|
|
cv::dft(x, out, scale_flag);
|
|
EXPECT_LE(dxtRelDiff(out, ref), thresh) << "C2C forward";
|
|
DFT_1D(x, ref, DFT_INVERSE | scale_flag);
|
|
cv::dft(x, out, DFT_INVERSE | scale_flag);
|
|
EXPECT_LE(dxtRelDiff(out, ref), thresh) << "C2C inverse";
|
|
cv::dft(x, out, 0);
|
|
cv::dft(out, back, DFT_INVERSE | DFT_SCALE);
|
|
EXPECT_LE(dxtRelDiff(back, x), thresh) << "C2C round trip";
|
|
|
|
// real -> CCS, real -> complex, and back
|
|
Mat xr = dxtRandom(rng, 1, n, depth), xc(1, n, CV_MAKETYPE(depth, 2)), full;
|
|
xc = Scalar::all(0);
|
|
cvtest::insert(xr, xc, 0);
|
|
DFT_1D(xc, ref, scale_flag);
|
|
cv::dft(xr, out, scale_flag);
|
|
ASSERT_EQ(out.type(), depth);
|
|
full.create(1, n, CV_MAKETYPE(depth, 2));
|
|
convertFromCCS(out, out, full, 0);
|
|
EXPECT_LE(dxtRelDiff(full, ref), thresh) << "R2C (CCS)";
|
|
cv::dft(xr, out, scale_flag | DFT_COMPLEX_OUTPUT);
|
|
ASSERT_EQ(out.type(), CV_MAKETYPE(depth, 2));
|
|
EXPECT_LE(dxtRelDiff(out, ref), thresh) << "R2C (complex output)";
|
|
|
|
cv::dft(xr, out, 0);
|
|
cv::dft(out, back, DFT_INVERSE | DFT_SCALE); // CCS -> real
|
|
ASSERT_EQ(back.type(), depth);
|
|
EXPECT_LE(dxtRelDiff(back, xr), thresh) << "C2R (CCS input)";
|
|
cv::dft(xr, out, DFT_COMPLEX_OUTPUT);
|
|
cv::dft(out, back, DFT_INVERSE | DFT_SCALE | DFT_REAL_OUTPUT); // complex -> real
|
|
ASSERT_EQ(back.type(), depth);
|
|
EXPECT_LE(dxtRelDiff(back, xr), thresh) << "C2R (complex input)";
|
|
|
|
// in-place
|
|
Mat y = x.clone();
|
|
cv::dft(y, y, scale_flag);
|
|
DFT_1D(x, ref, scale_flag);
|
|
EXPECT_LE(dxtRelDiff(y, ref), thresh) << "C2C in-place";
|
|
y = xr.clone();
|
|
cv::dft(y, y, scale_flag);
|
|
cv::dft(xr, out, scale_flag);
|
|
EXPECT_LE(dxtRelDiff(y, out), thresh) << "R2C in-place";
|
|
cv::dft(y, y, DFT_INVERSE | DFT_SCALE);
|
|
if (!sc)
|
|
{
|
|
EXPECT_LE(dxtRelDiff(y, xr), thresh) << "C2R in-place";
|
|
}
|
|
|
|
// column vectors
|
|
Mat xcol = x.t(), refcol;
|
|
DFT_1D(xcol, refcol, scale_flag);
|
|
cv::dft(xcol, out, scale_flag);
|
|
EXPECT_LE(dxtRelDiff(out, refcol), thresh) << "C2C column";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// several rows through one plan (DFT_ROWS): every row must match the 1D transform of that row
|
|
TEST(Core_DFT, sweep_rows)
|
|
{
|
|
RNG& rng = cvtest::TS::ptr()->get_rng();
|
|
static const int sizes[] = { 1, 2, 3, 4, 5, 6, 8, 12, 15, 16, 30, 64, 100, 128, 243, 256, 1000, 1024 };
|
|
for (size_t si = 0; si < sizeof(sizes)/sizeof(sizes[0]); si++)
|
|
{
|
|
int n = sizes[si], rows = 5;
|
|
for (int depth = CV_32F; depth <= CV_64F; depth += CV_64F - CV_32F)
|
|
{
|
|
double thresh = dxtSweepThresh(depth, n);
|
|
SCOPED_TRACE(cv::format("n=%d depth=%d", n, depth));
|
|
for (int cn = 1; cn <= 2; cn++)
|
|
{
|
|
for (int inv = 0; inv < 2; inv++)
|
|
{
|
|
int flags = DFT_ROWS | (inv ? DFT_INVERSE | DFT_SCALE : 0);
|
|
Mat x = dxtRandom(rng, rows, n, CV_MAKETYPE(depth, cn)), out, rowout;
|
|
cv::dft(x, out, flags);
|
|
for (int i = 0; i < rows; i++)
|
|
{
|
|
cv::dft(x.row(i), rowout, flags & ~DFT_ROWS);
|
|
EXPECT_LE(dxtRelDiff(out.row(i), rowout), thresh) << "cn=" << cn << " inv=" << inv << " row " << i;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST(Core_DCT, sweep_1d)
|
|
{
|
|
RNG& rng = cvtest::TS::ptr()->get_rng();
|
|
std::vector<int> sizes = dxtSweepSizes();
|
|
for (size_t si = 0; si < sizes.size(); si++)
|
|
{
|
|
int n = sizes[si];
|
|
if (n % 2 != 0)
|
|
continue; // cv::dct() supports even lengths only
|
|
for (int depth = CV_32F; depth <= CV_64F; depth += CV_64F - CV_32F)
|
|
{
|
|
double thresh = dxtSweepThresh(depth, n);
|
|
SCOPED_TRACE(cv::format("n=%d depth=%d", n, depth));
|
|
Mat x = dxtRandom(rng, 1, n, depth), ref, out, back;
|
|
DCT_1D(x, ref, 0);
|
|
cv::dct(x, out, 0);
|
|
EXPECT_LE(dxtRelDiff(out, ref), thresh) << "DCT";
|
|
DCT_1D(x, ref, DCT_INVERSE);
|
|
cv::dct(x, out, DCT_INVERSE);
|
|
EXPECT_LE(dxtRelDiff(out, ref), thresh) << "IDCT";
|
|
cv::dct(x, out, 0);
|
|
cv::dct(out, back, DCT_INVERSE);
|
|
EXPECT_LE(dxtRelDiff(back, x), thresh) << "DCT round trip";
|
|
|
|
// in-place
|
|
Mat y = x.clone();
|
|
cv::dct(y, y, 0);
|
|
EXPECT_LE(dxtRelDiff(y, out), thresh) << "DCT in-place";
|
|
|
|
// column vector (strided source/destination inside the engine)
|
|
Mat xcol = x.t(), refcol;
|
|
DCT_1D(xcol, refcol, 0);
|
|
cv::dct(xcol, out, 0);
|
|
EXPECT_LE(dxtRelDiff(out, refcol), thresh) << "DCT column";
|
|
DCT_1D(xcol, refcol, DCT_INVERSE);
|
|
cv::dct(xcol, out, DCT_INVERSE);
|
|
EXPECT_LE(dxtRelDiff(out, refcol), thresh) << "IDCT column";
|
|
|
|
// rows batch
|
|
Mat xm = dxtRandom(rng, 3, n, depth), outm, rowout;
|
|
cv::dct(xm, outm, DCT_ROWS);
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
cv::dct(xm.row(i), rowout, 0);
|
|
EXPECT_LE(dxtRelDiff(outm.row(i), rowout), thresh) << "DCT rows, row " << i;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// large 2D / DFT_ROWS / DCT transforms go through the parallel path; they must match the
|
|
// single-threaded result (same plan, different workspaces)
|
|
TEST(Core_DFT, parallel_large)
|
|
{
|
|
RNG& rng = cvtest::TS::ptr()->get_rng();
|
|
int nthreads0 = getNumThreads();
|
|
static const int sizes[][2] = { {1024, 1024}, {700, 1500}, {1, 1 << 20}, {1 << 18, 1}, {513, 2000} };
|
|
for (size_t si = 0; si < sizeof(sizes)/sizeof(sizes[0]); si++)
|
|
{
|
|
int rows = sizes[si][0], cols = sizes[si][1];
|
|
for (int depth = CV_32F; depth <= CV_64F; depth += CV_64F - CV_32F)
|
|
{
|
|
double thresh = dxtSweepThresh(depth, std::max(rows, cols));
|
|
for (int cn = 1; cn <= 2; cn++)
|
|
{
|
|
static const int flag_sets[] = { 0, DFT_ROWS, DFT_INVERSE | DFT_SCALE, DFT_COMPLEX_OUTPUT,
|
|
DFT_INVERSE | DFT_SCALE | DFT_REAL_OUTPUT };
|
|
for (size_t fi = 0; fi < sizeof(flag_sets)/sizeof(flag_sets[0]); fi++)
|
|
{
|
|
int flags = flag_sets[fi];
|
|
if ((flags & DFT_COMPLEX_OUTPUT) && cn != 1) continue;
|
|
if ((flags & DFT_REAL_OUTPUT) && cn != 2) continue;
|
|
SCOPED_TRACE(cv::format("%dx%d depth=%d cn=%d flags=%d", rows, cols, depth, cn, flags));
|
|
Mat x = dxtRandom(rng, rows, cols, CV_MAKETYPE(depth, cn)), ref, out;
|
|
if ((flags & DFT_INVERSE) && cn == 1)
|
|
{
|
|
Mat xr = dxtRandom(rng, rows, cols, depth);
|
|
cv::dft(xr, x, 0); // a valid CCS spectrum
|
|
}
|
|
setNumThreads(1);
|
|
cv::dft(x, ref, flags);
|
|
setNumThreads(nthreads0);
|
|
cv::dft(x, out, flags);
|
|
EXPECT_LE(dxtRelDiff(out, ref), thresh);
|
|
if (cn == 1 && !(flags & (DFT_INVERSE | DFT_COMPLEX_OUTPUT)) && rows % 2 == 0 && cols % 2 == 0)
|
|
{
|
|
setNumThreads(1);
|
|
cv::dct(x, ref, flags & DFT_ROWS ? DCT_ROWS : 0);
|
|
setNumThreads(nthreads0);
|
|
cv::dct(x, out, flags & DFT_ROWS ? DCT_ROWS : 0);
|
|
EXPECT_LE(dxtRelDiff(out, ref), thresh) << "dct";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
setNumThreads(nthreads0);
|
|
}
|
|
|
|
// 2D transform with nonzero_rows (only the first rows of the input are non-zero; the rest is
|
|
// not read on the forward pass, and only the first rows of the output are needed on the inverse)
|
|
TEST(Core_DFT, nonzero_rows)
|
|
{
|
|
RNG& rng = cvtest::TS::ptr()->get_rng();
|
|
static const int sizes[][2] = { {8, 16}, {15, 20}, {32, 32}, {60, 100}, {128, 96} };
|
|
for (size_t si = 0; si < sizeof(sizes)/sizeof(sizes[0]); si++)
|
|
{
|
|
int rows = sizes[si][0], cols = sizes[si][1];
|
|
for (int depth = CV_32F; depth <= CV_64F; depth += CV_64F - CV_32F)
|
|
{
|
|
double thresh = dxtSweepThresh(depth, rows*cols);
|
|
SCOPED_TRACE(cv::format("%dx%d depth=%d", rows, cols, depth));
|
|
for (int cn = 1; cn <= 2; cn++)
|
|
{
|
|
int nz = rows/2 + 1;
|
|
Mat x = dxtRandom(rng, rows, cols, CV_MAKETYPE(depth, cn));
|
|
x.rowRange(nz, rows) = Scalar::all(0);
|
|
Mat ref, out;
|
|
cv::dft(x, ref, 0);
|
|
cv::dft(x, out, 0, nz);
|
|
EXPECT_LE(dxtRelDiff(out, ref), thresh) << "forward, cn=" << cn;
|
|
Mat back, backref;
|
|
if (cn == 1)
|
|
{
|
|
// real inverse (columns first, then rows): only the first nz output rows are computed
|
|
cv::dft(ref, backref, DFT_INVERSE | DFT_SCALE);
|
|
cv::dft(ref, back, DFT_INVERSE | DFT_SCALE, nz);
|
|
EXPECT_LE(dxtRelDiff(back.rowRange(0, nz), backref.rowRange(0, nz)), thresh) << "inverse, cn=" << cn;
|
|
}
|
|
else
|
|
{
|
|
// complex inverse (rows first, then columns): nonzero_rows refers to the input rows
|
|
ref.rowRange(nz, rows) = Scalar::all(0);
|
|
cv::dft(ref, backref, DFT_INVERSE | DFT_SCALE);
|
|
cv::dft(ref, back, DFT_INVERSE | DFT_SCALE, nz);
|
|
EXPECT_LE(dxtRelDiff(back, backref), thresh) << "inverse, cn=" << cn;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}} // namespace
|