图像跟踪中像素误差、重叠率、AUC的计算:Matlab实现

        视频图像跟踪算法的评估常常需要计算重叠率(Overlap Rate)、像素误差(Pixel Error)还有AUC(Area Under Curve)。这些技术指标都比较好理解,如果不理解的话可以参见我之前的博客。这篇博客主要就是贴上Matlab的代码,使用Matlab计算这些参数,并绘制出曲线图。

        如果你是想放在Paper中的话,可以下载Visual Track BenchMark中提供的代码,可以画出和Paper上一样的样式,这篇博客只是博主对单个视频自己写的小demo,便于自己分析实验的,如果要在Paper使用还需要做样式的修改。

        关于各个指标,之前博客已经介绍过了,这里就简单讲一下

一、重叠率

        重叠率 = 重叠区域面积/(预测矩形的面积+真实矩形的面积-重叠区域的面积)

二、像素误差

        像素误差 = 真实中心和预测中心的欧式距离

三、AUC

        成功率图的曲线下面积

四、Matlab计算

        变量说明:

        数据文件为Object Tracker BenchMark中的数据格式存放在./results_TRE_CVPR13/文件夹中,图像序列(只用到了groundtruth.txt的真实值)存储在F:\Data\sequence\,这个两个量都可以在代码中修改。

        video_set: 图像数据集名称

        method_set: 方法名称

        line_style: 要和method_set的大小相等,每种线条样式method相对应

close all;
clear all;

video_set = {'Shaking'};
%video_set = {'BlurCar2','Car2','Car4','Car24','Doll','Human6','RedTeam','Singer1','Walking','Walking2'};
method_set = {'FMT', 'kcf_hog8'};
line_style = {'--r', '--g'};

base_path = 'F:\Data\sequence\';
results_path = './results_TRE_CVPR13/';
precision{numel(video_set), numel(method_set)} = 0;

auc_table{numel(video_set)+1, numel(method_set)+1} = 0;
auc_table{1,1}=0;

