从Python中的图像裁剪并仅选择检测到的区域


淋了一整夜的雨
2025-01-30 06:05:47 (1月前)
  1. 我使用了Tensorflow Object Detection API来检测来自图像的手。通过使用提供的示例代码(object_detection_tutorial.ipynb),我已经能够在图像上绘制边界框。在那儿 ...

2 条回复
  1. 0# !啊啊 | 2019-08-31 10-32



    是的,在教程中变量

    output_dict

    可以用来实现这一点。注意传递给函数的所有变量

    vis_util.visualize_boxes_and_labels_on_image_array

    ,它们包含方框,分数等



    首先,您需要获取图像形状,因为框坐标是标准化形式。




    1. img_height, img_width, img_channel = image_np.shape

    2. </code>


    然后将所有框坐标转换为绝对格式




    1. absolute_coord = []
      THRESHOLD = 0.7 # adjust your threshold here
      N = len(output_dict[‘detection_boxes’])
      for i in range(N):
      if output_dict[‘score’][i] < THRESHOLD:
      continue
      box = output_dict[‘detection_boxes’]
      ymin, xmin, ymax, xmax = box
      x_up = int(xminimg_width)
      y_up = int(ymin
      img_height)
      x_down = int(xmaximg_width)
      y_down = int(ymax
      img_height)
      absolute_coord.append((x_up,y_up,x_down,y_down))

    2. </code>


    然后,您可以使用numpy切片来获取边界框内的图像区域




    1. bounding_box_img = []
      for c in absolute_coord:
      bounding_box_img.append(image_np[c[1]:c[3], c[0]:c[2],:])

    2. </code>


    然后只需保存所有numpy数组

    bounding_box_img

    作为图像。保存时,您可能需要更改形状,因为img处于形状[img_height,img_width,img_channel]。如果您使用得分数组,您甚至可以过滤掉所有低置信度分数的检测。



    PS:我可能搞砸了

    img_height



    img_width

    但这些应该给你一个起点。


登录 后才能参与评论