批处理图像的索引不匹配

问题描述 投票:0回答:1

为了计算一组图像的精确召回率,我获得了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个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.