简体   繁体   English

Cimg FFT 显示噪声图像。如何获得正确的 FFT 图像?

[英]Cimg FFt is showing noise image.How to get the proper FFT Image?

CImg output i am getting CImg output 我得到
Desired FFT ouptut.Checked using ImageJ所需的 FFT 输出。使用 ImageJ 进行检查

#include "CImg.h"
using namespace cimg_library;

int main(int argc, char * argv[]) {

    const char * input_file = "Lenna.png";

    CImg<unsigned char> * input = new CImg<unsigned char>(input_file);

    //resize_fft(*input); //Resize the image for the FFT
    //CImg<unsigned char> gray = any2gray(*input); //to single-channel grayscale image
    //free(input);


    CImgList<unsigned char> fft = input->get_FFT();
    CImg<unsigned char>::FFT(fft[0], fft[1], false);
    fft[0].save("fft.jpg");

    return 1;
}

I tried this code.我试过这段代码。 But i am getting noise image.Anybody could help me to get the desired fft image?但是我得到了噪声图像。任何人都可以帮助我获得所需的 fft 图像吗? i am running on Linux.我在 Linux 上运行。 i have posted the images above.我已经发布了上面的图片。

Several things wrong here in your code:您的代码中有几处错误:

  1. By (mathematical) definition, result of a FFT is a float-valued image.根据(数学)定义,FFT 的结果是浮点值图像。 There is no way you can accurately store a FFT image into a CImgList<unsigned char> .您无法将 FFT 图像准确存储到CImgList<unsigned char>中。 Use a CImgList<float> or CImgList<double> instead when defining your fft variable.定义fft变量时,请改用CImgList<float>CImgList<double> Your current fft is defined as a CImgList<unsigned char> , there is no way CImg can store float-values inside.您当前的fft被定义为CImgList<unsigned char> ,CImg 无法在其中存储浮点值。

  2. CImg<T>::get_FFT() always returns a CImgList<float> or a CImgList<double> , depending on the original type T (if T is unsigned char , result will be a CImgList<float> probably). CImg<T>::get_FFT()总是返回一个CImgList<float>或一个CImgList<double> ,这取决于原始类型T (如果Tunsigned char ,结果可能是一个CImgList<float> )。 But if you write但是如果你写

CImgList<unsigned char> fft = input->get_FFT();

then, of course the result of get_FFT() is rounded back to unsigned char , which is not what you want.然后,当然get_FFT()的结果会四舍五入为unsigned char ,这不是您想要的。

So, the correct code would be:所以,正确的代码是:

 CImg<unsigned char> input(input_file); // (note: No need for a pointer here).
 CImgList<float> fft = input.get_FFT();

At this point, fft[0] is the real part of the FFT, and fft[1] the imaginary part of the FFT.此时, fft[0]是 FFT 的实部, fft[1]是 FFT 的虚部。 Note that it does not correspond to the classical Fourier visualization of images you'll find on the web, which are actually displayed as the centered module of the FFT (less often with the phase), eg请注意,它与您在 web 上找到的图像的经典傅立叶可视化不对应,它们实际上显示为 FFT 的居中模块(较少使用相位),例如

https://cs.appstate.edu/ret/imageJ/PClabs/imlab/FFT/gif/imj-fft1.gif https://cs.appstate.edu/ret/imageJ/PClabs/imlab/FFT/gif/imj-fft1.gif

Getting this requires additional steps to transform the complex-valued FFT you get as this scalar image representation (which is actually good only for visualization, not for actual computations in the Fourier space, as you lose half of the data of the FFT).实现这一点需要额外的步骤来将您获得的复值 FFT 转换为这种标量图像表示(这实际上仅适用于可视化,而不适用于傅里叶空间中的实际计算,因为您丢失了 FFT 的一半数据)。

Oh, and before saying any library you use has a lack of design, ask yourself if there is something that maybe you have not understood.哦,在说你使用的任何库都缺乏设计之前,问问自己是否有一些可能不理解的东西。 Looking at your code makes me think that you still have a lot to learn before having a relevant opinion on how to design a programming library.查看您的代码让我觉得在对如何设计编程库有相关意见之前,您还有很多东西需要学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM