Exemple #1
0
def handleSquareCells(origCropedImage, squares, triangles):
    blockedCells, regularCells = [], []
    image = convertToGray(origCropedImage.copy())

    if (False):
        stam = convertToColor(image.copy())
        stam = cv2.drawContours(stam, squares, -1, (255, 0, 0), 3)
        show(stam)

    # excluding all lines and other contours which are not cell square
    nativeSquares = list(
        filter(lambda x: not containedByOtherContour(x, squares), squares))
    # getting all squares which doesn't contain triangles
    nativeSquares = list(
        filter(lambda x: not containsAnyContour(x, triangles), nativeSquares))
    if (False):
        stam = convertToColor(image.copy())
        stam = cv2.drawContours(stam, nativeSquares, -1, (255, 0, 0), 3)
        show(stam)
    # getting all square contours
    nativeSquares = list(
        filter(
            lambda x: not checkIfFarBiggerThanAreaSize(
                image.shape[0] * image.shape[1], x), nativeSquares))

    ret, thresh = cv2.threshold(image, 170, 255, cv2.THRESH_BINARY)
    border = 5

    for square in nativeSquares:
        if (False):
            stam = convertToColor(image.copy())
            stam = cv2.drawContours(stam, [square], -1, (255, 0, 0), 3)
            show(stam)

        x, y, w, h = getRect(square)
        cell = thresh[y + border:y + h - border, x + border:x + w - border]

        if percentageOfWhitePixels(cell) > 30:
            regularCells.append(square)
        else:
            blockedCells.append(square)

    return blockedCells, regularCells
Exemple #2
0
def preProcess(image):
    #show(image)
    #TODO: delete this line
    image = convertToGray(image)
    #show(image)

    #image = dilate(image)
    #show(image)

    image = thresholdify(image)
    #show(image)
    #show(image)
    image = cv2.GaussianBlur(image, (7, 7), 0)
    #show(image)
    #show(image)
    #kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2)) #TODO: was 2,2
    #image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
    #image = dilate(thresh)
    #show(image)
    return image
Exemple #3
0
def convertSemiCellsToCells(image):
    image = convertToGray(image)
    #show(image)
    image = postForTriangles(image)
    #show(image)
    #TODO: no converting to color
    if (False):
        stam1 = getAllSquares(getAllContours(image))
        stam = convertToColor(image)
        stam = cv2.drawContours(stam, stam1, -1, (0, 255, 0), 5)
        show(stam)

    # getting all triangle contours
    triangle_contours = getAllTriangles(getAllContours(image))

    if (False):
        stam = convertToColor(image)
        stam = cv2.drawContours(stam, triangle_contours, -1, (0, 255, 0), 5)
        show(stam)

    if (len(triangle_contours) == 0):
        return image, triangle_contours
    # filter contours very below the average (noise contour)
    contourAvgSize = sum(cv2.contourArea(item)
                         for item in triangle_contours) / float(
                             len(triangle_contours))
    triangle_contours = list(
        filter(lambda x: not checkIfVeryBelowAreaSize(contourAvgSize, x),
               triangle_contours))

    if (False):
        simage = convertToColor(image)
        simage = cv2.drawContours(simage, triangle_contours, -1, (0, 255, 0),
                                  5)
        show(simage)

    onlyTriangleSquares = drawSquaresOnTriangleCells(image, triangle_contours)
    return onlyTriangleSquares, triangle_contours
Exemple #4
0
def drawSquare(image, topLeft, topRight, bottomRight, bottomLeft):
    colorOut = (255, 255, 255)  # white
    colorIn = (0, 0, 0)  # black
    outWidth = SAFETY_PIXEL_WIDTH
    inWidth = SAFETY_PIXEL_WIDTH

    image = convertToColor(image)

    # Drawing the outer border
    # top right to bottom right
    drawLine(image, topRight, bottomRight, colorOut, outWidth)
    # bottom right to bottom left
    drawLine(image, bottomRight, bottomLeft, colorOut, outWidth)
    # bottom left to top left
    drawLine(image, bottomLeft, topLeft, colorOut, outWidth)
    # top left to top right
    drawLine(image, topLeft, topRight, colorOut, outWidth)

    # Drawing the inner border
    # top right to bottom right
    drawLine(image, (topRight[0] - outWidth, topRight[1] + outWidth),
             (bottomRight[0] - outWidth, bottomRight[1] - outWidth), colorIn,
             inWidth)
    # bottom right to bottom left
    drawLine(image, (bottomRight[0] - outWidth, bottomRight[1] - outWidth),
             (bottomLeft[0] + outWidth, bottomLeft[1] - outWidth), colorIn,
             inWidth)
    # bottom left to top left
    drawLine(image, (bottomLeft[0] + outWidth, bottomLeft[1] - outWidth),
             (topLeft[0] + outWidth, topLeft[1] + outWidth), colorIn, inWidth)
    # top left to top right
    drawLine(image, (topLeft[0] + outWidth, topLeft[1] + outWidth),
             (topRight[0] - outWidth, topRight[1] + outWidth), colorIn,
             inWidth)

    image = convertToGray(image)
    return image
