Code covered by the BSD License  

Highlights from
Image retrieval - Query by Example Demo


4.1 | 17 ratings Rate this file 77 Downloads (last 30 days) File Size: 6.94 MB File ID: #22030
image thumbnail

Image retrieval - Query by Example Demo



This demo provides a simple query by image example method, based on HSV values.

| Watch this File

File Information

Content-based image retrieval is the task of searching images in databases by analyzing the image contents. In this demo, a simple image retrieval method is presented, based on the color distribution of the images. The user simply provides an "example" image and the search is based upon that example (query by image example). For this first version of the demo no relevance feedback is used.

Almost 1000 images have been used for populating the database. For each image a 3-D histogram of it's HSV values is computed. At the end of the training stage, all 3D HSV histograms are stored in the same .mat file.
In order to retrieve M (user-defined) query results, the following steps are executed:

The 3D (HSV) histogram of the query image is computed. Then, the number of bins in each direction (i.e., HSV space)is duplicated by means of interpolation.
For each image i in the database:
Load its histogram Hist(i).
Use interpolation for duplicating the number of bins in each direction.
For each 3-D hist bin, compute the distance (D) between the hist of the query image and the i-th database image.
Keep only distances (D2) for which, the respective hist bins of the query image are larger than a predefined threshold T (let L2 the number of these distances).
Use a 2nd threshold: find the distance (D3) values which are smaller than T2, and let L3 be the number of such values.
The similarity measure is defined as: S(i) = L2 * average(D3) / (L3^2).
Sort the similarity vector and prompt the user with the images that have the M smaller S values.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (41)
20 Nov 2013 shefalli

Pls tell me why we have taken i=3 in loop not 1???my id is
function [Hists,files] = getImageHistsDir(DirName)
D = dir(DirName);
count = 0;

for (i=3:length(D))
if ((strcmpi(D(i).name(end-3:end), '.jpg')==1) | (strcmpi(D(i).name(end-3:end), '.jpeg')==1))
count = count + 1;
Hists{count}=getImageHists([DirName '\\' D(i).name]);
files{count}=[DirName '\\' D(i).name];
fprintf('%.4d File %40s computed...\n',count,files{count});
save (savefile, 'files', 'Hists')

19 Nov 2013 shefalli

sir how can we train the data

20 Apr 2013 Joel

This program uses Euclidean Distance to compare the similarities. Instead can someone change the code to compute the similarities as one of the following please.

Canberra Distance or City Block Distance

09 Mar 2013 Joel

How can I train a batch of images to this project.


09 Mar 2013 Joel

How can I add my own images to this project?


08 Aug 2012 Johnny  
30 May 2012 Jess

Hi Muhammad Imran,

It won't help in your understanding by explaining line-by-line. Try running the command in Matlab workspace and utilize the plot/imshow etc to display the changes for each step. You will be able to understand how the code work and learn Matlab at the same time. For concepts learning, search related literature.

30 May 2012 Saddam Shahzad

Thanx for such a nice upload, plz tell me why you use interpolation and what is the concept behind threshold and range , waiting for your reply thanx

30 May 2012 M@lik Ali

Hi all,

I am new to matlab and image processing, this code is work fine with me, but i can't can understand it in detail. can any one help me to explaining the code line by line.
you can email me to malikimran110@gmail as well.
hoping to hear from you.

22 May 2012 Jess

Hi Muhammad Imran,
"modelName" is the name of a variable. Author provided model1Hist.mat as training data (generate own training data using getImageHists.m). Refer to documentation on how to properly run the command. For example,

27 Apr 2012 M@lik Ali

i try to run this program but it give error , any one can help
??? Input argument "modelName" is undefined.

Error in ==> searchImageHist at 14

18 Dec 2011 Adnan Munir

??? Error using ==> strfind
Input strings must have one row.

Error in ==> imread at 340
if (strfind(filename, '://'))

Error in ==> getImageHists at 4
RGB = imread(imageName);

Error in ==> searchImageHist at 18
[Hist, RGBQ] = getImageHists(imageName);

im getting this error by runnig ur code and im unable to solve help me plz its urgent

01 Dec 2011 prabagaran M

i want to know how to check similarity between two images can anyone help pls

28 Nov 2011 Mulugeta Ayele

Great work for all of you.

26 Nov 2011 Cevat  
25 Nov 2011 Onkar Singh Gujral

but i need some help to create code to check for similar images on the web in matlab
how do i do that as i am creating a cancer detecting software

22 Nov 2011 wu

hi, your code is very helpful for me, but i have a question about the measure of distance and similarity.
i want to know why use this formula S(i) = L2 * average(D3) / (L3^2) to compute similarity.
if any literature mention this method , it cant be better more

thanks for your answer

28 Apr 2011 Jess

Addition to ampire Ê (20 May 2009), can provide user with flexibility to select any directory (with images) by adding one line after the function name.

>function getImageHistsDir

DirName = uigetdir();

>D = dir(DirName);
>count = 0;
>for (i=3:length(D))
> if ((strcmpi(D(i).name(end-3:end), '.jpg')==1) | (strcmpi(D(i).name(end-3:end), '.jpeg')==1))
> count = count + 1;
> >Hists{count}=getImageHists([DirName '\\' D(i).name]);
> files{count}=[DirName '\\' D(i).name];
> fprintf('%.4d File %40s computed...\n',count,files{count});
> end
>save (savefile, 'files', 'Hists')

28 Apr 2011 Jess

hello Mr Rajkumar,

nResults is one of the input parameter when you call searchImageHist

26 Mar 2011 RAJKUMAR Palaniappan

how is the nResults computed?

