Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

{
  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 ?

like image 510
Abhik Avatar asked Jan 28 '17 11:01

Abhik


People also ask

How do you crop the detected bounding box in Python?

Calculate the bounding rectangle of the contour , it will return the top left coordinates and its height and width , like this: x, y, w, h = cv2. boundingRect(countour) . You can use this info to slice (crop) the original image.

How do I crop a picture using PIL?

crop() method is used to crop a rectangular portion of any image. Parameters: box – a 4-tuple defining the left, upper, right, and lower pixel coordinate. Return type: Image (Returns a rectangular region as (left, upper, right, lower)-tuple).


1 Answers

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.

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

like image 131
ImportanceOfBeingErnest Avatar answered Sep 21 '22 20:09

ImportanceOfBeingErnest