Skip to content

Commit 27dd274

Browse files
committed
fixed invalid output of cv::dft when using DFT_ROWS + DFT_COMPLEX_OUTPUT (http://code.opencv.org/issues/3428)
1 parent 6d691f2 commit 27dd274

File tree

2 files changed

+68
-36
lines changed

2 files changed

+68
-36
lines changed

modules/core/src/dxt.cpp

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,47 @@ typedef IppStatus (CV_STDCALL* IppDFTGetSizeFunc)(int, int, IppHintAlgorithm, in
14731473
typedef IppStatus (CV_STDCALL* IppDFTInitFunc)(int, int, IppHintAlgorithm, void*, uchar*);
14741474
#endif
14751475

1476+
namespace cv
1477+
{
1478+
static void complementComplexOutput(Mat& dst, int len, int dft_dims)
1479+
{
1480+
int i, n = dst.cols;
1481+
size_t elem_size = dst.elemSize1();
1482+
if (elem_size == sizeof(float))
1483+
{
1484+
float* p0 = dst.ptr<float>();
1485+
size_t dstep = dst.step / sizeof(p0[0]);
1486+
for (i = 0; i < len; i++)
1487+
{
1488+
float* p = p0 + dstep*i;
1489+
float* q = dft_dims == 1 || i == 0 || i * 2 == len ? p : p0 + dstep*(len - i);
1490+
1491+
for (int j = 1; j < (n + 1) / 2; j++)
1492+
{
1493+
p[(n - j) * 2] = q[j * 2];
1494+
p[(n - j) * 2 + 1] = -q[j * 2 + 1];
1495+
}
1496+
}
1497+
}
1498+
else
1499+
{
1500+
double* p0 = dst.ptr<double>();
1501+
size_t dstep = dst.step / sizeof(p0[0]);
1502+
for (i = 0; i < len; i++)
1503+
{
1504+
double* p = p0 + dstep*i;
1505+
double* q = dft_dims == 1 || i == 0 || i * 2 == len ? p : p0 + dstep*(len - i);
1506+
1507+
for (int j = 1; j < (n + 1) / 2; j++)
1508+
{
1509+
p[(n - j) * 2] = q[j * 2];
1510+
p[(n - j) * 2 + 1] = -q[j * 2 + 1];
1511+
}
1512+
}
1513+
}
1514+
}
1515+
}
1516+
14761517
void cv::dft( InputArray _src0, OutputArray _dst, int flags, int nonzero_rows )
14771518
{
14781519
static DFTFunc dft_tbl[6] =
@@ -1688,8 +1729,12 @@ void cv::dft( InputArray _src0, OutputArray _dst, int flags, int nonzero_rows )
16881729
memset( dptr0, 0, dst_full_len );
16891730
}
16901731

1691-
if( stage != 1 )
1732+
if (stage != 1)
1733+
{
1734+
if (!inv && real_transform && dst.channels() == 2)
1735+
complementComplexOutput(dst, nonzero_rows, 1);
16921736
break;
1737+
}
16931738
src = dst;
16941739
}
16951740
else
@@ -1831,41 +1876,7 @@ void cv::dft( InputArray _src0, OutputArray _dst, int flags, int nonzero_rows )
18311876
if( stage != 0 )
18321877
{
18331878
if( !inv && real_transform && dst.channels() == 2 && len > 1 )
1834-
{
1835-
int n = dst.cols;
1836-
if( elem_size == (int)sizeof(float) )
1837-
{
1838-
float* p0 = (float*)dst.data;
1839-
size_t dstep = dst.step/sizeof(p0[0]);
1840-
for( i = 0; i < len; i++ )
1841-
{
1842-
float* p = p0 + dstep*i;
1843-
float* q = i == 0 || i*2 == len ? p : p0 + dstep*(len-i);
1844-
1845-
for( int j = 1; j < (n+1)/2; j++ )
1846-
{
1847-
p[(n-j)*2] = q[j*2];
1848-
p[(n-j)*2+1] = -q[j*2+1];
1849-
}
1850-
}
1851-
}
1852-
else
1853-
{
1854-
double* p0 = (double*)dst.data;
1855-
size_t dstep = dst.step/sizeof(p0[0]);
1856-
for( i = 0; i < len; i++ )
1857-
{
1858-
double* p = p0 + dstep*i;
1859-
double* q = i == 0 || i*2 == len ? p : p0 + dstep*(len-i);
1860-
1861-
for( int j = 1; j < (n+1)/2; j++ )
1862-
{
1863-
p[(n-j)*2] = q[j*2];
1864-
p[(n-j)*2+1] = -q[j*2+1];
1865-
}
1866-
}
1867-
}
1868-
}
1879+
complementComplexOutput(dst, len, 2);
18691880
break;
18701881
}
18711882
src = dst;

modules/core/test/test_dxt.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,3 +866,24 @@ class Core_DFTComplexOutputTest : public cvtest::BaseTest
866866
};
867867

868868
TEST(Core_DFT, complex_output) { Core_DFTComplexOutputTest test; test.safe_run(); }
869+
870+
TEST(Core_DFT, complex_output2)
871+
{
872+
for (int i = 0; i < 100; i++)
873+
{
874+
int type = theRNG().uniform(0, 2) ? CV_64F : CV_32F;
875+
int m = theRNG().uniform(1, 10);
876+
int n = theRNG().uniform(1, 10);
877+
Mat x(m, n, type), out;
878+
randu(x, -1., 1.);
879+
dft(x, out, DFT_ROWS | DFT_COMPLEX_OUTPUT);
880+
double nrm = norm(out, NORM_INF);
881+
double thresh = n*m * 2;
882+
if (nrm > thresh)
883+
{
884+
cout << "x: " << x << endl;
885+
cout << "out: " << out << endl;
886+
ASSERT_LT(nrm, thresh);
887+
}
888+
}
889+
}

0 commit comments

Comments
 (0)