[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-04-26
21
Medium Priority
?
88 Views
Last Modified: 2016-02-10
Hi all,

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

Thanks!!
0
Comment
Question by:leonidn
  • 13
  • 8
21 Comments
 
LVL 27

Accepted Solution

by:
d-glitch earned 1400 total points
ID: 16542969
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

by:leonidn
ID: 16543368
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 27

Expert Comment

by:d-glitch
ID: 16543657
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:leonidn
ID: 16543722
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 27

Expert Comment

by:d-glitch
ID: 16543894
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

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

Author Comment

by:leonidn
ID: 16544088
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 27

Expert Comment

by:d-glitch
ID: 16544117
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 27

Expert Comment

by:d-glitch
ID: 16544182
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 27

Expert Comment

by:d-glitch
ID: 16544216
Sorry, that is backwards...

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

0
 

Author Comment

by:leonidn
ID: 16544217
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

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

Author Comment

by:leonidn
ID: 16544303
> 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

by:leonidn
ID: 16544323
Just to clarify the task:
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

by:leonidn
ID: 16544417
After normalizing your way:

       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

by:leonidn
ID: 16544452
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 27

Expert Comment

by:d-glitch
ID: 16544536
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

by:leonidn
ID: 16544567
> 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

by:leonidn
ID: 16544622
> What is the penalty for making an error?

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

Author Comment

by:leonidn
ID: 16545039
d-glitch,

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!
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 16545436
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever thought of installing a power system that generates solar electricity to power your house? Some may say yes, while others may tell me no. But have you noticed that people around you are now considering installing such systems in their …
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question