Solved

compare difference between 2 images

Posted on 2004-04-13
14
496 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
Independent Software Vendors: 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 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java imports not found 4 71
Print Rhino Java Array in Javascript 1 58
Running JavaFX on the Raspberry Pi 27 301
Java pass by reference 3 74
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…
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.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

752 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