【问题标题】:Problems when drawing bounding boxes manually手动绘制边界框时的问题
【发布时间】:2021-07-18 00:18:23
【问题描述】:

我正在尝试使用 openCV 手动绘制边界框。边界框坐标是使用 tensorflow 对象检测 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_width,im_height 交换为im_height,im_width 后,我现在得到:

【问题讨论】:

  • 尝试将im_width, im_height,_ = image.shape 切换为height, width, _ = ...
  • @NicolasGervais 感谢您的回复!我似乎帮助很大。但由于某种原因,它把它画得比它应该的大(新图片已附加在帖子中)。
  • ymin, xmin, ymax, xmax = int(xmin * im_width), int(xmax * im_width), int(ymin * im_height), int(ymax * im_height) 中左右两边不匹配。例如你有 ymin = int(xmin * im_width) 的等价物。
  • @DanMašek 当然!我怎么看不到?!现在可以完美运行,边界框彼此一致。非常感谢你们!

标签: tensorflow opencv object-detection


【解决方案1】:

逻辑错误在这一行:

im_width, im_height, _ = image.shape

由于 opencv 图像数组以这种方式构造:(height, width, channels),因此您需要交换您将 im_widthim_height 放入的位置:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2021-05-28
    相关资源
    最近更新 更多