Discussion:
Compare two images....?
(too old to reply)
TheBigPJ
2008-04-01 19:11:23 UTC
Permalink
Suggestions on the best (preferably easiest way) to compare two images
(captured both from the desktop via java robot) ?

The following code is the code I will be using to take the screen shot
(currently it saves just to a file):

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

class Screenshot{

static public void main()
{
try
{
//Get the screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Rectangle rect = new
Rectangle(0,0,screenSize.width,screenSize.height);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rect);
File file;

//Save the screenshot as a png
file = new File("screen.png");
ImageIO.write(image, "png", file);

//Save the screenshot as a jpg
file = new File("screen.jpg");
ImageIO.write(image, "jpg", file);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}


Thank you,
Peter (thebigpj)
Roedy Green
2008-04-01 21:04:35 UTC
Permalink
Post by TheBigPJ
Suggestions on the best (preferably easiest way) to compare two images
(captured both from the desktop via java robot) ?
Here's an idea for how to proceed.. Use the usual techniques to scale
both images down to a tiny size say 16x16. Reduce the colour map to
16 colours.

Then you only have 256 pixels to compare. See if all corresponding
pixels are within 1 of the other.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
TheBigPJ
2008-04-01 21:27:33 UTC
Permalink
Post by Roedy Green
Here's an idea for how to proceed.. Use the usual techniques to scale
both images down to a tiny size say 16x16. Reduce the colour map to
16 colours.
Go this far. Now sure how to reduce the colour map but will look that
up.
Post by Roedy Green
Then you only have 256 pixels to compare. See if all corresponding
pixels are within 1 of the other.
How would I get each pixel? And why within 1 of each other? One would
think that they would be exactly the same, would they not?

Thanks,
Peter
Andrea Francia
2008-04-01 21:52:27 UTC
Permalink
Post by TheBigPJ
Post by Roedy Green
Then you only have 256 pixels to compare. See if all corresponding
pixels are within 1 of the other.
How would I get each pixel? And why within 1 of each other? One would
think that they would be exactly the same, would they not?
If you compare a PNG with a JPG images they surely aren't the same,
because the JPEG use a lossy compression.

http://en.wikipedia.org/wiki/Portable_Network_Graphics#Comparison_with_JPEG
--
Andrea Francia
http://www.andreafrancia.it/
TheBigPJ
2008-04-01 22:47:29 UTC
Permalink
Post by Andrea Francia
If you compare a PNG with a JPG images they surely aren't the same,
because the JPEG use a lossy compression.
I cant deciede which to use, however I will eventually choose one. I
dont think it really matters to be honest which I use. PNG looks
easier to compare but could be wrong.

Thanks,
Peter
Roedy Green
2008-04-01 23:58:24 UTC
Permalink
Post by TheBigPJ
One would
think that they would be exactly the same, would they not?
If there were identical images to start. But if one had been colour
reduced, converted to some other format etc. then they would not
exactly match.

To get at individual pixels, use java.awt.image.PixelGrabber to
extract an array of RGB ints from some rectangle in an image,
or better still use BufferedImage.

See http://mindprod.com/jgloss/image.html to learn what the various
tools you have at your disposal are for.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
TheBigPJ
2008-04-02 00:25:35 UTC
Permalink
I have got this far....can anyone see anything ive missed? Because the
two images i have tried have been identical (by format and same
picture) but returns false in the comparison.

Thanks,
Peter

-----------------------

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;

public class Compare2Images {

static void processImage() {
String inFile = "D:/Deleteable/1 Theory/1 Theory/screen.png";
String inFile2 = "D:/Deleteable/1 Theory/1 Theory/screen2.png";

Image image = Toolkit.getDefaultToolkit().getImage(inFile);
Image image2 = Toolkit.getDefaultToolkit().getImage(inFile2);

try {

PixelGrabber grabber = new PixelGrabber(image, 0, 0, -1, -1,
false);
PixelGrabber grabber2 = new PixelGrabber(image, 0, 0, -1, -1,
false);

if (grabber.grabPixels()) {
int width = grabber.getWidth();
int height = grabber.getHeight();

int[] data = (int[]) grabber.getPixels();
int[] data2 = (int[]) grabber2.getPixels();

if(java.util.Arrays.equals(data, data2) )
System.out.println("The Same");

System.out.println("Not the Same");
}

}
catch (InterruptedException e1) {
e1.printStackTrace();
}
}

public static void main(String args[]) {
processImage();
}
}
John B. Matthews
2008-04-02 02:23:40 UTC
Permalink
In article
Post by TheBigPJ
I have got this far....can anyone see anything ive missed? Because the
two images i have tried have been identical (by format and same
picture) but returns false in the comparison.
Thanks,
Peter
There are several problems with your code: You need to call grabPixels()
for the second image, too; once you know the dimensions, you need to
allocate memory for the array returned by getPixels(); your output is
ambiguous. Here's an alternative:

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;

public class Compare {

static void processImage() {
String file1 = "pic1.png";
String file2 = "pic2.png";

Image image1 = Toolkit.getDefaultToolkit().getImage(file1);
Image image2 = Toolkit.getDefaultToolkit().getImage(file2);

try {

PixelGrabber grab1 =
new PixelGrabber(image1, 0, 0, -1, -1, false);
PixelGrabber grab2 =
new PixelGrabber(image2, 0, 0, -1, -1, false);

int[] data1 = null;
if (grab1.grabPixels()) {
int width = grab1.getWidth();
int height = grab1.getHeight();
data1 = new int[width * height];
data1 = (int[]) grab1.getPixels();
}

int[] data2 = null;
if (grab2.grabPixels()) {
int width = grab2.getWidth();
int height = grab2.getHeight();
data2 = new int[width * height];
data2 = (int[]) grab2.getPixels();
}

System.out.println("Pixels equal: " +
java.util.Arrays.equals(data1, data2));

} catch (InterruptedException e1) {
e1.printStackTrace();
}
}

public static void main(String args[]) {
processImage();
}
}

John
--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews
Andrea Francia
2008-04-01 21:24:28 UTC
Permalink
Post by TheBigPJ
Suggestions on the best (preferably easiest way) to compare two images
(captured both from the desktop via java robot) ?
I computer vision they do an image subtraction and check if the max of
resulting image is under a threeshold.

I think you need some image manipulation library to do this.
Try to search "image subtraction java" in google to find it.
--
Andrea Francia
http://www.andreafrancia.it/
Chase Preuninger
2008-04-04 01:49:28 UTC
Permalink
One way you could compare any file is first to see if they have the
same lengths, then go through it byte by byte. but for an image find
a way to get say a multidimensional array of all the pixels and go
through and compare each one.
TheBigPJ
2008-04-10 11:44:55 UTC
Permalink
Post by Chase Preuninger
One way you could compare any file is first to see if they have the
same lengths, then go through it byte by byte.  but for an image find
a way to get say a multidimensional array of all the pixels and go
through and compare each one.
You mean check the actual file size of the file, then the dimensions
and only if these two are the same then continue on and check each
pixel by pixel? A little pre-optermisation....
p***@nbo.samadc.org
2018-01-15 08:45:06 UTC
Permalink
Post by TheBigPJ
Suggestions on the best (preferably easiest way) to compare two images
(captured both from the desktop via java robot) ?
The following code is the code I will be using to take the screen shot
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
class Screenshot{
static public void main()
{
try
{
//Get the screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Rectangle rect = new
Rectangle(0,0,screenSize.width,screenSize.height);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rect);
File file;
//Save the screenshot as a png
file = new File("screen.png");
ImageIO.write(image, "png", file);
//Save the screenshot as a jpg
file = new File("screen.jpg");
ImageIO.write(image, "jpg", file);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
Thank you,
Peter (thebigpj)
https://photos.google.com/u/1/photo/AF1QipO13vU8-ye2z6EpePeG291YDp7MMupEVxl6rs9V

https://photos.google.com/u/1/photo/AF1QipPplix9mZCFzWdOl7GJiZWjrid2qcKxlagwluzY
Loading...