Solved

compare difference between 2 images

Posted on 2004-04-13
14
493 Views
Last Modified: 2008-02-01
Hi everyone.

I'm trying to compare a bmp file to another bmp which is generated at a later point in time. if the method finds a difference between the 2 images then it should return a new pixel array which represents the difference between the 2 bmp's else it should leave the original pixel array as it was. I'm trying not to work with a file on disk and would rather prefer to work with an image object in bmp format. The method I got is:

  // Grabs the pixels of the given Image object and returns the array of those
  // pixels.
  // Parameters:
  //   Image img: The image whose pixels to grab.

public static int [] grabPixels(Image img)
{
  int width = img.getWidth(null);
  int height = img.getHeight(null);
  int [] pixels = new int[width*height];
  PixelGrabber grabber = new PixelGrabber(img,0,0,width,height,pixels,0,width);
  try
    {
      if (!grabber.grabPixels())
        return null;
    }
    catch (InterruptedException e)
    {
      return null;
    }
    return pixels;
}
0
Comment
Question by:brimful
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10815953
Java doesn't support bitmaps as they are platform specific. Even if it did, what would

>>return a new pixel array which represents the difference between the 2 bmp's

mean?
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10816774
>>  Java doesn't support bitmaps as they are platform specific. Even if it did, what would
Yes it does! A TIFF file is a bitmap, not very platform specific. BMP files are a different story.

What type of difference are you trying to calculate? What is the purpose of what you're trying to do?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10817045
>>BMP files are a different story.

That's what i mean and that's what brimful means too AFAIK.

>>A TIFF file is a bitmap

Are you saying that Java supports tiffs?
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10817494
>> Are you saying that Java supports tiffs?
No, am saying that bitmaps aren't necessarily platform dependent, which you stated that they were.

My point is that once an image is read into Java it is converted into an Image, which AFAIK is a bitmap (platform independent).

And, btw, aren't PNGs bitmaps? ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10817515
As i mentioned, i meant .bmp files, since that is what was originally being discussed
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10817667
Indeed.
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 10817686
You could loop thru the grabbed pixels and calculate difference between the two arrays. In its simplest form for images of same size itd be something like:

int[] diff = new int[a.length];
for (int i=0; i<diff.length; i++)
{
   if (a[i]!=b[i])
   {
      diff = a[i] - b[i];
      different = true;
   }
}
0
 

Author Comment

by:brimful
ID: 10821318
sorry for replying so late but went home after work and net connection went down.

Anyway, what I'm trying to do is use java.awt.robot to perform a screen capture - which I assume is captured in some sort of bitmap format - then comapre a screen capture at a later time and give a result of delta(screenshot). So what I need is a way of finding the changes that have taken place. I'll be performing a run length encoding on it later on but that's a different story. The differences need to be stored in a new array which I would send over to a jpanel to display (again not required for this question).
0
 
LVL 92

Expert Comment

by:objects
ID: 10821340
> The differences need to be stored in a new array which I would send over to a jpanel to display (again not required for this question).

The code I posted above will basically achieve that, how you actuallu represent the difference is up to u.
0
 

Author Comment

by:brimful
ID: 10821379
also if I'm using the robot class to capture the content on the screen, do you know roughly how long it would take to computer the difference between 2 totally different images of size 1024*768?
0
 
LVL 92

Expert Comment

by:objects
ID: 10821407
however long it takes to compare 786 432 int's :)
which will depend on what you're running it on. some quick benchmarking should give you an idea.
0
 
LVL 92

Expert Comment

by:objects
ID: 10821523
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10823005
Why should the images be the same size?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10823461
...but let's assume for a moment that they are. How will subtracting the elements of two identically sized arrays help you, since what you're interested in is this?

>>So what I need is a way of finding the changes that have taken place

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 72
java.io.BufferedReader   readLine()  //returns the entire file ? 4 73
Java Loop 6 59
web application structure 18 76
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

786 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