def haughTransform(img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150, apertureSize=3) lines = cv2.HoughLines(edges, 1, np.pi / 180, 200) if lines is None: return None statAngle = Statistic() angleTable = [] for i in range(lines.shape[0]): rho, theta = lines[i][0] a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho x1 = int(x0 + 1000 * (-b)) y1 = int(y0 + 1000 * (a)) x2 = int(x0 - 1000 * (-b)) y2 = int(y0 - 1000 * (a)) dy = y2 - y1 dx = x2 - x1 A = 0 if abs(dx) > abs(dy): if x1 < x2: A = 1 angle = math.atan2(-dy, dx) else: A = 2 angle = math.atan2(dy, dx) else: if y1 < y2: A = 3 angle = math.atan2(dx, dy) else: A = 4 angle = math.atan2(-dx, -dy) angleTable.append(angle) statAngle.add(angle) # print( dx, dy , angle * 180.0 / math.pi , A) # reject any angle higher than 5 degree from target targetAngle = statAngle.mean() maxDegree = 5 * math.pi / 180 statAngle.clear() for angle in angleTable: if (abs(angle - targetAngle)) < maxDegree: statAngle.add(angle) if statAngle.count > 0: targetAngle = statAngle.mean() print("Best angle :", 180.0 * targetAngle / math.pi) return targetAngle
def getdigitsSpacing(self, cBoxes, minSpacing): self.digitsSpacing = 0 deltaX=[] deltaY=[] centerX = Statistic() centerY = Statistic() cx, cy = self.sudokuBoxCenter for digit in cBoxes: if abs(digit["center"][0] - cx) < minSpacing/2: centerX.add(digit["center"][0]) if abs(digit["center"][1] - cy) < minSpacing/2: centerY.add(digit["center"][0]) if centerX.count > 1: cx = centerX.mean() if centerY.count > 1: cx = centerY.mean() self.sudokuBoxCenter = (cx, cy) #get spacing between all objects for idx1 in range(len(cBoxes)-1): obj1 = cBoxes[idx1] if obj1["valid"] is False: continue for idx2 in range(idx1+1,len(self.allDigitsCenter)): obj2 = cBoxes[idx2] if obj2["valid"] is False: continue dx = abs(obj1["center"][0] - obj2["center"][0]) if dx >= minSpacing/2: deltaX.append(dx) dy = abs(obj1["center"][1] - obj2["center"][1]) if dy >= minSpacing/2: deltaY.append(dy) deltaX.sort() deltaY.sort() # now get he minimum distance stat = Statistic() for i in deltaX: if stat.count == 0: stat.add(i) else: if (i - stat.mean()) > minSpacing /2 : if stat.count == 1: stat.clear() stat.add(i) else: break spacingX = stat.mean() stat.clear() for i in deltaY: if stat.count == 0: stat.add(i) else: if (i - stat.mean()) > minSpacing /2 : if stat.count == 1: stat.clear() stat.add(i) else: break spacingY = stat.mean() # ok we got the spacing # we need to figure out if it is x,2x,or3x, or 4x for i in range(1,9): spacing = spacingX / i if (8 * spacing) < self.sudokuBoxSize: spacingX = spacing break for i in range(1,9): spacing = spacingY / i if (8 * spacing) < self.sudokuBoxSize: spacingY = spacing break print("spacing X", spacingX,"spacingY", spacingY) if (spacingX > 1) and ( spacingY > 1): self.digitsSpacing = (spacingX + spacingY)/2 elif spacingX > 1: self.digitsSpacing = spacingX elif spacingY > 1: self.digitsSpacing = spacingX else: self.digitsSpacing = self.sudokuBoxSize/9