图像的R-FFT傅里叶谱
我使用R Studio是为了完成我在图像处理方面的任务。我目前正在使用“EBImage,fftw,…”库。我有一个关于傅立叶分析和功率谱的问题 1) 我有2D矩阵,这是一个图像。此图像由黑白水平线组成。我想做傅里叶变换,通过绘图显示它的大小。因为图像中的黑线是水平的,所以功率谱将有一条垂直线。我找不到实现它的方法 现在我使用此图像进行测试: 如果为真,则该图像在水平轴和垂直轴上都有一些周期性频率。那么。FFT频谱应该看起来像一个“+”。但我发现有些不同 这是我的密码:图像的R-FFT傅里叶谱,r,image-processing,fft,R,Image Processing,Fft,我使用R Studio是为了完成我在图像处理方面的任务。我目前正在使用“EBImage,fftw,…”库。我有一个关于傅立叶分析和功率谱的问题 1) 我有2D矩阵,这是一个图像。此图像由黑白水平线组成。我想做傅里叶变换,通过绘图显示它的大小。因为图像中的黑线是水平的,所以功率谱将有一条垂直线。我找不到实现它的方法 现在我使用此图像进行测试: 如果为真,则该图像在水平轴和垂直轴上都有一些周期性频率。那么。FFT频谱应该看起来像一个“+”。但我发现有些不同 这是我的密码: setwd(".../P
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")
setwd(“…/Project/R/Workspace/Task1”)
库(“EBImage”,lib.loc=“~/R/win-library/3.2”)
库(“fftwtools”,lib.loc=“~/R/win-library/3.2”)
库(“fftw”,lib.loc=“~/R/win-library/3.2”)
#图像采集
img我解决了它。我在这里添加代码。请注意,FFT换档功能取自:[
setwd(“…/Project/R/Workspace/Task1”)
库(“EBImage”,lib.loc=“~/R/win-library/3.2”)
#形象
img你能给我们提供一个小的可复制的例子吗(例如,一个2D矩阵样本和你的代码)?Hello@Eugen,我已经纠正了我的问题,以获得清晰的定义。这是示例链接,我想达到的目的。[我解决了我的问题,这里我给出了代码。感谢你提供了这个非常有用的答案。
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")