Sil*_*lex 3 c++ opencv dft fourier-descriptors
我们已经实现了 DFT,并想用 OpenCV 的实现来测试它。结果不同。
这是我们对 DFT 的实现:
// complex number
std::complex<float> j;
j = -1;
j = std::sqrt(j);
std::complex<float> result;
std::vector<std::complex<float>> fourier; // output
// this->N = length of contour, 512 in our case
// foreach fourier descriptor
for (int n = 0; n < this->N; ++n)
{
// Summation in formula
for (int t = 0; t < this->N; ++t)
{
result += (this->centroidDistance[t] * std::exp((-j*PI2 *((float)n)*((float)t)) / ((float)N)));
}
fourier.push_back((1.0f / this->N) * result);
}
Run Code Online (Sandbox Code Playgroud)
这就是我们如何使用 OpenCV 计算 DFT:
std::vector<std::complex<float>> fourierCV; // output
cv::dft(std::vector<float>(centroidDistance, centroidDistance + this->N), fourierCV, cv::DFT_SCALE | cv::DFT_COMPLEX_OUTPUT);
Run Code Online (Sandbox Code Playgroud)
变量centroidDistance是在上一步中计算的。
注意:请避免回答说使用 OpenCV 而不是您自己的实现。
您忘记result为每次迭代进行初始化n:
for (int n = 0; n < this->N; ++n)
{
result = 0.0f; // initialise `result` to 0 here <<<
// Summation in formula
for (int t = 0; t < this->N; ++t)
{
result += (this->centroidDistance[t] * std::exp((-j*PI2 *((float)n)*((float)t)) / ((float)N)));
}
fourier.push_back((1.0f / this->N) * result);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1004 次 |
| 最近记录: |