dsm1998
dsm1998
2014-07-04 16:21
浏览 121
已采纳

在Google Go中对图像进行FFT

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.

图片转代码服务由CSDN问答提供 功能建议

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

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

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

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

这是文档的链接< /a>。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushan5222
    doushan5222 2014-07-04 22:30
    已采纳

    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 < 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)
        }
    }
    

    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 < 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)
    

    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.

    点赞 评论

相关推荐