【问题标题】:Take FFT of an Image in Google Go在 Google Go 中对图像进行 FFT
【发布时间】:2014-07-04 16:21:00
【问题描述】:

如何在 Google Go 中对图像进行 FFT?

Go DSP 库 (github.com/mjibson/go-dsp/fft) 具有用于 2D FFT 的函数,其签名如下:

func FFT2Real(x [][]float64) [][]complex128   

如何将图像从标准的 go 图像类型转换为 float64?这是正确的方法吗?

这是link to the documentation

【问题讨论】:

  • 你在哪里找到这个功能的? github.com/mjibson/go-dsp/search?q=ftt2real 不显示任何内容。
  • @creack:例如,package fft func FFT2Real(x [][]float64) [][]complex128。 FFT2Real 返回实值矩阵的二维前向 FFT。
  • @creack,我添加了链接。
  • 我的错,对不起。好的,我不是专家,所以你想要你的矩阵是什么?根据图像的类型,您将有不同的东西可用。 image.RGBA 的文档:Pix holds the image's pixels, in R, G, B, A order. The pixel at(x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*4].。这可以很容易地转换为 2 小矩阵,但这取决于您需要什么数据。
  • @creack:您的搜索错误。您搜索了q=ftt2real。试试https://github.com/mjibson/go-dsp/search?q=fft2real

标签: go signal-processing fft


【解决方案1】:

您有两个选择,都涉及复制像素。您可以使用方法provided by the Image interface,即At(x,y),也可以将图像断言为image 数据包提供的图像类型之一,并直接访问Pix 属性。

由于您很可能会使用灰色图像,因此您可以轻松地将图像断言为输入 *image.Gray 并访问 the pixels directly,但为了抽象起见,我在示例中没有:

inImage, _, err := image.Decode(inFile)

// error checking

bounds := inImage.Bounds()

realPixels := make([][]float64, bounds.Dy())

for y := 0; y < bounds.Dy(); y++ {
    realPixels[y] = make([]float64, bounds.Dx())
    for x := 0; x < bounds.Dx(); x++ {
        r, _, _, _ := inImage.At(x, y).RGBA()
        realPixels[y][x] = float64(r)
    }
}

这样您就可以读取图像inImage 的所有像素,并将它们作为float64 值存储在二维切片中,以供fft.FFT2Real 处理:

// apply discrete fourier transform on realPixels.
coeffs := fft.FFT2Real(realPixels)

// use inverse fourier transform to transform fft 
// values back to the original image.
coeffs = fft.IFFT2(coeffs)

// write everything to a new image
outImage := image.NewGray(bounds)

for y := 0; y < bounds.Dy(); y++ {
    for x := 0; x < bounds.Dx(); x++ {
        px := uint8(cmplx.Abs(coeffs[y][x]))
        outImage.SetGray(x, y, color.Gray{px})
    }
}

err = png.Encode(outFile, outImage)

在上面的代码中,我对存储在realPixels 中的像素应用了 FFT,然后,为了查看它是否有效,对结果使用了逆 FFT。预期结果是原始图像。

可以在here找到一个完整的例子。

【讨论】:

  • 嗨@nemo,这个例子效果很好。我正在尝试使用 ftt 实现过滤器。如果绘制 2D ftt,我会得到一个看起来像静态的图像。我见过一些例子,他们在 2D fft 中获得了非常有趣的模式,然后编辑它们(在应用逆 fft 之前)作为应用过滤器的一种方式。关于发生了什么的任何线索?如何获得“nice-pattern-ffts”?
  • 获取示例图像,看看您是否可以重现这些模式。如果您无法重现它们,那么您的代码中可能存在错误。
猜你喜欢
  • 2016-11-23
  • 1970-01-01
  • 2012-06-12
  • 2013-10-19
  • 1970-01-01
  • 2014-12-31
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多