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.

137 lines
4.8 KiB
Matlab

function [param_hough,param_dim]=ism_compare_methods(I,patches,patchClassList,Locations,similarityThres,scales)
% Performs the matching process of the implicit shape model (ISM). It applies two
% different methods. The first uses standard Hough voting for the object
% position, the second uses the PC/BC-DIM algorithm to perform the voting.
% Inputs:
% I - an image on which object detection is to be performed
% patches - the codebook image patches
% patchClassList - the object class associated with each codebook patch
% Locations - the locations of the votes cast by each codebook patch
% similarityThres - the similarity threshold used for clustering patches to form the codebook
%
% patches, patchClassList, Locations, similarityThres are all output by the train_ism function.
%
% Output:
% param_hough - the parameters of potential object locations found by hough voting
% param_dim - the parameters of potential object locations found by the PC/BC-DIM algorithm
%
% Both param_hough and param_dim are cell arrays. The number of cells equals the
% number of classes. Each cell array is a 3xn matrix, where n is the number of
% potential object locations found. For each location 3 parameters are returned,
% the first is the x-coordinate, the second is the y-coordinate, the third is
% the amplitude of the votes at that location.
%
% NOTE: this implementation confounds scale and class!!!
[a,b]=size(I);
%define blank feature maps - one for each codebook entry
indPatchesInClass=find(patchClassList>0); %all non-background patches
numCodebookEntries=length(indPatchesInClass);
for i=1:numCodebookEntries
Fmaps{i}=zeros(a,b,'single');
end
%MATCH CODEBOOK PATCHES TO IMAGE
%find keypoints in test image
patchLen=sqrt(size(patches,2));
patchHalfLen=(patchLen-1)/2;
keypoints = extract_keypoints(I,1000);
maxMatches=1;
for k=1:size(keypoints,1)
%for each keypoint extract an image patch around it
plot(keypoints(k,2),keypoints(k,1),'g+');
Ipatch=extract_patch(I,patchHalfLen,keypoints(k,1:2));
%determine if image patch matches a codebook entry
if ~isnan(Ipatch)
similarity=distance_measure(Ipatch(:)',patches,1);
%[matchingStrength,matchingCluster]=max(similarity);
[matchingStrength,matchingCluster]=sort(similarity,'descend');
for m=1:maxMatches
if patchClassList(matchingCluster(m))>0 && matchingStrength(m)>0 %similarityThres
plot(keypoints(k,2),keypoints(k,1),'ro','LineWidth',1+floor(5*matchingStrength(m)));
%if a match is found enter value into feature map for the matching codebook entry
matchedClassCluster=find(indPatchesInClass==matchingCluster(m));
Fmaps{matchedClassCluster}(keypoints(k,1),keypoints(k,2))=matchingStrength(m);
else
break;%jump out of for-loop the first time the matching criteria is failed
end
end
end
end
drawnow;
%APPLY GENERALISED HOUGH TRANSFORM TO COUNT VOTES
numClasses=length(Locations);
[plotRows,plotCols]=highest_integer_factors(numClasses);
%using standard voting method
figured(3),clf,
H=zeros(a,b,numClasses);
for c=1:numClasses
%convolve image feature maps with vote weight in order to find the accumulator
%array containing a count of votes
for i=1:numCodebookEntries
H(:,:,c)=H(:,:,c)+conv2(Fmaps{i},Locations{c}{i},'same');
end
end
%plot accumulator array
for c=1:numClasses
%plot accumulator array
subplot(plotRows,plotCols,c),
ism_plot_accumulator(H(:,:,c),0.95*max(H(:)));
if c==1, title('Hough'); end
end
if nargin>5 && ~isempty(scales)
%find peaks in a 3-dimensional accumulator array where dimentions represent x-
%and y-coordinates and scale
param_hough{1}=hough_find_peaks(H,[],[],scales);
else
for c=1:numClasses
%for each class separately find peaks in a 2-dimensional accumulator array where
%dimentions represent x- and y-coordinates
param_hough{c}=hough_find_peaks(H(:,:,c),[],[],[]);
end
end
%using DIM
figured(4),clf,
%calculate the accumulator array
H=ism_dim(Fmaps,Locations,[]);
%plot accumulator array
for c=1:numClasses
subplot(plotRows,plotCols,c),
ism_plot_accumulator(H(:,:,c),0.95*max(H(:)));
if c==1, title('PC/BC-DIM'); end
end
if nargin>5 && ~isempty(scales)
%find peaks in a 3-dimensional accumulator array where dimentions represent x-
%and y-coordinates and scale
param_dim{1}=dim_find_peaks(H,[],[],scales);
else
for c=1:numClasses
%for each class separately find peaks in a 2-dimensional accumulator array where
%dimentions represent x- and y-coordinates
param_dim{c}=dim_find_peaks(H(:,:,c),[],[],[]);
end
end
function ism_plot_accumulator(H,scale);
if nargin<2 || isempty(scale)
scale=max(1,0.95*max(max(H)));
end
imagesc(H,[0,max(1e-6,scale)]), hold on,
axis('equal','tight');
set(gca,'FontSize',24);
set(gca,'XTick',[],'YTick',[]);
set(gcf,'PaperSize',[20 15],'PaperPosition',[0 0 20 15]);
cmap=colormap('gray');cmap=1-cmap;colormap(cmap);
drawnow;