def find_sudoku_number_binary_arr(gray_pic_arr): ''' Find all numbers from a picture in which there is a soduku puzzle. The number form is a binary numpy.array in which number parts are 1, the others are 0. ''' ''' notice: the threshold_value is the key, if it directly impact the binary matrix. ''' threshed_pic_array = cv2_helper.threshold_white_with_mean_percent(gray_pic_arr,0.8) cv2_helper.show_pic(threshed_pic_array) square = find_max_square(threshed_pic_array) # cv2_helper.show_contours_in_pic(threshed_pic_array, [square]) square_rect=cv2.boundingRect(square) number_rects = cv2_helper.Rect.cal_split_ragion_rects(square_rect, 9, 9) # cv2_helper.show_rects_in_pic(gray_pic_arr, number_rects) binary_pic = numpy_helper.transfer_values_quickly(threshed_pic_array, {BLACK:0, WHITE:1}) number_binary_ragions = map(lambda c: cv2_helper.get_rect_ragion_with_rect(binary_pic, c), number_rects) number_binary_ragions = map(remove_border, number_binary_ragions) non_empty_indexs, number_binary_ragions = get_nonzero_ragions_and_indexs(number_binary_ragions) # indexs.pp() # cv2_helper.show_same_size_ragions_as_pic(number_binary_ragions, 9) number_binary_ragions = map(remove_margin, number_binary_ragions) number_binary_ragions = map(enlarge, number_binary_ragions) # cv2_helper.show_pic(threshed_pic_array) # cv2_helper.show_same_size_ragions_as_pic(number_binary_ragions, 9) return number_binary_ragions, non_empty_indexs
with test("is_almost_square"): contour = numpy.array([[[ 671, 421]], [[ 78, 426]], [[ 85, 1016]], [[ 675, 1012]]]) is_almost_square(contour).must_equal(True) contour = numpy.array([[[ 671, 421]], [[ 128, 426]], [[ 85, 1016]], [[ 675, 1012]]]) is_almost_square(contour).must_equal(False) with test("find_max_square"): current_pic_arr = cv2_helper.threshold_white_with_mean_percent(gray_arr) max_square = find_max_square(current_pic_arr) max_square.must_equal(numpy.array([[[ 671, 421]], [[ 78, 426]], [[ 85, 1016]], [[ 675, 1012]]]), numpy.allclose) (cv2.arcLength(max_square,True) > 2300).must_equal(True) pass # with test("show max square in full pic"): # current_pic_arr = color_arr # cv2.drawContours(current_pic_arr,[max_square],-1,(0,255,255),1) # cv2_helper.show_pic(current_pic_arr)