SOCR ≫ | TCIU Website ≫ | TCIU GitHub ≫ |
Let’s try to perform the same image synthesis to reconstruct the Cyrillic and English alphabet images.
img_CyrAlpha <- load.image("BulgAlpha.jpg")
# plot(img_CyrAlpha, axes = F)
# Grayscaled # img_gray <- im[ , , 1, 1]
img_CyrAlpha <- matrix(img_CyrAlpha, nrow = dim(img_CyrAlpha)[1], ncol = dim(img_CyrAlpha)[2])
# EBImage::display(img_CyrAlpha, title='Image', method = "raster")
img_EngAlpha <- load.image("EngAlpha.jpg")
img_EngAlpha <- matrix(img_EngAlpha, nrow = dim(img_EngAlpha)[1], ncol = dim(img_EngAlpha)[2])
# add an extra zero column at the end to make the 2D Alphabet images homologous: 411 353
img_EngAlpha <- cbind(img_EngAlpha, rep(0, dim(img_EngAlpha)[1]))
dim(img_CyrAlpha); dim(img_EngAlpha)
## [1] 411 353
## [1] 411 353
# FFT
ft_img_CyrAlpha <- fft(img_CyrAlpha) # fftw2d # Display Re(FT): display(fftshift(ft_img_CyrAlpha))
ft_img_EngAlpha <- fft(img_EngAlpha) # display(fftshift(ft_img_EngAlpha))
# Magnitude and Phase
mag_ft_img_CyrAlpha <- sqrt(Re(ft_img_CyrAlpha)^2+Im(ft_img_CyrAlpha)^2)
mag_ft_img_EngAlpha <- sqrt(Re(ft_img_EngAlpha)^2+Im(ft_img_EngAlpha)^2)
# Phase <- atan(Im(img_ff)/Re(img_ff))
phase_ft_img_CyrAlpha <- atan2(Im(ft_img_CyrAlpha), Re(ft_img_CyrAlpha))
phase_ft_img_EngAlpha <- atan2(Im(ft_img_EngAlpha), Re(ft_img_EngAlpha))
# FFT SHIFT
shift_ft_img_CyrAlpha <- fftshift(mag_ft_img_CyrAlpha)
shift_ft_img_EngAlpha <- fftshift(mag_ft_img_EngAlpha)
# Display FT
EBImage::display(log(shift_ft_img_CyrAlpha), title="FT Magnitude (Cyrillic Alphabet)")
# ImplicitlyInvert the FT (IFT)
fftinv <- function( x ) { fft( x, inverse=TRUE ) / length( x ) }
EBImage::display(Re(fftinv(ft_img_CyrAlpha)),title="(IFT o FT) Magnitude (Cyrillic Alphabet)")
############## FT of img_CyrAlpha
#X1 = fft(img_CyrAlpha); display(fftshift(Re(X1)), method = "raster")
#X1_mag <- sqrt(Re(X1)^2+Im(X1)^2); display(fftshift(X1_mag), method = "raster") # magnitude only
#X1_phase <- atan2(Im(X1), Re(X1)); display(fftshift(X1_phase), method = "raster") # phase only
##### Implicit Automated IFT
#hat_X1 = Re(fft(X1, inverse = T)/length(square_arr)); display(hat_X1, method = "raster")
###### Manually invert the FT (IFT) using the magnitudes and phases
#Real1 = X1_mag * cos(X1_phase)
#Imaginary1 = X1_mag * sin(X1_phase)
#man_hat_X1 = Re(fft(Real1+1i*Imaginary1, inverse = T)/length(X1)); display(man_hat_X1, method = "raster")
############### FT of img_EngAlpha
#X2 = fft(circle_arr); display(Re(X2), method = "raster")
#X2_mag <- sqrt(Re(X2)^2+Im(X2)^2); display(X2_mag, method = "raster") # magnitude only
#X2_phase <- atan2(Im(X2), Re(X2)); display(X2_phase, method = "raster") # phase only
###### Implicit Automated IFT
# hat_X2 = Re(fft(X2, inverse = T)/length(circle_arr)); display(hat_X2, method = "raster")
###### Manually invert the FT (IFT) using the magnitudes and phases
#Real2 = X2_mag * cos(X2_phase)
#Imaginary2 = X2_mag * sin(X2_phase)
#man_hat_X2 = Re(fft(Real2+1i*Imaginary2, inverse = T)/length(X1)); display(man_hat_X2, method = "raster")
# IFT Magnitude=mag_ft_img_CyrAlpha AND Phase=phase_ft_img_EngAlpha
Real = mag_ft_img_CyrAlpha * cos(phase_ft_img_EngAlpha)
Imaginary = mag_ft_img_CyrAlpha * sin(phase_ft_img_EngAlpha)
ift_MagCyr_PhaseEng = Re(fft(Real+1i*Imaginary, inverse = T)/length(mag_ft_img_CyrAlpha))
EBImage::display(ift_MagCyr_PhaseEng, method = "raster", title="(IFT o FT) Magnitude=Cyr | Phase=Eng")
# IFT Magnitude=mag_ft_img_EngAlpha and Phase=phase_ft_img_CyrAlpha
Real = mag_ft_img_EngAlpha * cos(phase_ft_img_CyrAlpha)
Imaginary = mag_ft_img_EngAlpha * sin(phase_ft_img_CyrAlpha)
ift_MagEng_PhaseCyr = Re(fft(Real+1i*Imaginary, inverse = T)/length(mag_ft_img_CyrAlpha))
EBImage::display(ift_MagEng_PhaseCyr, method = "raster", title="(IFT o FT) Magnitude=Eng | Phase=Cyr")
# IFTMagnitude=mag_ft_img_CyrAlpha and Phase=Nil
Real = mag_ft_img_CyrAlpha * cos(0)
Imaginary = mag_ft_img_CyrAlpha * sin(0)
ift_MagCyr_PhaseNil = Re(ifftshift(fft(Real+1i*Imaginary, inverse = T)/length(mag_ft_img_CyrAlpha)))
EBImage::display(ift_MagCyr_PhaseNil, method = "raster")