使用python裁剪边界框

时间:2021-06-15 07:40:04

标签: python tensorflow crop

获取边界框坐标的代码:

width=600
height=900
for box,score,cls in zip(detections['detection_boxes']  [0],detections['detection_scores'][0],detections['detection_classes'][0]):
   if score >= 0.5: # or any other value
      xmin = box[1]*width
      ymin = box[0]*height
      xmax = box[3]*width
      ymax = box[2]*height
      print("xmin: {} ".format(xmin),"ymin: {}".format(ymin),"xmax: {}".format(xmax),"ymax: {}".format(ymax))

它给出: enter image description here

现在我想根据这些边界框裁剪图像。如何裁剪所有这些坐标并存储为 jpg 文件?

2 个答案:

答案 0 :(得分:0)

如果你想使用tensorflow,你可以使用:

cropped_image_tensor = tf.image.crop_to_bounding_box(image, xmin, ymin, height, width)

然后从张量到图像:

output_image = tf.image.encode_png(cropped_image_tensor)

答案 1 :(得分:0)

如果您将图像存储为 numpy 数组,则可以在图像上使用它:

cropped_image = image[ymin:ymax, xmin:xmax:, :]

我假设您的图像尺寸是高度、宽度、通道数。

然后将图像另存为 jpg:

from PIL import Image

im = Image.fromarray(cropped_image)
im.save("your_file.jpeg")