Beispiel #1
0
def kim2014(orig, O=None, split=False, n_points_w=None, n_tries=30):
    lib.debug_imwrite('gray.png', binarize.grayscale(orig))
    im = binarize.binarize(orig, algorithm=lambda im: binarize.sauvola_noisy(im, k=0.1))
    global bw
    bw = im

    im_h, im_w = im.shape

    AH, lines, _ = get_AH_lines(im)

    if O is None:
        O = np.array((im_w / 2.0, im_h / 2.0))

    if split:
        # Test if line start distribution is bimodal.
        line_xs = np.array([line.left() for line in lines])
        bimodal = line_xs.std() / im_w > 0.10
        dual = bimodal and im_w > im_h
    else:
        dual = False

    if dual:
        print('Bimodal! Splitting page!')
        pages = crop.split_lines(lines)

        n_points_w = 1.2 * np.percentile(np.array([line.width() for line in lines]), 90)
        n_points_w = max(n_points_w, 1800)

        if lib.debug:
            debug = cv2.cvtColor(bw, cv2.COLOR_GRAY2BGR)
            for page in pages:
                page_crop = Crop.from_lines(page).expand(0.005)
                # print(page_crop)
                page_crop.draw(debug)
            lib.debug_imwrite('split.png', debug)

        page_crops = [Crop.from_lines(page) for page in pages]
        if len(page_crops) == 2:
            [c0, c1] = page_crops
            split_x = (c0.x1 + c1.x0) / 2
            page_crops = [
                c0.union(Crop(0, 0, split_x, im_h)),
                c1.union(Crop(split_x, 0, im_w, im_h))
            ]

        result = []
        for i, (page, page_crop) in enumerate(zip(pages, page_crops)):
            print('==== PAGE {} ===='.format(i))
            lib.debug_prefix.append('page{}'.format(i))

            page_image = page_crop.apply(orig)
            page_bw = page_crop.apply(im)
            page_AH, page_lines, _ = get_AH_lines(page_bw)
            new_O = O - np.array((page_crop.x0, page_crop.y0))
            lib.debug_imwrite('precrop.png', im)
            lib.debug_imwrite('page.png', page_image)

            bw = page_bw
            dewarper = Kim2014(page_image, page_bw, page_lines, [page_lines],
                               new_O, page_AH, n_points_w)
            result.append(dewarper.run_retry()[0])

            lib.debug_prefix.pop()

        return result
    else:
        lib.debug_prefix.append('page0')
        dewarper = Kim2014(orig, im, lines, [lines], O, AH, n_points_w)
        lib.debug_prefix.pop()
        return dewarper.run_retry(n_tries=n_tries)
Beispiel #2
0
def process_image(original, dpi=None):
    original_rot90 = original

    for i in range(args.rotate / 90):
        original_rot90 = np.rot90(original_rot90)

    # original_rot90 = cv2.resize(original_rot90, (0, 0), None, 1.5, 1.5)
    im_h, im_w = original_rot90.shape[:2]
    # image height should be about 10 inches. round to 100
    if not dpi:
        dpi = int(round(im_h / 1100.0) * 100)
        print('detected dpi:', dpi)

    split = im_w > im_h # two pages

    cropped_images = []
    if args.dewarp:
        lib.debug_prefix.append('dewarp')
        dewarped_images = dewarp.kim2014(original_rot90)
        for im in dewarped_images:
            bw = binarize.binarize(im, algorithm=binarize.sauvola, resize=1.0)
            lib.debug_prefix.append('crop')
            _, [lines] = crop(im, bw, split=False)
            lib.debug_prefix.pop()
            c = Crop.from_lines(lines)
            if c.nonempty():
                cropped_images.append(Crop.from_whitespace(bw).apply(im))
        lib.debug_prefix.pop()
    else:
        bw = binarize.binarize(original_rot90, algorithm=binarize.adaptive_otsu, resize=1.0)
        debug_imwrite('thresholded.png', bw)
        AH, line_sets = crop(original_rot90, bw, split=split)

        for lines in line_sets:
            c = Crop.from_lines(lines)
            if c.nonempty():
                lib.debug = False
                bw_cropped = c.apply(bw)
                orig_cropped = c.apply(original_rot90)
                angle = algorithm.skew_angle(bw_cropped, original_rot90, AH, lines)
                if not np.isfinite(angle): angle = 0.
                rotated = algorithm.safe_rotate(orig_cropped, angle)

                rotated_bw = binarize.binarize(rotated, algorithm=binarize.adaptive_otsu)
                _, [new_lines] = crop(rotated, rotated_bw, split=False)

                # dewarped = algorithm.fine_dewarp(rotated, new_lines)
                # _, [new_lines] = crop(rotated, rotated_bw, split=False)
                new_crop = Crop.union_all([line.crop() for line in new_lines])

                if new_crop.nonempty():
                    # cropped = new_crop.apply(dewarped)
                    cropped = new_crop.apply(rotated)
                    cropped_images.append(cropped)

    out_images = []
    lib.debug_prefix.append('binarize')
    for i, cropped in enumerate(cropped_images):
        lib.debug_prefix.append('page{}'.format(i))
        if lib.is_bw(original_rot90):
            out_images.append(binarize.otsu(cropped))
        else:
            out_images.append(
                binarize.ng2014_fallback(binarize.grayscale(cropped))
            )
        lib.debug_prefix.pop()
    lib.debug_prefix.pop()

    return dpi, out_images