Can't tell the difference between two images

Asked by Tseno Tsenov on 2017-05-17

Hi,
I'm trying to click on an image on screen that has a specific shape and is red, the problem is that there is another image with the same shape but different color, sikuli can't tell the difference between the two and keeps switching between the two and clicking on both, I want it to keep clicking on the red one.

How can I go around this ?

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
masuo
Solved:
2017-05-18
Last query:
2017-05-18
Last reply:
2017-05-18
Tseno Tsenov (wowsuchusername) said : #1

Can I use ColorImageTargetExample? And how I don't find it in the imports in Eclipse, ugh I just want to click on the red image....

masuo (masuo-ohara) said : #2

By using class java.awt.Color we can get RGB value.

[example codes to get color:]
import java.awt.Color

setFindFailedResponse(SKIP)

f = findAll("shape.png")
m = []
while f.hasNext():
    m.append(f.next())

for x in m:
    x.highlight(1)
    print x.getCenter().getColor()
    print x.getCenter().getColor().getRed()
    print x.getCenter().getColor().getGreen()
    print x.getCenter().getColor().getBlue()

setFindFailedResponse(ABORT)

Tseno Tsenov (wowsuchusername) said : #3

I don't understand what you mean, I'm using Eclipse and Java, my mouse moves back and forward between the two images because it keeps detecting them as identical instead of staying on the desired one , so I can't just read the RGB value.

import org.sikuli.script.Screen;
.....
this.screen.type("C:\\Images\\TestImage.png", "a"); - this is what I am using for the image search, im also sending a click but that's not relevant.

RaiMan (raimund-hocke) said : #4

try:
Pattern image = new Pattern("C:\\Images\\TestImage.png").exact()
this.screen.type(image, "a");

The Pattern class is the feature, that allows, to tell SikuliX a needed match score for an image (0.99 in this case, which means exact).

read this to understand the magic behind:
http://sikulix-2014.readthedocs.io/en/latest/basicinfo.html#sikulix-how-does-it-find-images-on-the-screen

Best masuo (masuo-ohara) said : #5

#2 sample codes are Python.

If you show codes about the part of click() or while and its surroundings, I think that answer is more easy to obtain.

Tseno Tsenov (wowsuchusername) said : #6

Thank you so much, for the example and for the article, it works flawlessly !

Tseno Tsenov (wowsuchusername) said : #7

Thanks masuo, that solved my question.

masuo (masuo-ohara) said : #8

[Add a description]
If matching similarity of both images is 1.0, whatever set minimum similarity to 0.99, script does not work.
In such case, to get RGB value is useful. In such case, try the solution by color.

RaiMan (raimund-hocke) said : #9

@masuo:
Agreed - problem with SikuliX version 1: internally with the normal find the similarity score is truncated to 2 decimals.

Only if you use findAll(), then you can access the scores of the matches with all its decimals.

So in this case this is worth a try:
Pattern image = new Pattern("C:\\Images\\TestImage.png").exact()
Iterator<Match> matches = this.screen.findAll(image)
match found = null;
if (matches.hasNext()) {
    match = matches.next()
}
if (null != match) {
    match.type("a")
} else {
  // should not happen ;-)
}

match should be the wanted image now.

This solution should work even in cases, where you do not have a chance to check some pixel-RGB.