← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #692519]: findChanges has no function

 

Question #692519 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/692519

    Status: Answered => Solved

Michael Böhm confirmed that the question is solved:
Dear RaiMan,

Thanks for the script, it helped me a lot.
Following was my mistake:
*) This part of the script did not print out the matches. Your solution of looping through works fine.

while f.hasNext():    # does NOT work, please review your docs
     match=f.next()
    print "Found:",match

(I copied from your documentation
https://sikulix-2014.readthedocs.io/en/latest/finder.html#Finder.findChanges
Please revise your docs.)

*) I further found an issue in your above script:
On my PC (2.0.4) I needed to use:
f = Finder(Image.create(img1))

to make it work. (I know its a bug, but for completeness sake).

*) as the documentation and samplescripts for findChanges are quite
difficult to find, here is my working example to compare images on the
screen.

*) documentation of
    f.setFindChangesPixelDiff(40)# to adjust PIXEL_DIFF        # on documentationpage THIS is missing (typing error)
    f.setFindChangesImageDiff(5)# to adjust IMAGE_DIFF - seems to have no function at all, I did not find any differences in changing it to any value

It lets you select a region, finds the right region on the left side of that region, finds all the changes, filters out the smaller ones and double matches and compares the results to doublecheck. Thats needed if these images are not totally the same bit-wise.
I hope this helps somebody.

    Reg=selectRegion()
    RegNeu=Region(Reg2.getLastMatch().getX(),Reg2.getLastMatch().getY(),Reg.getW(),Reg.getH())
    Reg.highlight()
    RegNeu.highlight()
    wait(1)
    Reg.highlight()
    RegNeu.highlight()

    Settings.MinSimilarity = 0.85  # for the doublecheck
    matches=checkDifference(Reg,RegNeu)
    
    for match in matches:
        match.highlightOn()
    for match in matches: 
        if not RegNeu.exists(capture(match)): 
           mouseMove(match.getCenter())
           print match
        else:
           match.highlightOff() 
           print RegNeu.getLastMatch()
    wait(5)

def checkDifference(Reg,RegNeu,size=30):
    print Reg,RegNeu
    RegNeu.setW(Reg.getW())  # ensure same size
    RegNeu.setH(Reg.getH())  # ensure same size 
    img1=capture(Reg)
    img2=capture(RegNeu)
    
    f = Finder(Image.create(img1))    # Image.create due to a bug, might not be needed later
    #both images are converted to grayscale
    #only pixels whose gray value differs +-3 (PIXEL_DIFF) are looked at
    #if more than 5 pixels are detected (IMAGE_DIFF) a change is assumed
    f.setFindChangesPixelDiff(40)# to adjust PIXEL_DIFF
    f.setFindChangesImageDiff(5)# to adjust IMAGE_DIFF - seems to have no function at all
    changes = f.findChanges(img2)
    print "changes:", len(changes)
 
    matches=[]
    for i,match in enumerate(changes[1:]):
            match.moveTo(Location(match.getX()+Reg.getX(),match.getY()+Reg.getY()))
            if match.getW()>size or match.getH()>size:
                print "Right size:",i,match
                matches_temp=[]
                addMatch=True
                for t,compareMatch in enumerate(matches):
                    biggerMatch=isRegionOverlap(compareMatch,match)
                    if biggerMatch:
                        if biggerMatch==compareMatch:                            
                            addMatch=False  # drop adding smaller ones
                    else:
                        matches_temp.append(compareMatch)
                matches=matches_temp
                if addMatch: matches.append(match)

    print "real changes:", len(matches) 
    return matches

def isRegionOverlap(Reg1, Reg2): # find out wether Region1 overlaps Region2
      R1=[Reg1.getX(),Reg1.getY(),Reg1.getBottomRight().getX(),Reg1.getBottomRight().getY()]
      R2=[Reg2.getX(),Reg2.getY(),Reg2.getBottomRight().getX(),Reg2.getBottomRight().getY()]     
      if (R1[0]>=R2[2]) or (R1[2]<=R2[0]) or (R1[3]<=R2[1]) or  (R1[1]>=R2[3]):
         return False
      else:
         return True

def isRegionCompleteOverlap(Reg1, Reg2): # find out wether Region1 is completely inside Region2 or viceversa
      
      R1=[Reg1.getX(),Reg1.getY(),Reg1.getBottomRight().getX(),Reg1.getBottomRight().getY()]
      R2=[Reg2.getX(),Reg2.getY(),Reg2.getBottomRight().getX(),Reg2.getBottomRight().getY()]     
      print Reg1,Reg2
      print R1,R2
      if (R1[0]<=R2[0]<=R2[2]<=R1[2]) and (R1[1]<=R2[1]<=R2[3]<=R1[3]):
         return Reg2  #True return bigger one
      else:
               if (R2[0]<=R1[0]<=R1[2]<=R2[2]) and (R2[1]<=R1[1]<=R1[3]<=R2[3]):
                      return Reg1  #True return bigger one         
               else:
                      return False

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.