blob.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:sudokuextract 作者: hbldh 项目源码 文件源码
def iter_blob_extremes(image, n=5):
    original_shape = image.shape[::-1]
    if max(original_shape) < 2000:
        size = (500, 500)
        y_scale = original_shape[0] / 500
        x_scale = original_shape[1] / 500
    else:
        size = (1000, 1000)
        y_scale = original_shape[0] / 1000
        x_scale = original_shape[1] / 1000

    img = resize(image, size)
    bimg = gaussian_filter(img, sigma=1.0)
    bimg = threshold_adaptive(bimg, 20, offset=2/255)
    bimg = -bimg
    bimg = ndi.binary_fill_holes(bimg)
    label_image = label(bimg, background=False)
    label_image += 1

    regions = regionprops(label_image)
    regions.sort(key=attrgetter('area'), reverse=True)
    iter_n = 0

    for region in regions:
        try:
            iter_n += 1
            if iter_n > n:
                break

            # Skip small images
            if region.area < int(np.prod(size) * 0.05):
                continue
            coords = get_contours(add_border(label_image == region.label,
                                             size=label_image.shape,
                                             border_size=1,
                                             background_value=False))[0]
            coords = np.fliplr(coords)

            top_left = sorted(coords, key=lambda x: np.linalg.norm(np.array(x)))[0]
            top_right = sorted(coords, key=lambda x: np.linalg.norm(np.array(x) - [img.shape[1], 0]))[0]
            bottom_left = sorted(coords, key=lambda x: np.linalg.norm(np.array(x) - [0, img.shape[0]]))[0]
            bottom_right = sorted(coords, key=lambda x: np.linalg.norm(np.array(x) - [img.shape[1], img.shape[0]]))[0]
            scaled_extremes = [(int(x[0] * y_scale), int(x[1]*x_scale)) for x in (top_left, top_right, bottom_left, bottom_right)]

            yield scaled_extremes
        except Exception:
            pass
    raise SudokuExtractError("No suitable blob could be found.")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号