示例#1
0
def drawSquaresOnTriangleCells(image, triangle_contours):
    # Converting the triangle contours into squares
    for cnt in triangle_contours:
        # todo: delete
        if (False):
            simage = convertToColor(image)
            simage = cv2.drawContours(simage, [cnt], -1, (0, 255, 0), 5)
            show(simage)

        # curr vars
        cX, cY = getContourCenter(cnt)
        approx = getContourApprox(cnt)
        middleVertex, isUpper = getMiddleVertex(approx, (cX, cY))

        # twin
        twin = getTwinContour(cnt, triangle_contours)
        twinCenterX, twinCenterY = getContourCenter(twin)
        twinApprox = getContourApprox(twin)
        twinMiddleVertex, twinIsUpper = getMiddleVertex(
            twinApprox, (twinCenterX, twinCenterY))

        # upper
        if (isUpper and not twinIsUpper):
            rightVertex = getRightVertex(approx, middleVertex)
            leftVertex = getLeftVertex(approx, middleVertex)

            twinRightVertex = getRightVertex(twinApprox, twinMiddleVertex)
            twinLeftVertex = getLeftVertex(twinApprox, twinMiddleVertex)

            topLeft = getTopLeft(leftVertex, twinLeftVertex)
            bottomRight = getBottomRight(rightVertex, twinRightVertex)
            # Drawing a square
            image = drawSquare(image, topLeft, middleVertex[0], bottomRight,
                               twinMiddleVertex[0])

            # cv2.circle(image, (topLeft[0], topLeft[1]), 20, (255, 0, 0), -1)
            # cv2.circle(image, (bottomRight[0], bottomRight[1]), 20, (0, 0, 255), -1)
            # lower
            # else:
            # cv2.circle(image, (middleVertex[0][0], middleVertex[0][1]), 20, (255, 255, 0), -1)
            # Handling square cells

    boardSize = image.shape[0] * image.shape[1]
    # getting all square contours
    square_contours = getAllSquares(getAllContours(image))
    square_contours = list(
        filter(lambda x: containsAnyContour(x, triangle_contours),
               square_contours))
    # filter the board contour if exists
    square_contours = list(
        filter(lambda x: not checkIfFarBiggerThanAreaSize(boardSize, 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))
    square_contours = list(
        filter(lambda x: not checkIfVeryBelowAreaSize(contourAvgSize, x),
               square_contours))
    return square_contours
示例#2
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
示例#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
示例#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
示例#5
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}