Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

compare difference between 2 images

Posted on 2004-04-13
14
Medium Priority
?
500 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

718 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