在Google Go中对图像进行FFT变换。

huangapple go评论69阅读模式
英文:

Take FFT of an Image in Google Go

问题

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

Go DSP库(github.com/mjibson/go-dsp/fft)提供了一个用于进行2D FFT变换的函数,其签名如下:

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

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

这里是文档链接

英文:

How do you take the FFT of an image in Google Go?

The Go DSP library (github.com/mjibson/go-dsp/fft) has a function for a 2D FFT with the following signature:

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

How do I convert an image from the standard go image types to float64? Is this the right approach?

Here is a link to the documentation.

答案1

得分: 4

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

由于你很可能使用的是灰度图像,你可以轻松地将图像断言为*image.Gray类型,并直接访问像素,但出于抽象的考虑,我在示例中没有这样做:

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

// 错误检查

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处理:

// 对realPixels应用离散傅里叶变换。
coeffs := fft.FFT2Real(realPixels)

// 使用逆傅里叶变换将fft值转换回原始图像。
coeffs = fft.IFFT2(coeffs)

// 将所有内容写入新图像。
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。预期结果是原始图像。

完整的示例可以在这里找到。

英文:

You have two options, both involve copying the pixels. You can either use the methods provided by the Image interface, namely At(x,y) or you can assert the image to one of the image types provided by the image packet and access the Pix attribute directly.

Since you will most likely be using a Gray image, you could easily assert your image to type *image.Gray and access the pixels directly but for the sake of abstraction I did not in my example:

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

// error checking

bounds := inImage.Bounds()

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

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

This way you read all the pixels of your image inImage and store them as float64 values in a two-dimensional slice, ready to be processed by 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 &lt; bounds.Dy(); y++ {
	for x := 0; x &lt; bounds.Dx(); x++ {
        px := uint8(cmplx.Abs(coeffs[y][x]))
		outImage.SetGray(x, y, color.Gray{px})
	}
}

err = png.Encode(outFile, outImage)

In the code above I applied FFT on the pixels stored in realPixels and then, to see whether it worked, used inverse FFT on the result. The expected result is the original image.

A full example can be found here.

huangapple
  • 本文由 发表于 2014年7月5日 00:21:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/24578126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定