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:
- Locating the Region of Interest (Where the Objects appear in the given image),
- Re-sizing the ROIs in to a common size,
- Substracting ROIs,
- and Calculating the Black and White Ratio of the resultant image after subtraction.
- Pre-process the image, if needed (e.g. to enhance contrast, filter noise, etc.).
- 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.
- 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.
- 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.
- 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).
- Store the image's regions' descriptor for further processing. Repeat those steps for other images.
- 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.
11 comments:
sarath said...
hi,
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.
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 ;-)
SanJ Wijeratne said...
@ Sarath,
Please check http://www.lac.inpe.br/~rafael.santos/JIPCookbook/6090-howto-findobjects.jsp#howtohowdoifindwhetheranimagecontainsaspecificobject for a tutorial on template matching. That part of the book is not yet available but soon it will be I guess ;-)
Unknown said...
@Sanj
hi this is vivek.
the sample code which is to comapre two image ,i am not able to execute.becouse of
javax.media.* and com.son.*,these two statement trubling me.
could u plz help me.
Unknown said...
@Sanj
hi sanj the code u r talking about two image comparison i am not able to execute,javax.media.*,com.sun.* is troubling me.
could u plz help me in this
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.
Binod Suman said...
Very nice posting. Thanks.
Shataj 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...
Unknown said...
hi,
Can you help me with java code for comparing two images.. plz....... urgent!!!!!!
Unknown said...
hi,
Can u help me with source code for comparing two images... Plzzzz........
Very urgent!!!!!!!!!!
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..!