finding a sub-image in a larger image

need a little help with looping through array

Page 1 of 1

5 Replies - 7341 Views - Last Post: 26 February 2010 - 07:31 AM Rate Topic: -----

#1 jimdandy75  Icon User is offline

  • D.I.C Regular

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

finding a sub-image in a larger image

Posted 23 February 2010 - 11:02 PM

OK I was finally able to get some working code for this, thanks go to MichelD at LivePerson. :bananaman: It works great for comparing and finding a smaller image in a larger image. What I'm trying to do now is get it to go through an array of small images and compare to the one larger image. But it's just comparing smallBmp[0] 13 different times, instead of looping through the array. Any help would be appreciated. :bigsmile:
import java.awt.*;
import java.io.FileInputStream;

public class findImage {

	public static String bigBmp = "examples/9.bmp";
	public static String[] smallBmp = {"Cs/Ac.bmp","Cs/Kc.bmp","Cs/Qc.bmp","Cs/Jc.bmp","Cs/Tc.bmp","Cs/9c.bmp"
		,"Cs/8c.bmp","Cs/7c.bmp","Cs/6c.bmp","Cs/5c.bmp","Cs/4c.bmp","Cs/3c.bmp","Cs/2c.bmp"};

	public static int count = 0;
	static MyImage bigImage = new MyImage(bigBmp);
	static MyImage smallImage = new MyImage(smallBmp[count]);

	// The matching position if any or null
	static MatrixPosition match = MatrixMatch(bigImage.rgbData, smallImage.rgbData);

	static MatrixPosition MatrixMatch(int[][] big, int[][] small){
		int firstElem = small[0][0];

		__linsescan:
		for ( int i = 0 ; i < big.length - small.length + 1 ; i++ ){
			// Scan column by column
			__columnscan:
			for ( int j = 0 ; j < big[0].length - small[0].length; j++ ){
				if (big[i][j] != firstElem) continue __columnscan; // No first match
				// There is a match for the first element in small
				// Check if all the elements in small matches those in big
				for ( int ii = 0 ; ii < small.length ; ii++ )
				for ( int jj = 0 ; jj < small[0].length ; jj++ )
				// If there is at least one difference, there is no match
				if ( big[i + ii][j + jj] != small[ii][jj]) continue __columnscan;
				// If arrived here, then the small matches a region of big
				MatrixPosition result = new MatrixPosition();
				result.line = i; result.column = j;
				//System.out.println("Matching at line=" + result.line + ", column=" + result.column);
				return result;
			}
		}
		return null;
	}

	public static void main(String[] args) {
		for(int x = 0; x < 13; x++){
			if ( match == null ){
				System.out.println("No Match Found!");
				count++;}
			else{
				System.out.println("Matching at column(x)=" + match.column + " and line(y)=" + match.line);
				count++;}     
		}
		}

		static class MyImage {
			// Integers representing the image pixels
			int[][] rgbData = null;
			// The AWT image
			Image   img     = null;

