RMS( Image1 - Image2)

If --------------------------

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.

Solved

Posted on 2006-04-26

Hi all,

How can I check the level of similarity between two images?

(Basically they are nearly the same size).

Thanks!!

How can I check the level of similarity between two images?

(Basically they are nearly the same size).

Thanks!!

21 Comments

RMS( Image1 - Image2)

If --------------------------

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.

I do like the FFT2 idea, but it the result matrixes are of (slightly) different sizes. How can I compare these ones?

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?

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.

(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.

Example images (3 sqares and a triangle):

http://www.organizermp3.com/matlab.mat

http://www.organizermp3.co

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?

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

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.

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?

Here are the images: a, b, c, d and e (left to right):

http://www.organizermp3.com/matlab.jpg

http://www.organizermp3.co

>

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

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.

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).

sum(sum(abs(f1-f2)))

K = --------------------------

sum(sum(abs(f1+f2)))

but got really similar results as without fftshift.

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

> 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.co

Pretty bad because it may probably cause cascading errors...

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" :-)

Increased by 100 points, and accepted your answer as 'excellent'.

Thank you very much for your quick and good answer.

Your help is very much appreciated!

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.

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

Formatting Rounded Math Output in Visual Basic VS2015 | 4 | 27 | |

Graph Function | 6 | 47 | |

Simplify expression | 3 | 47 | |

Windows Batch File - Count Down | 4 | 26 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**16** Experts available now in Live!