# HG changeset patch # User Laman # Date 2019-02-22 17:49:44 # Node ID 184d592b02dd5fb5bf750aec34bb686931c21f0b # Parent 1bdf16ab4c2ca2ebcfc1661eeb576b48b9450c0b returning and visualizing detected lines 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,7 @@ import scipy.cluster import scipy.ndimage import scipy.signal -from polar_hough import PolarHough +from geometry import Line from annotations import DataFile,computeBoundingBox from hough import show,prepareEdgeImg,HoughTransform from analyzer.epoint import EPoint @@ -111,8 +111,13 @@ class BoardDetector: 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.extract() + hough.update(stonesImg,10) + lines=hough.extract() + + linesImg=np.copy(rect) + for line in lines: + self._drawLine(linesImg,line) + show(linesImg,"detected lines") # # detect vanishing points of the lines # imgCenter=EPoint(w//2-x1, h//2-y1) @@ -222,6 +227,24 @@ class BoardDetector: return matrix + def _drawLine(self,img,line): + (h,w)=img.shape[:2] + corners=[EPoint(0,0),EPoint(w,0),EPoint(0,h),EPoint(w,h)] # NW NE SW SE + borders=[ + [Line.fromPoints(corners[0],corners[1]), Line.fromPoints(corners[2],corners[3])], # N S + [Line.fromPoints(corners[0],corners[2]), Line.fromPoints(corners[1],corners[3])] # W E + ] + + (a,b)=(line.intersect(borders[0][0]), line.intersect(borders[0][1])) + log.debug("%s %s",line,(a,b)) + if not a or not b: + (a,b)=(line.intersect(borders[1][0]), line.intersect(borders[1][1])) + log.debug("* %s %s",line,(a,b)) + if any(abs(x)>10**5 for x in [*a,*b]): + log.debug("ignored") + return + cv.line(img,(int(a.x),int(a.y)),(int(b.x),int(b.y)),[0,255,0]) + if __name__=="__main__": detector=BoardDetector(sys.argv[2]) diff --git a/exp/geometry.py b/exp/geometry.py --- a/exp/geometry.py +++ b/exp/geometry.py @@ -13,7 +13,8 @@ class Line: @staticmethod def fromNormal(a,b,c): """ax + by + c = 0""" - norm=-c/abs(c)*math.sqrt(a**2+b**2) + sign=-c/abs(c) if c!=0 else 1 + norm=sign*math.sqrt(a**2+b**2) (a_,b_,c_)=(a/norm,b/norm,c/norm) alpha=math.acos(a_) if b_>=0 else 2*math.pi-math.acos(a_) return Line(alpha,-c_) diff --git a/exp/hough.py b/exp/hough.py --- a/exp/hough.py +++ b/exp/hough.py @@ -10,6 +10,8 @@ import scipy.optimize import scipy.signal import cv2 as cv +from geometry import EPoint,Line + DEBUG=True @@ -45,15 +47,25 @@ class HoughTransform: def extract(self): img=self._createImg() self.show(img) - (ab,cd)=self._detectLines() + lines=self._detectLines() + res=[] i=0 - for (score,alpha,beta,peaks) in (ab,cd): + for (score,alpha,beta,peaks) in lines: log.debug("score: %s",score) log.debug("alpha, beta: %s, %s",alpha,beta) self._drawLine(img,alpha,beta,peaks,i) + + keys=self._readLineKeys(alpha,beta) + for k in peaks: + (alphaDeg,d)=keys[k] + alphaRad=alphaDeg*math.pi/180 + baseLine=Line(alphaRad,0) + dd=baseLine.distanceTo(EPoint(*self._center)) # to shift d from the center to 0,0 + res.append(Line(alphaRad, dd+d-self._diagLen//2)) i+=1 self.show(img) + return res def update(self,img,weight=1): start=datetime.now().timestamp()