How to make the Fourier Descriptor result to be Insensitive? - matlab

I am trying to distinguish shape for images in Matlab by using the Fourier Descriptor. What I want to do is : 1. Generate the Fourier Descriptors for each image; 2. Calculate the Euclidian Distance between these Fourier Descriptors to compare the shapes.
My problem is I cannot make the result of calculating the Fourier Descriptor to be insensitive for the geometric transformation (e.g. Rotation & Scaling).
The code I use now is the "Gonzales matlab version", the one in this link. I have tried to normalize the result by doing this:
% Normalization
DC = f(1);
f = f(2:11); % getting the first 20 & deleting the dc component
f = abs(f) ; % use magnitudes to be invariant to translation & rotation
f = f/DC; % devide the fourier coeffients by the DC-coefficient to be invariant to scale
But I don't think it worked as I expected. The result is different if I change the direction or the scale of a same image.
I have been trapped by this question for a couple of days. I will appreciate any suggestion, thank you all in advance!

I recommend you to read
"Feauture Extraction and Image Processing for Computer Vision"
by Nixon and Aguado.
You will find there what you are looking for

Related

perfect reconstruction of wavelet transform using CWT

If I perform a standard wavelet transform and then perform the inverse, I was expecting to get the original signal back:
% dummy series:
Fs = 1e3;
t = 0:1/Fs:1;
x = exp(cos(2*pi*32*t).*(t>=0.1 & t<0.3) + sin(2*pi*64*t).*(t>0.7));
% perform default transform and inverse
wt=cwt(x)
rx=icwt(wt)
% plot
plot(t,x,t,rx)
Apart from the offset, the flat period signals are distorted.
It seems to be possible to perform a transform/inverse and have something close to the identity function, as here Wavelet reconstruction of time series , but reading the tutorials/help for cwt I do not see how to achieve this.
The matlab documentation explains that the CWT is not the best choice for perfect reconstruction. However if you want to compare different bands as signals with the same size as the original, you can use the MODWT (or the shift-invariant DWT by cycle-spinning, sometimes calles à trous).

Matlab fft on one period of sinewave returns phase of -pi/2. Why?

While trying to understand Fast Fourier Transform I encountered a problem with the phase. I have broken it down to the simple code below. Calculating one period of a 50Hz sinewave, and applying an fft algorithm:
fs = 1600;
dt = 1/fs;
L = 32;
t=(0:L-1)*dt;
signal = sin(t/0.02*2*pi);
Y = fft(signal);
myAmplitude = abs(Y)/L *2 ;
myAngle = angle(Y);
Amplitude_at_50Hz = myAmplitude(2);
Phase_at_50Hz = myAngle(2);
While the amplitude is ok, I don't understand the phase result. Why do I get -pi/2 ? As there is only one pure sinewave, I expected the phase to be 0. Either my math is wrong, or my use of Matlab, or both of them... (A homemade fft gives me the same result. So I guess I am stumbling over my math.)
There is a similar post here: MATLAB FFT Phase plot. However, the suggested 'unwrap' command doesn't solve my problem.
Thanks and best regards,
DanK
The default waveform for an FFT phase angle of zero is a cosine wave which starts and ends in the FFT window at 1.0 (not a sinewave which starts and ends in the FFT window at 0.0, or at its zero crossings.) This is because the common nomenclature is to call the cosine function components of the FFT basis vectors (the complex exponentials) the "real" components. The sine function basis components are called "imaginary", and thus infer a non-zero complex phase.
That is what it should be. If you used cosine, you would have found a phase of zero.
Ignoring numerical Fourier transforms for a moment and taking a good old Fourier transform of sin(x), which I am too lazy to walk through, we get a pair of purely imaginary deltas.
As for an intuitive reason, recall that a discrete Fourier transform is averaging a bunch of points along a curve in the complex plane while turning at the angular frequency of the bin you're computing and using the amplitude corresponding to the sample. If you sample a sine curve while turning at its own frequency, the shape you get is a circle centered on the imaginary axis (see below). The average of that is of course going to be right on the imaginary axis.
Plot made with wolfram alpha.
Fourier transform of a sine function such as A*sin((2*pi*f)*t) where f is the frequency will yield 2 impulses of magnitude A/2 in the frequency domain at +f and -f where the associated phases are -pi/2 and pi/2 respectively.
You can take a look at its proof here:
http://mathworld.wolfram.com/FourierTransformSine.html
So the code is working fine.

How to compute distance and estimate quality of heterogeneous grids in Matlab?

