提问者:小点点

手动绘制边界框时的问题


我试图绘制边界框手动使用openCV。边界框坐标是使用tenstorflow目标检测API收集的。

当我得到坐标时,它们被归一化,所以我转换它们,然后像这样将它们添加到图像中:

boxes = np.squeeze(detections['detection_boxes'])
scores = np.squeeze(detections['detection_scores'])
min_score_thresh = 0.7
bboxes = boxes[scores > min_score_thresh]

im_width, im_height,_ = image.shape
for box in bboxes:
    ymin, xmin, ymax, xmax = box
    ymin, xmin, ymax, xmax = int(xmin * im_width), int(xmax * im_width), int(ymin * im_height), int(ymax * im_height)
    cv2.rectangle(image_np_with_detections, (xmin,ymin),(xmax,ymax),(0,0,255),5)
    cv2.putText(image_np_with_detections,"TEST",(int(xmin),int(ymin)-5),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,0,255), 1)

然后我将此矩形与Tensorflow API生成的矩形进行比较,如下所示:

viz_utils.visualize_boxes_and_labels_on_image_array(
    image_np_with_detections,
    detections['detection_boxes'],
    detections['detection_classes'] + label_id_offset,
    detections['detection_scores'],
    category_index,
    use_normalized_coordinates=True,
    max_boxes_to_draw=5,
    min_score_thresh=.7,
    agnostic_mode=False)

#Show the image
cv2.imshow('object detection', image_np_with_detections)

但是如下图所示,手动绘制的边界框是关闭的。这可能是什么原因?

感谢任何帮助!

编辑:将im\u宽度、im\u高度切换到im\u高度、im\u宽度后,我现在得到:


共1个答案

匿名用户

逻辑错误在这一行:

im_width, im_height, _ = image.shape

由于opencv图像数组的结构是这样的:(高度、宽度、通道),您需要交换放置im\u宽度im\u高度的位置:

im_height, im_width, _ = image.shape

此外,这一行:

ymin, xmin, ymax, xmax = int(xmin * im_width), int(xmax * im_width), int(ymin * im_height), int(ymax * im_height)

如您所见,应该是:

ymin, xmin, ymax, xmax = int(ymin * im_height), int(xmin * im_width), int(ymax * im_height), int(xmax * im_width)