Question

How compare the different between two pictures using matlab?

Asked by: YANKAUSKAS

I'm trying to find the similarity of 2 images by measuring the histogram error between them, the smaller, the better similar. The method I currently use is:
+ Calculate normalized histogram of both 2 images
+ Calculate the sum of the squared differences between the normalized histograms using the below attached formula.
My code in matlab like this (code below).

I have questions:
1. I think what I do is correct. Do you have any comments of potential problems?
2. I convert these above code to function: findsim(image1,image2). Now I want to write a small code using this function above. I have total 10 pictures, and now I want to get individual picture, compare to the remains 9 pictures using above function. And export the list of 9 pictures according to the error from minimum to maximum. I'm thinking using a for loop but, is there any other way less painful? How to get the list of all images, take only one each time and do the task I want with a short number of codes?
Any comment or suggestions are appreciate!

% read two images
Im1 = imread('image1.bmp');
Im2 = imread('image2.bmp');
 
%  convert images to type double (range from from 0 to 1 instead of from 0 to 255)
Im1 = im2double(Im1);
Im2 = im2double(Im2);
 
% Calculate the Normalized Histogram of Image 1 and Image 2
hn1 = imhist(Im1)./numel(Im1);
hn2 = imhist(Im2)./numel(Im2);
 
% Calculate the histogram error
f = sum((hn1 - hn2).^2);
f %display the result to console

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-09-21 at 22:30:17ID24750602
Tags

matlab

Topics

MatLab Programming Language

,

Math & Science

,

Signal Processing

Participating Experts
1
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Matlab
    Hello, i have a say 10 X 1 matrix. i should divide it into 5 2 X 1 matrices. i want to values to be the same. Please tell me how i can do it in matlab. Thank you, Suma

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: YANKAUSKASPosted on 2009-09-21 at 22:32:02ID: 25389752

I forgot to attach the formula.

 

by: YANKAUSKASPosted on 2009-09-21 at 22:58:08ID: 25389885

Hi,
I have found the way to deal with images list using list. I define imageList {'image1', 'image2', 'image3', ...., 'image10'} as a list of all images. After that I use a for loop running from 1 to the length of list, use the second for loop inside the first one, if the index in the second is different the index in the first loop, it will print out the list of file. The problem now I have is how to call the function findsim(im1,im2) in file findsim.m in this context. I use
findsim(imageList{index1}, imageList{index2}) but it didn't display anything.
Function findsim(im1,im2) now below:
Can anyone take a look at it and suggest me?

function histErr = findsim(im1,im2)
 
Im1 = imread(im1);
Im2 = imread(im2);
 
Im1 = im2double(Im1);
Im2 = im2double(Im2);
 
hn1 = imhist(Im1)./numel(Im1);
hn2 = imhist(Im2)./numel(Im2);
 
histErr = sum((hn1 - hn2).^2);

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window

 

by: YANKAUSKASPosted on 2009-09-21 at 23:20:39ID: 25389987

I only have problem with below code:

disp(['----', imageList{index2}, ': ', 'Error = ', findsim(imageList{index1}, imageList{index2})]);

The result will be:

Image name from  imageList. Error=
Image name from  imageList. Error=
Image name from  imageList. Error=
Image name from  imageList. Error=
.....

If I move the findsim function to below, it works.
disp(['----', imageList{index2}, ': ', 'Error = ']);
findsim(imageList{index1}, imageList{index2});


The result will be:
Image name from  imageList. Error=
Ans = results 1
Image name from  imageList. Error=
Ans = results 2
Image name from  imageList. Error=
Ans = results 3
Image name from  imageList. Error=
Ans = results 4
.....

How to make it in the same line and arrange it in the order from minimum to maximum?

imageList = {'image1.png', 'image2.png','image3.png','image4.png', 'image5.png',
'image6.png','image7.png','image8.png','image9.png','image10.png'}
 
for i=1ndex1:length(imageList)
	    for index2 = 1:length(imageList) 
        if (index 2 ~= index1)
            disp(['----', imageList{index2}, ': ', 'Error = ', findsim(imageList{index1}, imageList{index2})]);
        end
    end
    
end

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

 

by: yuk99Posted on 2009-09-22 at 10:03:34ID: 25394991

To add the function result to your output string convert it to string first with num2str:
[ ... num2str(findsim(...))];
Use second argument if you want to control precision.
Or you can use fprintf function (see below).

To arrange from min to max you have to calculate all the distances first, sort them, and only then display.
Check the code below. I didn't test it thoroughly but it should work. Let me know in case of problems.

imageList = {'image1.png', 'image2.png','image3.png','image4.png',...
    'image5.png','image6.png','image7.png','image8.png',...
    'image9.png','image10.png'};
imNum = length(imageList);
imdist = zeros((imNum^2-imNum)/2,3);
count=0;
for index1 = 1:imNum
    for index2 = index1+1:imNum
        count = count + 1;
        imdist(count,:) = [image1 image2 findsim(imageList{index1},...
            imageList{index2})];
    end
end
[imdistsorted, idx] = sort(imdist(:,3));
% display the marix
disp(imdist(idx,:))
% display formatted
for i = idx
    fprintf('Error between %s and %s is %d\n',...
        imageList{imdist(i,1)},imageList{imdist(i,2)},imdist(i,3));
