# Matlab - how can I check the level of similarity between two images?

Hi all,

How can I check the level of similarity between two images?
(Basically they are nearly the same size).

Thanks!!
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You can calculat the sum and differences of the images and calculate the RMS value of each:

RMS( Image1 - Image2)
If      ------------------------------    is close to zero it means the images are similar.
RMS( Image1 + Image2)

If it is close to one, then they are different.

You could also look at the differences between the two dimensional FFT's of the two images.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
The RMS idea doesn't seem to be very robust...

I do like the FFT2 idea, but it the result matrixes are of (slightly) different sizes. How can I compare these ones?
0
Commented:
Both of these techniques will work better if you can make the file the same size before processing.
Either stretch the smaller one or shrink the larger.

You could also normalize the FFT after processing.

What are your images?  Faces or maps or fingerprints?  What kind of differences are you looing for?
There are particular techniques for some types of images?
0
Author Commented:
Here is some information that is (hopefully) useful.

1) Average size of an image is 24x24, but it can be 23x26, etc...

2) The images are binary (that is, a pixel is either white or black).

3) Possible images: square, triangle, cross (maybe a couple of others, but very different from each other).
Even if both images are squares, that ARE different (because the images are a result of an edge-detection algorithm.
0
Commented:
Put your little images in the center of a 32x32 square to normalize them.
(You have to pad to 2^n anyway for FFT's.)

The FFT should be able to pick out the various features pretty easily.

You might also want to look at eigenvalue decomposition.
This is one of the techniques they use for comparing faces.
Matlab certainly has the tools for this.  Check the SVD function.
0
Author Commented:
Example images (3 sqares and a triangle):
http://www.organizermp3.com/matlab.mat
0
Author Commented:
I've just tried FFT2 on the padded 32x32 images (original images in center).

Then I tried:
f1 = real(fft2(im1));
f2 = real(fft2(im2));
sum(sum(abs(f1-f2)))

For square vs. square it returned 1.3695e+006
And for a square vs. triangle it returned 1.7954e+006
Not a difference you oculd trust  :'-(

Am I doing something wrong?
0
Commented:
Sorry I can not read that file.

Are your images lines or blobs?  Are they aligned with the x or y axes?

If they are aligned line drawings, maybe all you have to do is look at the sums of each horizontal and vertical row.

A square would have two peaks in each of the sum vectors.
A triangle woud have one peak in one of the sum vectors.
A cross would have one peak in each of the sum vectors.
And so on...
0
Commented:
You still have to normalize.

sum(sum(abs(f1-f2)))
K  = --------------------------
sum(sum(abs(f1+f2)))

Your K values should be near 1 for matches and near 0 for mismatches.
0
Commented:
Sorry, that is backwards...

Your K values should be near 0 for matches and near 1 for mismatches.

0
Author Commented:
Another test:

I took 3 squares (a, b, c), and compared to each other, and then same with two triangles (d and e):

1.3695e+006   <-- a vs. b
7.1811e+005   <-- a vs. c
1.3644e+006   <-- b vs. c
6.3247e+005   <-- d vs. e

Then I took each of the 3 squared, and compared to a triangle d:

1.7983e+006   <-- a vs. d
1.7392e+006   <-- b vs. d
1.7954e+006   <-- c vs. d

Then I took each of the 3 squared, and compared to another triangle e:

1.7663e+006   <-- a vs. e
1.7510e+006   <-- b vs. e
1.7852e+006   <-- c vs. e

###################################

The results are always correct (I can match between the two objects), but I do not think the difference is very reliable.
What do you think?
0
Author Commented:
Here are the images: a, b, c, d and e (left to right):
http://www.organizermp3.com/matlab.jpg
0
Author Commented:
> If they are aligned line drawings, maybe all you have to do is look at the sums of each horizontal and vertical row.

> A square would have two peaks in each of the sum vectors.
> A triangle woud have one peak in one of the sum vectors.
> A cross would have one peak in each of the sum vectors.
> And so on...

Yes, they are aligned, but I can't assume these are all the shapes I can get. The algorithm has to be more generic than just squares and triangles...
By the way, the shapes do not rotate, so this makes the task a little easier.
0
Author Commented:
I get some objects in pairs (for example, 2 squares, and two triangles).
The images can be passed to the function in any order,
I need to match between every pair.
0
Author Commented:

sum(sum(abs(f1-f2)))
K  = --------------------------
sum(sum(abs(f1+f2)))

I get:

################################################################

Comparing two similar shapes:

>> k_ab = sum(sum(abs(aa-bb))) / sum(sum(abs(aa+bb)))
0.6372

>> k_ac = sum(sum(abs(aa-cc))) / sum(sum(abs(aa+cc)))
0.2880

>> k_bc = sum(sum(abs(bb-cc))) / sum(sum(abs(bb+cc)))
0.6240

>> k_de = sum(sum(abs(dd-ee))) / sum(sum(abs(dd+ee)))
0.2531

################################################################

Comparing two different shapes:

>> k_ad = sum(sum(abs(aa-dd))) / sum(sum(abs(aa+dd)))
0.9060

>> k_ae = sum(sum(abs(aa-ee))) / sum(sum(abs(aa+ee)))
0.8928

>> k_bd = sum(sum(abs(bb-dd))) / sum(sum(abs(bb+dd)))
0.8821

>> k_ce = sum(sum(abs(cc-ee))) / sum(sum(abs(cc+ee)))
0.8987

################################################################

So it too gives correct reults (I can match pairs), but the results are still quite close to each, I think).
0
Author Commented:
P.S.  I also tried to use fftshift before applying your forumla:

sum(sum(abs(f1-f2)))
K  = --------------------------
sum(sum(abs(f1+f2)))

but got really similar results as without fftshift.
0
Commented:
Those results look pretty good to me.
You could set your threshold at 0.7 and call it a day.
What is the penalty for making an error.

The only thing that keeps the results from looking very good is that     k_ab and k_bc ~  0.6
I would have hoped for something less than 0.5

Is there something odd about image b??
0
Author Commented:
> The only thing that keeps the results from looking very good is that     k_ab and k_bc ~  0.6
> I would have hoped for something less than 0.5

That's exactly my point...

Is there something odd about image b??

I don't think so... have a look at the middle square in the image I uploaded:
http://www.organizermp3.com/matlab.jpg
0
Author Commented:
> What is the penalty for making an error?

I will try to allow a fault surrounded by several successes, but I prefer to avoid the situation...

The direct effect of an error in the algorithm may cause the robot not recognize an object that gets nearer and nearer to it. As a result, the robot won't bend, and the ball will hit it just in the "face" :-)
0
Author Commented:
d-glitch,

Your help is very much appreciated!
0
Commented:
I think the problem might  be with the ragged corners on the squares.
You might be able to improve things by some simple filtering.

Vertical lines have high frequency        X components and low frequency Y components.
Horizontal lines have high frequency     Y components and low frequency X components.

Slanted lines will have intermediate X and Y components.

But the jagged corners will have HF X and Y components.  This is essentially noise.
You may be able to eliminate it before you do the comparisons.

Look at what parts of the FFT are contributing the most to k_ab and k_bc ==> 0.6
and set those elements to zero.  It should be appox 1/4 or less of the array.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Math / Science

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.