function to return the region between two images

Asked by rob

hello,

i sometimes find myself wanting to search for an image between two other images so i made a function that returns a region between two other regions (sort of like an intersection, but i guess not quite). if the input regions overlap it returns the overlapping region. it works well enough but it requires two calls to find, so it only takes patterns as input. i would eventually like it to accept any combination of 2 patterns, matchs, regions or locations just like the native sikuli functions (minus string i guess, because i don't really understand how that input type works).

i looked up createUnion and createIntersection from java.awt.Rectangle but they dont seem quite the same, although im not familiar with java.

anyway, here it is. feedback would be cool.

def between(img1, img2):
    '''
        returns the region between two given images.
        if the images overlap, returns the overlapping region.
    '''
    r1 = find(img1)
    r2 = find(img2)
    leftBoundary = max(r1.getX(), r2.getX())
    rightBoundary = min(r1.getTopRight().getX(), r2.getTopRight().getX())
    topBoundary = max(r1.getY(), r2.getY())
    bottomBoundary = min(r1.getBottomRight().getY(), r2.getBottomRight().getY())
    x_coord = min(leftBoundary, rightBoundary)
    y_coord = min(topBoundary, bottomBoundary)
    w = abs(leftBoundary - rightBoundary)
    h = abs(topBoundary - bottomBoundary)
    print 'leftBoundary: ', leftBoundary
    print 'rightBoundary: ', rightBoundary
    print 'topBoundary: ', topBoundary
    print 'bottomBoundary:', bottomBoundary
    print 'x: %s\ny: %s\n w: %s\n h: %s' % (x_coord, y_coord, w, h)
    return Region(x_coord, y_coord, w, h)

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

This is a solution, that works with 1.0.1+

def between(item1, item2):
    regs = [None, None]
    ix = 0
    for item in (item1, item2):
        if isinstance(item, str) or isinstance(item, Pattern):
            if exists(item):
                regs[ix] = Region(getLastMatch())
        elif isinstance(item, Region) or isinstance(item, Match):
            regs[ix] = Region(item)
        elif isinstance(item, Location):
            regs[ix] = item.grow(1)
        ix += 1
    if None in regs:
        return None
    # now we have 2 valid regions
    # sort them with respect to x of top left
    # for further more sophisticated evals
    regs = sorted(regs, key=lambda reg: reg.x)
    # we now simply return the union region containing both regions
    # what really means "between" has to be defined more clearly
    return regs[0].add(regs[1])

it accepts Image, Pattern, Region, Match or Location for both parameters.
Image or Pattern are first searched on the whole screen.
Both items must be on the same screen.

Returns None, if there is a problem with one of the given parameters.

Returns the region, that contains both given items.

11133333333333333
11133333333333333
11133333333333222
33333333333333222
33333333333333222

1 the pixels of the one region
2 the pixels of the other region
3 additional pixels that belong to the resulting region

Can you help with this problem?

Provide an answer of your own, or ask rob for more information if necessary.

To post a message you must log in.