diff --git a/exp/board_detect.py b/exp/board_detect.py --- a/exp/board_detect.py +++ b/exp/board_detect.py @@ -5,6 +5,7 @@ sys.path.append("../src") import os import math import random +import itertools import logging as log import cv2 as cv @@ -109,30 +110,21 @@ class BoardDetector: 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,10) lines=hough.extract() linesImg=np.copy(rect) - for line in lines: + for line in itertools.chain(*lines): self._drawLine(linesImg,line) show(linesImg,"detected lines") - # # 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]) - # (p,q,r,s)=(Line(a,b),Line(b,c),Line(c,d),Line(d,a)) - # v1=p.intersect(r) - # v2=q.intersect(s) - # log.debug("true vanishing points: %s ~ %s, %s ~ %s",v1,v1.toPolar(imgCenter),v2,v2.toPolar(imgCenter)) - # vanish=self._detectVanishingPoints(lines,imgCenter,(v1.toPolar(imgCenter),v2.toPolar(imgCenter))) - # # # rectify the image - # matrix=self._computeTransformationMatrix(vanish,lines) - # transformed=cv.warpPerspective(rect,matrix,(self._rectW,self._rectH)) - # - # # determine precise board edges + matrix=self._computeTransformationMatrix(lines[0][0],lines[0][-1],lines[1][0],lines[1][-1]) + transformed=cv.warpPerspective(rect,matrix,(self._rectW,self._rectH)) + + # determine precise board edges def _detectRough(self,img,filename): corners=self._annotations[filename][0] @@ -177,36 +169,8 @@ class BoardDetector: show(stones,"black and white areas") return stones - def _printLines(self,lines,allPoints,img): - for (i,line) in enumerate(lines): - img_=np.copy(img) - points=list(line.getSortedPoints()) - (a,b)=max(((a,b) for a in points for b in points if a %s",point,point.toPolar(imgCenter)) - polarHough.put(point.toPolar(imgCenter)) - vanish=[EPoint.fromPolar(p,imgCenter) for p in polarHough.extract(2,trueVs)] - log.debug(vanish) - return vanish - - def _computeTransformationMatrix(self,vanish,lines): - (v1,v2)=vanish - (p,r)=sorted(lines,key=lambda p: point2lineDistance(p.a,p.b,v1))[:2] - (q,s)=sorted(lines,key=lambda p: point2lineDistance(p.a,p.b,v2))[:2] - (a,b,c,d)=Corners([p.intersect(q),q.intersect(r),r.intersect(s),s.intersect(p)]) # canonize the abcd order + def _computeTransformationMatrix(self,p,q,r,s): # p || q, r || s + (a,b,c,d)=Corners([p.intersect(r),p.intersect(s),q.intersect(r),q.intersect(s)]) # canonize the abcd order a_=EPoint(b.x,min(a.y,d.y)) b_=EPoint(b.x,max(b.y,c.y)) c_=EPoint(c.x,max(b.y,c.y)) @@ -223,7 +187,7 @@ class BoardDetector: cv.drawMarker(rect,(int(point.x),int(point.y)),(0,255,255),cv.MARKER_TILTED_CROSS) show(rect) transformed=cv.warpPerspective(rect,matrix,(self._rectW,self._rectH)) - show(transformed) + show(transformed,"rectified image") return matrix