# Image retrieval via colour histogram

Hi

I am working on a program which retrieves an image based on the query image.
The images are stored and displayed in a frame and their histograms for each image are also obtained (including the query image).

However I have problem with following part of the program. I need to obtain the distances (of histograms) between the query image and each of the other images. and then display the closest image to the query image.

for (int b = 0; b < 3; b++) {
for (int j = 0; j < 256; j++) {   // for RGB 256*3
for (int i = 1; i < 11; i++)   // number of images 1 to 11
{

D1= histogram[0].getFrequency(b, j) - histogram[i].getFrequency(b, j);

Sum += D1*D1;
if(Sum < Summ){
Sum = Summ;
}
Summ = Sum;
//       else{
//           Summ = Sum;
//       }
}
}

System.out.println(" Distance " + Sum + "");

However 1st of all I don't know how to find the shortest distance
And then how to use the shortest distance to know that which histogram and therefore image it belongs to
and finaly how to use it to retreive that image to display

###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Feed the following method with your histograms and keep track of which generated the lowest distance value. That one should be the closest match.

public static int calculateDistance(Histogram histo1, Histogram histo2) {
int distance = 0;
for (int b = 0; b < 3; b++) {
for (int j = 0; j < 256; j++) {   // for RGB 256*3
int d = histo1.getFrequency(b, j) - histo2.getFrequency(b, j);
distance += Math.abs(d);
}
}
return distance;
}
Commented:
hi,

if u want to retrive image based on histogram, i will suggest u to process gray scale image instead of color one.... but if u want to continue with color one and find out distance do as follw

for(int b = 0;b<3;b++) {
for(int j = 0; j<256; j++) {
int d = histo1.getFrequency(b,j) - histo2.getFrequencey(b,j);

distance += (d*d);
}
}

after this u need to normalize ur distance based on size of image

distance = ((Math.sqrt(distance)/(npixels1 * npixels2));

(declare distance as double or float)

if u have all images with same size then u need not to normalise the distance.........

hope this will help............

Experts Exchange Solution brought to you by