findChanges has no function

Asked by Michael Böhm on 2020-08-20

Hi!
I am using 2.0.4 on Windows10.

Following Script shows that findChanges has no function.

    img=capture(Reg)
    img2=capture(RegNeu) # ensure same size
    print(img,img2)
    f=Finder(Image.create(img)) #workaround from another bug
    f.findChanges(img2) # this has no function, also IDE does not highlight it as known function
    while f.hasNext():
        match=f.next()
        print "Found:",match

I did the implementation as described in https://sikulix-2014.readthedocs.io/en/latest/finder.html , did the workaround for another known bug, but it still does not find differences in samesize pictures.

And please put on the wishlist to be able to compare Regions, not just imagefiles.

Thanks
Michael

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Michael Böhm
Solved:
2020-08-28
Last query:
2020-08-28
Last reply:
2020-08-23
RaiMan (raimund-hocke) said : #1

should first be a question

Michael Böhm (badboisikulix) said : #3

Dear RaiMan.

Seriously I still believe it is a bug. because the IDE does not highlight the word "findChanges" as known command, and also all other finder functions work fine, just not findChanges.

Regards
MIchael

RaiMan (raimund-hocke) said : #4

--- does not highlight the word "findChanges"
This is because the word is missing in the respective IDE support file (as many other features)

Relevant is, whether something like someRegionOrWhatever.someFunction() does not lead to a name error.

--- this is a findChanges test on Windows having the page https://github.com/RaiMan open in Google Chrome and triggering changes by selecting a different tab

App.focus("Google Chrome")
wait(2)
#observe area in app window
reg = App.focusedWindow()
reg = reg.grow(-10, -540, -150, -30)
reg.highlight(1)
# put the app window in an startup state
if not reg.has("1598188841340.png"):
    btnOver = "btnOver.png"
    reg.click(btnOver)
    reg.wait("1598188674947.png", 10)
# capture startup state
img1 = capture(reg)
# chnage the content in the observed area
btnRepo = "img.png"
reg.click(btnRepo)
reg.wait("1598188724213.png", 10)
# capture changed state
img2 = capture(reg)

# evaluate changes
f = Finder(img1)
changes = f.findChanges(img2)
print "changes:", len(changes)
changed = Region(0, 0, reg.w, reg.h)
if len(changes) > 0:
    changed = changes[0]
    if len(changes) > 1:
        for change in changes[1:]:
            changed = changed.union(change)
changed = Region(reg.x + changed.x, reg.y + changed.y, changed.w, changed.h)
changed.highlight(2)

The result is 29 changes, which result in a changed area union in the right side of the page (as expected).

Hence, findChanges works as expected.

Be aware: in the current implementation (which surely has to be enhanced ;-) this is the safest way:

Finder(absolute-filename-string-base).findChanges(absolute-filename-string-changed)

which in my testcase is assured by using capture(reg).

Michael Böhm (badboisikulix) said : #5

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