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

在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.

  • 写回答

1条回答 默认 最新

  • 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大