【发布时间】:2018-11-28 23:00:21
【问题描述】:
我尝试使用以下代码实现 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 结果很好,但是将它与图像一起使用会返回上述结果。
【问题讨论】:
标签: javascript opencv image-processing fft