def split_cell_ragion(intersections, square_ragion):
    cell_contours = Points.get_quadrilaterals(intersections, SUDOKU_SIZE, SUDOKU_SIZE)

    threshed_square_ragion = square_ragion
    # blured_ragion = cv2.GaussianBlur(square_ragion, ksize=(5,5), sigmaX=0)
    # threshed_square_ragion = cv2.adaptiveThreshold(blured_ragion,WHITE,
    #     cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, blockSize=3, C=3)
    # test
    # Display.image(threshed_square_ragion)
    get_ragion_func = lambda c: Contour.get_rect_ragion(c, threshed_square_ragion)
    cell_ragions = map(get_ragion_func,  cell_contours)
    # from picture_sudoku.helpers.common import Resource
    # cv2.imwrite(Resource.get_path('test', 'sample_15_null_38_image.jpg'), cell_ragions[38])

    def adjust_one(the_ragion):
        # the_ragion = cv2.GaussianBlur(the_ragion, ksize=(5,5), sigmaX=0)
        # blockSize = the_ragion.shape[1]/2
        # if blockSize % 2 == 0:
        #     blockSize += 1
        # thresholded_ragion = cv2.adaptiveThreshold(the_ragion,WHITE,
        #     cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, blockSize=blockSize, C=2)
        # thresholded_ragion = Image.threshold_white_with_mean_percent(the_ragion, 0.70)
        threshold_value = Ragion.cal_threshold_value(the_ragion, square_ragion, 0.70)
        thresholded_ragion = Image.threshold_white(the_ragion, threshold_value)

        return thresholded_ragion
    cell_ragions = map(adjust_one, cell_ragions)
    # flag_test()
    # Display.ragions(cell_ragions)
    return cell_ragions
def find_square_ragion(gray_image):
    # for some big size pictures which need to blur,
    # but cannot using ksize=(5,5), since some picture will get wrong number value.
    blured_image = cv2.GaussianBlur(gray_image, ksize=(3,3), sigmaX=0)

    # Display.image(blured_image)
    threshed_image = cv2.adaptiveThreshold(blured_image,WHITE,
        cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, blockSize=7, C=2)

    # flag_test()
    # Display.image(threshed_image)

    ''' 
        It's very depend on the threshed_image.
    '''
    max_contour = find_max_contour(threshed_image)

    # Display.contours(gray_image, [max_contour])
    # max_contour.ppl()
    # 'test'.pl()
    # from picture_sudoku.helpers.common import Resource
    # numpy.save(Resource.get_path("for_issues/max_contour.npy"), max_contour)
    # square_contour = convert_to_square(max_contour)
    # this is using the border line.
    square_contour = extract_square_from_contour(max_contour)
    # square_contour.ppl()

    if square_contour == None:
        raise SudokuError("Cannot find sudoku square!")
    # flag_test()
    # Display.contours(gray_image, [square_contour])

    larged_contour = Quadrilateral.enlarge(square_contour, 0.007)
    larged_contour = Contour.check_beyond_borders(larged_contour, gray_image.shape)
    # Display.contours(gray_image, [larged_contour])
    # larged_contour.ppl()

    square_ragion = Contour.get_rect_ragion(larged_contour, gray_image)
    # Display.image(square_ragion)

    return square_ragion
def extract_square_from_contour(contour):
    the_image = Image.generate_mask(Contour.get_shape(contour))
    # Display.contours(the_image, [contour])
    cv2.drawContours(the_image, [contour], -1, 255, 1)
    # Display.image(the_image)
    # lines = PolarLines.find_suitable_lines(the_image)
    square_ragion = the_image
    vertical_lines = find_vertical_lines(square_ragion)
    border_line_count = 2
    if len(vertical_lines) > border_line_count:
        raise SudokuError("The count of vertical border lines is larger than {0}".format(border_line_count))

    horizontal_lines = find_horizontal_lines(square_ragion)
    if len(horizontal_lines) > border_line_count:
        raise SudokuError("The count of horizontal border lines is larger than {0}".format(border_line_count))
    Display.polar_lines(square_ragion, vertical_lines + horizontal_lines)
    # Display.polar_lines(square_ragion, horizontal_lines)

    intersections = main_analyzer.find_intersections(vertical_lines, horizontal_lines)
    # intersections.ppl()
    Points.to_contour(intersections).ppl()
def extract_square_from_contour(contour):
    '''
        it will extract square from the contour which could have many noise points.
    '''
    the_image = Image.generate_mask(Contour.get_shape(contour))
    # Display.contours(the_image, [contour])
    cv2.drawContours(the_image, [contour], -1, 255 ,1)
    # Display.image(the_image)
    # lines = PolarLines.find_suitable_lines(the_image)
    square_ragion = the_image
    vertical_lines = find_vertical_lines(square_ragion)

    horizontal_lines = find_horizontal_lines(square_ragion)
    # flag_test()
    # Display.polar_lines(square_ragion, vertical_lines+horizontal_lines)

    border_line_count = 2
    if len(vertical_lines) > border_line_count:
        logger.info("The count of vertical border lines is larger than {0}"
            .format(border_line_count))
        vertical_lines = [vertical_lines[0],vertical_lines[-1]]
    if len(horizontal_lines) > border_line_count:
        logger.info("The count of horizontal border lines is larger than {0}"
            .format(border_line_count))
        horizontal_lines = [horizontal_lines[0],horizontal_lines[-1]]

    # flag_test()
    # Display.polar_lines(square_ragion, vertical_lines+horizontal_lines)

    intersections = find_intersections(vertical_lines, horizontal_lines)
    # intersections.ppl()
    square_contour =  Points.to_contour(intersections)
    # order the points
    square_contour = Points.to_contour(Quadrilateral.vertices(square_contour))
    # 'test'.pl()
    # Display.contours(the_image,[square_contour])
    return square_contour