end
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:

Select allOpen in new window

 

by: yuk99Posted on 2009-09-22 at 10:08:53ID: 25395044

You may consider reading the images outside of the function. Because your code will actually read the same images several times. You can read all the images at once to a cell array, then distance calculations will be faster, but will it take more memory. Depending on amount and size of your images think what is more important for you - memory or speed.

 

by: YANKAUSKASPosted on 2009-09-22 at 10:59:04ID: 25395624

Thank you yuk99.

I have the function findsim in file findsim.m, now I want to use it here. I have changed a little to make it works. As in your code:

imdist(count,:) = [image1 image2 findsim(imageList{index1}, imageList{index2})];

I see error:
??? Undefined function or variable 'image1'

AFAIK, image1 and image2 here is imageList{index1} and imageList{index2}. So I change it. And I received this error:

??? Subscripted assignment dimension mismatch.

Error in ==> tmp at 20
       imdist(count,:) = [imageList{index1} imageList{index2}
       findsim(imageList{index1},imageList{index2})];

Could you please tell me why ?

I also don't clearly understand these lines of code:
imdist = zeros((imNum^2-imNum)/2,3);
Why you use (imNum^2-imNum)/2 here?

As the for loop, I think it only display the error between each image in the whole. But what I want is get one image as a template and findsim of this one to all remain image, and sort it. I'm not sure this code above can do it.

Thank you yuk99.

I have the function findsim in file findsim.m, now I want to use it here. I have changed a little to make it works. As in your code:

imdist(count,:) = [image1 image2 findsim(imageList{index1}, imageList{index2})];

I see error:
??? Undefined function or variable 'image1'

AFAIK, image1 and image2 here is imageList{index1} and imageList{index2}. So I change it. And I received this error:

??? Subscripted assignment dimension mismatch.

Error in ==> tmp at 20
       imdist(count,:) = [imageList{index1} imageList{index2}
       findsim(imageList{index1},imageList{index2})];

Could you please tell me why ?

I also don't clearly understand these lines of code:
imdist = zeros((imNum^2-imNum)/2,3);
Why you use (imNum^2-imNum)/2 here?

As the for loop, I think it only display the error between each image in the whole. But what I want is get one image as a template and findsim of this one to all remain image, and sort it. I'm not sure this code above can do it.

imageList = {'image1.png', 'image2.png','image3.png','image4.png','image5.png','image6.png','image7.png','image8.png','image9.png','image10.png'};
 
imNum = length(imageList);
imdist = zeros((imNum^2-imNum)/2,3);
count = 0;
for index1 = 1:imNum
    for index2 = index1+1:imNum
        count = count + 1;
        imdist(count,:) = [imageList{index1} imageList{index2} findsim(imageList{index1},imageList{index2})];
    end
end
 
[imdistsorted, idx] = sort(imdist(:,3));
% display the marix
disp(imdist(idx,:))
% display formatted
for i = idx
    fprintf('Error between %s and %s is %d\n', imageList{imdist(i,1)},imageList{imdist(i,2)},imdist(i,3));
end

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:

Select allOpen in new window

 

by: yuk99Posted on 2009-09-22 at 11:07:54ID: 25395707

For the first error, sorry, it should be index1 and index2.

imdist = zeros((imNum^2-imNum)/2,3); do memory pre-allocation. Number of rows calculated as number of unique combinations (distance between images 1 and 2 is the same as between 2 and 1). 3 is number of columns to hold image indexes and the distance. Of course, this matrix is for doubles only, you cannot put strings there. If you want image names together with distance in one matrix use cell array.





 

by: yuk99Posted on 2009-09-22 at 11:18:07ID: 25395825

Of course, you can do it for one image and compare it with the rest of images. Just assign a constant to index1.

imdist = zeros(imNum-1,3);
 
index1 = 1 % or 2 etc
for index2 = 1:imNum
    if (index 2 ~= index1)
        count = count + 1;
        imdist(count,:) = [imageList{index1} imageList{index2} findsim(imageList{index1},imageList{index2})];
    end
end
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window

 

by: YANKAUSKASPosted on 2009-09-22 at 11:30:11ID: 25395950

What i mean is using one image as a template and compare with the remain 9 pictures.
Pic 1 compare to pic 2->pic10
Pic 2 compare to pic 1 and pic 3->pic10
.............
Pic 8 compare to pic1-> pic 7 and pic9->pic10
Pic 9 compare to pic1-> pic 8 and pic 10
Pic 10 compare to pic1->pic 9

So I think I need 10 matrix to arrange, right? So I have to use a big for loop for index1 to get it run as the above code?

 

by: yuk99Posted on 2009-09-22 at 12:14:37ID: 25396400

Ah, ok. Keep the outer FOR loop and put sorting and printing into it. Can you do it yourself or need a help?

I would calculate the distances between all possible pair of images and store them in a square matrix. In this case you can do a lot of analysis of images similarity.

 

by: YANKAUSKASPosted on 2009-09-22 at 12:20:35ID: 25396457

I will try it and will comeback if I can't. Thank you very much!

 

by: YANKAUSKASPosted on 2009-09-22 at 18:25:49ID: 31631754

Finally it works, although I have changed nearly all of the code :D. Thank you very much yuk99.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...