【问题标题】:Calculate the number of conenected pixels of a particular value in Numpy/OpenCV计算 Numpy/OpenCV 中特定值的连接像素数
【发布时间】:2021-02-22 06:59:33
【问题描述】:
def get_area(center_x: int, center_y: int, mask: np.ndarray) -> int:
    if mask[center_x][center_y] != 255:
        return -1
    return ...

现在我得到了上面的这个函数,它接受一个 x 和 y 的值,并找到与这个值为 255 的像素相连的像素数。

现在假设,我有一个简单的 np.ndarray,如下所示:

[
    [255,255,  0,  0,  0,  0,  0,255,255],
    [255,  0,  0,255,255,255,  0,  0,255],
    [  0,  0,255,  0,  0,  0,255,  0,  0],
    [  0,255,  0,  0,255,  0,  0,255,  0],
    [  0,255,  0,255,255,255,  0,255,  0],
    [  0,255,  0,  0,255,  0,  0,255,  0],
    [  0,  0,255,  0,  0,  0,255,  0,  0],
    [255,  0,  0,255,255,255,  0,  0,255],
    [255,255,  0,  0,  0,  0,  0,255,255]
]

如果我将 255 的中心像素作为输入,我尝试构建的函数的输出将为 5,因为有 4 个相邻像素为 255s。

我可以同时使用opencvnumpy,但np 更可取。

【问题讨论】:

    标签: python-3.x opencv numpy-ndarray


    【解决方案1】:
    from PIL import Image
    import numpy as np
    from scipy import ndimage
    
    imgMtx = [
        [255,255,  0,  0,  0,  0,  0,255,255],
        [255,  0,  0,255,255,255,  0,  0,255],
        [  0,  0,255,  0,  0,  0,255,  0,  0],
        [  0,255,  0,  0,255,  0,  0,255,  0],
        [  0,255,  0,255,255,255,  0,255,  0],
        [  0,255,  0,  0,255,  0,  0,255,  0],
        [  0,  0,255,  0,  0,  0,255,  0,  0],
        [255,  0,  0,255,255,255,  0,  0,255],
        [255,255,  0,  0,  0,  0,  0,255,255]
    ]
    
    img = Image.fromarray(np.asarray(imgMtx))
    
    blobs = np.asarray(img) > 125
    labels, nlabels = ndimage.label(blobs)
    
    unique, counts = np.unique(labels, return_counts=True)
    

    【讨论】:

    • 没有 scipy 我们可以做到这一点吗?
    • 和 PIL?我认为为 BFS + numpy 任务导入这样的重量级库有点矫枉过正。
    【解决方案2】:
    import numpy as np
    
    def get_area(center_x: int, center_y: int, mask: np.ndarray) -> int:
        """
        Basic flood-fill area calculation on the mask using Breadth First Search
        """
    
        area = 0
        moves = [(1, 1), (0, 1), (1, 0), (-1, -1), (0, -1), (-1, 0), (-1, 1), (1, -1)]
    
        if mask[center_x][center_y] != 255:
            return -1
    
        visited = [[False for _ in range(mask.shape[0])] for _ in range(mask.shape[1])]
        q = deque()
        q.append((center_x, center_y))
        visited[center_x][center_y] = True
    
        while q:
            x, y = q.popleft()
            for move in moves:
                if (x + move[0] >= mask.shape[0] or y + move[1] >= mask.shape[1] or
                        x + move[0] < 0 or y + move[1] < 0):
                    continue
                if mask[x + move[0]][y + move[1]] == 255 and not visited[x + move[0]][y + move[1]]:
                    area += 1
                    q.append((x + move[0], y + move[1]))
                    visited[x + move[0]][y + move[1]] = True
    
        return area
    

    决定自己编写一个答案,并在提供的掩码上进行广度优先搜索填充算法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 1970-01-01
      • 2012-04-27
      • 2023-04-04
      • 2014-05-22
      • 2019-07-03
      • 1970-01-01
      相关资源
      最近更新 更多