本文介绍了如何将边界框(x1、y1、x2、y2)转换为 YOLO 样式(X、Y、W、H)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在训练一个 YOLO 模型,我有这种格式的边界框:-

I'm training a YOLO model, I have the bounding boxes in this format:-

x1, y1, x2, y2 => ex (100, 100, 200, 200)

我需要将其转换为 YOLO 格式,如下所示:-

I need to convert it to YOLO format to be something like:-

X, Y, W, H => 0.436262 0.474010 0.383663 0.178218

我已经计算了中心点 X、Y、高度 H 和重量 W.但仍然需要将它们转换为前面提到的浮点数.

I already calculated the center point X, Y, the height H, and the weight W.But still need a away to convert them to floating numbers as mentioned.

推荐答案

YOLO 将图像空间标准化为在 xy 两个方向上从 0 到 1 运行.要在 (x, y) 坐标和 yolo (u, v) 坐标之间进行转换,您需要将数据转换为 u = x/XMAXy = y/YMAX 其中 XMAX, YMAX 是您正在使用的图像数组的最大坐标.

YOLO normalises the image space to run from 0 to 1 in both x and y directions. To convert between your (x, y) coordinates and yolo (u, v) coordinates you need to transform your data as u = x / XMAX and y = y / YMAX where XMAX, YMAX are the maximum coordinates for the image array you are using.

这一切都取决于以相同方式定向的图像阵列.

This all depends on the image arrays being oriented the same way.

这是一个执行转换的 C 函数

Here is a C function to perform the conversion

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>

struct yolo {
    float   u;
    float   v;
    };

struct yolo
convert (unsigned int x, unsigned int y, unsigned int XMAX, unsigned int YMAX)
{
    struct yolo point;

    if (XMAX && YMAX && (x <= XMAX) && (y <= YMAX))
    {
        point.u = (float)x / (float)XMAX;
        point.v = (float)y / (float)YMAX;
    }
    else
    {
        point.u = INFINITY;
        point.v = INFINITY;
        errno = ERANGE;
    }

    return point;
}/* convert */


int main()
{
    struct yolo P;

    P = convert (99, 201, 255, 324);

    printf ("Yolo coordinate = <%f, %f>
", P.u, P.v);

    exit (EXIT_SUCCESS);
}/* main */

这篇关于如何将边界框(x1、y1、x2、y2)转换为 YOLO 样式(X、Y、W、H)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-16 14:32