目标跟踪CLE绘图 OTB数据跟踪绘图 mat文件txt文件 相互转换

该博客主要介绍如何将目标跟踪的txt数据转换为mat文件以便在MATLAB中绘图,以及如何从mat文件转换回txt并绘制CLE图。还提供了针对跟踪中心距离误差的CLE画图代码,涉及Python和MATLAB程序实现。
摘要由CSDN通过智能技术生成

跟踪  OTB数据集 绘图

文章主要3个内容  

1.txt文件数据--》转换 mat文件 (在matlab上画图)

2.mat文件--》txt文件 画CLE图

3.CLE画图代码  跟踪目标中心距离误差 

1.txt文件转换成 mat文件

把python跟踪得到txt数据集 转换成 matlab使用mat文件  画图

matlab程序代码


clear;clc;
close all;
% txt路径
txt_path  = 'D:\atom\default_000\';
% mat路径
mat_path = 'D:\atom\default\';
mkdir(mat_path);
% 跟踪器名字
tracker='ATOM';
 
txt_files = dir([txt_path '*.txt']);
 
txt_files={txt_files.name};
 
for i=1:numel(txt_files)
    
    region=csvread([txt_path txt_files{i}]);
    results{1}.type = 'rect';
    results{1}.res = region;
    results{1}.len = length(region);
    
    videoname=txt_files{i};
    videoname=videoname(1:length(videoname)-4);
    save([mat_path videoname '_' tracker '.mat'], 'results');
    
end

把得到mat文件放入OTB下面results文件目录下  perfplot.m一下 

 

2.mat文件转换txt文件

matlab程序代码

clear;clc;
close all;
% txt路径
txt_path  = 'D:\liux_code\deep_sort_yolov3\results\txtpsps1\';
% mat路径
%mat_path = 'D:\liux_code\deep_sort_yolov3\results\mat\';
mat_path = 'D:\OTB\tracker_benchmark_v1\results\results_OPE_CVPR12\';

mkdir(txt_path);
% 跟踪器名字
tracker='bacf';
 
mat_files = dir([mat_path '*.mat']);
 
mat_files={mat_files.name};
 
for i=1:numel(mat_files)
    
    region=load([mat_path mat_files{i}]);
     results= region.results{1};
     results=results.res;
%     results{1}.res = region;
%     results{1}.len = length(region);
    
    videoname=mat_files{i};
    videoname=videoname(1:length(videoname)-4);
    %fid = fopen([txt_path videoname '_' tracker '.txt'] ,'wt');
     fid = fopen([txt_path videoname '.txt'] ,'wt');
    [M,N]=size(results);
    for m =1:M
        for n= 1:N
            if n ==4 
                
                fprintf(fid,'%d',results(m,n));
            else
                
                fprintf(fid,'%d\t',results(m,n));
            end
        end
        fprintf(fid,'\n');
    end
    fclose(fid);
    %save([txt_path videoname '_' tracker '.txt'  ], 'results','-ASCII', '-tabs');%'-ASCII -double'
    
end

TRE只需要转换第一栏第一个数据集代码  

转换后这样算正常

如果出现这样的 说明你的mat文件  出现问题 在跟踪程序里需要修改

在跟踪程序中(matlab 相关滤波跟踪)

寻找box 跟踪框


		box = [floor(pos([2,1])) - floor(target_sz([2,1])/2), floor(target_sz([2,1]))];
        rect_results(frame,:)=box;
   		%visualization

floor是取实数 代码更改成类似的 只要跟踪结果mat文件 都是实数就可以 

得到txt跟踪数据集  我们来画CLE图像  

3.CLE绘图 

python程序代码

from pylab import *
import math
import matplotlib.pyplot as plt
import numpy

import os



# calculate the Pre,CLE is CENTOR LOCATION ERROR,and it is a list
def calculatePre(CLE):
    res = []
    for thresh in range(1, 100):
        tmp = numpy.array(CLE)  # get the temporary variable
        tmp[tmp <= thresh] = 1
        tmp[tmp > thresh] = 0
        num = sum(tmp)
        rate = float(num) / float(tmp.size)
        res.append(rate)
    return res


