python OpenCV画 bounding box并标明数据类

import cv2

    class_name = "car"
    # b_box 左上角坐标
    ptLeftTop = np.array([40, 145])
    # 文本框左上角坐标
    textleftop = []
    # b_box 右下角坐标
    ptRightBottom =np.array([365, 400])
    # 框的颜色
    point_color = (0, 255, 0)
    # 线的厚度
    thickness = 2
    # 线的类型
    lineType = 4

    # (500, 375, 3) -> h w c
    src = cv2.imread('data/detect/000020.jpg')
    cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
    src = np.array(src)
    # 画 b_box
    cv2.rectangle(src, tuple(ptLeftTop), tuple(ptRightBottom), point_color, thickness, lineType)

    # 获取文字区域框大小
    t_size = cv2.getTextSize(class_name, 1, cv2.FONT_HERSHEY_PLAIN, 1)[0]
    # 获取 文字区域右下角坐标
    textlbottom = ptLeftTop + np.array(list(t_size))
    # 绘制文字区域矩形框
    cv2.rectangle(src, tuple(ptLeftTop), tuple(textlbottom),  point_color, -1)
    # 计算文字起始位置偏移
    ptLeftTop[1] = ptLeftTop[1] + (t_size[1]/2 + 4)
    # 绘字
    cv2.putText(src, class_name , tuple(ptLeftTop), cv2.FONT_HERSHEY_PLAIN, 1.0, (255, 0, 255), 1)
    # 打印图片的shape
    print(src.shape)

    cv2.imshow('image', src)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

结果:
python OpenCV画 bounding box并标明数据类_第1张图片

你可能感兴趣的:(python,opencv,python,计算机视觉)