diff --git a/exp/board_detect.py b/exp/board_detect.py --- a/exp/board_detect.py +++ b/exp/board_detect.py @@ -13,7 +13,6 @@ import scipy.cluster import scipy.ndimage import scipy.signal -from geometry import Line from polar_hough import PolarHough from annotations import DataFile,computeBoundingBox from hough import show,prepareEdgeImg,HoughTransform @@ -73,21 +72,6 @@ def filterStones(contours,bwImg,stoneDim return res -def groupLines(points,minCount,tolerance): - random.shuffle(points) - sample=points[:] - for (i,a) in enumerate(sample): - for (j,b) in enumerate(sample): - if j<=i: continue - ab=Line(a,b) - for c in points: - if c is a or c is b: continue - if point2lineDistance(a,b,c)<=tolerance: - ab.points.add(c) - if len(ab.points)>=minCount: - yield ab - - class BoardDetector: def __init__(self,annotationsPath): self._annotations=DataFile(annotationsPath) @@ -130,9 +114,6 @@ class BoardDetector: hough.update(stonesImg,5) hough.extract() - # # detect lines passing through the stones - # lines=self._constructLines(stones) - # # # detect vanishing points of the lines # imgCenter=EPoint(w//2-x1, h//2-y1) # (a,b,c,d)=(p-EPoint(x1,y1) for p in self._annotations[filename][0]) @@ -191,44 +172,6 @@ class BoardDetector: show(stones,"black and white areas") return stones - def _constructLines(self,stoneLocs): - lineDict=dict() - # minCount=min(max(math.sqrt(len(stoneLocs))-4,3),7) - minCount=6 - log.debug("min count: %s",minCount) - points=[point for (point,contour) in stoneLocs] - for line in groupLines(points,minCount,2): - key=line.getSortedPoints() - if key in lineDict: # we already have a line with the same incident points - continue - lineDict[line.getSortedPoints()]=line - obsolete=set() - for ab in lineDict.values(): - if ab is line: continue - if line.pointsb>c, only a is kept.""" - minDist=13 - center=EPoint(*self._center) - res=[] - for (alphaDeg,d) in peaks: - alphaRad=alphaDeg*math.pi/180 - point=EPoint.fromPolar((alphaRad,d),center) - ctrl=True - for (betaDeg,e) in peaks: - betaRad=betaDeg*math.pi/180 - point_=EPoint.fromPolar((betaRad,e),center) - if point.dist(point_)angles[k2]) or (k2>=n and angles[k1]+bandwidth>angles[k2%n]+180): - k2+=1 - histogram.append((angles[k1],k2-k1)) - k1+=1 - 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 180-bandwidth>abs(alpha[0]-beta[0])>bandwidth] - beta=dominantAngles[0] - log.debug("dominant angles: %s, %s",alpha,beta) - return (alpha[0],beta[0]) - def _detectLines(self): bag=LineBag() for alpha in range(0,180,2): @@ -134,15 +93,6 @@ class HoughTransform: res.append((k,i)) return res - def _computeGridParams(self,lines): - log.debug("computing grid parameters for: %s",lines) - angles=[alpha for (alpha,d) in lines] - dists=[d for (alpha,d) in lines] - curve=lambda x,a,b,c,d: a*x**3+b*x**2+c*x+d - (params,cov)=scipy.optimize.curve_fit(curve,dists,angles) - log.debug("result: %s",params) - return params - def show(self,img=None): if img is None: img=self._createImg() @@ -162,24 +112,6 @@ 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] - (a,b,c,d)=params - (h,w)=img.shape[:2] - curve=lambda x: a*x**3+b*x**2+c*x+d - for x in range(0,w,3): - y=round(curve(x)) - if y<0 or y>=2*h: continue - if y=0 else 180) - if y<0 or y>=h: continue - img[y,x]=color - - -def findPeaks(arr2d): # a naive implementation - """Scan 8-neighbourhood and for each peak or top plateau yield one point. For plateaus yield the """ - (h,w)=arr2d.shape - neighbours=[(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)] - for r in range(h): - for c in range(w): - if all(r+dr<0 or r+dr>=h or c+dc<0 or c+dc>=w or arr2d[r,c]>arr2d[r+dr,c+dc] or (i<4 and arr2d[r,c]>=arr2d[r+dr,c+dc]) for (i,(dr,dc)) in enumerate(neighbours)): - yield (r,c) - def show(img,filename="x"): cv.imshow(filename,img) @@ -256,13 +167,3 @@ def prepareEdgeImg(img): edges=filterHor(edges)+filterVert(edges)+filterDiag(edges) show(edges,"kernel filtered edges") return edges - -def houghLines(bwImg): - colorImg=cv.cvtColor(bwImg,cv.COLOR_GRAY2BGR) - lines = cv.HoughLinesP(bwImg,1,np.pi/180,10,minLineLength=10,maxLineGap=40) - if lines is None: lines=[] - for line in lines: - x1,y1,x2,y2 = line[0] - cv.line(colorImg,(x1,y1),(x2,y2),(0,255,0),1) - - show(colorImg)