from go import *
class ImageAnalyzer:
def __init__(self):
self.board=[[Go.EMPTY]*19 for r in range(19)]
self.grid=None
def analyze(self,image,tresB,tresW,sizeCoef,shift):
if self.grid==None: return False
for r in range(19):
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)
def analyzePoint(self,image,imageCoords,stoneWidth,stoneHeight,tresB,tresW):
b=w=e=0
cmax=max(int(stoneWidth*2//5),2)
rmax=max(int(stoneHeight*2//5),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))
I=(red+green+blue)/255/3
m=min(red,green,blue)
S=1-m/I
if 100*I<tresB: b+=1
elif 100*I>tresW: w+=1
else: e+=1
if b>=w and b>=e: return Go.BLACK
if w>=b and w>=e: return Go.WHITE
return Go.EMPTY
def setGrid(self,grid):
self.grid=grid