mirror of
https://github.com/opencv/opencv.git
synced 2026-11-25 02:38:14 -06:00
Further FFT/DFT/DCT optimizations - #30618 Merge together with https://github.com/opencv/opencv_extra/pull/1456 ### Summary Follow-up to #30061 and relates to #30467. It fixes the 64F 1D regressions measured on x86 after that PR, removes the O(r²) memory of the big prime radix, adds radix-8 stages, and makes the twiddle tables ~1 ulp accurate. Everything below was measured without IPP (`WITH_IPP=OFF`), so the numbers compare the OpenCV code paths only; the IPP logic is not touched. Four commits, each self-contained: 1. **Memory, plan overhead, vectorization** (784be814b2) - Root cause of the x86 1D regressions: `cv::dft()` builds a plan per call, and at n = 65536 the engine allocated three MB-sized blocks per call (twiddles, permutation, workspace). glibc trims the heap after every free (its dynamic trim threshold is 2x the largest freed block), so every call page-faulted the buffers again: 65536 64FC2 ran at 620 us, ~215 us of it outside the transform. Fix: tables, permutation and the owner's workspace are **one allocation** per plan. No memory is retained between calls, no plan cache. - Static, lazily built tables for power-of-two lengths nc ≤ 1024 (stage twiddles, digit-reversal, real-FFT twiddles): plan building for 256..2048 points is table lookups only (0.06 us). - Generic odd radix: `cs/sn` tables of r entries with a cyclic index instead of h×h (h = (r−1)/2). n = 4093 in double: peak RSS 72.6 MB → 7.7 MB. - `DFT_STAGE_SIMD128`: stages with span shorter than the full vector but ≥ 128 bits run on `v_float32x4`/`v_float64x2` instead of scalar code (span 4 on AVX2 floats, i.e. every 4^k length). - Fused twiddle-free first stage of radix 3 and 5 for odd lengths (e.g. 4725), fused last radix-4 stage writing the interleaved scaled output, depth-first chunking of the leading stages for long transforms. 2. **`AutoBuffer` alignment** (02c6f39c82): `alignas(max(alignof(_Tp), 8))` on the local storage, so `AutoBuffer<uchar>` used as raw storage for double/int64 data is 8-byte aligned on every platform (it was only 4-byte aligned on 32-bit targets). 3. **Radix-8 stages and cache-set padding** (c7bf418d06): the power-of-two part is factored as r0·8^a·4^b (256: 2 middle stages instead of 3, 4096: 3 instead of 5, 65536: 5 instead of 7). The butterfly follows FFTW's `t1fv_8` (odd legs first, W8 rotations folded into the outputs with FMAs). The real reason an earlier radix-8 attempt was slower turned out to be **4K aliasing**: with a span of a 4 KB multiple, the data legs, the twiddle leg tables and the re/im arrays of one butterfly all map to one L1 set. One cache line of padding between the twiddle legs and between re and im gives 1.3–1.4x at n ≥ 16384 on its own; radix-8 adds 1.1–1.4x on top for complex transforms. 4. **Accurate twiddle generation** (2831df79c8): the tables were built by rotation recurrences and chained leg products and were 12–27 ulps off in double. Now every entry is one product of exactly computed seeds (Cephes sin/cos polynomials on an integer-reduced octant, ~0.5 ulp), computed in double, vectorized and rounded once. The real-FFT twiddles are stored as one table (`rtw_im = rtw_re + nc/2`). Also: `perf_dft.cpp` gains 256x256 and 512x512 2D sizes (matchTemplate / filter2D tile sizes); `test_dxt.cpp` sweep gains prime and 2·prime lengths (257, 1009, 2018). ### Performance vs 5.x head (c4f7634256) Environment: Intel Core Ultra 7 255H, Ubuntu 26.04, GCC 15.2, Release, default `CPU_BASELINE`/`CPU_DISPATCH` (AVX2 kernels are dispatched), `WITH_IPP=OFF`. Each binary was run twice and the minimum per case taken; the tables show the speedup `t(5.x) / t(PR)` as geomean over the flags (and row/column for 1D). Everything single-threaded unless stated. **1D DFT (`dft1d`)** | | 32FC1 | 64FC1 | 32FC2 | 64FC2 | |---|---|---|---|---| | 256 | 2.11 | 1.84 | 2.25 | 1.73 | | 1000 | 1.39 | 1.17 | 0.99 | 1.04 | | 1024 | 2.62 | 2.07 | 2.49 | 1.68 | | 4096 | 1.38 | 1.36 | 1.58 | 1.44 | | 4725 | 1.81 | 1.71 | 1.86 | 1.72 | | 65536 | 1.79 | 1.55 | 2.05 | 1.38 | geomean 1.66 over 192 cases, min 0.98. **1D DCT (`dct1d`)** | | 32FC1 | 64FC1 | |---|---|---| | 256 | 1.65 | 1.51 | | 1000 | 1.50 | 1.22 | | 1024 | 1.96 | 1.63 | | 4096 | 1.52 | 1.37 | | 65536 | 1.82 | 1.29 | **2D DFT, 1 thread** (geomean over flags; `DFT_ROWS` cases in the second group) | | 32FC1 | 64FC1 | 32FC2 | | ROWS 32FC1 | ROWS 64FC1 | ROWS 32FC2 | |---|---|---|---|---|---|---|---| | 256x256 | 1.14 | 1.02 | 1.33 | | 0.97 | 0.97 | 1.70 | | 512x512 | 1.14 | 1.04 | 1.04 | | 1.44 | 1.01 | 1.09 | | 320x480 | 0.98 | 1.00 | 1.16 | | 0.98 | 1.00 | 1.62 | | 800x600 | 1.10 | 1.00 | 0.99 | | 1.34 | 0.98 | 1.00 | | 1280x1024 | 1.12 | 1.02 | 1.26 | | 1.00 | 1.00 | 1.43 | | 1920x1080 | 1.12 | 1.00 | 0.99 | | 1.39 | 1.03 | 1.00 | | 2048x2048 | 1.09 | 1.04 | 1.07 | | 1.34 | 0.99 | 1.21 | 2D DCT, 1 thread: 0.99–1.23 (geomean 1.06). With the default number of threads the 2D geomeans are the same (dft 1.07, dct 1.09) but individual cases are noisy on this hybrid-core CPU (0.75–1.76 between runs of the same code). **imgproc**: matchTemplate 1.00–1.25 per image/template size (geomean 1.07), filter2D 1.00–1.03. The one case consistently below 0.95 in the 1-thread tables, 2D 256x256 32FC2 `DFT_INVERSE` (0.84), is bimodal (0.11 / 0.19 ms) with both the old and the new library: it depends on the addresses malloc returns for the row buffers (set aliasing between src/dst and the 2D driver's buffers), so the per-run winner is luck. It is a candidate for a follow-up. **Plain `cv::dft()` loop** (outside the perf harness, one thread, time per call in us, `create` = plan + workspace, `apply` = transform): | n | type | 5.x total | PR total | 5.x create / apply | PR create / apply | |---|---|---|---|---|---| | 256 | 32FC1 | 0.54 | 0.23 | 0.35 / 0.09 | 0.06 / 0.09 | | 1024 | 32FC2 | 1.88 | 0.76 | 0.82 / 0.97 | 0.06 / 0.58 | | 4096 | 64FC2 | 13.3 | 8.7 | 5.1 / 9.1 | 2.3 / 5.7 | | 4725 | 64FC2 | 47.6 | 17.9 | 8.2 / 24.7 | 3.0 / 14.5 | | 65536 | 32FC2 | 358 | 109 | 97 / 175 | 26 / 80 | | 65536 | 64FC2 | 632 | 199 | 155 / 255 | 33 / 159 | At 65536 the 5.x numbers include ~90–220 us of page faults per call (the glibc trimming described above); the perf harness hides part of this because its warm-up changes the heap state. ### Accuracy `cv::dft` output vs a long double reference, error in units of eps (2^−52 for double): | n | impulse response, 5.x | PR | random input (rel. to max\|X\|), 5.x | PR | |---|---|---|---|---| | 256 | 12.1 | 0.9 | 15.9 | 0.8 | | 1024 | 2.4 | 1.1 | 5.2 | 1.25 | | 4096 | 26.6 | 1.35 | 29.5 | 1.1 | | 65536 | 11.7 | 1.2 | | | | 1048576 | 26.4 | 1.4 | | | Float tables are correctly rounded (0.25 eps). Round trip `idft(dft(x))` at n = 4093 (generic odd radix): max error 5.2e-13 → 7.0e-15. ### Memory n = 4093 64FC2 (prime length → generic odd radix 4093): peak RSS 72.6 MB → 7.7 MB. No memory is kept between calls apart from the static tables of the power-of-two lengths ≤ 1024 (34 KB float + 67 KB double, built on first use). ### Tests - `opencv_test_core` (18297 tests) with AVX2 and with `OPENCV_CPU_DISABLE=AVX2`; `Core_DFT`/`Core_DCT` sweeps now include prime lengths. - `opencv_test_imgproc` matchTemplate / filter2D / phaseCorrelate. - `dxt.cpp` / `dxt.simd.hpp` syntax-checked for aarch64 NEON, riscv64 RVV, x86 without SIMD, AVX2 baseline, and with `-Wall -Wextra -Wshadow`. ### Pull Request Readiness Checklist - [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 - [ ] The feature is well documented and sample code can be built with the project CMake 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1595 lines
51 KiB
C++
1595 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);
|
|
}
|
|
// odd and prime lengths (generic odd radix, r0 = 1/3/5 first stages, radix >= 127 tables)
|
|
static const int extra[] = { 7, 11, 13, 14, 21, 22, 26, 49, 57, 77, 98, 121, 143, 154, 242, 257, 284, 338, 1001, 1009, 1136, 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
|