Comparing images, need to find a sub-image in a larger image

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 7873 Views - Last Post: 15 November 2009 - 01:50 PM Rate Topic: -----

#1 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Comparing images, need to find a sub-image in a larger image

Post icon  Posted 13 November 2009 - 09:44 PM

I've been trying to figure out how to compare a large image with a smaller image pixel by pixel to see if the larger image contains the sub-image, and I found a tutorial. But it is either, way too complicated for me or completely inaccurate. It looks like by the tutorial, that it is comparing height and width and not actual pixels. I tried to come
up with what the tutorial is showing and I'm getting 4 errors:
2 with the getWidth() and getHeight(), and 2 saying array required but java.awt.Image found
Here's what the tutorial says: and it's code:
a) Simple Image Matching
This operation is as simple as it sounds, it involves a pixel by pixel
comparison between two images. In pseudocode:
ExactMatch(image_1, image_2)
Input: Two images
Output: True if the images are pixel by pixel matches,
false otherwise
   for(x = 0; x < image_1.width; x++) {
	  for(y = 0; y < image_1.height; y++) {
		  if(image_1[x][y] ≠ image_2[x][y]) {
	   return false;
						  }
			}
}
return true;


Here's the best I could do with it, I fear I'm way off !!!!! :crazy:
import java.awt.*;
import javax.swing.*;
public class CompareImg{
	public static void main(String[] args){

		String b;
		Image image1 = Toolkit.getDefaultToolkit().getImage("main.jpg");
		Image image2 = Toolkit.getDefaultToolkit().getImage("9.jpg");
		for(int x = 0; x < image1.getWidth(); x++) {
			for(int y = 0; y < image1.getHeight(); y++) {
				if((image1[x][y]) == (image2[x][y])) {
					b = "Images match";
				} else {
					b = "No match found";
				}
			}
		}
		System.out.println("" + b);
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Comparing images, need to find a sub-image in a larger image

#2 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 14 November 2009 - 09:19 AM

Anyone good with the images? :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 14 November 2009 - 11:01 PM

Your algorithm is just checking if both images are equivalent starting at (0,0) and (0,0) and return false if it is not the case
You will have to check if the "subimage" fits the sub pattern in all positions of the "master" image
Was This Post Helpful? 0
  • +
  • -

#4 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 09:32 AM

Sounds complicated :blink: I basically want to do a screenscraper type deal, I know how to use the Robot Class to get the screenshot, and I have my sub-images I want to scan for, but then I'm stuck. Do you know any methods or classes that do a quick pixel scan or comparison of images? Or could you give me an example? Thanks
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,263
  • Joined: 27-December 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 09:45 AM

Let's say you have a 100x100 image and you want to check for a 50x50 subimage. You are going to have to start by comparing the pixel[0] in the 50x50 with pixel[0] in the 100x100. If they don't match, then move on to pixel[1] in the 100x100. Keep repeating this process until you get to the pixel[51] in the 100x100, because then the image lengths won't match. Then go to the next row and start at 0 again. Now how many rows (worst case) do you think you will have to check to find your 50x50 image?
Was This Post Helpful? 0
  • +
  • -

#6 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 09:59 AM

The main image would be around a 60 x 70 .jpg, and the sub-images would be like 20 x 30. But I'll need to loop through maybe 20 subimages, to try and find 2 of them and keep it fast.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,263
  • Joined: 27-December 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 10:07 AM

Have you considered using multiple Threads if you have to check for multiple images? For each image after the first, create a new Thread (you might want to create your own subclass of Thread) and feed it the original image, the sub-image, and the names of each image. And just have it output image1Name == image2Name or image1Name != image2Name.
Was This Post Helpful? 0
  • +
  • -

#8 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 10:12 AM

View Postmacosxnerd101, on 15 Nov, 2009 - 09:07 AM, said:

Have you considered using multiple Threads if you have to check for multiple images? For each image after the first, create a new Thread (you might want to create your own subclass of Thread) and feed it the original image, the sub-image, and the names of each image. And just have it output image1Name == image2Name or image1Name != image2Name.

Sounds good, but I have no idea how to do that. Could you give me some example code? :D
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,263
  • Joined: 27-December 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 12:23 PM

If you don't know how to use Threads, then it might just be better to store all the test images in an array of Collection and loop through, checking each. Go ahead and try modifying your solution, and see what comes of it. Good luck!
Was This Post Helpful? 0
  • +
  • -

#10 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 12:34 PM

Maybe you can help me with something else. I found the method I believe I need to make an array of pixels. It is here,
getPixels()
but can you show me where I can find this method and the class it belongs to in the actual Java Docs online?
Like here!
Thanks! I'm kinda new to Java and I really like being able to find things in the Docs, it gives me a better understanding of what
I'm actually doing. o_0
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,263
  • Joined: 27-December 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 12:38 PM

That's a good find. In order to use it though, you will have to download the library from this organization as it does not come standard in the JDK.
Was This Post Helpful? 1
  • +
  • -

#12 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 12:40 PM

View Postmacosxnerd101, on 15 Nov, 2009 - 11:38 AM, said:

That's a good find. In order to use it though, you will have to download the library from this organization as it does not come standard in the JDK.

I see, thank you! So, is there a similar method in the Java Library that will convert an image to an array of pixels?
Also, having a hard time finding where to download their library.

This post has been edited by jimdandy75: 15 November 2009 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,263
  • Joined: 27-December 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 12:48 PM

Not that I'm aware of. Sorry. It looks like you might be stuck doing things the old fashioned way with more loops. :)
Was This Post Helpful? 0
  • +
  • -

#14 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 12:55 PM

View Postmacosxnerd101, on 15 Nov, 2009 - 11:48 AM, said:

Not that I'm aware of. Sorry. It looks like you might be stuck doing things the old fashioned way with more loops. :)

hmmm....... So, there's no way to get this Library and use the getPixels() method in it? :crazy:
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,263
  • Joined: 27-December 08

Re: Comparing images, need to find a sub-image in a larger image

Posted 15 November 2009 - 12:58 PM

You could always download the library.

I also checked into the BufferedImage class. You may find some of its methods helpful.
http://java.sun.com/...feredImage.html

Good luck!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2