tags:

views:

649

answers:

3

hii to all I m doing a final year project on data extraction from an image for that purpose i want to know how, can I compare two images pixel by pixel in java can any body give me answer plz its important to me also code plz if possible

and i also want to extract data from image in editable form

+1  A: 

If this is a school project, about the furthest I should take you is to tell you about the excellent Imagemagick library: JMagick. It won't take you too long to work through the documentation and discover how to do subtractive blending of images.

thanks fro suggestion butits not a school project,its my MCA final year projectit actually about data extraction from an imageand i ve done some part,I want to how i compare one image data to other imageactually i ve done a code for getting characters from an image but in pixel form, now i want check characters like either is A,B,C....Zand 1,2,3...0
Hussain
OCR perhaps? But does that have anything to do with the original question? It sounds like you are asking other people to lead you down a different path...
MJB
+4  A: 

Store both images in a BufferedImage, and then use the method getRGB(int x, int y) this returns the RGB colors as in the pattern RRGGBB in hex code, you can get both RGBs of the same pixel in each image and compare.

Here is the code:

File file1 = new File("path/image1.jpg");
File file2 = new File("path/image2.jpg");

BufferedImage image1 = ImageIO.read(file1);
BufferedImage image2 = ImageIO.read(file2);

int columns = image1.getWidth();
int rows = image1.getHeight();

for (int row=0; row<rows; row++){
    for (int col=0; col<columns; col++){
       int rgb = image1.getRGB(col, row);
       int rgb2= image2.getRGB(col, row);
       //compare here
    }
 }
medopal
thanks fro suggestion but i want check them,like they having macthing data or not
Hussain
+1: This is by far the simplest approach and will work fine for RGB images with no alpha channel.
Adamski
@program-java : what exactly do you call matching data if it's not the colour of pixels?
Stroboskop
actually i ill convert both images into binary form,by doing this both images become in black and white form,after that i ill compare them
Hussain
@program-java: so you want to convert the images into 2D matrices of 0s and 1s? or you still want them to be images? My code should help if they are images, even if they are in grayscale. But if you want to extract in binary then thats a different question.
medopal
+3  A: 

Medopal's answer will perform an approximate comparison for RGB images with no alpha chanel. The reason the comparison is approximate is because the RGB values are being "squashed" into a single integer, whereas the image colour resolution could be a lot higher than this.

To perform an exhaustive comparison you need to compare each image band separately:

BufferedImage image1 = ...
BufferedImage image2 = ...

Raster r1 = image1.getData();
Raster r2 = image2.getData();
boolean ret; // Stores result.

// Check image sizes and number of bands.  If there are different
// then no need to compare images as they are definitely not equal.
if (r1.getNumBands() != r2.getNumBands() ||
  r1.getWidth() != r2.getWidth() ||
  r1.getHeight() != r2.getHeight()) {
  ret = false;
} else {
  // #Bands and image bounds match so compare each sample in turn.

  ret = true;

  search:
  for (int i=0; i<r1.getNumBands(); ++i) {
    for (int x=0; x<r1.getWidth(); ++x) {
      for (int y=0; y<r1.getHeight(); ++y) {
        if (r1.getSample(x, y, i) != r2.getSample(x, y, i) {
          // At least one sample differs so result is false;
          ret = false;
          // Use labeled break to terminate all loops.
          break search;
        }
      }
    }
  }

  done:
}
Adamski
thanks for cooperationnow i want to extract data from both images which are matchedand convert them into editable formis there any algorithm?
Hussain
+1 for mentioning bands, i tried to show the simple straight forward answer only.
medopal
okaysrry actually i m new in that,what i ve to do for my query.?
Hussain
If you want an editable data structure you should consider using WritableRaster.
Adamski