Ask Your Question
4

Shape matching using fourier descriptor - Frequency domain

asked 2015-03-09 09:36:10 -0600

Spark gravatar image

updated 2015-03-11 02:38:21 -0600

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.

Reference Links

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.

Please help in understanding any mistakes that is being done.

Sample image in recognition Knife Input

Plier Input

contour knife

contour plier

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 flag offensive close merge delete

Comments

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

juanmanpr gravatar imagejuanmanpr ( 2015-03-13 09:52:51 -0600 )edit

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

Spark gravatar imageSpark ( 2015-03-17 03:36:08 -0600 )edit

@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 gravatar imageSpark ( 2015-03-20 09:24:25 -0600 )edit

@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

juanmanpr gravatar imagejuanmanpr ( 2015-03-22 09:05:03 -0600 )edit

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!

accqq gravatar imageaccqq ( 2016-09-11 02:48:42 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2015-03-10 09:33:36 -0600

LBerger gravatar image

updated 2015-03-11 10:09:19 -0600

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 image description

edit flag offensive delete link more

Comments

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.

Spark gravatar imageSpark ( 2015-03-17 03:35:37 -0600 )edit
0

answered 2015-03-12 02:48:02 -0600

chengweige gravatar image

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

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-03-09 09:36:10 -0600

Seen: 6,556 times

Last updated: Mar 12 '15