javascript - 快速傅立叶变换出错

标签 javascript opencv image-processing fft

我尝试使用以下代码实现 FFT:Rosetta Code FFT

这是我得到的结果的屏幕截图: FFT gone wrong

这是我在图像上使用上述 FFT 的代码:

function fastFourier(img){

let height=img.rows;
let width=img.cols;
let tmp=createArray(height,width);
let temp=createArray(height,width);
let rows=createArray(height,width);
let prettypls=img.clone();

//new complex array
for(i=0;i<height;i++){
  for(j=0;j<width;j++){
  rows[i][j]=new Complex(0, 0);
    }
}

//put pixel values in complex array
if(height%2==0&&width%2==0){
  for ( y = 0; y < height; y++) {
    for ( x = 0; x < width; x++) {
    let pixel = img.ucharPtr(y,x);
    rows[y][x].re=pixel[0];
  }
}

//perform fft
for(y=0;y<height;y++){
  tmp[y]=cfft(rows[y]);
}

//take the magnitudes
for(i=0;i<height;i++){
  for(j=0;j<width;j++){
    temp[i][j]=Math.round(tmp[i][j].re);
  }
}

//do a log transform
temp=logTransform(temp,height,width);

//put the real values into Mat
for(i=0;i<height;i++){
  for(j=0;j<width;j++){
    let pixel = prettypls.ucharPtr(i,j);
    pixel[0]=Math.round(temp[i][j]);
  }
}
cv.imshow('fourierTransform', prettypls);
rows=[];temp=[];tmp=[];prettypls.delete();
}
else alert('Image size must be a power of 2.');
}

我根据 this 进行了对数转换FFT 的描述。这是我的日志转换代码:

function logTransform(img,h,w){
//https://homepages.inf.ed.ac.uk/rbf/HIPR2/pixlog.htm
let max=findMax2d(img,h,w);
let c=255/(Math.log(1+max));
for(i=0;i<h;i++){
  for(j=0;j<w;j++){
    img[i][j]=c*Math.log(1+Math.abs(img[i][j]));
  }
}
return img;
}

我不知道我做错了什么。当它只是一个普通数组时,FFT 结果很好,但将其与图像一起使用会返回上述结果。

最佳答案

您得到的正是您所要求的:对于图像中的每一行,对该行中的强度频率进行分析。。您的代码将每一行视为一个单独的样本数组,并对其进行 FFT。

您可能想要的是二维 FFT,如下所述:http://www.robots.ox.ac.uk/~az/lectures/ia/lect2.pdf

现在,您只是计算一系列一维 FFT,这是不一样的。

关于javascript - 快速傅立叶变换出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53514937/

相关文章:

javascript - 存储选择框的选项值并将它们存储到变量中(以逗号分隔值)

javascript - 密码存储在谷歌浏览器的浏览器内存中

javascript - IFrame 到底可以用 top.Location 对象做什么(跨域)?

android - 如何用图像OpenCV填充多边形

javascript - Meteor 和 ReactJS - 从 MongoDB 检索日期到字符串?

java - 如何在OpenCV Java中将二进制图像转换为灰度

python - 无法保存背景减去视频Python openCV

c++ - 只有三分之一的图像是用 OpenCV 编写的

c++ - 在 opencv 中绘制、编号和识别网格单元

python - 导入 python 模块 - ImageChops