def gen_cell_tuple(para_tuple):
     index, cell_ragion = para_tuple
     cell_rect = nonzero_rect.analyze_from_center(cell_ragion)
     if cell_rect:
         cell_ragion = Rect.get_ragion(cell_rect, cell_ragion)
         return (index, cell_ragion)
     return False
def generate_first_rect(the_ragion):
    ragion_height, ragion_width = the_ragion.shape

    centroid = Image.centroid(the_ragion)
    single_width = int(round(ragion_width*0.16))
    single_height = int(round(ragion_height*0.16))
    # (single_width, single_height).ppl()
    return Rect.cal_center_rect(centroid, single_width, single_width, single_height, single_height)
        # the_ragion.mean().ppl()
        # the_ragion.ppl()
        # thresholded_ragion = Image.threshold_white_with_mean_percent(the_ragion, 0.8)
        # thresholded_ragion.ppl()
        # Display.image(thresholded_ragion)
        file_path = Resource.get_test_path("sample_15_square.jpg")
        square_ragion = cv2.imread(file_path, 0)
        # square_ragion.mean().ppl()

        threshold_value = Ragion.cal_threshold_value(the_ragion, square_ragion, 0.69)
        thresholded_ragion = Image.threshold_white(the_ragion, threshold_value)
        # thresholded_ragion = cv2.adaptiveThreshold(the_ragion, 255,
        #     cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, blockSize=7, C=2)
        cell_rect = nonzero_rect.analyze_from_center(thresholded_ragion)
        if cell_rect:
            cell_ragion = Rect.get_ragion(cell_rect, thresholded_ragion)
        cell_rect.pl()
        # Display.image(cell_ragion)

        file_path = Resource.get_test_path("sample_19_07_05_image.jpg")
        the_ragion = cv2.imread(file_path, 0)
        # the_ragion.mean().ppl()

        file_path = Resource.get_test_path("sample_19_square.jpg")
        square_ragion = cv2.imread(file_path, 0)
        # square_ragion.mean().ppl()

        threshold_value = Ragion.cal_threshold_value(the_ragion, square_ragion, 0.8)
        thresholded_ragion = Image.threshold_white(the_ragion, threshold_value)
        cell_rect = nonzero_rect.analyze_from_center(thresholded_ragion)
        if cell_rect:
def center_rect_enlarge_search(the_ragion):
    '''
        it has been deprecated, since it will take more time and 
        cannot get the whole rect in some case.
    '''
    ragion_height, ragion_width = the_ragion.shape

    centroid = Image.centroid(the_ragion)
    single_width = int(round(ragion_width*0.18))
    single_height = int(round(ragion_height*0.18))
    # (single_width, single_height).ppl()
    left_x, top_y, edge_width, edge_height = \
            Rect.cal_center_rect(centroid, single_width, single_width, single_height, single_height)
    right_x = left_x + edge_width - 1
    bottom_y = top_y + edge_height - 1
    # (left_x, top_y, edge_width, edge_height).ppl()

    top_nonzero = True
    bottom_nonzero = True
    left_nonzero = True
    right_nonzero = True
    while(top_nonzero or bottom_nonzero or left_nonzero or right_nonzero):
        # (left_x, top_y, right_x, bottom_y).ppl()
        # (left_nonzero, top_nonzero, right_nonzero, bottom_nonzero).ppl()
        # cur_rect = Rect.create(left_x, top_y, right_x, bottom_y)
        # Display.rect(the_ragion, cur_rect)

        if top_nonzero:
            top_nonzero = Rect.has_nonzero((left_x, top_y, right_x-left_x+1, 1),the_ragion)
            if top_nonzero:
                top_y -= 1
            if top_y <= 0:
                top_nonzero = False
                top_y = -1
        if bottom_nonzero:
            bottom_nonzero = Rect.has_nonzero((left_x, bottom_y, right_x-left_x+1, 1),the_ragion)
            if bottom_nonzero:
                bottom_y += 1
            if bottom_y >= ragion_height-1:
                bottom_nonzero = False
                bottom_y = ragion_height
        if left_nonzero:
            left_nonzero = Rect.has_nonzero((left_x, top_y, 1, bottom_y-top_y+1),the_ragion)
            if left_nonzero:
                left_x -= 1
            if left_x <= 0:
                left_nonzero = False
                left_x = -1
        if right_nonzero:
            right_nonzero = Rect.has_nonzero((right_x, top_y, 1, bottom_y-top_y+1),the_ragion)
            if right_nonzero:
                right_x += 1
            if right_x >= ragion_width-1:
                right_nonzero = False
                right_x = ragion_width

    final_top = top_y + 1
    final_bottom = bottom_y - 1
    final_left = left_x + 1
    final_right = right_x - 1
    return Rect.create(final_left, final_top, final_right, final_bottom)