Python 从 Numpy 数组的图像中裁剪边界框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41909408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:49:36  来源:igfitidea点击:

Crop a Bounding Box from an Image which is a Numpy Array

pythonimagenumpymatplotlib

提问by Abhik

So I have an Image which is of shape (224,244,3) as an ndarray. I have a bounding box annotation for the image that looks like this

所以我有一个形状为 (224,244,3) 的图像作为 ndarray。我有一个看起来像这样的图像的边界框注释

{
  annotations: [
  {
    class: "rect",
    height: 172,
    width: 341,
    x: 282,
    y: 165
  },
  {
    class: "rect",
    height: 172,
    width: 353,
    x: 592,
    y: 90
  }
 ],
   class: "image",
   filename: "img_05974.jpg"
}

How do I crop the numpy array so that it gives me an image like the above bounding rectangles ?

我如何裁剪 numpy 数组,以便它给我一个像上面的边界矩形这样的图像?

回答by ImportanceOfBeingErnest

In principle cropping is easily done simply by slicing the correct part out of the array. E.g. image[100:200, 50:100, :]slices the part between pixels 100 and 200 in y (vertical) direction, and the part between pixels 50 and 100 in x (horizontal) direction.

原则上,只需从数组中切出正确的部分,即可轻松完成裁剪。例如image[100:200, 50:100, :],在 y(垂直)方向上对像素 100 和 200 之间的部分进行切片,在 x(水平)方向上对像素 50 和 100 之间的部分进行切片。

See this working example:

请参阅此工作示例:

import matplotlib.pyplot as plt

mydic = {
  "annotations": [
  {
    "class": "rect",
    "height": 98,
    "width": 113,
    "x": 177,
    "y": 12
  },
  {
    "class": "rect",
    "height": 80,
    "width": 87,
    "x": 373,
    "y": 43
  }
 ],
   "class": "image",
   "filename": "https://i.stack.imgur.com/9qe6z.png"
}


def crop(dic, i):
    image = plt.imread(dic["filename"])
    x0 = dic["annotations"][i]["x"]
    y0 = dic["annotations"][i]["y"]
    width = dic["annotations"][i]["width"]
    height = dic["annotations"][i]["height"]
    return image[y0:y0+height , x0:x0+width, :]


fig = plt.figure()
ax = fig.add_subplot(121)
ax.imshow(plt.imread(mydic["filename"]))

ax1 = fig.add_subplot(222)
ax1.imshow(crop(mydic, 0))

ax2 = fig.add_subplot(224)
ax2.imshow(crop(mydic, 1))

plt.show()

enter image description here

在此处输入图片说明