# Shape matching using fourier descriptor - Frequency domain

Hey algorithm enthusiast,

Here is an implementation I am trying to make it work for shape matching that is invariant to rotation, translation and scaling as per theory.

What is the process that I tried

• Get RGB input image, convert to gray scale

• Guassian Blur the image with 3x3 kernel and perform thresholding

• Do some morphological operation to close the edges and find external contour of this thresholded image

• Find the largest contour that forms the boundary of desired object. The returned contour points are in counter clock wise direction, so reverse it to make it clockwise order. (As explained in few papers)

• Perform differential coding (current coordinate - next coordinate) for contour coordinates and save it as vector of new points. Convert this to two planes (one for x and another for y).

• Perform DFT for these differential encoded contour points. Calculate Magnitude from result of DFT, discard DC coefficient and normalize other Fourier coefficients with first harmonic (this is done for scale invariancy).

• Perform Euclidean distance for template image and input test image.

Stuck up with

The Euclidean distance calculated is coming same/closer for any test sample I give. Not sure where I do mistake. I do not find any sample implementation for frequency domain shape matching.

Sample image in recognition    EDIT:

I am trying to perform rotation, scale and translation invariant shape matching using Fourier descriptor as proved in technical papers. I am making use of OpenCV 2.4.9 on Windows.

edit retag close merge delete

Just a note. If you can use OpenCV 3.0, Shape Context is available and it is exactly suited for this kind of problems.

Looks promising, will try and update you the result of Shape context in OpenCV 3.0.

@juanmanpr: Shape context in OpenCV 3.0 works well with their samples. But produces poor result with image attached in my original post. The distance value calculated by Shape context is high for same type of object, but low for different object, and varies with same object.

@Spark, did you try tweaking the parameters? There is a lot to try there, I would start by looking at the vectors of points that form the edges, the rotation invariance, sometimes you should mirror your training shapes along the two axis so you achieve more robustness

Hi, I am currently doing a project of comparing pictures as well. Now, I could only take out the edge, then find the contours and draw them with colors. However, the edges are not connected, I heard that morphological operation can close the edge. Could you elaborate more on which operation should I use ? Thanks!

Sort by » oldest newest most voted

Hi,

I don't know exactely what you want to do with fourier descriptor but you must care about number of point in your contour. May be you should use first a software like matlab scilab octave.... to check your algorithm For example with scilab a small program rotating a shape :

fs=mopen('Ctr.txt',"r");
c=[];
while meof(fs)==0
x=mfscanf(2,fs,"%d")
c=[c x];
end
mclose(fs)
z=c(1,:)+%i*c(2,:);
Z=fft(z)
Z(1)=0;
scf(1)
clf()
subplot(2,2,1)
title('Contour')
plot(c(1,:),c(2,:))
subplot(2,2,2)
title('Fourier descriptor')
Z(1)=0;
f=(0:length(z)-1)/length(z);
plot(abs(Z))
zc=ifft(Z.*exp(%i*%pi/3));
subplot(2,2,3)
title("$Rotate \frac{\pi}3$');
plot(real(zc),imag(zc))
Z=Z/10;
zc=ifft(Z.*exp(%i*%pi/3));
subplot(2,2,4)
plot(real(zc),imag(zc))
title('$Scale \frac1{10}$');


the result is more

Yeah, number of points chosen plays very important role in matching. But for now I am using all the descriptor points (say, 625, 535 points for few object). I find minimum of two contours and match those points by finding distance between them.

here is a method i found usful (http://www.codeproject.com/Articles/1...)

more

Official site

GitHub

Wiki

Documentation