You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从YOLOv3 Darknet中获取检测目标的边界框坐标?

YOLOv3 获取检测目标坐标的具体实现方法

YOLOv3 推理过程本身就会输出每个检测框的坐标信息,根据你用的框架版本不同,具体获取方式如下:

1. 官方 Darknet 框架版本

默认推理返回的检测框坐标格式为中心点x、中心点y、宽度w、高度h,数值为相对于原图的归一化值,你可以按以下步骤获取实际像素坐标:

  • 调用推理接口拿到检测结果:如果用Python封装接口,执行detections = darknet.detect_image(net, class_names, raw_img, thresh=0.5),返回的每个检测结果的第三个字段就是(x, y, w, h)
  • 转换为实际像素坐标:
    // C语言接口写法
    int x_abs = bbox.x * img_width;
    int y_abs = bbox.y * img_height;
    int w_abs = bbox.w * img_width;
    int h_abs = bbox.h * img_height;
    
    # Python接口写法
    x_abs, y_abs, w_abs, h_abs = map(lambda i: i * img_shape[index], bbox)
    
  • 如果你需要左上角+右下角的坐标格式,可额外做转换:
    x1 = int(x_abs - w_abs / 2)
    y1 = int(y_abs - h_abs / 2)
    x2 = int(x_abs + w_abs / 2)
    y2 = int(y_abs + h_abs / 2)
    

2. PyTorch/TensorFlow 复现版本

绝大多数开源复现版本完成NMS后,输出的检测结果格式为[x1, y1, x2, y2, confidence, class_id],这里的坐标已经是映射回原图的绝对像素坐标,直接取值即可:

# PyTorch版本示例
pred = model(img)
pred = non_max_suppression(pred, conf_thres=0.5, iou_thres=0.4)
for det in pred[0]:
    x1, y1, x2, y2, conf, cls = det
    x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
    # 上述四个值就是你需要的坐标

注意事项

  • 部分实现返回的坐标是相对于模型输入尺寸(比如416*416)的,需要你按原图和输入图的缩放比例做映射校正
  • 建议拿到坐标后直接在原图上绘制矩形框,和官方自带的可视化结果做对比,确认坐标取值正确

内容的提问来源于stack exchange,提问作者prathvi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 22:24:00