【问题标题】:Converting opencv rectangle coordinate to yolo object coordinates for image labeling将opencv矩形坐标转换为yolo对象坐标进行图像标注
【发布时间】:2020-11-27 18:29:58
【问题描述】:

我想用 opencv c++ 创建图像标记程序来标记 yolo 对象检测器的图像,但我正在努力将矩形坐标 (x1,y1,x2,y2) 转换为 yolo 格式,即“对象类 x_center y_center宽高”。根据文档,x_center 和 y_center 是矩形的中心(不是左上角)。

我在已标记的图像上尝试了此代码

            double centerX = (x1 + x2) / (2.0 * imageWidth);
            double centerY = (y1 + y2) / (2.0 * imageHeight);
            double width = double(abs(x2 - x1) / imageWidth);
            double height = double(abs(y2- y1) / imageHeight);

然后得到 0 0.396759 0.278906 0.0109375 0.326852 这是从 0 0.40703125 0.5194444444444445 0.25364583333333335 0.5851851851851851。 我怎样才能让它工作?

【问题讨论】:

  • 你能给我输入吗?
  • 图片尺寸为1920x1080,选择矩形坐标(x1,y1,x2,y2)=(539, 253, 522, 601),输出为0 0.276302 0.39537 0.00885417 0.322222。但是输出应该大约是 0 0.40703125 0.5194444444444445 0.25364583333333335 0.5851851851851851。
  • 为什么要划分图像宽度和图像高度?它背后的数学原理是什么?为什么这么少的数字,你不想得到中心?
  • Center x 0.40 表示图像宽度的 40%,中心 y 是图像高度的 %,依此类推。这就是 yolo 用来检测边界框的位置。
  • 所以中心 x 是所选矩形宽度的中心,相对于图像宽度,中心 y 是所选矩形高度的中心,相对于图像高度。

标签: c++ opencv yolo labeling


【解决方案1】:

我终于找到了有效的代码:

Point centralPoint = Point((selectedRect.tl().x + selectedRect.br().x) / 2, (selectedRect.tl().y + selectedRect.br().y) / 2);
                centerX = centralPoint.x / imageWidth;
                centerY = centralPoint.y / imageHeight;

                rectWidth = abs(selectedRect.br().x - selectedRect.tl().x) / imageWidth;
                rectHeight = abs(selectedRect.br().y - selectedRect.tl().y) / imageHeight;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    相关资源
    最近更新 更多