Solved

compare difference between 2 images

Posted on 2004-04-13
14
491 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
Comment Utility
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
Comment Utility
>>  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
Comment Utility
>>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
Comment Utility
>> 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
Comment Utility
As i mentioned, i meant .bmp files, since that is what was originally being discussed
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
Indeed.
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:brimful
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Why should the images be the same size?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
...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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

763 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

10 Experts available now in Live!

Get 1:1 Help Now