Пример #1
0
    def test_find_sudoku_sqare():
        pic_file_path = '../resource/example_pics/sample14.dataset.jpg'
        gray_pic_array = cv2.imread(pic_file_path, 0)
        color_pic_array  = cv2.imread(pic_file_path)
        gray_pic_array = cv2_helper.resize_with_fixed_height(gray_pic_array)
        color_pic_array = cv2_helper.resize_with_fixed_height(color_pic_array)
        # threshed_pic_array = cv2.adaptiveThreshold(gray_pic_array,WHITE,
        #     cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, 19, 2)
        ''' I've tried 3,3 19,2, 5,5 9,2'''
        threshed_pic_array = cv2.adaptiveThreshold(gray_pic_array,WHITE,
            cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, blockSize=5, C=2)
        # cv2_helper.show_pic(color_pic_array)
        cv2_helper.show_pic(threshed_pic_array)

        max_contour = find_sudoku_sqare(threshed_pic_array)
        cv2_helper.show_contours_in_pic(color_pic_array, [max_contour])
Пример #2
0
    def show_square(pic_file_path):
        ''' 7, 11 get wrong number, 8 clip too much, 9, 12, 13, 14 get more number,
            7 is so special, put it at last
            8 is almost ok

        '''
        pic_file_path = '../resource/example_pics/sample01.dataset.jpg'
        pic_file_path.pp()
        gray_pic_array = cv2.imread(pic_file_path, 0)
        color_pic_array  = cv2.imread(pic_file_path)
        gray_pic_array = cv2_helper.Image.resize_keeping_ratio_by_height(gray_pic_array)
        color_pic_array = cv2_helper.Image.resize_keeping_ratio_by_height(color_pic_array)
        # threshed_pic_array = cv2.adaptiveThreshold(gray_pic_array,WHITE,
        #     cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, 19, 2)
        threshed_pic_array = cv2.adaptiveThreshold(gray_pic_array,WHITE,
            cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, blockSize=7, C=2)

        max_contour = find_sudoku_sqare(threshed_pic_array)
        max_contour.pp()
        cv2_helper.show_contours_in_pic(color_pic_array, [max_contour])

        def warp_square(pic_array, approximated_square):
            ''' 
                let the approximated_square become a true square.
                
            '''
            square_rect = cv2.boundingRect(approximated_square)
            square_rect = cv2_helper.Rect.adjust_to_minimum(square_rect)
            # square_rect_contour = cv2_helper.rect_to_contour(square_rect)

            # approximated_square.pp()
            # square_rect_contour.pp()

            # approximated_square.shape is (4,1,2)
            approximated_square_float = numpy.float32(approximated_square.copy())
            approximated_square_float = cv2_helper.Quadrilateral.vertices(approximated_square_float)
            # square_rect_contour_float = numpy.float32(square_rect_contour.copy())
            square_rect_contour_float = numpy.float32(cv2_helper.Rect.vertices(square_rect))

            retval = cv2.getPerspectiveTransform(approximated_square_float,square_rect_contour_float)
            dsize = pic_array.shape[::-1]
            warp = cv2.warpPerspective(pic_array,retval,dsize)
            return warp, square_rect

        # four_endpoints = cv2_helper.Quadrilateral.vertices(max_contour)
        # # top_points = cv2_helper.Points.cal_step_points(four_endpoints[:2], 10)
        # internal_points = cv2_helper.Points.cal_internal_points(four_endpoints,10,10)
        # cv2_helper.Image.show_points_with_color(gray_pic_array, internal_points)


        # warp, max_rect = warp_square(gray_pic_array, max_contour)
        # cv2_helper.show_rect(warp, max_rect)
        # cv2_helper.show_pic(warp)
        # cv2_helper.show_pic(gray_pic_array)
        # cv2_helper.show_contours_in_pic(warp, [max_contour, rect_max_contour])

        # sub_warp = gray_pic_array
        sub_warp = cv2_helper.get_rect_ragion_with_contour(threshed_pic_array, max_contour)
        # sub_warp = cv2_helper.get_rect_ragion_with_rect(gray_pic_array, max_rect)
        cv2_helper.show_pic(sub_warp)

        sub_warp = remove_vertical_lines(sub_warp)
        sub_warp = remove_horizontal_lines(sub_warp)