Groundtruth8个维度转成4个维度的方法(Python)

VOT2015以后groundtruth从4变为8的原因:
在使用vot数据集时一个令人疑惑的地方是,一般来说groundtruth由四个值(cx,cy,w,h)(矩形中心坐标加矩形长宽)或者(x,y,w,h)(左上角点坐标和长宽)四个数据进行表示即可,但是在vot数据集中每一个groundtruth确有8个数值。此时,四个角点确定的bounding box并不是与坐标轴平行的,是有一定旋转角度的矩形框。
所以如果需要使用轴对齐的bounding box(groundtruth为4个值),就需要对数据进行转化,github项目pysot(位于/pysot-master/utils/bbox.py)提供的代码如下:

def get_axis_aligned_bbox(region):
    # region = np.array([region[0][0][0], region[0][0][1], region[0][1][0], region[0][1][1],
    #                    region[0][2][0], region[0][2][1], region[0][3][0], region[0][3][1]])
    cx = np.mean(region[0::2])
    # print(cx)
    cy = np.mean(region[1::2])
    # print(cy)
    x1 = min(region[0::2])
    # print(x1)
    x2 = max(region[0::2])
    # print(x2)
    y1 = min(region[1::2])
    # print(y1)
    y2 = max(region[1::2])
    # print(y2)
    A1 = np.linalg.norm(region[0:2] - region[2:4]) * np.linalg.norm(region[2:4] - region[4:6])
    A2 = (x2 - x1) * (y2 - y1)
    s = np.sqrt(A1 / A2)
    w = s * (x2 - x1) + 1
    h = s * (y2 - y1) + 1
    return cx, cy, w, h

但是这种方法并不能直接用,于是我自己写了段代码,让他能被实现:

#自己的groundtruth.txt文件的路径
f = open("D:/vot2020/baby/groundtruth.txt",encoding = "utf-8")
regiont=f.read()
regiont=re.sub('[\n]',',',regiont)
b = list(regiont.split(","))
a=[]
t=0
for i in b[:-1]:
#插入数据
    a.append(float(i))
    t=t+1
    if t==8:
        region = np.array(a)
        c=str(get_axis_aligned_bbox(region))[1:-1]
# 将转换的结果储存的位置
        with open('D:/vot2020/baby/test_office.txt', 'a', encoding='utf-8') as file:
            file.write(c+'\n')
        t=0

顺利将:
在这里插入图片描述
转换为:
在这里插入图片描述

因为这里是一个一个seq里的groundtruth.txt文件发生变化,且txt文件里面保存的数据小数点位数太多,故而我做了以下改进:

import re

import numpy as np

def get_axis_aligned_bbox(region):
    # region = np.array([region[0][0][0], region[0][0][1], region[0][1][0], region[0][1][1],
    #                    region[0][2][0], region[0][2][1], region[0][3][0], region[0][3][1]])
    cx = round(np.mean(region[0::2]),1)
    # print(cx)
    cy = round(np.mean(region[1::2]),1)
    # print(cy)
    x1 = min(region[0::2])
    # print(x1)
    x2 = max(region[0::2])
    # print(x2)
    y1 = min(region[1::2])
    # print(y1)
    y2 = max(region[1::2])
    # print(y2)
    A1 = np.linalg.norm(region[0:2] - region[2:4]) * np.linalg.norm(region[2:4] - region[4:6])
    A2 = (x2 - x1) * (y2 - y1)
    s = np.sqrt(A1 / A2)
    w = round(s * (x2 - x1) + 1,1)
    h = round(s * (y2 - y1) + 1,1)
    return cx, cy,w, h

# seqlist_path='data/rgbt234.txt'
# with open(seqlist_path,'r') as fp:
#     seq_list = fp.read().splitlines()
# for i in seq_list:
#     print(i)
# 自己的groundtruth.txt文件的路径
seqlist_path='D:/vot2020/list.txt'
with open(seqlist_path,'r') as fp:
    seq_list = fp.read().splitlines()
for i in seq_list:
    print(i)
    f = open("D:/vot2020/"+i+"/groundtruth.txt",encoding = "utf-8")
    regiont=f.read()
    regiont=re.sub('[\n]',',',regiont)
    b = list(regiont.split(","))
    a=[]
    t=0
    for m in b[:-1]:
        #插入数据
        a.append(float(m))
        t=t+1
        if t==8:
            region = np.array(a)
            c=str(get_axis_aligned_bbox(region))[1:-1]
# 将转换的结果储存的位置
            with open("D:/vot2020/"+i+"/ground_truth.txt", 'a', encoding='utf-8') as file:
                file.write(c+'\n')
            t=0

这样直接执行一次就可以把所有的groundtruth.txt文件全部进行处理,且数据全改为保留一位小数:
在这里插入图片描述

参考CSDN:
1.VOT 数据集 groundtruth 8个维度 转成 4个维度的方法
2.VOT2016数据集groundtruth每行有八个数据的原因

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值