Exemple #5
0
def getGrid(image, mode):
    boardCopy = image.copy()
    # Handling semi cells (triangles)
    triangles, trianglesDivided = convertSemiCellsToCells(boardCopy.copy())

    if (False):
        # image = convertToColor(image)
        ssimage = cv2.drawContours(boardCopy, triangles, -1, (255, 0, 0), 3)
        show(ssimage)

    if (len(triangles) == 0):
        #print("Invalid board. number of triangles is: " + str(len(triangles) / 2))
        isSquareBoard = False
        return isSquareBoard, None, None, None

    #image = convertToGray(boardCopy)
    #image = threshPost(image)#threshPostAllSquares(image)
    image = postForTriangles(convertToGray(image))
    #show(image)
    # Handling square cells
    boardSize = image.shape[0] * image.shape[1]
    # getting all square contours
    square_contours = getAllSquares(getAllContours(image))
    # filter the board contour if exists
    square_contours = list(
        filter(lambda x: not checkIfFarBiggerThanAreaSize(boardSize, x),
               square_contours))
    square_contours = list(
        filter(lambda x: areaBiggerThan(20 * 20, x), square_contours))
    # filter contours very below the average (noise contour)
    contourAvgSize = sum(cv2.contourArea(item)
                         for item in square_contours) / float(
                             len(square_contours))
    regularCells = list(
        filter(lambda x: not checkIfVeryBelowAreaSize(contourAvgSize, x),
               square_contours))

    if (False):
        # image = convertToColor(image)
        ssimage = cv2.drawContours(boardCopy, regularCells, -1, (255, 0, 0), 3)
        show(ssimage)

    image = postForBlocked(convertToGray(boardCopy), 90, mode)
    #show(image)
    # getting all square contours
    blockedCells = getAllSquares(getAllContours(image))
    if (False):
        stam = cv2.drawContours(boardCopy, blockedCells, -1, (0, 255, 0), 5)
        show(stam)
    # filter the board contour if exists
    blockedCells = list(
        filter(lambda x: not checkIfFarBiggerThanAreaSize(boardSize, x),
               blockedCells))
    # filter contours very below the average (noise contour)
    contourAvgSize = sum(cv2.contourArea(item)
                         for item in blockedCells) / float(len(blockedCells))
    blockedCells = list(
        filter(lambda x: not checkIfVeryBelowAreaSize(contourAvgSize, x),
               blockedCells))
    # excluding other squares
    blockedCells = list(
        filter(lambda x: not containsAnyContour(x, regularCells),
               blockedCells))

    if (False):
        stam = cv2.drawContours(boardCopy, blockedCells, -1, (0, 255, 0), 5)
        show(stam)

    allCells = blockedCells + regularCells + triangles

    rootSize = math.sqrt(len(allCells))
    kakuroSize = int(rootSize)
    if (rootSize != kakuroSize):
        #print("Invalid board.")
        #print("number of regular squares is: " + str(len(regularCells)))
        #print("number of blocking squares is: " + str(len(blockedCells)))
        #print("number of triangles is: " + str(len(triangles) / 2))
        isSquareBoard = False
        return isSquareBoard, None, None, None
    else:
        isSquareBoard = True
        #print("The board is square of " + str(kakuroSize) + "x" + str(kakuroSize))

    if (kakuroSize != 9):
        isSquareBoard = True
        return isSquareBoard, None, None, getSolvedJson(kakuroSize)

    gridCells = getBoardGrid(kakuroSize, allCells)

    if gridCells == None:
        isSquareBoard = False
        return isSquareBoard, None, None, None

    mnistCells = []

    boardCells = []
    for i in range(0, kakuroSize):
        lineCells = []

        for j in range(0, kakuroSize):
            alon = (i, j)
            result = readCellFromImage(
                boardCopy, image, gridCells[i][j],
                (regularCells, blockedCells, trianglesDivided), alon)

            if (result['valid'] == False):
                #print("Invalid cell on [" + str(i + 1) + "][" + str(j + 1) + "]")
                isSquareBoard = False
                return isSquareBoard, None, None, None
            else:
                if ('block' in result):
                    lineCells.append({'block': True})
                else:
                    cell = result['cell']
                    if (cell['value']['hasValue'] == True):
                        lineCells.append({
                            'cellType': cell['cellType'],
                            'value': cell['value']
                        })
                    else:
                        mnistCell = (i, j, cell)
                        mnistCells.append(mnistCell)
                        lineCells.append(None)

        boardCells.append(lineCells)

    mnistResults = getDigitsFromImages(mnistCells)

    for cell in mnistResults:
        i, j, cellType, value = cell['row'], cell['col'], cell['type'], cell[
            'value']
        if (boardCells[i][j] == None):
            boardCell = {}

            if cellType == 'square':
                boardCell['cellType'] = 'square'
                boardCell['value'] = value
            elif cellType == 'upper':
                boardCell['cellType'] = 'triangle'
                boardCell['value'] = {'upper': {'data': value}}
            elif cellType == 'bottom':
                boardCell['cellType'] = 'triangle'
                boardCell['value'] = {'bottom': {'data': value}}

            boardCells[i][j] = boardCell
        else:
            if (cellType == 'square' or
                (cellType == 'upper' and 'upper' in boardCells[i][j]['value'])
                    or (cellType == 'bottom'
                        and 'bottom' in boardCells[i][j]['value'])):
                print('Wrong cell input.')
                isSquareBoard = False
                return isSquareBoard, None, None, None
            elif (cellType == 'upper'):
                boardCells[i][j]['value']['upper'] = {'data': value}
            elif (cellType == 'bottom'):
                boardCells[i][j]['value']['bottom'] = {'data': value}

    return isSquareBoard, boardCells, image, None
