【问题标题】:Index mismatch for batch image processing批量图像处理的索引不匹配
【发布时间】:2019-11-11 05:32:57
【问题描述】:

为了计算一组图像的精确召回率,我得到了一个program。但是我在自定义它时遇到了一些问题。由于程序文件中没有任何图像,我无法实现原始命名模式。在我的例子中,Method_Path 和 GT_Path 中的图像被命名为 1 到 N(例如 500)。

%% initialization
clear all
close all;clc;
method = 'mine'; % name of the salient object method you want to evaluate, you need to change it
dataset = 'Pascal'; % name of dataset, you need to change this
method_path='./AMC_AE_Pascal/';
GT_path='./Pascal_GT/';
resultpath = [method_path '*' '.png']; % path to saliency maps, you need to change this
truthpath = [GT_path '*' '.png']; % path to ground-truth masks, yoiu need to change this
savepath = './result/PRcurve/'; % save path of the 256 combinations of precision-recall values
if ~exist(savepath,'dir')
mkdir(savepath);
end
dir_im = dir(resultpath);
assert(~isempty(dir_im),'No saliency map found, please check the path!');
dir_tr= dir(truthpath);
assert(~isempty(dir_tr),'No ground-truth image found, please check the path!');
assert(length(dir_im)==length(dir_tr),'The number of saliency maps and ground-truth images are not equal!')
imNum = length(dir_tr);
precision = zeros(256,1);
recall = zeros(256,1);
%% compute pr curve
for i = 1:imNum
imName = dir_tr(i).name;
input_im = imread([imName(1:end-4),resultpath(end-3:end)]);
truth_im = imread([truthpath(1:end-4),imName]);
truth_im = truth_im(:,:,1);
input_im = input_im(:,:,1);
if max(max(truth_im))==255
    truth_im = truth_im./255;
end
for threshold = 0:255
index1 = (input_im>=threshold);
truePositive = length(find(index1 & truth_im));
groundTruth = length(find(truth_im));
detected = length(find(index1));
if truePositive~=0
 precision(threshold+1) = precision(threshold+1)+truePositive/detected;
 recall(threshold+1) = recall(threshold+1)+truePositive/groundTruth;
end
end
display(num2str(i));
end
precision = precision./imNum;
recall = recall./imNum;
pr = [precision'; recall'];
fid = fopen([savepath dataset, '_', method, '_PRCurve.txt'],'at');
fprintf(fid,'%f %f\n',pr);
fclose(fid);
disp('Done!');

运行代码,出现以下错误。

【问题讨论】:

    标签: matlab image-processing


    【解决方案1】:

    使用fullfile 比连接文件名的各个部分更好。

    使用像[imName(1:end-4),resultpath(end-3:end)] 这样的代码并不可靠,因为它过多地假设了扩展长度。

    我对你的代码做了一些修改:

    %% initialization
    clear all
    close all;clc;
    method = 'mine'; % name of the salient object method you want to evaluate, you need to change it
    dataset = 'Pascal'; % name of dataset, you need to change this
    method_path='./AMC_AE_Pascal/';
    GT_path='./Pascal_GT/';
    im_pattern = '*.png'; %Image pattern (all PNG file in the folder).
    resultpath = method_path;%resultpath = [method_path '*' '.png']; % path to saliency maps, you need to change this
    truthpath = GT_path;%truthpath = [GT_path '*' '.png']; % path to ground-truth masks, yoiu need to change this
    savepath = './result/PRcurve/'; % save path of the 256 combinations of precision-recall values
    if ~exist(savepath,'dir')
        mkdir(savepath);
    end
    dir_im = dir(fullfile(resultpath, im_pattern));%dir_im = dir(resultpath);
    assert(~isempty(dir_im),'No saliency map found, please check the path!');
    dir_tr= dir(fullfile(truthpath, im_pattern));%dir_tr= dir(truthpath);
    assert(~isempty(dir_tr),'No ground-truth image found, please check the path!');
    assert(length(dir_im)==length(dir_tr),'The number of saliency maps and ground-truth images are not equal!')
    imNum = length(dir_tr);
    precision = zeros(256,1);
    recall = zeros(256,1);
    %% compute pr curve
    for i = 1:imNum
        imName = dir_tr(i).name;
        input_im = imread(fullfile(resultpath, imName));%input_im = imread([imName(1:end-4),resultpath(end-3:end)]);
        truth_im = imread(fullfile(truthpath, imName));%truth_im = imread([truthpath(1:end-5),imName]);%truth_im = imread([truthpath(1:end-4),imName]);
        truth_im = truth_im(:,:,1);
        input_im = input_im(:,:,1);
        if max(max(truth_im))==255
            truth_im = truth_im./255;
        end
        for threshold = 0:255
            index1 = (input_im>=threshold);
            truePositive = length(find(index1 & truth_im));
            groundTruth = length(find(truth_im));
            detected = length(find(index1));
            if truePositive~=0
                precision(threshold+1) = precision(threshold+1)+truePositive/detected;
                recall(threshold+1) = recall(threshold+1)+truePositive/groundTruth;
            end
        end
        display(num2str(i));
    end
    precision = precision./imNum;
    recall = recall./imNum;
    pr = [precision'; recall'];
    fid = fopen([savepath dataset, '_', method, '_PRCurve.txt'],'at');
    fprintf(fid,'%f %f\n',pr);
    fclose(fid);
    disp('Done!');
    

    如果您在发布时遇到更多问题,您可以简单地使用调试器...


    我看不到任何文件名敏感度。
    我尝试了以下 PNG 文件列表:

    1.png
    12345.png
    12346.png
    2.png
    3 - Copy (2).png
    3 - Copy (3).png
    3 - Copy (4).png
    3 - Copy (5).png
    3 - Copy (6).png
    3 - Copy (7).png
    3 - Copy (8).png
    3 - Copy (9).png
    3 - Copy.png
    3.png
    

    【讨论】:

    • 感谢解答,当N小于10时,一切正常。我认为处理两位或三位数字存在一些问题。我想不通的东西!
    • 在我发布的代码中,当imNum 大于10 时,我看不到任何问题,并且在任何文件名中都没有。我用我测试过的文件名编辑了我的帖子......
    猜你喜欢
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2011-02-24
    相关资源
    最近更新 更多