diff --git a/exp/board_detect.py b/exp/board_detect.py --- a/exp/board_detect.py +++ b/exp/board_detect.py @@ -105,7 +105,11 @@ class BoardDetector: # quantize colors colors=self._sampleColors(rect) quantized=quantize(rect,colors) - show(quantized,filename) + gray=cv.cvtColor(rect,cv.COLOR_BGR2GRAY) + edges=cv.Canny(gray,70,130) + show(edges,"edges") + quantized=quantized & (255-cv.cvtColor(edges,cv.COLOR_GRAY2BGR)) + show(quantized,"quantized, edges separated") # detect black and white stones stones=self._detectStones(quantized,colors) @@ -165,16 +169,16 @@ class BoardDetector: def _maskStones(self,quantized,colors): unit=np.array([1,1,1],dtype=np.uint8) - kernel=np.ones((3,3),np.uint8) maskB=cv.inRange(quantized,colors[0]-unit,colors[0]+unit) - maskB=cv.morphologyEx(maskB,cv.MORPH_OPEN,kernel,iterations=1) - maskB=cv.erode(maskB,kernel,iterations=3) - # distTransform = cv.distanceTransform(maskB,cv.DIST_L2,5) - # maskB=cv.inRange(distTransform,6,10) + + distTransform=cv.distanceTransform(maskB,cv.DIST_L2,5) + maskB=cv.inRange(distTransform,6,20) show(maskB,"black areas") + maskW=cv.inRange(quantized,colors[1]-unit,colors[1]+unit) - maskW=cv.morphologyEx(maskW,cv.MORPH_OPEN,kernel,iterations=1) - maskW=cv.erode(maskW,kernel,iterations=2) + distTransform=cv.distanceTransform(maskW,cv.DIST_L2,5) + maskW=cv.inRange(distTransform,6,20) + show(maskW,"white areas") stones=cv.bitwise_or(maskB,maskW) show(stones,"black and white areas")