Solved

compare difference between 2 images

Posted on 2004-04-13
14
492 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 79
What is JNDI datasource in spring 1 50
Java Options in Zimbra Server 2 46
web services creation SOAP vs REST 5 20
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now