# 定义画中心位置误差图像的函数
def drawCLE(title, ResGroundLines, ResKcfLines, ResKcfILines,ResKcfILines2,ResKcfILines3):
    CleKcf = []
    list1 =['Car4','CarScale','Couple', 'Crossing', 'Faceocc1','Girl','Singer1','Subway','Walking',
            'Walking2','Woman','Sylvester','Jogging-1',]# 读取groundtruth_rect里数据 
    CleKcfI = []                                        # 有的 , 隔开  有的是空格 隔开 
    CleKcfI2 = []
    CleKcfI3 = []
    num_of_frame = len(ResGroundLines)   # 帧数,去掉表头和最后一帧(主要是我结果好像少写了一帧)
    for index in range(1, (num_of_frame )):
        # 每一行拿出来,第一列是分别是 frame   x   y   width   height,分离出来并转换成数字
        if title  in list1:
            GroundPos = (ResGroundLines[index]).split('\t')
        else:
            GroundPos = (ResGroundLines[index]).split(',')
        #KcfPos = (ResKcfLines[index]).split('\t')
        KcfIPos = (ResKcfILines[index]).split('\t')
       # KcfIPos = KcfIPos.replace('\n')
        KcfIPos2 = (ResKcfILines2[index]).split('\t')
        KcfIPos3 = (ResKcfILines3[index]).split('\t')
        GroundPos = list(map(int, GroundPos))
        #KcfPos = list(map(int, KcfPos))
        KcfIPos = list(map(int, KcfIPos))
        KcfIPos2 = list(map(int, KcfIPos2))
        KcfIPos3 = list(map(int, KcfIPos3))
        # 提取中心位置
        P_G = [GroundPos[0] + GroundPos[2] / 2, GroundPos[1] + GroundPos[3] / 2]

        #P_K = [KcfPos[0] + KcfPos[2] / 2, KcfPos[2] + KcfPos[3] / 2]
        P_KI = [KcfIPos[0] + KcfIPos[2] / 2, KcfIPos[2] + KcfIPos[3] / 2]
        P_KI2 = [KcfIPos2[0] + KcfIPos2[2] / 2, KcfIPos2[2] + KcfIPos2[3] / 2]
        P_KI3 = [KcfIPos3[0] + KcfIPos3[2] / 2, KcfIPos3[2] + KcfIPos3[3] / 2]
        #CLE_KCF = math.sqrt((P_K[0] - P_G[0]) ** 2 + (P_K[1] - P_G[1]) ** 2)
        CLE_KCF_I = math.sqrt((P_KI[0] - P_G[0]) ** 2 + (P_KI[1] - P_G[1]) ** 2)
        CLE_KCF_I2 = math.sqrt((P_KI2[0] - P_G[0]) ** 2 + (P_KI2[1] - P_G[1]) ** 2)
        CLE_KCF_I3 = math.sqrt((P_KI3[0] - P_G[0]) ** 2 + (P_KI3[1] - P_G[1]) ** 2)
        #CleKcf.append(CLE_KCF)
        CleKcfI.append(CLE_KCF_I)
        CleKcfI2.append(CLE_KCF_I2)
        CleKcfI3.append(CLE_KCF_I3)
    plt.figure()  # CLE  CENTOR LOCATION ERROR
    plt.title(title + "CLE Plot")
    #plt.plot(CleKcf, color='red', label="atom")
    #####################################################
    plt.plot(CleKcfI, color='black', label="samf")
    #plt.plot(CleKcfI2, color='red', label="samfpaceauto")
    plt.plot(CleKcfI3, color='red', label="our")

    mpl.rcParams['font.sans-serif'] = ['SimHei']
    plt.xlabel('帧数',fontsize =13)
    plt.ylabel('中心位置误差',fontsize =13)
    plt.legend()
    # 保存图片
    plt.savefig("results//samfpsps1//" + title + ".png", dpi=600)

    # PreKcf = calculatePre(CleKcf)
    # PreKcfI = calculatePre(CleKcfI)
    #
    # plt.figure()  # PRECISION PERCENT
    # plt.title(title + "Precision Plot")
    # plt.plot(PreKcf, color='red', label='atom')
    # plt.plot(PreKcfI, color='blue', label="bacf")
    # plt.legend()
    # plt.savefig("results//" + title + "_Pre.png", dpi=600)


# 主函数
# for target in lines:
    # target有个回车,这里需要把这个回车给去掉,然后下面把当前target下的文件读取
    # AveFpsKcf = open(path + target[:-1] + ave_fps_kcf)
    # AveFpsKcfI = open(path + target[:-1] + ave_fps_kcf_inter)
#s = "basketball"
a = ['Jogging-1',
    'Basketball', 'Bolt', 'Boy', 'Car4', 'CarDark', 'CarScale',
     'Coke', 'Couple', 'Crossing', 'David2', 'David3', 'David', 'Deer',
     'Dog1', 'Doll', 'Dudek', 'Faceocc1', 'Faceocc2', 'Fish', 'Fleetface',
    'Football', 'Football1', 'Freeman1', 'Freeman3', 'Freeman4', 'Girl',
     'Ironman',  'Jumping', 'Lemming', 'Liquor', 'Matrix',
     'Mhyang', 'MotorRolling', 'MountainBike', 'Shaking', 'Singer1',
     'Singer2', 'Skating1', 'Skiing', 'Soccer','Subway', 'Suv','Sylvester','Tiger2', 'Trellis',
       'Walking', 'Walking2', 'Woman']  #'Jogging-1''Tiger1',

path = "results//"
otbpath = "D://OTB//OTB100//"
txtfile = "txtpsps1//"
#os.makedirs(r"results//samfpsps1" )
for s in a:
    if s =='Jogging-1':
        res_ground = otbpath+'jogging'+"./groundtruth_rect.1.txt"
    else:
        res_ground = otbpath + s + "./groundtruth_rect.txt"

    res_bacfapceauto = "./results/" + txtfile + s +"_samfapceauto.txt"

    res_bacf = "./results/"+txtfile + s +"_samf.txt"
    res_bacfapce = "./results/" +txtfile + s +"_samfpsps1.txt"
    ResGround = open(res_ground)
    #Resatom= open(res_atom)
    Resbacfauto = open(res_bacfapceauto)
    Resbacf = open(res_bacf)
    Resbacfapce = open(res_bacfapce)
    # AveFpsKcfLines = AveFpsKcf.readlines()
    # AveFpsKcfILines = AveFpsKcfI.readlines()
    ResGroundLines = ResGround.readlines()
    #ResKcfLines = Resatom.readlines()
    ResKcfLines = 0
    ResKcfILines = Resbacf.readlines()
    ResKcfILines2 = Resbacfauto.readlines()
    ResKcfILines3 = Resbacfapce.readlines()
    target = s
    drawCLE(target, ResGroundLines, ResKcfLines, ResKcfILines,ResKcfILines2,ResKcfILines3)

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sou6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值