簡體   English   中英

使用 python 裁剪邊界框

[英]Crop bounding boxes using python

獲取邊界框坐標的代碼:

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))

它給: 在此處輸入圖像描述

現在我想在給定這些邊界框的情況下裁剪圖像。 如何裁剪所有這些坐標並存儲為 jpg 文件?

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

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

然后從張量到圖像:

output_image = tf.image.encode_png(cropped_image_tensor)

如果您將圖像存儲為 numpy 數組,則可以在圖像上使用它:

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

我假設您的圖像尺寸是高度、寬度、通道數。

然后將圖像保存為jpg:

from PIL import Image

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM