在Python中从图像中裁剪并仅选择检测到的区域 [英] Crop and Select Only the Detected Region from an Image in Python

查看:222
本文介绍了在Python中从图像中裁剪并仅选择检测到的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Tensorflow对象检测API来检测图像中的手.通过使用提供的示例代码(object_detection_tutorial.ipynb),我已经能够在图像上绘制边框.有什么方法可以只选择检测到的区域(位于边界框内)并将其作为图像获取?

I have used Tensorflow Object Detection API to detect hands from images. By using the provided sample code (object_detection_tutorial.ipynb) I have been able to draw bounding boxes on images. Is there any way to select only the detected region (which is inside a bounding box) and get it as an image?

例如,

对象检测API示例代码可在此处找到. https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb

Object detection API sample code can be found here. https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb

任何帮助将不胜感激!

推荐答案

是的,在本教程中,变量output_dict可用于实现此目的.注意所有传递到函数vis_util.visualize_boxes_and_labels_on_image_array的变量,它们包含方框,分数等.

Yes, in the tutorial the variable output_dict can be used to achieve that. Notice all the variables passed into function vis_util.visualize_boxes_and_labels_on_image_array, they contain the boxes, scores, etc.

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

First you need to get the image shape as the box coordinates are in normalized form.

img_height, img_width, img_channel = image_np.shape

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

Then transform all the box coordinates to the absolute format

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(xmin*img_width)
    y_up = int(ymin*img_height)
    x_down = int(xmax*img_width)
    y_down = int(ymax*img_height)
    absolute_coord.append((x_up,y_up,x_down,y_down))

然后,您可以使用numpy slices获取边界框中的图像区域

Then you can use numpy slices to get the image area within the bounding box

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

然后只需将bounding_box_img中的所有numpy数组另存为图像.保存时,您可能需要更改形状,因为img的形状为[img_height,img_width,img_channel].如果使用分数数组,您甚至还可以过滤掉所有置信度较低的检测结果.

Then just save all the numpy arrays in bounding_box_img as images. When saving you might need to change the shape as the img is in shape [img_height, img_width, img_channel]. Also you can even filter out all detections with low confidence scores if you use the score array.

PS:我可能已经弄混了img_heightimg_width,但是这些应该为您提供一个起点.

PS: i might have messed up with img_height and img_width but these should give you a starting point.

这篇关于在Python中从图像中裁剪并仅选择检测到的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