diff --git a/src/image_analyzer.py b/src/image_analyzer.py old mode 100644 new mode 100755 --- a/src/image_analyzer.py +++ b/src/image_analyzer.py @@ -14,22 +14,16 @@ class ImageAnalyzer: for c in range(19): intersection=self.grid.intersections[r][c] - if c>0: stoneWidth=sizeCoef*(intersection.x-self.grid.intersections[r][c-1].x) - else: stoneWidth=sizeCoef*(self.grid.intersections[r][c+1].x-intersection.x) - if r>0: stoneHeight=sizeCoef*(intersection.y-self.grid.intersections[r-1][c].y) - else: stoneHeight=sizeCoef*(self.grid.intersections[r+1][c].y-intersection.y) - - self.board[r][c]=self.analyzePoint(image,intersection*sizeCoef+shift,stoneWidth,stoneHeight,tresB,tresW) + self.board[r][c]=self.analyzePoint(image,intersection*sizeCoef+shift,*(self.grid.stoneSizeAt(r,c,sizeCoef)),tresB,tresW) def analyzePoint(self,image,imageCoords,stoneWidth,stoneHeight,tresB,tresW): b=w=e=0 - - cmax=max(int(stoneWidth*2//7),2) # !! optimal parameters subject to further research - rmax=max(int(stoneHeight*2//7),2) - - for r in range(-rmax,rmax+1): - for c in range(-cmax,cmax+1): - red,green,blue=image.getpixel((imageCoords.x+c,imageCoords.y+r)) + + ((x1,y1),(x2,y2))=self.relevantRect(imageCoords,stoneWidth,stoneHeight) + + for y in range(y1,y2+1): + for x in range(x1,x2+1): + red,green,blue=image.getpixel((x,y)) I=(red+green+blue)/255/3 m=min(red,green,blue) @@ -41,6 +35,14 @@ class ImageAnalyzer: if b>=w and b>=e: return Go.BLACK if w>=b and w>=e: return Go.WHITE return Go.EMPTY + + def relevantRect(self,imageCoords,stoneWidth,stoneHeight): + x=int(imageCoords.x) + y=int(imageCoords.y) + xmax=max(int(stoneWidth*2//7), 2) # !! optimal parameters subject to further research + ymax=max(int(stoneHeight*2//7), 2) + + return ((x-xmax,y-ymax), (x+xmax,y+ymax)) def setGrid(self,grid): self.grid=grid