Image retrieval via colour histogram

Posted on 2006-04-19
Last Modified: 2012-05-05

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    
Question by:khawaib
    LVL 14

    Assisted Solution

    by:Tommy Braas
    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;
    LVL 2

    Accepted Solution


    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............
    LVL 14

    Expert Comment

    by:Tommy Braas

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now