Code Comments
Programming Forum and web based access to our favorite programming groups.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)
Post Follow-up to this messageOn Tue, 1 Apr 2008 12:11:23 -0700 (PDT), TheBigPJ <TheBigPJ@gmail.com> wrote, quoted or indirectly quoted someone who said : >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
Post Follow-up to this messageTheBigPJ wrote: > 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/
Post Follow-up to this message> 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. > 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
Post Follow-up to this messageTheBigPJ wrote: > > 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/ Porta...it h_JPEG -- Andrea Francia http://www.andreafrancia.it/
Post Follow-up to this message> 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
Post Follow-up to this messageOn Tue, 1 Apr 2008 14:27:33 -0700 (PDT), TheBigPJ <TheBigPJ@gmail.com> wrote, quoted or indirectly quoted someone who said : >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
Post Follow-up to this messageI 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();
}
}
Post Follow-up to this messageIn article
<833d8bd2-b056-49de-9db1-294d7b534482@d21g2000prf.googlegroups.com>,
TheBigPJ <TheBigPJ@gmail.com> wrote:
> 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.