Numpy:获得与面具大小相同的矩形区域

时间:2016-08-29 13:00:27

标签: python opencv numpy

我有一张图片和一个面具。两者都是numpy数组。我通过GraphSegmentation(cv2.ximgproc.segmentation)获取掩码,因此该区域不是矩形,而是不分割。我想得到一个与蒙面区域大小相同的矩形,但我不知道有效的方法。

  

换句话说,未屏蔽的像素值为0,屏蔽像素的值大于0,所以我想得到一个矩形......

  • top =轴0的最小索引,其值> 0
  • bottom =轴0的最大索引,其值> 0
  • =其值>>的最小索引轴1 0
  • =最大索引轴1,其值> 0
  • 图片 = src [top:bottom,left:right]

我的代码低于

segmentation = cv2.ximgproc.segmentation.createGraphSegmentation()
src = cv2.imread('image_file')
segment = segmentation.processImage(src)
for i in range(np.max(segment)):
    dst = np.array(src)
    dst[segment != i] = 0
    cv2.imwrite('output_file', dst)

3 个答案:

答案 0 :(得分:3)

如果您更喜欢纯Numpy,可以使用np.wherenp.meshgrid实现此目的:

i, j = np.where(mask)
indices = np.meshgrid(np.arange(min(i), max(i) + 1),
                      np.arange(min(j), max(j) + 1),
                      indexing='ij')
sub_image = image[indices]

np.where返回一个数组元组,为mask的每个非零元素指定每个轴中的索引。然后,我们使用np.arange创建我们想要的所有行和列索引的数组,并使用np.meshgrid生成两个网格形状的数组,这些数组索引我们感兴趣的图像部分。请注意,我们使用index='ij'指定矩阵式索引,以避免必须转置结果(默认为笛卡尔式索引)。

基本上,meshgrid构建indices以便:

image[indices][a, b] == image[indices[0][a, b], indices[1][a, b]]

实施例

从以下开始:

>>> image = np.arange(12).reshape((4, 3))
>>> image
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

我们想要提取[[3,4],[6,7]]子矩阵,它是以下掩码的边界矩形:

>>> mask = np.array([[0,0,0],[0,1,0],[1,0,0],[0,0,0]])
>>> mask
array([[0, 0, 0],
       [0, 1, 0],
       [1, 0, 0],
       [0, 0, 0]])

然后,应用上述方法:

>>> i, j = np.where(mask)
>>> indices = np.meshgrid(np.arange(min(i), max(i) + 1), np.arange(min(j), max(j) + 1), indexing='ij')
>>> image[indices]
array([[3, 4],
       [6, 7]])

这里,indices[0]是行索引的矩阵,而indices[1]是列索引的对应矩阵:

>>> indices[0]
array([[1, 1],
       [2, 2]])
>>> indices[1]
array([[0, 1],
       [0, 1]])

答案 1 :(得分:2)

运行两种方法(使用NumPy 1.18.5)时,我没有得到汉斯的结果。无论如何,有一种更有效的方法,您可以沿每个尺寸取arg-max

bar = foo.get_booster().copy()

花费38毫秒

i, j = np.where(mask)
y, x = np.meshgrid(
    np.arange(min(i), max(i) + 1),
    np.arange(min(j), max(j) + 1),
    indexing="ij",
)

花费35毫秒

where = np.array(np.where(mask))
y1, x1 = np.amin(where, axis=1)
y2, x2 = np.amax(where, axis=1) + 1
sub_image = image[y1:y2, x1:x2]

花2毫秒

Timings script

答案 2 :(得分:1)

我认为使用np.amaxnp.amin并裁剪图像要快得多。

i, j = np.where(mask)
indices = np.meshgrid(np.arange(min(i), max(i) + 1),
              np.arange(min(j), max(j) + 1),
              indexing='ij')
sub_image = image[indices]

所花费的时间:50毫秒

where = np.array(np.where(mask))

x1, y1 = np.amin(where, axis=1)
x2, y2 = np.amax(where, axis=1)
sub_image = image[x1:x2, y1:y2]

所花费的时间:5.6毫秒