diff --git a/exp/board_detect.py b/exp/board_detect.py --- a/exp/board_detect.py +++ b/exp/board_detect.py @@ -56,9 +56,9 @@ def filterStones(contours,bwImg,stoneDim coverage1=area/(w*h or 1) hull=cv.convexHull(c) coverage2=area/(cv.contourArea(hull) or 1) - if coverage2<0.8: - cv.drawMarker(contourImg,tuple(map(int,center)),(0,127,255),cv.MARKER_DIAMOND,12) - keep=False + # if coverage2<0.8: + # cv.drawMarker(contourImg,tuple(map(int,center)),(0,127,255),cv.MARKER_DIAMOND,12) + # keep=False if keep: res.append((EPoint(*center),c)) cv.drawMarker(contourImg,tuple(map(int,center)),(255,0,0),cv.MARKER_CROSS) @@ -117,14 +117,12 @@ class BoardDetector: # detect lines from edges and stones edgeImg=prepareEdgeImg(rect) hough=HoughTransform(edgeImg) - hough.extract() stonesImg=np.zeros((self._rectH,self._rectW),np.uint8) for (point,c) in stones: cv.circle(stonesImg,(int(point.x),int(point.y)),2,255,-1) # cv.drawContours(stonesImg,[c for (point,c) in stones],-1,255,-1) show(stonesImg,"detected stones") - # hough.update(stonesImg,5) - hough=HoughTransform(stonesImg) + hough.update(stonesImg,5) hough.extract() # # detect lines passing through the stones diff --git a/exp/hough.py b/exp/hough.py --- a/exp/hough.py +++ b/exp/hough.py @@ -27,7 +27,8 @@ class HoughTransform: def extract(self): shift=self._diagLen//2 - peaks=sorted(list(findPeaks(self._acc)),key=lambda rc: self._acc[rc],reverse=True)[:2*19] + allPeaks=sorted(list(findPeaks(self._acc)),key=lambda rc: self._acc[rc],reverse=True) + peaks=allPeaks[:38] peaks=[(alpha,d-shift) for (alpha,d) in peaks] peaks=self._filterClose(peaks) peaks.sort(key=lambda rc: rc[0]) @@ -35,6 +36,7 @@ class HoughTransform: (alpha,beta)=self._detectDominantAngles(peaks) img=self._createImg() + img=self._markPeaks(img,self._filterClose(allPeaks[:38])) doublePeaks=peaks+[(alpha+180,-d) for (alpha,d) in peaks] params=self._computeGridParams([(gamma,d+shift) for (gamma,d) in doublePeaks if alpha<=gamma<=alpha+self._angleBandwidth]) self._drawGridCurve(img,params,0) @@ -63,7 +65,7 @@ class HoughTransform: """Discard points with Euclidean distance on the original image lower than 10. From such pairs keep only the one with a higher value in the accumulator. This can delete a series of points. If a-b and b-c are close and a>b>c, only a is kept.""" - minDist=10 + minDist=13 center=EPoint(*self._center) res=[] for (alphaDeg,d) in peaks: @@ -93,7 +95,7 @@ class HoughTransform: log.debug("angles histogram: %s",histogram) dominantAngles=sorted(histogram,key=lambda xy: xy[1],reverse=True) alpha=dominantAngles[0] - dominantAngles=[beta for beta in dominantAngles if 165>abs(alpha[0]-beta[0])>15] + dominantAngles=[beta for beta in dominantAngles if 180-bandwidth>abs(alpha[0]-beta[0])>bandwidth] beta=dominantAngles[0] log.debug("dominant angles: %s, %s",alpha,beta) return (alpha[0],beta[0]) @@ -126,6 +128,12 @@ class HoughTransform: return img + def _markPeaks(self,img,peaks): + colors=[[255,0,0],[255,255,0],[0,255,0],[0,255,255],[0,0,255]] + for (i,(alpha,d)) in enumerate(peaks[:38]): + cv.drawMarker(img,(d,alpha),colors[i//9],cv.MARKER_TILTED_CROSS) + return img + def _drawGridCurve(self,img,params,colorKey=0): colors=[[0,255,255],[255,0,255],[255,255,0]] color=colors[colorKey]