Files
@ a0db29122ab6
Branch filter:
Location: OneEye/src/image_analyzer.py - annotation
a0db29122ab6
1.4 KiB
text/x-python
researching image colors
f6b3d570893c f6b3d570893c 42f4533a5029 42f4533a5029 42f4533a5029 a1b8c202099c f6b3d570893c a1b8c202099c a1b8c202099c a1b8c202099c f6b3d570893c f6b3d570893c f6b3d570893c f6b3d570893c a1b8c202099c c842bba99503 a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c f6b3d570893c a1b8c202099c f6b3d570893c a1b8c202099c a1b8c202099c a1b8c202099c c842bba99503 a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c f6b3d570893c f6b3d570893c a1b8c202099c c842bba99503 f6b3d570893c f6b3d570893c a1b8c202099c a1b8c202099c a1b8c202099c c842bba99503 a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c a1b8c202099c | import logging
from go import *
class ImageAnalyzer:
def __init__(self,tresB=30,tresW=60):
self.board=[[Go.EMPTY]*19 for r in range(19)]
self.grid=None
self.tresB=tresB
self.tresW=tresW
def analyze(self,image,sizeCoef,shift):
if self.grid==None: return False
for r in range(19):
for c in range(19):
intersection=self.grid.intersections[r][c]
self.board[r][c]=self.analyzePoint(image,r,c,intersection*sizeCoef+shift,*(self.grid.stoneSizeAt(r,c,sizeCoef)))
def analyzePoint(self,image,row,col,imageCoords,stoneWidth,stoneHeight):
b=w=e=0
((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)
S=1-m/I
if 100*I<self.tresB: b+=1
elif 100*I>self.tresW: w+=1
else: e+=1
logging.debug("(%d,%d) ... (b=%d,w=%d,e=%d)", row, col, b, w, e)
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
|