Image retrieval via colour histogram


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

Thanks for your time    
Who is Participating?

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............
Tommy BraasCommented:
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;
Tommy BraasCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.