matlab函数(bwconncomp)的python实现

6 篇文章 0 订阅
4 篇文章 1 订阅

Matlab 中的 bwconncomp 函数

bwconncomp 函数用于查找二值图像中的连通分量.

  • 语法
CC = bwconncomp(BW)
CC = bwconncomp(BW,conn)
  • 说明

CC = bwconncomp(BW) 返回在二值图像 BW 中找到的连通分量 CCbwconncomp 默认对二维使用 8 连通,对三维使用 26 连通,对更高维使用 conndef(ndims(BW),'maximal') 连通。

CC = bwconncomp(BW,conn) 返回连通分量,其中 conn 指定连通分量所需的连通性。

详细说明请参见 bwconncomp的帮助文档.

python实现

以下代码只实现了二维图像的bwconncomp 函数.

import numpy as np

def bwconncomp_2d(bw : np.ndarray, conn):
    PixelIdxList = []
    mask = np.zeros(bw.shape, dtype=np.bool8)

    while np.any(bw != mask):
        BW = bw != mask
        r0 = -1
        c0 = -1
        for r in range(BW.shape[0]):
            for c in range(BW.shape[1]):
                if BW[r, c] == True:
                    r0, c0 = r, c
                    break
            if r0 != -1:
                break

        idxlist = [(r0,c0)]
        
        mask[r0,c0] = True
        k = 0
        while k < len(idxlist):
            r,c = idxlist[k]
            if conn in (4, 8):
                if r - 1 >= 0 and BW[r-1, c] == True and (r-1, c) not in idxlist:
                    idxlist.append((r-1, c))
                    mask[r-1,c] = True
                if c - 1 >= 0 and BW[r, c-1] == True and (r, c-1) not in idxlist:
                    idxlist.append((r, c-1))
                    mask[r,c-1] = True
                if r + 1 < BW.shape[0] and BW[r+1, c] == True and (r+1, c) not in idxlist:
                    idxlist.append((r+1, c))
                    mask[r+1,c] = True
                if c+1 < BW.shape[1] and BW[r, c+1] == True and (r, c+1) not in idxlist:
                    idxlist.append((r, c+1))
                    mask[r,c+1] = True
            if conn == 8:
                if r - 1 >= 0:
                    if c-1 >= 0 and BW[r-1, c-1] == True and (r-1, c-1) not in idxlist:
                        idxlist.append((r-1, c-1))
                        mask[r-1,c-1] = True
                    if c+1 < BW.shape[1] and BW[r-1, c+1] == True and (r-1, c+1) not in idxlist:
                        idxlist.append((r-1, c+1))
                        mask[r-1,c+1] = True
                if r+1 < BW.shape[0]:
                    if c-1 >= 0 and BW[r+1, c-1] == True and (r+1, c-1) not in idxlist:
                        idxlist.append((r+1, c-1))
                        mask[r+1,c-1] = True
                    if c+1 < BW.shape[1] and BW[r+1, c+1] == True and (r+1, c+1) not in idxlist:
                        idxlist.append((r+1, c+1))
                        mask[r+1,c+1] = True
                    
            k += 1
        
        a = np.array(idxlist, dtype=np.int64)

        PixelIdxList.append((a[:,0], a[:,1]))
        
    return PixelIdxList

def bwconncomp(BW: np.ndarray, conn=None):
    """ 查找二值图像中的连通分量

    Parameters
    ----------
    BW : array_like
        二值图像.
    conn : int
        连通分量所需的连通性.

    Returns
    -------
    CC : dict
    - 'Connectivity' :  等于 conn
    - 'ImageSize' : BW.shape
    - 'NumObjects' : 连通分量的数量
    - 'PixelIdxList' : list, 每个元素为连通分量的索引

    Examples
    --------
    >>> import numpy as np
    >>> BW = np.array([[1,1, 0], [0,0,0], [0, 1, 1]])
    >>> CC = bwconncomp(BW, conn=8) 
    >>> CC['PixelIdxList']
    out: [(array([0, 0], dtype=int64), array([0, 1], dtype=int64)), 
        (array([2, 2], dtype=int64), array([1, 2], dtype=int64))]}

    """
    bw = BW.astype(np.bool8)
    if conn is None:
        if bw.ndim == 2:
            conn = 8
        elif bw.ndim == 3:
            conn = 26
        else:
            raise Exception('todo...')

    if bw.ndim == 2:
        PixelIdxList = bwconncomp_2d(bw, conn)
    else:
        raise Exception('todo...')
    CC = {'Connectivity' : conn,
        'ImageSize' : bw.shape,
        'NumObjects' : len(PixelIdxList),
        'PixelIdxList' : PixelIdxList}

        
    return CC

if __name__ == '__main__':
    BW = np.array([[1,1, 0], [0,0,0], [0, 1, 1]])
    cc = bwconncomp(BW, conn=8)
    print(cc['PixelIdxList'])
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

falwat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值