Find the co-ordinates of matching image using sikuli in java

1.2k views Asked by At

I need to get the co-ordinates of matched image within the actualImage so that I can perform operations on it. However, I tried below two approaches,but both doesn't seem to work:

Approach 1: Using below, I'm able to find a match but co-ordinates returned are just the width & height of image to be matched(which I already know). I want to get the position of the same within actual image.

    BufferedImage actualImg = ImageIO.read(new File("C:/Images/SrcImg.PNG"));
    ImageTarget actualTgt = new ImageTarget(actualImg);
    BufferedImage searchImg = ImageIO.read(new File("C:/Images/TgtImg.PNG"));       
    ImageTarget searchTgt = new ImageTarget(searchImg);
    ScreenRegion scrReg = new StaticImageScreenRegion(actualTgt.getImage());
    ScreenRegion resReg = scrReg.find(searchTgt);
    ScreenLocation center = resReg.getCenter();
    System.out.println(":getElementFromImage: x_loc,y_loc =["+center.getX()+","+center.getY()+"]");

Approach 2: In below code I tried with sikulix Finder. However, with this src.hasNext() returned true BUT src.next() threw nullpointer exception.Not sure what is the problem here:

    Finder src = new Finder("C:/Images/SrcImg.PNG");
    Pattern pat = new Pattern("C:/Images/TgtImg.PNG").similar(0.5);
    src.find(pat);
    Match m;
    while( src.hasNext()) 
        m = src.next();
    src.destroy();

java.lang.NullPointerException
at org.sikuli.script.Finder.next(Finder.java:484)
at com.work.ImageFinder.main(ImageFinder.java:38)

I already spent good amount of time to make this work. Any help would be much appreciated.

Thanks!

1

There are 1 answers

0
nagpai On

It works fine after passing the Region to Finder like below:

Finder src = new Finder("C:/Images/SrcImg.PNG", new Region(0,0,<width>,<height>))
Pattern pat = new Pattern("C:/Images/TgtImg.PNG").similar(0.5);
src.find(pat);
Match m;
while( src.hasNext()) 
    m = src.next();
src.destroy();

More details can be found below link: Is it possible to use Sikuli to assert that images are the same in GUI-less mode?