首页 > 解决方案 > 在 numpy 数组中裁剪 ROI 区域

问题描述

我正在编写一些代码来切割 numpy 数组中的两个单独的 ROI 区域。该数组是一个带有布尔值的掩码数组,它由左右两个主要部分组成。

我需要从我原来的 numpy 数组中裁剪出左右部分。我的代码如下,它们是函数的一部分(图像和蒙版被传递给这个函数)

if mask.shape[-1] > 0:
    # We're treating all instances as one, so collapse the mask into one layer
    mask = (np.sum(mask, -1, keepdims=True) >= 1)
    zeros=np.zeros(image.shape)
    #splash = np.where(mask, image, gray).astype(np.uint8)
    splash = np.where(mask, image, zeros).astype(np.uint8)

我不确定如何实现这一点,因为我对颠簸真的很陌生。我可以飞溅图像,但我需要裁剪左右两个部分,为此我需要裁剪或重塑蒙版阵列。我已将飞溅的输出示例附加到此线程

颠簸溅起的图像

标签: pythonnumpy

解决方案


这是计算机视觉中一个非常典型的问题。一种流行的解决方案是连接组件标签(或 CCL)。OpenCV 已经有一个实现:

https://docs.opencv.org/3.4/d3/dc0/group__imgproc__shape.html#gaedef8c7340499ca391d459122e51bef5

然后您可以使用 blob 分析来裁剪对象:

https://docs.opencv.org/3.4/d0/d7a/classcv_1_1SimpleBlobDetector.html


推荐阅读