使用2组坐标

时间:2018-03-14 16:05:12

标签: algorithm matlab coordinates

我们说我有两套坐标。集合A是地面实况坐标,集合B是新生成的坐标。

我按以下方式分类:

  1. 如果集合A中的坐标至少在B中坐标的5个像素内,则为正数。
  2. 对于集合A中找不到与集合B中任何坐标匹配的所有坐标的假阴性。
  3. 集合B中与集合A中的任何坐标匹配的所有坐标的误报。
  4. 这些集合不对应。我的意思是,集合A中的第一个坐标与集合B中的第一个坐标没有任何关联。

    这是我的代码:

    clear;
    w = warning ('off','all');
    coordA = dir('./GT*.txt');
    coordB = dir('./O*.txt');
    
    for i =1:length(coordA)
        TP = [];
        FP = [];
        FN = [];
        %read coordinate files
        fir = fopen(coordA(i).name, 'r');
        disp(coordA(i).name);
        A = textscan(fir, '%d %d\n');
        fclose(fir);
    
        disp(coordB(i).name);
        sec = fopen(coordB(i).name, 'r');
        B = textscan(fir, '%d, %d\n');
        fclose(sec);
    
        A_x = A{1};
        A_y = A{2};
        B_x = B{1};
        B_y = B{2};
        for j = 1:length(A_x)
            flag = 1;  %this flag indicates false negatives
            for k = 1:length(B_x)
                X = [A_x(j), A_y(j); B_x(k), B_y(k)];
                d = pdist(X);
                if(d <= 5)
                    flag = 0; %Ax and Ay 
                    %the problem is here---------
                    TP = [TP [B_x(k) B_y(k)]];
                    B_x(k) = 0;
                    B_y(k) = 0;
                end  
            end
            if(flag)
                FN = [FN [A_x(j) A_y(j)]];
            end
        end
        for b = find(B_x)
            FP = [FP [B_x(b) B_y(b)]];
        end
    end
    

    问题(请注意下面的代码和示例中的注释)我面临以下问题。让我们说集合A中有两个坐标彼此非常接近。当我去检查集合B中的TP并且我找到一个在5个像素内的坐标时,我将其标记为真正的正面然后从集合B中移除该坐标。但是,让我们说我&#39;我试图从集合A中检查附近的其他坐标。好吧,它会被标记为假阴性,因为我在检查不同的坐标时删除了集合B中的坐标。

    我想到即使找到真正的阳性,也不会删除集合B中的坐标,但是我怎么会发现误报?

    我在Matlab中做到了这一点,但任何语言都适合我。

    示例坐标: 答:

    250 500
    251 500
    

    B:

    250 501
    

    第二个坐标也应该被认为是真阳性,但它被认为是假阴性。

1 个答案:

答案 0 :(得分:1)

通过更改代码,我相信以下部分应该可以满足您的需求。基本上,您可以只使用逻辑索引来代替删除条目:

clear;
w = warning ('off','all');
coordA = dir('./GT*.txt');
coordB = dir('./O*.txt');

radius = 5;

for i =1:length(coordA)

    % read coordinate files
    fir = fopen(coordA(i).name, 'r');
    disp(coordA(i).name);
    A = textscan(fir, '%d %d\n');
    fclose(fir);

    disp(coordB(i).name);
    sec = fopen(coordB(i).name, 'r');
    B = textscan(fir, '%d, %d\n');
    fclose(sec);

    A_x = A{1};
    A_y = A{2};
    B_x = B{1};
    B_y = B{2};

    % Initialize logical arrays to reflect the 
    % Status of each entry
    TP = zeros(length(A_x),1); % no entry of A is considered true positive
    FN = ones(length(A_x),1);  % all entries of A are considered false negative
    FP = ones(length(B_x),1);  % all entries of B are considered false positive

    for ij = 1:length(A_x)
        for ijk = 1:length(B_x)
            X = [A_x(ij), A_y(ij); B_x(ijk), B_y(ijk)];
            d = pdist(X)
            if (d <= radius)
                TP(ij) = 1; % just found a true positive
                FP(ijk) = 0;% no more a false positive
            end
        end 
    end

    % Obtain the lists containing the appropriate values
    % For true positive, just use logical indexing and TP array
    True_Pos = [A_x(logical(TP))', A_y(logical(TP))'];
    % For false negative, remove TP from FN
    False_Neg = [A_x(logical(FN-TP))', A_y(logical(FN-TP))']; 
    % For false positive, whatever remained switched on in FP
    False_Pos = [B_x(logical(FP))', B_y(logical(FP))'];
end

如果您确实需要删除条目,还有其他方法(请参阅上面的评论)。此外,有一种更简洁的方式来编码它,但我试图明确并遵循您的实现。

相关问题