在BufferedImage上应用FFT给我一个ArrayIndexOutOfBoundsException

时间:2018-11-30 14:48:53

标签: java image-processing java-8 fft bufferedimage

我用BufferedImage创建了一个Robot,并尝试将Rosetta代码中的FFT algorithm应用到Robot生成的DataBufferInt上,但实际上失败了方法,我不知道为什么。

Robot r = new Robot();
Rectangle rect = new Rectangle(10,10,200,200);
BufferedImage capture = r.createScreenCapture(rect);
// All data in getDataBuffer() seems to be negative
int[] pixels = ((DataBufferInt)buffer.getRaster().getDataBuffer()).getData();
Complex[] cinput = new Complex[pixels.length];
for (int i = 0; i < pixels.length; i++) {
    cinput[i] = new Complex(pixels[i], 0.0);
}
// Fail (method from rosetta code)
FastFourierTransform.fft(cinput);

给我:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 40000
at engine.FastFourierTransform.fft(FastFourierTransform.java:42)
at engine.ImageProcessing.FFT(ImageProcessing.java:30)
at test.Testor.main(Testor.java:24)

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您可以使用Catalano Framework,可以轻松地执行快速傅立叶变换。也适用于MxN尺寸的图像。

Robot r = new Robot();
Rectangle rect = new Rectangle(10,10,200,200);
BufferedImage capture = r.createScreenCapture(rect);

FastBitmap fb = new FastBitmap(capture);
fb.toGrayscale();

FourierTransform ft = new FourierTransform(fb);
ft.Forward();
fb = ft.toFastBitmap();

//Display the image
JOptionPane.showMessageDialog(null, fb.toIcon());