26 Mar 2011 RAJKUMAR Palaniappan

hello Mr. Nicola Franzoso
i tried ur code for developing a new model for a new database but i got an error as shown bellow. i used 653 .jpg images

Driver: []
URL: []
Constructor: []
Message: 'Function requires three or five arguments to be defined'
Handle: []
TimeOut: []
AutoCommit: []
Type: []
is there any other means to correct it?

17 Feb 2011 Abhishek Kumar  
03 Feb 2011 Theodoros Giannakopoulos

Hi MUKUND SARDA, thank you for your interest. There is a quite detailed description of the algorithm above. In general the idea is quite simple: an empirical measure is defined on the thresholded distances of the HSV feature space. Then this similarity measure is sorted for returning the closest images. If you have any particular questions, please feel free to ask.

29 Jan 2011 MUKUND SARDA

sir will u pls provide detail algorithm of this program so that i can undersatnd it easily...

15 Dec 2010 sjye  
06 Oct 2010 Faizal

How about the GUI?

22 Sep 2010 Faizal

i'm very appreciate with... u has any idea if i want to used Integrated Histogram Bin Matching tecnique to image similarity..
i hope u can help me..

05 May 2010 akash mehra

hi... everyone thnx 4 the code and the posts....
i am trying to add new images to the existing database but i'm not able to write the code....
if anyone could... plz post here......
thnx........ a lot...

24 Oct 2009 mansi verma

image retrieval code is not getting downloaded. its showing an error of invalid archieve. please update it and if possible send a copy to

thank u!!

05 Oct 2009 Raksha  
20 May 2009 ampire Ê

thank you for your code! But I think the code above is not very's code based on you:
function [Hists,files] = getImageHistsDir(DirName)
D = dir(DirName);
count = 0;

for (i=3:length(D))
if ((strcmpi(D(i).name(end-3:end), '.jpg')==1) | (strcmpi(D(i).name(end-3:end), '.jpeg')==1))
count = count + 1;
Hists{count}=getImageHists([DirName '\\' D(i).name]);
files{count}=[DirName '\\' D(i).name];
fprintf('%.4d File %40s computed...\n',count,files{count});
save (savefile, 'files', 'Hists')
Making use of it,you can get imagesdir in your images files.thank you!

22 Apr 2009 mubashar khan

i managed to get the images to be computed however i couldnt get the .mat file saved in a way so that when i used the code from the original to run it, it would jus come up with the error about Nfiles. can any 1 help me out with this? thanks

22 Apr 2009 mubashar khan

its k.. managed to solve it!! :-D

20 Apr 2009 Liviu Vladutu

Try the example with the 'Classical_guitar.jpg'. It fails miserably to find the closest (as resemblance) figure, ie 'Classical_Guitar_two_views.jpg'.
Try: warning off;searchImageHist('Classical_Guitar.jpg', 'model1Hist', 11);

24 Feb 2009 Theodoros Giannakopoulos

Hi guys, thanx alot for your comments. Sorry for the looong delay, but I hadn't checked the forum for quite a long time. Anyway, I am posting a simple function that computes the color histograms of a whole directory of images. In this way you will be also able to build your training data. You can use this function and then store the returned histograms (along with the filenames) in a mat file.

Matlab code:

function [Hists, files] = getImageHistsDir(DirName)

D = dir(DirName);

count = 0;

for (i=3:length(D))
if ((strcmpi(D(i).name(end-3:end), '.jpg')==1) | (strcmpi(D(i).name(end-3:end), '.jpeg')==1))
count = count + 1;
[Hists{count}] = getImageHists([DirName '\\' D(i).name]);
files{count} = [DirName '\\' D(i).name];
fprintf('%.4d File %40s computed...\n',count,files{count})

As you can see, you just specify the directory name in which the images are stored. This is actually what Nicola Franzoso has provided, but in a function mode.

15 Jan 2009 kartik

i am trying to build a image retrival system with information provided for each image ,hence actin as an image based search engine
program works great!!!!!!!!!!!!!!!!!

i would like to know about the deccison threshold used in in the program.on what basis the threshold were set ,how threshold values mentioned in the program were calculated,were they on the basis of intensity value or something else .i would like to undrstand the threshold in program

the threshold given are t=0.010 AND t2=0.8
how were these given and how it was calcualted and compared

15 Jan 2009 kartik

since i am new to matlab,and i am trying to learn all the basics as quick as possible,,

i would like to know how exactly the database is created for images say if i have 10 images,where would i write the program for creating the database in matlab,and if images are in folder then how would i link it
explanation given above in one of the comment is not making me understand it properly

can anybody make this thing clear to me
requesting all the pioneers in Digtial image processing
need help in quick time.

15 Jan 2009 cqupt cqupt

I has solve it!

29 Dec 2008 Nicola Franzoso

Great work Theodoros.
About for create a database of new images i have try with this and work. In my sample the images are 17 from 1 to 17.jpg:

savefile = 'mydatabase.mat';

for numimma = 1:17 % 17 images in my sample

imageName = strcat(num2str(numimma), '.jpg');

% compute 3-D image histograms (HSV color space):
[Hist, RGBQ] = getImageHists(imageName);

isto = {Hist};
dataisto(numimma) = isto;
datafile(numimma) = {imageName};


save (savefile, 'datafile', 'dataisto')

19 Dec 2008 Amir abbas  
22 Nov 2008 Harsh Sundani

Thanks a lot for the program....It works great!!.....

I am trying to create a image retrival program for medical images and will be using this code for the program. Could you please help me create my own database(with new image histograms) and storing it in a new .mat file...

Contact us