【问题标题】:R - FFT Fourier Spectrum of ImageR - 图像的 FFT 傅里叶谱
【发布时间】:2016-11-02 09:22:31
【问题描述】:

我使用 R Studio 是为了完成图像处理方面的任务。我目前正在使用“EBImage、fftw、...”库。我有一个关于傅立叶分析和功率谱的问题。

1) 我有 2D 矩阵,这是一个图像。此图像由水平线组成,黑色和白色。我想进行傅里叶变换并通过绘图显示其大小。由于图像中的黑线是水平的,因此功率谱将有一条垂直线。我找不到实现它的方法。

现在我正在使用这张图片进行测试: Square

如果为真,则此图像在水平轴和垂直轴上都有一些周期性频率。然后。 FFT 频谱应该看起来像一个“+”。但我发现有些不同。

这是我的代码:

setwd(".../Project/R/Workspace/Task1")
library("EBImage"  , lib.loc="~/R/win-library/3.2")
library("fftwtools", lib.loc="~/R/win-library/3.2")
library("fftw", lib.loc="~/R/win-library/3.2")

# Image Acquisition
img <- readImage(".../Project/Beispielbilder/drmcircle.jpg")
display(img, title='Image')

# Grayscaled
img_gray<-channel(img,"gray")

# FFT
img_ff <- fft(img_gray)    #fftw2d

magntd <- sqrt(Re(img_ff)^2+Im(img_ff)^2)
phase  <- atan(Im(img_ff)/Re(img_ff))

plot(log(magntd),main="FFT")

结果,这就是我所拥有的: FFT Spectrum

这是我的问题:

1) 如何获得正确的光谱图像? 2)我怎样才能将其视为图像,而不是情节? (请参阅顶部的示例链接。)

提前感谢您的帮助。

【问题讨论】:

标签: r image-processing fft


【解决方案1】:

我解决了。我在这里添加代码。请注意,fftshift 函数取自:[How to write fftshift and ifftshift in R?

setwd(".../Project/R/Workspace/Task1")
library("EBImage"  , lib.loc="~/R/win-library/3.2")

# Image
img <- readImage(".../Project/Beispielbilder/drmtri.jpg")
display(img, title='Image')

# Grayscaled
img_gray<-channel(img,"gray")

# FFT
img_ff <- fft(img_gray) #fftw2d


###################################################
###################################################
# FFT SHIFT
fftshift <- function(img_ff, dim = -1) {

  rows <- dim(img_ff)[1]    
  cols <- dim(img_ff)[2]    

  swap_up_down <- function(img_ff) {
    rows_half <- ceiling(rows/2)
    return(rbind(img_ff[((rows_half+1):rows), (1:cols)], img_ff[(1:rows_half), (1:cols)]))
  }

  swap_left_right <- function(img_ff) {
    cols_half <- ceiling(cols/2)
    return(cbind(img_ff[1:rows, ((cols_half+1):cols)], img_ff[1:rows, 1:cols_half]))
  }

  if (dim == -1) {
    img_ff <- swap_up_down(img_ff)
    return(swap_left_right(img_ff))
  }
  else if (dim == 1) {
    return(swap_up_down(img_ff))
  }
  else if (dim == 2) {
    return(swap_left_right(img_ff))
  }
  else {
    stop("Invalid dimension parameter")
  }
}

ifftshift <- function(img_ff, dim = -1) {

  rows <- dim(img_ff)[1]    
  cols <- dim(img_ff)[2]    

  swap_up_down <- function(img_ff) {
    rows_half <- floor(rows/2)
    return(rbind(img_ff[((rows_half+1):rows), (1:cols)], img_ff[(1:rows_half), (1:cols)]))
  }

  swap_left_right <- function(img_ff) {
    cols_half <- floor(cols/2)
    return(cbind(img_ff[1:rows, ((cols_half+1):cols)], img_ff[1:rows, 1:cols_half]))
  }

  if (dim == -1) {
    img_ff <- swap_left_right(img_ff)
    return(swap_up_down(img_ff))
  }
  else if (dim == 1) {
    return(swap_up_down(img_ff))
  }
  else if (dim == 2) {
    return(swap_left_right(img_ff))
  }
  else {
    stop("Invalid dimension parameter")
  }
}
###################################################
###################################################
# FFT SHIFT


# Magnitude and Phase
magntd <- sqrt(Re(img_ff)^2+Im(img_ff)^2)
phase  <- atan(Im(img_ff)/Re(img_ff))

img_fftsh <- fftshift(magntd)

display(log(img_fftsh),title="FFT")

【讨论】:

  • 感谢您提供这个非常有用的答案。
猜你喜欢
  • 2021-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多