matlab函数

matlab函数,第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'])

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/715322.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存