			// Adapted from http://www.javaworld.com/javaworld/javatips/jw-javatip43.html
			MyImage( String bmpFileName ){
				// Just print the filename before loading
				java.io.File file = new java.io.File(bmpFileName);
				try{
				file.getCanonicalFile();
				}catch(Exception exc ){}

				// Load the image and the array of integers
				try {
					FileInputStream fs = new FileInputStream(bmpFileName);
					int bflen = 14;                 // 14 byte BITMAPFILEHEADER
					byte bf[] = new byte[bflen];
					fs.read(bf, 0, bflen);          // Read the file header
					int bilen=40;                   // 40-byte BITMAPINFOHEADER
					byte bi[] = new byte[bilen];
					fs.read(bi, 0, bilen);          // Read the bitmap header

					// Interperet data.
					int nsize = (((int)bf[5] & 0xff) << 24)
					| (((int)bf[4] & 0xff) << 16)
					| (((int)bf[3] & 0xff) << 8)
					| (int)bf[2] & 0xff;

					int nbisize = (((int)bi[3] & 0xff) << 24)
					| (((int)bi[2] & 0xff) << 16)
					| (((int)bi[1] & 0xff) << 8)
					| (int)bi[0] & 0xff;

					int nwidth = (((int)bi[7] & 0xff) << 24)
					| (((int)bi[6] & 0xff) << 16)
					| (((int)bi[5] & 0xff) << 8)
					| (int)bi[4] & 0xff;

					int nheight = (((int)bi[11] & 0xff) << 24)
					| (((int)bi[10] & 0xff) << 16)
					| (((int)bi[9] & 0xff) << 8)
					| (int)bi[8] & 0xff;

					int nbitcount = (((int)bi[15] & 0xff) << 8) | (int)bi[14] & 0xff;

					int nsizeimage = (((int)bi[23] & 0xff) << 24)
					| (((int)bi[22] & 0xff) << 16)
					| (((int)bi[21] & 0xff) << 8)
					| (int)bi[20] & 0xff;

					if (nbitcount != 24){
						System.err.println("Use only 24bit color .bmp files");
						System.exit(1);
					}

					int npad = (nsizeimage / nheight) - nwidth * 3;
					int ndata[] = new int [nheight * nwidth];
					this.rgbData = new int[nheight][nwidth]; // [line][column]
					byte brgb[] = new byte [( nwidth + npad) * 3 * nheight];

					fs.read (brgb, 0, (nwidth + npad) * 3 * nheight); // Read the bitmap

					int nindex = 0; // current postion in brgb
					for (int j = 0; j < nheight; j++){ // by lines, from bottom to top
						for (int i = 0; i < nwidth; i++){ // by columns
							int rgbValue = (255 & 0xff) << 24
							| (((int)brgb[nindex + 2] & 0xff) << 16)
							| (((int)brgb[nindex + 1] & 0xff) << 8)
							| (int)brgb[nindex] & 0xff;
							ndata[nwidth * (nheight - j - 1) + i] = rgbValue;
							this.rgbData[nheight - j - 1][i] = rgbValue;

							nindex += 3;
						}
						nindex += npad;
					}
				} catch (Exception e) {
					System.out.println("Caught exception in loadbitmap!");
					System.exit(3);
				}
			}
		}

		static class MatrixPosition{
			int line = -1;
			int column = -1;
		}
	}
 // @author MichelD
 // @author http://www.liveperson.com/micheld/


Is This A Good Question/Topic? 0
  • +

Replies To: finding 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: finding a sub-image in a larger image

Posted 24 February 2010 - 06:06 AM

Please?! :blink:
Was This Post Helpful? 0
  • +
  • -

#3 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: finding a sub-image in a larger image

Posted 24 February 2010 - 09:20 AM

Next time, plz try to post the images too. If its sumthing simple like blurring then I would've tried it with any of my images but this program needs 14 images in total.. which I didn't want to create.

I am trying my best to help here...
So...

You got 13 small images which you want to find in 1 large image.
You have a function,
MatrixMatch(int[][] big, int[][] small)

which will find if a SINGLE small image is in the big image. So you need to call this 13 times passing in 13 different images.

some small changed I would make with your codes are...
first: java naming convention. Class name starts with capital.
second:
remove all the static variables and move those inside main method.
		for(int x = 0; x < 13; x++)
		{ 

			MyImage bigImage = new MyImage(bigBmp); //your big image
			MyImage smallImage = new MyImage(smallBmp[x]); //your small image at index x
			MatrixPosition match = MatrixMatch(bigImage.rgbData, smallImage.rgbData); //compare
			if ( match == null ) //check if it returns null.
                              //some more code...
                  
                          //more codes..
                 }//end of for loop




Was This Post Helpful? 1
  • +
  • -

#4 jimdandy75  Icon User is offline

  • D.I.C Regular

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

Re: finding a sub-image in a larger image

Posted 24 February 2010 - 03:27 PM

You are Awesome!!!! It works now :bananaman: Thank you very much, I really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#5 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: finding a sub-image in a larger image

Posted 25 February 2010 - 09:50 PM

If it's working then great..
If you don't mind why don't you post the full source code and few images so that I could run and SEE how your program works and it will also help others who might run into the same problem. They could learn something from it. After all, "reusing" code is encouraged and someone might 'reuse' your code in the future and hopefully gives you credit.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Sanjay*


Reputation:

Re: finding a sub-image in a larger image

Posted 26 February 2010 - 07:31 AM

Does it work on all type of Bitmap I mean to say is there any limitation with this because I run it but it could not find the the right sub image.
Was This Post Helpful? 0

Page 1 of 1