空洞填充

本文介绍了一种基于OpenCV的空洞填充算法实现方法。该方法通过复制阈值图像并使用比原始图像尺寸大两像素的掩膜进行洪水填充,然后反转洪水填充图像并与原始图像结合来获得前景区域,从而实现对二值图像中空洞的有效填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import numpy as np

import cv2



def fillholse(im_th):

    '''

    空洞填充

    param:

        im_th:二值图像

    return:

        im_out:填充好的图像

    '''

    # Copy the thresholded image.

    im_floodfill = im_th.copy()



    # Mask used to flood filling.

    # Notice the size needs to be 2 pixels than the image.

    h, w = im_th.shape[:2]

    mask = np.zeros((h + 2, w + 2), np.uint8)



    # Floodfill from point (0, 0)

    retval, image, mask, rect = cv2.floodFill(im_floodfill, mask, (0, 0), 255)



    # Invert floodfilled image

    im_floodfill_inv = cv2.bitwise_not(im_floodfill)



    # Combine the two images to get the foreground.

    im_out = im_th | im_floodfill_inv

    return im_out

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值