【问题标题】:FFT Convolution - Really low PSNRFFT 卷积 - 非常低的 PSNR
【发布时间】:2011-11-01 20:53:05
【问题描述】:

我正在用 FFT 滤波器 (kernelsize=10) 对图像 (512*512) 进行卷积,看起来不错。

但是当我将它与我以正常方式进行卷积的图像进行比较时,结果很糟糕。
PSNR 约为 35。

67,187/262,144 像素值相差 1 或更多(峰值约为 8)(最大像素值为 255)。

我的问题是,在频率空间中进行卷积时是否正常,或者我的卷积/变换函数是否存在问题? .因为奇怪的是,当我使用 double 作为数据类型时,我应该得到更好的结果。但它完全一样。

当我将图像转换到频率空间时,不要对其进行卷积,然后将其转换回来就可以了,使用浮点数时 PSNR 约为 140。

另外,由于像素差异只有 1-10,我想我可以排除缩放错误

编辑:无聊感兴趣的人的更多详细信息

我使用开源的 KissFFT 库。带真实二维输入(kiss_fftndr.h)

我的图像数据类型是 PixelMatrix。简单的 alpha、red、green 和 blue 值从 0.0 到 1.0 float 的矩阵

我的内核也是一个 PixelMatrix。

这里有一些卷积函数的sn-ps

使用的数据类型:

#define kiss_fft_scalar float
#define kiss_fft_cpx struct {
    kiss_fft_scalar r;
    kiss_fft_scalar i,
}

FFT 的配置:

//parameters to kiss_fftndr_alloc:
//1st param = array with the size of the 2 dimensions (in my case dim={width, height})
//2nd param = count of the dimensions (in my case 2)
//3rd param = 0 or 1 (forward or inverse FFT)
//4th and 5th params are not relevant

kiss_fftndr_cfg stf = kiss_fftndr_alloc(dim, 2, 0, 0, 0);
kiss_fftndr_cfg sti = kiss_fftndr_alloc(dim, 2, 1, 0, 0);

填充和转换内核:

I make a new array:

kiss_fft_scalar kernel[width*height];

I fill it with 0 in a loop.

Then I fill the middle of this array with the kernel I want to use.
So if I would use a 2*2 kernel with values 1/4, 1/4, 1/4 and 1/4 it would look like

0 0 0 0 0 0
0 1/4 1/4 0
0 1/4 1/4 0
0 0 0 0 0 0

The zeros are padded until they reach the size of the image.

Then I swap the quadrants of the image diagonally. It looks like:

1/4 0 0 1/4
 0  0 0  0
 0  0 0  0
1/4 0 0 1/4

now I transform it: kiss_fftndr(stf, floatKernel, outkernel);

outkernel is declarated as 
kiss_fft_cpx outkernel= new kiss_fft_cpx[width*height]

将颜色放入数组中:

kiss_fft_scalar *red = new kiss_fft_scalar[width*height];
kiss_fft_scalar *green = new kiss_fft_scalar[width*height];
kiss_fft-scalar *blue = new kiss_fft_scalar[width*height];

for(int i=0; i<height; i++) {
 for(int j=0; i<width; j++) {
  red[i*height+j] = input.get(j,i).getRed();  //input is the input image pixel matrix
  green[i*height+j] = input.get(j,i).getGreen();
  blue{i*height+j] = input.get(j,i).getBlue();
 }
}

Then I transform the arrays:

kiss_fftndr(stf, red, outred);
kiss_fftndr(stf, green, outgreen);
kiss_fftndr(stf, blue, outblue);      //the out-arrays are type kiss_fft_cpx*

卷积:

我们现在拥有的:

  • kiss_fft_cpx* 类型的 3 个转换颜色数组
  • 1 个从 Kiss_fft_cpx* 类型转换的内核数组

它们都是复数数组

现在是卷积:

for(int m=0; m<til; m++) {
 for(int n=0; n<til; n++) {
  kiss_fft_scalar real = outcolor[m*til+n].r;      //I do that for all 3 arrys in my code!
  kiss_fft_scalar imag = outcolor[m*til+n].i;      //so I have realred, realgreen, realblue
  kiss_fft_scalar realMask = outkernel[m*til+n].r; // and imagred, imaggreen, etc.
  kiss_fft_scalar imagMask = outkernel[m*til+n].i;

  outcolor[m*til+n].r = real * realMask - imag * imagMask; //Same thing here in my code i
  outcolor[m*til+n].i = real * imagMask + imag * realMask; //do it with all 3 colors
 }
}

现在我将它们转换回来:

kiss_fftndri(sti, outred, red);
kiss_fftndri(sti, outgreen, green);
kiss_fftndri(sti, outblue, blue);

and I create a new Pixel Matrix with the values from the color-arrays

PixelMatrix output;

for(int i=0; i<height; i++) {
 for(int j=0; j<width; j++) {
  Pixel p = new Pixel();
  p.setRed( red[i*height+j] / (width*height) ); //I divide through (width*height) because of the scaling happening in the FFT;
  p.setGreen( green[i*height+j] );
  p.setBlue( blue[i*height+j] );
  output.set(j , i , p);
 }
}

注意事项:

  • 我已经提前注意图像的大小是 2 的幂 (256*256)、(512*512) 等。

示例:

内核大小:10

输入:

输出:

普通卷积的输出:

我的控制台说:

142519 out of 262144 Pixels have a difference of 1 or more (maxRGB = 255)

PSNR: 32.006027221679688
MSE: 44.116752624511719

虽然在我看来它们看起来是一样的°.°

