Solved

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

Posted on 2006-04-26
79 Views
Hi all,

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

Thanks!!
0
Question by:leonidn

LVL 26

Accepted Solution

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

Author Comment

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

LVL 26

Expert Comment

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 Comment

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

LVL 26

Expert Comment

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 Comment

Example images (3 sqares and a triangle):
http://www.organizermp3.com/matlab.mat
0

Author Comment

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

LVL 26

Expert Comment

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

LVL 26

Expert Comment

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

LVL 26

Expert Comment

Sorry, that is backwards...

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

0

Author Comment

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 Comment

Here are the images: a, b, c, d and e (left to right):
http://www.organizermp3.com/matlab.jpg
0

Author Comment

> 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 Comment

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 Comment

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 Comment

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

LVL 26

Expert Comment

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 Comment

> 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 Comment

> 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 Comment

d-glitch,

Your help is very much appreciated!
0

LVL 26

Expert Comment

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

## Featured Post

### Suggested Solutions

This article seeks to propel the full implementation of geothermal power plants in Mexico as a renewable energy source.
Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, anâ€¦
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files â€” any plâ€¦
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the fileâ€¦