python 消除二值图像中面积小于某个阈值的连通域(消除孤立点)

from skimage import measure
import cv2
import numpy as np

##
##image:二值图像
##threshold_point:符合面积条件大小的阈值
def remove_small_points(image,threshold_point):
    img = cv2.imread(image, 0)       #输入的二值图像
    img_label, num = measure.label(img, neighbors=8, return_num=True)#输出二值图像中所有的连通域
    props = measure.regionprops(img_label)#输出连通域的属性,包括面积等

    resMatrix = np.zeros(img_label.shape)
    for i in range(1, len(props)):
        if props[i].area > threshold_point:
            tmp = (img_label == i + 1).astype(np.uint8)
            resMatrix += tmp #组合所有符合条件的连通域
    resMatrix *= 255
    return resMatrix

res = remove_small_points(image,threshold_point)

使用了morphology.remove_small_objects但是不知道什么原因,效果不好。自己写一个吧。

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值