Java image search algorithm and java Image Search Algorithm

Source: Internet
Author: User

Java image search algorithm and java Image Search Algorithm

Previously, I wrote some game AIDS using the press-key genie. There is a function called FindPic in it. I will search for a given image on the screen and return the coordinates found.

Now, Java Implements similar functions of this function.

Algorithm Description:

Here, the comparison between pixels is to obtain the RGB value of each pixel through the BufferedImage object. Convert BufferedImage to an int two-dimensional array as follows:

1/** 2 * obtain the image RGB Array Based on BufferedImage 3 * @ param bfImage 4 * @ return 5 */6 public static int [] [] getImageGRB (BufferedImage bfImage) {7 int width = bfImage. getWidth (); 8 int height = bfImage. getHeight (); 9 int [] [] result = new int [height] [width]; 10 for (int h = 0; h 

Compare the RGB values of two pixels through an exclusive or operation (it is said that the ratio = is more efficient). If the value obtained after an exclusive or operation is 0, it indicates that the two pixels have the same RGB color; otherwise, they are different.

The complete java code of the algorithm is attached below:

1 package com. jebysun. test. imagefind; 2 3 import java. awt. AWTException; 4 import java. awt. rectangle; 5 import java. awt. robot; 6 import java. awt. toolkit; 7 import java. awt. image. bufferedImage; 8 import java. io. file; 9 import java. io. IOException; 10 11 import javax. imageio. imageIO; 12/** 13 * Find the specified picture on the screen 14 * @ author Jeby Sun 15 * @ date 2014-09-13 16 * @ website http://www.jebysun.com 17 */18 public c Lass ImageFindDemo {19 20 BufferedImage screenShotImage; // screen 21 BufferedImage keyImage; // search Target Image 22 23 int scrShotImgWidth; // screen width 24 int scrShotImgHeight; // screen height 25 26 int keyImgWidth; // search for the target image width 27 int keyImgHeight; // search for the Target Image Height 28 29 int [] [] screenShotImageRGBData; // display RGB data 30 int [] [] keyImageRGBData; // search for the RGB data of the Target Image 31 32 int [] [] [] findImgData; // query result, coordinate data of the target icon on the screen 33 34 35 public ImageFindDem O (String keyImagePath) {36 screenShotImage = this. getFullScreenShot (); 37 keyImage = this. getBfImageFromPath (keyImagePath); 38 screenShotImageRGBData = this. getImageGRB (screenShotImage); 39 keyImageRGBData = this. getImageGRB (keyImage); 40 scrShotImgWidth = screenShotImage. getWidth (); 41 scrShotImgHeight = screenShotImage. getHeight (); 42 keyImgWidth = keyImage. getWidth (); 43 keyImgHeight = keyImag E. getHeight (); 44 45 // start searching 46 this. findImage (); 47 48} 49 50/** 51 * full screen 52 * @ return BufferedImage 53 */54 public BufferedImage getFullScreenShot () {55 BufferedImage bfImage = null; 56 int width = (int) Toolkit. getdefatooltoolkit (). getScreenSize (). getWidth (); 57 int height = (int) Toolkit. getdefatooltoolkit (). getScreenSize (). getHeight (); 58 try {59 Robot robot = new Robot (); 60 bfImage = rob Ot. createScreenCapture (new Rectangle (0, 0, width, height); 61} catch (AWTException e) {62 e. printStackTrace (); 63} 64 return bfImage; 65} 66 67/** 68 * read the target image from the local file 69 * @ param keyImagePath-absolute path of the image 70 * @ return BufferedImage object of the local image 71 */72 public BufferedImage getBfImageFromPath (String keyImagePath) {73 BufferedImage bfImage = null; 74 try {75 bfImage = ImageIO. read (new File (keyImagePath) ); 76} catch (IOException e) {77 e. printStackTrace (); 78} 79 return bfImage; 80} 81 82/** 83 * obtain the image RGB array from BufferedImage 84 * @ param bfImage 85 * @ return 86 */87 public int [] [] getImageGRB (BufferedImage bfImage) {88 int width = bfImage. getWidth (); 89 int height = bfImage. getHeight (); 90 int [] [] result = new int [height] [width]; 91 for (int h = 0; h 

This algorithm is accurate comparison. If there is a difference between pixels, the image cannot be found. Of course, if you want to specify a comparison accuracy, I also want to make a statistical statement when algorithm Step 4 compares all the pixels in the ing range. If 90% of the vertices are the same, that is to say, the accuracy is 0.9.

In addition, efficiency may need to be considered. However, I do not care too much about efficiency in my application scenarios. If a friend has read this article and has a better idea about this topic, leave a message.


How to use java to create an interface with the ability to search for images

Import java. io .*;
// Get the folder content
Public class getthing
{
Public static void main (String [] args) throws Exception
{
System. out. println (welstr );
ListFile (new File ("e: \ aa"); // The path to be searched
}

Public static void listFile (File file) throws Exception
{

If (file. isFile ())
{
// Output the path of the complete file in the folder
System. out. println ("File:" + file. getAbsolutePath ());
// 01.jpg is the image you are looking
If (file. getAbsolutePath (). endsWith ("01.jpg "))
System. out. println ("Images with search ");
}
Else
{
System. out. println ("Dir:" + file. getAbsolutePath ());
File [] files = file. listFiles ();
For (int I = 0; I <files. length; I ++)
{
ListFile (files [I]);
System. out. println ("Press ENTER ");
}
}
}
}

Java image Algorithms

// Do you know your requirements more clearly?

One of my previous programs used BufferedImage to read the RGB values of image pixels, as shown in the following example.

In this example, you can see how to use an integer to represent the RGB color. Then, use the setRGB method to meet your requirements.

Method:
Java. awt. image. BufferedImage

Public int getRGB (int x, int y)
Return the integer pixels in the default RGB color model (TYPE_INT_ARGB) and the default sRGB color space. If the default model does not match the ColorModel of the image, color conversion occurs. In the data returned by this method, each color component has only 8-bit precision.

Public void setRGB (int x, int y, int rgb)
Set the pixels in this BufferedImage to the specified RGB value. Assume that the pixel uses the default RGB color model, TYPE_INT_ARGB, and default sRGB color space. For images with IndexColorModel, select the nearest color index.
Parameters:
X-X coordinate of the pixel to be set
Y-Y coordinate of the pixel to be set
Rgb-RGB Value

Example: (obtain the RGB value) --- measure the test taker's knowledge about how to use an integer to represent the RGB color.
Import java. awt. image. BufferedImage;
Import javax. imageio. ImageIO;
Import java. io .*;

Public class Test {
Public static void main (String args []) {
Int [] rgb = new int [3];

File file = new File ("a.bmp ");
BufferedImage bi = null;
Try {
Bi = ImageIO. read (file );
} Catch (Exception e ){
E. printStackTrace ();
}

Int width = bi. getWidth ();
Int height = bi. getHeight ();
Int minx = bi. getMinX ();
Int miny = bi. getMinY ();
System. out. println ("width =" + width + ", height =" + height + ".");
System. out. println ("minx =" + minx + ", miniy =" + miny + ".");

For (int I = minx; I <width; I ++ ){
For (int j = miny; j // System. out. print (bi. getRGB (jw, ih ));
Int pixel = bi. getRGB (I, j );
Rgb [0] = (pixel & 0xff0000)> 16;
Rgb [1] = (pixel & 0xff00)> 8;
Rgb [2] = (pixel & 0xff );
System. out. println ("I =" + I + ", j =" + j + ":(" + rgb [0] + ", "+ rgb [1] + & quot ...... remaining full text>
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.