for i = 1:numel(video_set)
    auc_table{i+1,1}=video_set{i};
    % read groundtruth
    varlist = {'gt','gt_center','gt_rect','gt_area','center','boxes,res_rect','res_area',...
        'overlap_rect','overlap_area','overlap_rate','mask','pixel_error','er'};
    clear(varlist);
    gt_file = fopen([base_path video_set{i} '\groundtruth_rect.txt']);
    try
        groundtruth_roi = textscan(gt_file, '%f,%f,%f,%f', 'ReturnOnError', false);
        gt(:,1)=groundtruth_roi{1};
        gt(:,2)=groundtruth_roi{2};
        gt(:,3)=groundtruth_roi{3};
        gt(:,4)=groundtruth_roi{4};
    catch  %#ok, try different format (no commas)
        frewind(gt_file);
        groundtruth_roi = textscan(gt_file, '%f %f %f %f');
        gt(:,1)=groundtruth_roi{1};
        gt(:,2)=groundtruth_roi{2};
        gt(:,3)=groundtruth_roi{3};
        gt(:,4)=groundtruth_roi{4};
    end
    gt_center(:,1) = gt(:,1) + gt(:,3)/2;
    gt_center(:,2) = gt(:,2) + gt(:,4)/2;
    gt_rect(:,1) = gt(:,1);
    gt_rect(:,2) = gt(:,2);
    gt_rect(:,3) = gt(:,1) + gt(:,3);
    gt_rect(:,4) = gt(:,2) + gt(:,4);
    gt_area = gt(:,3).*gt(:,4);
    % caculate different method precision
    for ii = 1:numel(method_set)
        auc_table{1,ii+1}=method_set{ii};
        clear('fig','boxes','center','er','pixel_error','res_rect','overlap_rect','mask',...
            'overlap_rate','overlap_area','xx','yy');
        %read results (标准格式)
        r = load([results_path lower(video_set{i}) '_' method_set{ii} '.mat']);
        boxes = r.results{1}.res;
        assert(size(boxes,1)==size(gt,1));
        % caculate pixel error
        center(:,1) = boxes(:,1)+boxes(:,3)/2;
        center(:,2) = boxes(:,2)+boxes(:,4)/2;
        er = center - gt_center;
        pixel_error = sqrt(er(:,1).*er(:,1)+er(:,2).*er(:,2));
        % caculate overlaprate
        res_rect(:,1) = boxes(:,1);
        res_rect(:,2) = boxes(:,2);
        res_rect(:,3) = boxes(:,1)+boxes(:,3);
        res_rect(:,4) = boxes(:,2)+boxes(:,4);
        res_area = boxes(:,3).*boxes(:,4);
        overlap_rect(:,1) = max(res_rect(:,1), gt_rect(:,1));
        overlap_rect(:,2) = max(res_rect(:,2), gt_rect(:,2));
        overlap_rect(:,3) = min(res_rect(:,3), gt_rect(:,3));
        overlap_rect(:,4) = min(res_rect(:,4), gt_rect(:,4));
        mask = or((overlap_rect(:,1)>overlap_rect(:,3)), overlap_rect(:,2)>overlap_rect(:,4));
        overlap_area = (overlap_rect(:,3)-overlap_rect(:,1)).*(overlap_rect(:,4)-overlap_rect(:,2));
        overlap_area(mask) = 0;
        overlap_rate = overlap_area ./ (gt_area + res_area - overlap_area + eps);
        % visualization and result
        fig.video = video_set{i};
        fig.method = method_set{ii};
        fig.len = size(boxes,1);
        [yy, xx] = cdfcc(overlap_rate);
        fig.ol = overlap_rate;
        fig.olm = mean(overlap_rate);
        fig.olcdf.x = xx;
        fig.olcdf.x(1) = 0;
        fig.olcdf.x(end) = 1;
        fig.olcdf.y = 1-yy;
        [yy, xx] = cdfcc(pixel_error);
        fig.pe = pixel_error;
        fig.pem = mean(pixel_error);
        fig.pecdf.x = xx;
        fig.pecdf.y = yy;
        precision{i,ii} = fig;
        auc_table{i+1,ii+1} = fig.olm;
    end
    
    % show plot
    if 1,
        figure('Name', 'Pixel Error', 'Color', 'w');
        for ii = 1:numel(method_set),
            plot(1:precision{i,ii}.len, precision{i,ii}.pe, line_style{ii}, 'LineWidth', 2), hold on;
            lg{ii} = [method_set{ii} ' ' sprintf('%.2f', precision{i,ii}.pem)];
        end
        title(video_set{i});xlabel('Frame#');ylabel('Pixel Error');
        legend(lg);
        
        figure('Name', 'Overlap Rate', 'Color', 'w');
        for ii = 1:numel(method_set),
            plot(1:precision{i,ii}.len, precision{i,ii}.ol, line_style{ii}, 'LineWidth', 2), hold on;
            lg{ii} = [method_set{ii} ' ' sprintf('%.2f', precision{i,ii}.olm)];
        end
        title(video_set{i});xlabel('Frame#');ylabel('Overlap Rate');
        legend(lg);
        
        figure('Name', 'Pixel Error Success Plot', 'Color', 'w');
        for ii = 1:numel(method_set),
            plot(precision{i,ii}.pecdf.x, precision{i,ii}.pecdf.y, line_style{ii}, 'LineWidth', 2), hold on;
            %lg{ii} = [method_set{ii} ' ' sprintf('%.2f', trapz(precision{i,ii}.pecdf.x(2:end-1), precision{i,ii}.pecdf.y(2:end-1)))];
        end
        title(video_set{i});xlabel('Pixel Error Threshold');ylabel('Success Rate');
        legend(method_set);
        
        figure('Name', 'Overlap Rate Success Plot', 'Color', 'w');
        for ii = 1:numel(method_set),
            plot(precision{i,ii}.olcdf.x, precision{i,ii}.olcdf.y, line_style{ii}, 'LineWidth', 2), hold on;
            lg{ii} = [method_set{ii} ' ' sprintf('%.2f', trapz(precision{i,ii}.olcdf.x, precision{i,ii}.olcdf.y))];
        end
        title(video_set{i});xlabel('Overlap Rate Threshold');ylabel('Success Rate');
        legend(lg);
        
    end
    
end

其中用到了一个函数,也贴上来

function [ yCDF, xCDF ] = cdfcc( rate )
[yy, xx, ~, ~, ~] = cdfcalc(rate);
k = length(xx);
n = reshape(repmat(1:k, 2, 1), 2*k, 1);
xCDF = [-Inf; xx(n); Inf];
yCDF = [0; 0; yy(1+n)];
end

 

        这里已经在图例中放入了计算出的平均像素误差、平均重叠率、AUC的数值。

        OK,See You Next Chapter!

《图像跟踪中像素误差、重叠率、AUC的计算:Matlab实现》有3条评论

  1. 请问为什么总出现引用了不存在的字段 ‘results’。

    出错 zhuhans (line 52)
    boxes = r.results{1}.res;我看代码里确实没有results,跪求解决(毕设要凉了)

    回复
    • 请问为什么拷贝了这个代码,会报错呢

      出错 calPixelErr (line 50)
      r = load([results_path lower(video_set{i}) ‘_’ method_set{ii} ‘.mat’]);

      回复

王兴进行回复 取消回复