i have converted sudoku image into sudoku grid using opencv
now i want to extract each box from image what is best way to do this?
as per my knowledge i am trying to find intersection points of lines to find corner of each box
class SudokuSolverPlay:
def __init__(self, image):
def __preProcess(self, img):
"""return grayscale image"""
def __maskSudoku(self, img):
"""return masked image"""
def __dectactEdge(self, img):
"""return sudoku grid"""
def drawLines(src, dest, iteration=1):
minLineLength = 100
src = cv2.convertScaleAbs(src)
for _ in range(iteration):
lines = cv2.HoughLinesP(image=src, rho=1, theta=np.pi / 180,
threshold=100, lines=np.array([]),
minLineLength=minLineLength, maxLineGap=100)
a, b, c = lines.shape
for i in range(a):
x1, y1, x2, y2 = lines[i][0][0], lines[i][0][1], lines[i][0][2], lines[i][0][3]
cv2.line(dest, (x1, y1), (x2, y2),255, 1, cv2.LINE_AA)
src = cv2.convertScaleAbs(dest)
def findVerticalLines(img):
imgX = cv2.GaussianBlur(img, (5, 5), 0)
kernelx = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 10))
imgY = cv2.Sobel(img, cv2.CV_64F, 1, 0)
imgY = cv2.convertScaleAbs(imgY)
cv2.normalize(imgY, imgY, 0, 255, cv2.NORM_MINMAX)
imgY = cv2.morphologyEx(imgY, cv2.MORPH_CLOSE, kernelx, iterations=1)
return imgY
def findHorizontalLines(img):
"""same as above only args different"""
img1 = np.zeros(img.shape)
edges = cv2.Canny(img, 50, 150, apertureSize=3)
laplacian = cv2.Laplacian(edges, cv2.CV_64F)
drawLines(laplacian, img1, iteration=1)
sby = findVerticalLines(img1)
sbx = findHorizontalLines(img1)
return img1
def solveSudoku(self):
gray = self.__preProcess(self.__originalImg)
masked = self.__maskSudoku(gray)
grid = self.__dectactGrid(masked)
if __name__ == '__main__':
colorImg = cv2.imread('sudoku1.jpg')
solver = SudokuSolverPlay(colorImg)
solver.solveSudoku()
here findVerticalLines()
and findHorizontalLines()
are not able to dictect horizontal and vertical lines properly