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; } Mat变换(常量Mat&src、int行、int列) { 复ci{0,1}; 向量实数; 矢量图像; 双排=静态\u铸造\u双排(行); 双色=静态双色(色); 对于(int u=0;u

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; } Mat变换(常量Mat&src、int行、int列) { 复ci{0,1}; 向量实数; 矢量图像; 双排=静态\u铸造\u双排(行); 双色=静态双色(色); 对于(int u=0;u,c++,opencv,image-processing,C++,Opencv,Image Processing,原始图像: 傅里叶变换的结果: 将上述变换的结果作为参数放入函数逆: 可以看出,逆变换的结果与原始图像完全不同。我怎样才能纠正这个问题?谢谢你的关注 更新:转换过程被证明是正确的,并且可以通过OpenCV中的idft获得原始图像。但是反函数仍然不能…在多次检查反函数之后,我仍然不知道有什么错误…问题可能是反变换中的静态\u cast\u uchar。尝试不强制转换,创建浮点输出。然后查看以确保它是正确的,并且在预期范围内。将0-255范围之外的值强制转换为uchar会做一些奇怪的事情!我仍然找不

原始图像:

傅里叶变换的结果:

将上述变换的结果作为参数放入函数逆:

可以看出,逆变换的结果与原始图像完全不同。我怎样才能纠正这个问题?谢谢你的关注


更新:转换过程被证明是正确的,并且可以通过OpenCV中的idft获得原始图像。但是反函数仍然不能…在多次检查反函数之后,我仍然不知道有什么错误…

问题可能是反变换中的
静态\u cast\u uchar
。尝试不强制转换,创建浮点输出。然后查看以确保它是正确的,并且在预期范围内。将0-255范围之外的值强制转换为uchar会做一些奇怪的事情!我仍然找不到哪里出了问题…太奇怪了…你是如何显示你的浮点图像的?许多观众会投uint8,有时是在乘以255之后。您需要按原样显示浮点图像。将“最小值”缩放为0,“最大值”缩放为1,然后显示,或将“最大值”缩放为255,或根据查看器实用程序的要求进行调整。如果您发布一个,而不是只发布代码段,则会有所帮助。Mat ift=inverse(ft,ft.rows,ft.cols);正常化(ift,ift,0,1,NORM_MINMAX);namedWindow(“IFT”,窗口正常);调整窗口大小(“IFT”,IFT.rows,IFT.cols);imshow(“FT逆”,ift);等待键(0);问题可能在于逆变换中的
static\u cast\u uchar
。尝试不强制转换,创建浮点输出。然后查看以确保它是正确的,并且在预期范围内。将0-255范围之外的值强制转换为uchar会做一些奇怪的事情!我仍然找不到哪里出了问题…太奇怪了…你是如何显示你的浮点图像的?许多观众会投uint8,有时是在乘以255之后。您需要按原样显示浮点图像。将“最小值”缩放为0,“最大值”缩放为1,然后显示,或将“最大值”缩放为255,或根据查看器实用程序的要求进行调整。如果您发布一个,而不是只发布代码段,则会有所帮助。Mat ift=inverse(ft,ft.rows,ft.cols);正常化(ift,ift,0,1,NORM_MINMAX);namedWindow(“IFT”,窗口正常);调整窗口大小(“IFT”,IFT.rows,IFT.cols);imshow(“FT逆”,ift);等待键(0);