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

如何将图像标注的右上角设置为定位锚点?

图像标注右上角定位锚点实现方案

1. 自定义Python标注脚本(基于OpenCV)

如果是自行开发标注逻辑,核心是调整锚点的坐标计算方式:左上角锚点取标注框的(x_min, y_min),右上角锚点则取标注框的(x_max, y_min)(即x_min + width,y_min),再对应调整标注文本的绘制位置。

示例代码:

import cv2

# 读取目标图像
img = cv2.imread("test_image.jpg")
# 标注框参数:x_min, y_min, width, height
bbox = [120, 60, 220, 160]
x_min, y_min, bbox_width, bbox_height = bbox

# 计算右上角锚点坐标
x_anchor = x_min + bbox_width
y_anchor = y_min
anchor_point = (x_anchor, y_anchor)

# 绘制标注框
cv2.rectangle(img, (x_min, y_min), (x_anchor, y_min + bbox_height), (0, 255, 0), 2)
# 绘制右上角锚点标记(红色实心圆点)
cv2.circle(img, anchor_point, 4, (0, 0, 255), -1)

# 绘制对齐锚点的标注文本
label_text = "car"
text_font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.6
font_thickness = 2
# 获取文本尺寸,用于调整位置使文本右上角对齐锚点
text_width, text_height = cv2.getTextSize(label_text, text_font, font_scale, font_thickness)[0]
text_x = anchor_point[0] - text_width
text_y = anchor_point[1] + text_height // 2
cv2.putText(img, label_text, (text_x, text_y), text_font, font_scale, (0, 0, 255), font_thickness)

# 保存标注后的图像
cv2.imwrite("annotated_image.jpg", img)

2. LabelImg工具自定义锚点设置

如果使用LabelImg这类可视化标注工具,可通过修改源码调整锚点逻辑:

  • 找到LabelImg安装目录下的libs/labelImg.py文件
  • 定位到绘制标注文本的代码段,默认逻辑是用bbox.left和bbox.top作为文本左上角坐标,修改为:
    # 原代码示例(左上角锚点)
    # text_pos = (bbox.left, bbox.top - 10)
    # 修改为右上角锚点对齐
    text_width = self.getTextWidth(label, self.font)
    text_pos = (bbox.right - text_width, bbox.top - 10)
    
  • 保存修改后重启LabelImg,标注文本的锚点将自动对齐标注框右上角。

3. 已有标注数据集的锚点批量转换

如果需要将已标注好的数据集(比如YOLO格式)的锚点从左上角转为右上角,可批量修改坐标:
假设原标注格式为[类别, x_min, y_min, width, height],转换时直接计算右上角锚点坐标(x_min + width, y_min)即可。

批量转换脚本示例:

import os
from pathlib import Path

def convert_anchor_format(input_label_dir, output_label_dir):
    Path(output_label_dir).mkdir(parents=True, exist_ok=True)
    for label_file in os.listdir(input_label_dir):
        if not label_file.endswith(".txt"):
            continue
        input_path = os.path.join(input_label_dir, label_file)
        output_path = os.path.join(output_label_dir, label_file)
        with open(input_path, "r") as f_in, open(output_path, "w") as f_out:
            for line in f_in:
                line = line.strip()
                if not line:
                    continue
                parts = line.split()
                cls_id = parts[0]
                x_min = float(parts[1])
                y_min = float(parts[2])
                bbox_w = float(parts[3])
                bbox_h = float(parts[4])
                # 计算右上角锚点坐标
                anchor_x = x_min + bbox_w
                anchor_y = y_min
                # 按新格式写入(类别, 锚点x, 锚点y, 宽度, 高度)
                f_out.write(f"{cls_id} {anchor_x:.4f} {anchor_y:.4f} {bbox_w:.4f} {bbox_h:.4f}\n")

# 示例调用
convert_anchor_format("./original_labels", "./right_anchor_labels")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 14:22:36