I want to evaluate the grid quality where all coordinates differ in the real case.
Signal is of a ECG signal where average life-time is 75 years.
My task is to evaluate its age at the moment of measurement, which is an inverse problem.
I think 2D approximation of the 3D case is hard (done here by Abo-Zahhad) with with 3-leads (2 on chest and one at left leg - MIT-BIT arrhythmia database):
where f is a piecewise continuous function in R^2, \epsilon is the error matrix and A is a 2D matrix.
Now, I evaluate the average grid distance in x-axis (time) and average grid distance in y-axis (energy).
I think this can be done by Matlab's Image Analysis toolbox.
However, I am not sure how complete the toolbox's approaches are.
I think a transform approach must be used in the setting of uneven and noncontinuous grids. One approach is exact linear time euclidean distance transforms of grid line sampled shapes by Joakim Lindblad et all.
The method presents a distance transform (DT) which assigns to each image point its smallest distance to a selected subset of image points.
This kind of approach is often a basis of algorithms for many methods in image analysis.
I tested unsuccessfully the case with bwdist (Distance transform of binary image) with chessboard (returns empty square matrix), cityblock, euclidean and quasi-euclidean where the last three options return full matrix.
Another pseudocode
% https://stackoverflow.com/a/29956008/54964
%// retrieve picture
imgRGB = imread('dummy.png');
%// detect lines
imgHSV = rgb2hsv(imgRGB);
BW = (imgHSV(:,:,3) < 1);
BW = imclose(imclose(BW, strel('line',40,0)), strel('line',10,90));
%// clear those masked pixels by setting them to background white color
imgRGB2 = imgRGB;
imgRGB2(repmat(BW,[1 1 3])) = 255;
%// show extracted signal
imshow(imgRGB2)
where I think the approach will not work here because the grids are not necessarily continuous and not necessary ideal.
pdist based on the Lumbreras' answer
In the real examples, all coordinates differ such that pdist hamming and jaccard are always 1 with real data.
The options euclidean, cytoblock, minkowski, chebychev, mahalanobis, cosine, correlation, and spearman offer some descriptions of the data.
However, these options make me now little sense in such full matrices.
I want to estimate how long the signal can live.
Sources
J. Müller, and S. Siltanen. Linear and nonlinear inverse problems with practical applications.
EIT with the D-bar method: discontinuous heart-and-lungs phantom. http://wiki.helsinki.fi/display/mathstatHenkilokunta/EIT+with+the+D-bar+method%3A+discontinuous+heart-and-lungs+phantom Visited 29-Feb 2016.
There is a function in Matlab defined as pdist which computes the pairwisedistance between all row elements in a matrix and enables you to choose the type of distance you want to use (Euclidean, cityblock, correlation). Are you after something like this? Not sure I understood your question!
cheers!
Simply, do not do it in the post-processing. Those artifacts of the body can be about about raster images, about the viewer and/or ... Do quality assurance in the signal generation/processing step.
It is much easier to evaluate the original signal than its views.

Fourier transformation

I'm writing currently a program in Matlab which is related on image hashing. Loading the image and performing a simple down-sampling was not a problem.Here is my code
clear all;
close all;
clc;
%%load Image
I = im2single(imread('cameraman.tif'));
%%Perform filtering and downsampling
gaussPyramid = vision.Pyramid('PyramidLevel', 2);
J = step(gaussPyramid, I); %%Preprocessed Image
%%Get 2D Fourier Transform of Input Image
Y = fft2(I); %%fft of input image
The algorithm next assumes that 2D Fourier Transform ( Y in my case ) must be in the form Y(f_x,f_y) where f_x,f_y are the normalized spatial frequencies
in the range [0, 1].I'm not able to transform the output of fft2 function from Matlab as it is required by the algorithm.
I looked up the paper in question (Multimedia Forensic Analysis via Intrinsic and Extrinsic Fingerprints by Ashwin Swaminathan) and found
We take a Fourier transform on the preprocessed image
to obtain I(fx, fy). The Fourier transform output is converted into polar co-ordinates to arrive at I′(ρ, θ)
By this, they seem to mean that I(fx, fy) is the Fourier transformation of the image. But they're trying to use the Fourier-Mellin transformation, which is different from a simple fft2 on the image. Based on the information found in this set of slides,
If an input image is expressed in terms of coordinates that are natural logarithms of the original coordinates, then the magnitude of its FT is insensitive to any change in scale of the original image (since it is Mellin Transform in the original coordinates)
and in files on the MATLAB Central File exchange, you have to do additional work to get the Mellin-Fourier transform; particularly, taking the magnitude (which appears to be your missing step), converting the coordinates to log polar and taking a second fft2. It's unclear to me why the log of log polar coordinates was omitted from the paper in the steps required. See an implementation for image registration here for an example. Note that this code is old, and the transformImage method appears not to exist; it does the log polar transform.

Order of frequency-shifting operations in 2D FFT for far-field diffraction simulations (with Matlab/Octave/Scipy)

In Matlab/Octave/Scipy, what is the correct way of shifting the frequency components after performing 2D Fourier transforms back and forth between two planes (with the output plane being the far-field diffraction plane of the input plane) ?
Let's say that I would like to run an iterative Fourier-transform algorithm between these two planes in order to compute a hologram (the phase-delay pattern to be applied to the input plane), in order to obtain a given intensity pattern in the output plane.
Let the field in the input plane be called A
and the field in the output plane called B.
So B will be the Fourier transform of A, and A the inverse Fourier transform of B (this follows from the Franhauffer diffraction theory).
The question is: in what order shall I shift the frequency components with the command fftshift in between the Fourier transforms connecting the two planes ?
It seems that writing the following does not do the job:
B=fftshift(fft2(A));
A2=fftshift(ifft2(B));
This is probably a common issue but I couldn't find the answer anywhere..
I'm not sure I understand your use-case, but the general purpose for fftshift is to rotate the output of fft so that the DC (zero frequency) bin is centred. So the usual pattern is:
X = fft2(x); % DC at X(1,1)
X_shift = fftshift(X); % DC at X_shift(M/2+1,N/2+1)
% ... processing occurs here (Y_shift = foo(X_shift)) ...
Y = ifftshift(Y_shift);
y = ifft2(Y);