Image Processing is one of the emerging areas in the field of Information Technology and people yet exploring the limits of it. It is heavily used in major areas of IT such as Computer Vision, Artificial Intelligence, Computer Security etc. Recently I got an opportunity to work with Image Processing Technologies as a part of one of my projects and my task was to find matching images from an image store when a new image is given. I started my project with googling "How to compare images using java" and I got some good articles on finding the similarity of two images. Allmost all of them were based on four basic steps, they are:

  1. Locating the Region of Interest (Where the Objects appear in the given image),
  2. Re-sizing the ROIs in to a common size,
  3. Substracting ROIs,
  4. and Calculating the Black and White Ratio of the resultant image after subtraction.
Though this sounds as a good algorithm to compare images, it took a considerable amount of time after implementing it using JAI in my project. Therefore I had to find an alternate method of doing it. After searching the Web for few hours, I came across with this amazing article by Rafael Santos on "How do I compare two images to see if they are equal?" in his famous (but not yet completed) book "Java Image Processing Cookbook" which describes a different approach to compare images for similarity using image segmentation and region manipulation. It mainly focuses on finding some regions of a given image that matches with the images in the image store rather than searching for equality of the objects in given images. He describes an algorithm to find similar regions in a set of images and provides some sample code based on image segmentation and region manipulation to compare the equality of two images. After reading the article and playing with the sample code given there, I was able to further extend it to suit my need. So I wrote this note to express my gratitude towards him :-) The algorithm proposed by him (which is also available in his book) is as follows.

  1. Pre-process the image, if needed (e.g. to enhance contrast, filter noise, etc.).
  2. Do an Image Segmentation, process in which the image is converted to regions which contains pixels that are similar to pixels in the same region and different from pixels to other regions. This can be done using region-growing, mathematical morphology, clustering or classification algorithms. There are many algorithms to do that, just google for "image segmentation" and other keywords to get more information.
  3. With the regions, create descriptors for them. Descriptors are calculated from the region and can include shape, area, perimeter, number of holes, general color of the region, texture, orientation, position, etc.
  4. If needed, do a Re-Segmentation of the image, process in which regions are merged if they can be considered as belonging to the same object. Note that this step may require some high-level knowledge of the objects and the task in general, seldom being fully automatic and often being task-dependent.
  5. If needed, filter the regions that seem relevant to the task in hand, eliminating small regions or regions which are deemed unrelated to the task (again this may require some knowledge about the task).
  6. Store the image's regions' descriptor for further processing. Repeat those steps for other images.
  7. Use the descriptors for comparison of the contents of the images, using some of many algorithms for pattern matching, classification, clustering, artificial intelligence and data mining in general.
Bookmark and Share

This entry was posted on 5:06 PM and is filed under , , , , , , , , . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


    sarath said...

    can this program be used for finding a particular object (say a ball) in an image given an image of a ball as one of the sample image.

  1. ... on December 26, 2008 at 3:51 AM  
  2. SanJ Wijeratne said...

    @ Sarath,
    No, The sample code given in the book under "How do I compare two images to see if they are equal?" is based on segmenting an image into few regions (25 Regions) and searching for the same region in the given set of images. So if you use this code with an image of a ball and give it another set of images of the same ball with different viewports say, it will recognize it by searching for matching reagions but without performing any operations on object manipulation such as Identify the Region of Interest, Edge detection etc. Though you will get the best matching image as the result, but not taking the objects in to consideration. So if you give two images of say a black coloured football and white coloured football with different backgrounds or images of the same football taken in different backgrounds, there is a tendency of this program to fail. But you may give it a try ;-)

  3. ... on December 26, 2008 at 4:28 AM  
  4. SanJ Wijeratne said...

    @ Sarath,
    Please check for a tutorial on template matching. That part of the book is not yet available but soon it will be I guess ;-)

  5. ... on December 26, 2008 at 4:30 AM  
  6. vivek said...

    hi this is vivek.
    the sample code which is to comapre two image ,i am not able to execute.becouse of* and com.son.*,these two statement trubling me.

    could u plz help me.

  7. ... on March 31, 2009 at 9:01 PM  
  8. vivek said...


    hi sanj the code u r talking about two image comparison i am not able to execute,*,com.sun.* is troubling me.

    could u plz help me in this

  9. ... on March 31, 2009 at 9:04 PM  
  10. SanJ Wijeratne said...

    @ Vivek,
    The code uses some classes defined in those packages, so without importing them to your class path, you will not be able to run the code. So first try import the jar files in jar file directory in this resource pack to your class path and run the example. It should work. If you still have problems, try to create another package called display and copy the files in the display directory in resource pack there. It will sort out the errors due to missing classes and packages.

  11. ... on April 1, 2009 at 7:22 PM  
  12. Binod Suman said...

    Very nice posting. Thanks.

  13. ... on June 9, 2009 at 7:51 AM  
  14. Shahataj said...

    in my project i need to compare an image received by the client with the images stored in server to find the relevent match..i tried the code in the image processing cookbook and i got all the api's also but its still not working..will you plz help me with the full source code...

  15. ... on February 20, 2010 at 10:58 PM  
  16. Rajalakshmi said...


    Can you help me with java code for comparing two images.. plz....... urgent!!!!!!

  17. ... on March 24, 2010 at 9:33 AM  
  18. Rajalakshmi said...


    Can u help me with source code for comparing two images... Plzzzz........
    Very urgent!!!!!!!!!!

  19. ... on March 24, 2010 at 9:35 AM  
  20. hemanth said...

    hi, i want to compare the two images(one is the actual image and second one is part of actual image). But with image processing cookbook, it is not able to match those 2 images.Because the part of actual image is also going to be splits as 25 regions which is not suitable for this scenario. Could u please give instructions to do this..! thanks in advance..!

  21. ... on November 23, 2010 at 12:24 AM