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

OpenCV目标识别中边界框中心检测错误问题求助

边界框中心定位错误的问题分析与解决方案

问题根源

OpenCV 4.x的cv2.dnn_DetectionModel.detect()方法返回的边界框bbox格式为**(x, y, width, height)**:

  • box[0]:边界框左上角的x坐标
  • box[1]:边界框左上角的y坐标
  • box[2]:边界框的宽度
  • box[3]:边界框的高度

你错误地将box[2]、box[3]当成了边界框右下角的x、y坐标,导致中心计算逻辑完全错误,目标越靠右,偏移量越大。

修正后的代码

核心修改show_distance函数的中心计算逻辑,其他逻辑保持不变:

import cv2

thres = 0.45  # Threshold to detect object

cap = cv2.VideoCapture(0)

CLASS_FILE = "coco.names"
CONFIG_PATH = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
WEIGHTS_PATH = 'frozen_inference_graph.pb'
TEST_OBJECT = 'person'

cap.set(3, 1280)
cap.set(4, 720)
cap.set(10, 70)

def show_distance(img, box, screen_center_x, screen_center_y):
    # 修正:根据左上角坐标+宽高计算中心
    obj_center_x = box[0] + box[2] // 2
    obj_center_y = box[1] + box[3] // 2
    distance_x = obj_center_x - screen_center_x
    distance_y = obj_center_y - screen_center_y
    
    text = f"Dist X: {distance_x}, Dist Y: {distance_y}"
    cv2.putText(img, text, (box[0] + 10, box[1] - 10),
        cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0), 1)
    
    cv2.circle(img, (obj_center_x, obj_center_y), 10, (0, 255, 0), -1)
    cv2.circle(img, (screen_center_x, screen_center_y), 10, (0, 0, 255))

def add_one_object(img, box, current_obj, confidence):
    color = (0, 255, 0)
    cv2.rectangle(img, box, color, thickness=2)
    cv2.putText(
        img, current_obj.upper(), (box[0] + 10, box[1] + 30),
        cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)

def main():
    classNames = []

    with open(CLASS_FILE, 'rt') as f:
        classNames = f.read().rstrip('\n').split('\n')

    net = cv2.dnn_DetectionModel(WEIGHTS_PATH, CONFIG_PATH)
    net.setInputSize(320, 320)
    net.setInputScale(1.0 / 127.5)
    net.setInputMean((127.5, 127.5, 127.5))
    net.setInputSwapRB(True)

    entered_name=TEST_OBJECT

    while True:
        success, img = cap.read()
        classIds, confs, bbox = net.detect(img, confThreshold=thres)

        if len(classIds) != 0:
            screen_center_x = img.shape[1] // 2
            screen_center_y = img.shape[0] // 2

            for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox):
                if classNames[classId - 1].lower() == entered_name:
                    add_one_object(img, box, classNames[classId - 1], confidence)
                    show_distance(img, box, screen_center_x, screen_center_y)
                    break

        cv2.imshow('Output', img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

验证说明

修改后,绿色圆圈(边界框中心)会准确落在绿色检测框的几何中心,无论目标处于屏幕左侧、中间还是右侧,偏移问题都会消失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 07:17:04