也许一个人感到无聊并浏览了代码。不急,但就是一种问题,我就是想知道我到底做错了什么^^

最后但并非最不重要的是,我的 PSNR 功能,虽然我并不认为这是问题所在:D

void calculateThePSNR(const PixelMatrix first, const PixelMatrix second, float* avgpsnr, float* avgmse) {

int height = first.getHeight();
int width = first.getWidth();

BMP firstOutput;
BMP secondOutput;

firstOutput.SetSize(width, height);
secondOutput.SetSize(width, height);

double rsum=0.0, gsum=0.0, bsum=0.0;
int count = 0;
int total = 0;
for(int i=0; i<height; i++) {
    for(int j=0; j<width; j++) {
        Pixel pixOne = first.get(j,i);
        Pixel pixTwo = second.get(j,i);

        double redOne = pixOne.getRed()*255;
        double greenOne = pixOne.getGreen()*255;
        double blueOne = pixOne.getBlue()*255;

        double redTwo = pixTwo.getRed()*255;
        double greenTwo = pixTwo.getGreen()*255;
        double blueTwo = pixTwo.getBlue()*255;

        firstOutput(j,i)->Red = redOne;
        firstOutput(j,i)->Green = greenOne;
        firstOutput(j,i)->Blue = blueOne;

        secondOutput(j,i)->Red = redTwo;
        secondOutput(j,i)->Green = greenTwo;
        secondOutput(j,i)->Blue = blueTwo;

        if((redOne-redTwo) > 1.0 || (redOne-redTwo) < -1.0) {
            count++;
        }
        total++;

        rsum += (redOne - redTwo) * (redOne - redTwo);
        gsum += (greenOne - greenTwo) * (greenOne - greenTwo);
        bsum += (blueOne - blueTwo) * (blueOne - blueTwo);

    }
}
fprintf(stderr, "%d out of %d Pixels have a difference of 1 or more (maxRGB = 255)", count, total);
double rmse = rsum/(height*width);
double gmse = gsum/(height*width);
double bmse = bsum/(height*width);

double rpsnr = 20 * log10(255/sqrt(rmse));
double gpsnr = 20 * log10(255/sqrt(gmse));
double bpsnr = 20 * log10(255/sqrt(bmse));

firstOutput.WriteToFile("test.bmp");
secondOutput.WriteToFile("test2.bmp");

system("display test.bmp");
system("display test2.bmp");

*avgmse = (rmse + gmse + bmse)/3;
*avgpsnr = (rpsnr + gpsnr + bpsnr)/3;
}

【问题讨论】:

  • 图像空间中的卷积在数学上等同于频率空间中的乘法,因此它们应该是相同的。你能发布一些示例图像和/或代码吗?
  • 由于数据类型的精度障碍应该存在一些差异,但我认为并没有那么大。是的,我会尽快发布一些示例图像/代码
  • 亲吻窗口你的数据吗?另外,你为什么不绘制一个误差图,这样你就可以看到哪里的像素有最大的误差。
  • 什么是数据窗口化?是的,感谢您的评论,我现在就这样做。
  • 尚未检查您的代码,但我强烈怀疑您生成的两个图像以某种方式发生了偏移。通过计算它们的互相关系数来确保它们完全对齐。

标签: image-processing fft convolution kissfft


【解决方案1】:

Phonon 的想法是正确的。你的图像被转移了。如果您将图像移动 (1,1),则 MSE 将近似为零(前提是您相应地屏蔽或裁剪图像)。我使用下面的代码(Python + OpenCV)确认了这一点。

import cv
import sys
import math

def main():
    fname1, fname2 = sys.argv[1:]
    im1 = cv.LoadImage(fname1)
    im2 = cv.LoadImage(fname2)

    tmp = cv.CreateImage(cv.GetSize(im1), cv.IPL_DEPTH_8U, im1.nChannels)
    cv.AbsDiff(im1, im2, tmp)
    cv.Mul(tmp, tmp, tmp)
    mse = cv.Avg(tmp)
    print 'MSE:', mse

    psnr = [ 10*math.log(255**2/m, 10) for m in mse[:-1] ]
    print 'PSNR:', psnr

if __name__ == '__main__':
    main()

输出:

MSE: (0.027584912741602553, 0.026742391458366047, 0.028147870144492403, 0.0)
PSNR: [63.724087463606452, 63.858801190963192, 63.636348220531396]

【讨论】:

  • 我无法获得 MSE,但我确实确认移动图像只会在提供的输出图像之间的差异中留下随机的 1 级噪声。
  • 是的,你是对的,它不会完全为零。我更新了我的帖子以获取每个频道的实际值。
  • 谢谢。我自己已经想通了,只是忘了在这里发布。真的就是这么简单的 x.x .. 我现在也在 FFT 中使用 double 而不是 float,这给了我无限的 psnr,所以图像几乎相等
【解决方案2】:

我建议您尝试实现以下代码:

A=double(inputS(1:10:length(inputS))); %segmentation 
A(:)=-A(:);
%process the image or signal by fast fourior transformation and inverse fft
fresult=fft(inputS);
fresult(1:round(length(inputS)*2/fs))=0;
fresult(end-round(length(fresult)*2/fs):end)=0;
Y=real(ifft(fresult));

该代码可以帮助您获得相同大小的图像并有利于去除直流分量,您可以进行卷积。

【讨论】:

    猜你喜欢
    • 2012-12-10
    • 2016-12-31
    • 2011-03-06
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 2016-08-18
    • 1970-01-01
    相关资源
    最近更新 更多