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)
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)