Java implementation of image template matching (Snail College)

Source: Internet
Author: User

Package Com.woniu.test;import Java.awt.awtexception;import Java.awt.rectangle;import java.awt.robot;import Java.awt.toolkit;import Java.awt.image.bufferedimage;import Java.io.file;import javax.imageio.ImageIO;// Java image template matching//core idea: Use a small picture, in a large picture to find, and return the small picture where the coordinates, and then move the mouse to the place, and implement the appropriate action. And more according to the feedback of the page, make the corresponding judgment (assertion)//1. For objects that need to be manipulated, get a small picture and save it. 2. On the current screen, get a large picture, save it or put it in memory//3. Using template matching, in large images, slide the small picture by pixel to find the location of the small picture. 4. For this coordinate position (x, y), plus the small picture height of half (H), the width of the general (W), to get the small picture of the center position, that is, we want to manipulate the coordinates (X+W,Y+H)//5. Move the mouse to the coordinate (x, y) and do the following (input, stand-alone, double-click, right, and so on).//6. Proceed to the second round of operation, third round operation. Until the nth round. Finally, a template match is made to confirm whether there is a small picture that is consistent with the expected result, and then implement the assertion. Because it is based on pixel matching, if the interface style changes, there may be a situation that cannot be identified. public class Imagematch {public static void main (string[] args) throws Exception {Imagematch im = new Imagematch ();//buffe  Redimage bi = im.getscreenshot ();//int[][] result = Im.getimagergb (BI);//for (int y = 0; y < result.length; y++) {//for (int x = 0; x < result[y].length; × x + +) {//system.out.println (result[y][x]);//}//}runtime.getruntime (). EXEC ("C:/windows/system32/calc.exe"); Thread.sleeP (3000); String Imagedir = System.getproperty ("User.dir") + "/wincalc/"; int[] target = im.findimage (Imagedir + "3.png"); System.out.println ("Find a point:" + target[0] + ":" + target[1]);} Public BufferedImage Getscreenshot () {bufferedimage Bfimage = null;int capturewidth = (int) Toolkit.getdefaulttoolkit ( ). Getscreensize (). getwidth (); int captureheight = (int) toolkit.getdefaulttoolkit (). Getscreensize (). GetHeight (); try {Robot Robot = new Robot (); Rectangle screenrect = new Rectangle (0,0,capturewidth,captureheight); bfimage = Robot.createscreencapture (screenrect) ;} catch (Awtexception e) {e.printstacktrace ();} return bfimage;} Get pixel point value public int[][] Getimagergb (bufferedimage bfimage) {int width = bfimage.getwidth (); int height = Bfimage.getheight (); int[][] result = new Int[width][height];for (int y = 0; y < height; y++) {for (int x = 0; x < width; + +) {//For a Pixel RGB encoded and stored in database result[x][y] = Bfimage.getrgb (x, y) & 0xffffff;//Gets the Red,green, and blue values of each pixel separately. int r = (Bfimage.getrgb (x, y) &0xFF0000) >> 16;//int g = (Bfimage.getrgb (x, y) & 0xff00) >> 8;//int b = Bfimage.getrgb (x, y) & 0xFF;} }return result;} Make template match public int[] Findimage (String imagePath) {bufferedimage bigimage = This.getscreenshot ();//Current screen BufferedImage Smallimage = null;//Open preselection saved small picture try {smallimage = Imageio.read (new File (ImagePath));} catch (Exception e) {E.printstacktra CE ();} int bigwidth = Bigimage.getwidth (), int bigheight = Bigimage.getheight (); int smallwidth = Smallimage.getwidth (); int Smallheight = Smallimage.getheight (); int[][] Bigdata = This.getimagergb (bigimage); int[][] Smalldata = This.getImageRGB (smallimage); int[] target = {-1, -1};for (int y=0; y<bigheight-smallheight; y++) {for (int x=0; X<bigwidth-smallwidt H X + +) {//The key points are pre-matched to reduce the complexity of the operation. If the keys themselves do not match, there is no need to match each pixel of the small graph. if (bigdata[x][y] = = Smalldata[0][0] &&//upper left corner bigdata[x+smallwidth-1][y] = = Smalldata[smallwidth-1][0] &&am  P upper right corner bigdata[x][y+smallheight-1] = = Smalldata[0][smallheight-1] &&Lower left corner bigdata[x+smallwidth-1][y+smallheight-1] = = Smalldata[smallwidth-1][smallheight-1] &&//lower right corner bigdata[x+ SMALLWIDTH/2][Y+SMALLHEIGHT/2] = = Smalldata[smallwidth/2][smallheight/2]) {//For full pixel match Boolean ismatched = This.checkallmatch (x, Y, Smallheight, Smallwidth, Bigdata, smalldata); if (ismatched) {System.out.println ("pixel x" + x + ":  Y "+ y +", the corresponding value is: "+ bigdata[x][y]);//Gets the center position of the small graph point int centerx = x + smallwidth/2;int centery = y + smallheight/2;target[0] = Centerx;target[1] = Centery;return target;}}}} return target;} Full pixel Match public boolean checkallmatch (int x, int y, int smallheight, int smallwidth, int[][] bigdata, int[][] smalldata) {b Oolean ismatched = true;for (int smally=0; smally<smallheight; smally++) {for (int smallx=0; smallx<smallwidth; smal lx++) {//If a pixel is found, the values are different, it is considered unequal, and if not equal, there is no need to continue comparing the other points. if (bigdata[x+smallx][y+smally]! = smalldata[smallx][smally]) {ismatched = False;return ismatched;}}} return ismatched;}}

Package Com.woniu.test;public class Calctestdemo {static Autotestrobot robot = new Autotestrobot ();p ublic static void main (string[] args) throws Exception {Runtime.getruntime (). EXEC ("Java-jar D:/other/javaswingcalc.jar"); Thread.Sleep (3000); String Imagedir = System.getproperty ("User.dir") + "/javacalc/"; Robot.moveandinput (Imagedir + "Numberx.png", "300"); Robot.moveandinput (Imagedir + "Numbery.png", "$") Robot.moveandselect (Imagedir + "Calctype.png", 1); Robot.moveandclick (Imagedir + "Docalc.png"), if (robot.isexists (Imagedir + "Result.png")) {System.out.println ("Test succeeded."); else {System.out.println ("Test failed.");}}}

  

Java implementation of image template matching (Snail College)

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.