grabbing location details of matches out of a list

Asked by JD

Hi,

I am trying to get the location (x,y) of a series of matches. I followed the suggestion from the Sikuli Match Documentation:

findAll(image1)
list1 = list(getLastMatches())

which now provides me with a list that prints:
print list1 =
[Match[735,298 17x17 score=1.00 target=center], Match[735,390 17x17 score=0.99 target=center], Match[827,390 17x17 score=0.99 target=center], Match[920,575 17x17 score=0.99 target=center], Match[966,390 17x17 score=0.99 target=center], Match[643,390 17x17 score=0.99 target=center], Match[781,483 17x17 score=0.98 target=center], Match[920,483 17x17 score=0.97 target=center], Match[643,483 17x17 score=0.97 target=center]]

my question is: how do i extract the location values out of that into another list? Preferably something along the lines of
[x1,y1,x2,y2,x3,y3........etc....]

Thank you for your help, this forum is brilliant and sorry if my question is a stupid one,

JD

Thank you for your help

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:

This question was reopened

  • by JD
Revision history for this message
RaiMan (raimund-hocke) said :
#1

From another answer:

If you want to grab many matches of the same image at once, you have to use findAll().

matches = findAll(Pattern("image.png").similar(0.9))
for match in matches:
    click(match)

The returned matches are in arbitrary order. If you want them sorted somehow:

matches = findAll(Pattern("image.png").similar(0.9))
sorted_matches = sorted(matches, key=lambda m:m.y)
for match in sorted_matches:
    click(match)

This will go through the matches according to their y coordinate. Still the matches having the same y value, are in arbitrary order.

in the for loop you can do what you want: e.g build your list

points = ()
for match in sorted_matches:
    points.append((match.x, match.y))

or an iterator shortcut:

points = [ (m.x, m.y) for m in sorted_matches]

or just this:
matches = findAll(Pattern("image.png").similar(0.9))
points = [ (m.x, m.y) for m in sorted(matches, key=lambda m:m.y)]

now in points you have the 2-element-lists (x,y) of the top left corners of your matches from findAll in the order of increasing y value.

Revision history for this message
JD (keelbo) said :
#3

that works well, I now have the list showing:
points = [(735, 298), (735, 390), (827, 390), (966, 390), (643, 390), (781, 483), (920, 483), (643, 483), (920, 575)]

so [(x0,y0), (x1,y1),(x2,y2)........]

I would however like to get them in a format of

[x0,y0,x1,y1,x2,y2] to be able to perform calculations on the x values and the y values.
Using the above method retrieving points[0] returns (735,298), rather than just a single value.

I want to trigger a response if for example match 1 is located on the x-axis between 730 and 740, and between 290 and 300 on the y-axis.

thank you for your help and I apologize for not being clearer before
JD

Revision history for this message
Best RaiMan (raimund-hocke) said :
#4

(x, y) = points[0]
now you have the 2 values in x and y.

Revision history for this message
JD (keelbo) said :
#5

Thanks RaiMan, that solved my question.