You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
4.5 KiB
Matlab
175 lines
4.5 KiB
Matlab
function stats=stats_ism_horses
|
|
thresholds=[0.05:0.05:2];
|
|
method{1}='Hough';method{2}='PC/BC-DIM';%method{3}='baseline';method{4}='groundtruth';
|
|
numTest=228;
|
|
spread=10
|
|
|
|
%DEFINE CODEBOOK
|
|
load('ISM_codebook_horses.mat');
|
|
Locations=postprocess_codebook(Locations,patchClassList,spread);
|
|
|
|
%FOR EACH TEST IMAGE PERFORM ISM TO LOCATE HORSE
|
|
disp('Processing Images')
|
|
for i=1:numTest*2
|
|
fprintf(1,'.%i.',i);
|
|
%load image and display
|
|
if i<=numTest
|
|
[I,groundtruth]=load_image_horse(100+i);%1st 100 images were used for training
|
|
else
|
|
I=load_image_101(100+i-numTest); groundtruth=I.*0;
|
|
%I=load_image_car(100+i-numTest,2); groundtruth=I.*0;
|
|
end
|
|
I=padarray(I,[4,4],'replicate','both');
|
|
groundtruth=padarray(groundtruth,[4,4],0,'both');
|
|
figured(2),clf, imagesc(I), hold on, axis('equal','tight'); title(' ');
|
|
colormap('gray');
|
|
|
|
%APPLY METHODS
|
|
[param{i}{1},param{i}{2}]=ism_compare_methods(I,patches,patchClassList,Locations,similarityThres);
|
|
trueBoundingBox{i}=groundtruth;
|
|
|
|
if length(method)>=3,
|
|
param{i}{3}{1}(1:3,1)=[size(I')'./2;1]; %baseline - object in centre of image
|
|
end
|
|
if length(method)>=4,
|
|
param{i}{4}{1}(1:3,1)=[centroid(groundtruth)';1]; %groundtruth - object at correct position
|
|
end
|
|
%[~,la(i),lb(i)]=bounding_box(groundtruth); %measure bounding box size (need to change for-loop parameters to use training images)
|
|
end
|
|
%mean(la),mean(lb)
|
|
|
|
%FOR EACH METHOD AND THRESHOLD TEST ACCURACY WITH WHICH HORSES ARE LOCATED
|
|
for m=1:length(method)
|
|
disp(' ');
|
|
disp(method{m});
|
|
|
|
disp('Testing Accuracy for threshold:')
|
|
t=0;
|
|
for thres=thresholds.*m %*m to make thresholds larger for DIM
|
|
t=t+1;
|
|
fprintf(1,'.%2.2f.',thres);
|
|
stats{m}(t,:)=[0,0,0];
|
|
|
|
%evaluate performance with each test image
|
|
for i=1:numTest*2
|
|
%find coordinates for which the votes exceed the threshold
|
|
index=find(param{i}{m}{1}(3,:)>thres);
|
|
coords=param{i}{m}{1}(1:2,index);
|
|
|
|
stats{m}(t,:)=horse_evaluation(stats{m}(t,:),coords,trueBoundingBox{i});
|
|
end
|
|
f1score{m}(t)=calc_f1score(stats{m}(t,:));
|
|
end
|
|
end
|
|
|
|
%PLOT THE RESULTS
|
|
disp(' ');
|
|
max_error=0;
|
|
for m=1:length(method)
|
|
max_error=max(max_error,max(sum(stats{m}(:,2:3),2)));
|
|
disp(['f1score ',method{m}]);
|
|
disp(f1score{m})
|
|
end
|
|
lineStyle{1}='b-s';lineStyle{2}='r-d';lineStyle{3}='g-o';lineStyle{4}='m-x';
|
|
|
|
figured(11),clf
|
|
for m=1:length(method)
|
|
subplot(1,length(method),m),plot_errors(thresholds.*m,stats{m},max_error);title(method{m})
|
|
end
|
|
|
|
figured(12),clf
|
|
for m=1:length(method)
|
|
plot_precision_recall(stats{m},lineStyle{m}), hold on
|
|
end
|
|
legend(method,'Location','southeast')
|
|
|
|
figured(13),clf
|
|
for m=1:length(method)
|
|
plot_RFPPI(stats{m},lineStyle{m},numTest), hold on
|
|
end
|
|
legend(method,'Location','southeast')
|
|
|
|
|
|
|
|
|
|
|
|
function stats=horse_evaluation(stats,pos,groundtruth)
|
|
%assumes images contain exactly 1 or exactly zero objects, as overlap is calculated for whole image, not for multiple separate bounding boxes.
|
|
if sum(sum(groundtruth))>0, numObj=1; else, numObj=0; end
|
|
|
|
|
|
%make bounding box for groundtruth (padding image to ensure any predictions of box position will fit)
|
|
[a,b]=size(groundtruth);
|
|
pad=max(a,b);
|
|
groundtruth=padarray(groundtruth,[pad,pad],0,'both');
|
|
box_true=bounding_box(groundtruth);
|
|
|
|
%make bounding box around each predicted position - and calculate overlap with
|
|
%groundtruth bounding box. Note: average size of bounding box for the 100
|
|
%training images is 67x54 pixels
|
|
num=size(pos,2);
|
|
overlap=[];
|
|
for i=1:num
|
|
y=pad+pos(2,i);
|
|
x=pad+pos(1,i);
|
|
box_predict=zeros(size(groundtruth));
|
|
box_predict(floor(y-54/2):ceil(y+54/2),floor(x-67/2):ceil(x+67/2))=1;
|
|
overlap(i)=sum(sum(box_predict.*box_true))./sum(sum(max(box_predict,box_true)));
|
|
end
|
|
overlap=max(overlap);
|
|
|
|
%update counts of true positives, false positives, and false negatives
|
|
if num==0
|
|
TP=0;FP=0;FN=numObj;
|
|
elseif num==1
|
|
if overlap>0.5
|
|
TP=1;FP=0;FN=0;
|
|
else
|
|
TP=0;FP=1;FN=numObj;
|
|
end
|
|
else
|
|
if overlap>0.5
|
|
TP=1;FP=num-1;FN=0;
|
|
else
|
|
TP=0;FP=num;FN=numObj;
|
|
end
|
|
end
|
|
stats=stats+[TP,FP,FN];
|
|
|
|
|
|
|
|
|
|
function stats=horse_evaluation_dist(stats,pos,pos_true)
|
|
pos
|
|
num=size(pos,2);
|
|
dist=min(sqrt(sum(bsxfun(@minus,pos,pos_true).^2)));
|
|
|
|
thres=8;
|
|
|
|
if num==0
|
|
TP=0;FP=0;FN=1;
|
|
elseif num==1
|
|
if dist<thres
|
|
TP=1;FP=0;FN=0;
|
|
else
|
|
TP=0;FP=1;FN=1;
|
|
end
|
|
else
|
|
if dist<thres
|
|
TP=1;FP=num-1;FN=0;
|
|
else
|
|
TP=0;FP=num;FN=1;
|
|
end
|
|
end
|
|
stats=stats+[TP,FP,FN];
|
|
|
|
|
|
|
|
|
|
function [box,la,lb]=bounding_box(I)
|
|
box=zeros(size(I));
|
|
[arange,brange]=find(I>0.5);
|
|
box(min(arange):max(arange),min(brange):max(brange))=1;
|
|
la=max(arange)-min(arange)+1;
|
|
lb=max(brange)-min(brange)+1;
|