Reputation: 105
I am using R Studio in sake of doing my tasks in Image Processing. I am currently using the 'EBImage, fftw, ...' libraries. I have a question about fourier analysis and power spectrum.
1) I have 2D matrix, which is an image. This image consist on horizontal lines, black and white. I want to make Fourier Transformation and show its magnitude by ploting. Since the black lines in image are horizontal, the power spectrum will have an vertical line. I couldn't find a way to achieve it.
Now I am using this image for test: Square
When it is true, this image has some periodic frequencies on both horizontal and vertical axis. Then. the FFT Spectrum should look like a "+". But what I found is something way different.
Here is my code:
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")
As a result, this is what I have: FFT Spectrum
Here are my questoions:
1) How can I get a correct spectrum image? 2) How can I see it as an image, not a plot? (See Example Link on top.)
Thank you for your help in advance.
Upvotes: 3
Views: 4058
Reputation: 105
I solved it. Here I am adding the code. Please see that, fftshift functions were taken from: [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")
Upvotes: 5