Exemple #6
0
def handleTriangleImage(origCroped, image, contour, minX, minY, alon):
    origGray = convertToGray(origCroped)
    if (True):
        # since we draw a square outside the triangle, we need to look for it's inner contours
        kernel = np.ones((3, 3), np.uint8)
        #image = cv2.erode(image, kernel, iterations=3)
        #image = cv2.GaussianBlur(image, (3, 3), 0)
        #show(image)
        stam = thresholdify(convertToGray(origCroped))
        stam = cv2.GaussianBlur(stam, (3, 3), 0)
        if (alon[0] == 2 and alon[1] == 0):
            a = 5
            #show(stam)

    digitContours = getAllContours(stam)
    # excluding all lines and other contours which are not cell square
    digitContours = list(
        filter(lambda x: not containedByOtherContour(x, digitContours),
               digitContours))

    digits = []
    for digitContour in digitContours:
        (x, y, w, h) = rect = getRect(digitContour)

        digitHeightInPercent, digitWidthInPercent = h / image.shape[
            0], w / image.shape[1]
        # not the crossing line of the triangle
        if ((digitWidthInPercent > 0.10 and digitWidthInPercent < 0.4) and
            (digitHeightInPercent > 0.10 and digitHeightInPercent < 0.7)
                and (x > 5 and y > 5)):
            # todo: debug
            if (alon[0] == 2 and alon[1] == 0):
                stam1 = convertToColor(stam)
                cv2.drawContours(stam1, [digitContour], -1, (255, 0, 0), 5)
                #show(stam1)
            digitCenter = getContourCenter(digitContour)
            # since we croped, we want to test the original image X,Y of the contour
            origDigitCenter = (digitCenter[0] + minX, digitCenter[1] + minY)
            # TODO: delete these 4 lines
            # cv2.drawContours(croped, [digitContour], -1, (0, 0, 0), 5)
            # show(croped)

            if (isPointInContour(origDigitCenter, contour)):
                global alonW
                global alonH
                alonW.append(digitWidthInPercent)
                alonH.append(digitHeightInPercent)
                digits.append({'contour': digitContour, 'rect': rect})

    # sorting the digits from the left to the right (x axis)
    digits = sorted(digits, key=lambda x: x['rect'][0])
    # todo: delete imageRect references
    #(imageX, imageY, w, h) = imageRect
    safeBorder = 3
    digitsWithBorder = []
    for digit in digits:
        (x, y, w, h) = digit['rect']
        digitImage = image[y - safeBorder:y + h + safeBorder,
                           x - safeBorder:x + w + safeBorder]

        if (False):
            p = cv2.GaussianBlur(digitImage, (7, 7), 0)
            thresh = cv2.adaptiveThreshold(p.astype(np.uint8), 255,
                                           cv2.ADAPTIVE_THRESH_MEAN_C,
                                           cv2.THRESH_BINARY, 11, 10)
            # 3 #TODO: was 11,10 or 11,7 or 5,2
            #show(digitImage)
            #p = putDigitInCenter(digitImage)
            #show(p)
            #value = getDigitsFromMNIST([p])
            #show(p, str(value[0]))
        # show(digitImage)
        # since we croped the digit from the croped image (minY)
        # since we croped the board from the original image (imageY). same goes for X
        # digitImage = origImage[y + minY + imageY - safeBorder: y + h + minY + imageY + safeBorder,
        #                       x + minX + imageX - safeBorder: x + w + minX + imageX + safeBorder]
        # show(255 - digitImage)
        # digitImage = convertToGray(255 - digitImage)

        digitImage = putDigitInCenter(digitImage)
        digitImage = cv2.resize(digitImage, (sizeToMNIST, sizeToMNIST))
        #show(digitImage)
        #thresh = cv2.adaptiveThreshold(digitImage.astype(np.uint8), 255, cv2.ADAPTIVE_THRESH_MEAN_C,
        #cv2.THRESH_BINARY, 3, 10)
        #digitImage = cv2.GaussianBlur(digitImage, (7, 7), 0)
        #digitImage = cv2.blur(digitImage, (3, 3))
        digitImage = cv2.bilateralFilter(digitImage, 17, 75, 75)
        #show(digitImage)
        #show (thresh)
        #show(digitImage)
        digitsWithBorder.append(digitImage)

    if (len(digitsWithBorder) == 0):
        return {'hasValue': True, 'data': None}
    else:
        return {'hasValue': False, 'data': digitsWithBorder}