c++ - opencv C++中的傅里叶变换和逆变换
问题描述
傅里叶变换及其逆变换在C++ 中实现OpenCV
。但是我无法获得正确的结果,并且无法通过逆变换恢复图像。下面的代码有问题吗?
Mat transform(const Mat& src, int rows, int cols)
{
complex<double> ci{ 0, 1 };
vector<double> real_t;
vector<double> imag_t;
double row = static_cast_double(rows);
double col = static_cast_double(cols);
for (int u = 0; u < rows; u++)
{
for (int v = 0; v < cols; v++)
{
complex<double> tmp(0.0, 0.0);
for (int x = 0; x < rows; x++)
{
complex<double> temp(0.0, 0.0);
for (int y = 0; y < cols; y++)
{
temp += exp(-2 * PI * (u * x / row + y * v / col) * ci) * static_cast_double(src.at<uchar>(x, y));
}
tmp += temp;
}
complex<double> tempor = tmp / sqrt(row * col);
real_t.push_back(static_cast_float(tempor.real()));
imag_t.push_back(static_cast_float(tempor.imag()));
}
}
Mat s1(real_t);
s1 = s1.reshape(0, rows);
Mat s2(imag_t);
s2 = s2.reshape(0, rows);
Mat s[2] = { s1, s2 };
Mat dst;
merge(s, 2, dst);
return dst;
}
Mat inverse(const Mat& src, int rows, int cols)
{
assert(src.channels() == 2);
vector<Mat> ft_mats;
cv::split(src, ft_mats);
complex<double> ci{ 0.0, 1.0 };
vector<uchar> real_t, img_t;
double row = static_cast_double(rows);
double col = static_cast_double(cols);
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
{
complex<double>sum{ 0.0, 0.0 };
for (int u = 0; u < rows; u++)
{
complex<double> tmp(0.0, 0.0);
for (int v = 0; v < cols; v++)
{
double rt = ft_mats[0].at<double>(u, v);
double it = ft_mats[1].at<double>(u, v);
complex<double> temp{ rt, it };
tmp += temp * exp(2 * PI * (u * x / row + v * y / col) * ci);
}
sum += tmp;
}
//sum /= sqrt(col * row);
real_t.push_back(static_cast_uchar((sum.real())));
}
}
Mat s1(real_t);
s1 = s1.reshape(0, rows);
return s1;
}
原图:
傅里叶变换的结果:
将上述变换的结果作为参数放入函数 inverse:
可以看出,逆变换的结果与原始图像完全不同。我该如何纠正?感谢您的关注。
更新:证明变换过程是正确的,可以通过OpenCV中的idft得到原图。但是反函数还是不行……查了很多次反函数还是不知道哪里出了问题……
解决方案
推荐阅读
- docker - 容器无法连接到互联网
- javascript - 参数列表后缺少错误 - Selenium Python(执行 Javascript)
- ios - 我是否需要物理 iPhone 来编译和分发 IOS 应用程序?
- c# - 错误:未定义或导入预定义类型“System.Range”
- python - VSCode FileNotFoundError
- haskell - 如何让 GHCi 重新加载包括本地依赖库中的更改?
- java - Spring Boot 应用程序问题
- c++ - 如何在 C++ 中创建具有不同行大小的向量?
- javascript - 如何以角度制作路线
- swift - UICollectionView 布局错误