我一直致力于使用冲浪功能和使用matlab的搜索树进行面部识别的项目2014A我遇到的问题是我无法将knn搜索树的结果与手动与数据库中的每个文件相匹配所以我可以指定哪个面最像样本。任何人都可以帮我找到数据库中最近的三个邻居吗? 这是我的代码
%将所有要素合并到数据集中 我在数据库中为每个脸都有128个冲浪功能 featureDataset = double(vertcat(imageCollection.featureVectors));
% instantiate a kd tree
imageFeatureKDTree = KDTreeSearcher(featureDataset);
query.wholeImage = imread('F\:trio.jpg');
faceDetector = vision.CascadeObjectDetector();
bbox = step(faceDetector, query.wholeImage)
Out=query.wholeImage;
for k = 1:size(bbox,1)
figure; axesHandle=axes; imshow(query.wholeImage); title('Query Image');
rectangleHandle=imrect(axesHandle,bbox(k,:)) ;
% Consider only selected region
query.image=imcrop(query.wholeImage,getPosition(rectangleHandle));
query.image=rgb2gray(query.image);
% Detect SURF features
query.points = detectSURFFeatures(query.image);
% Extract SURF descriptors
[query.featureVectors,query.points] = ...
extractFeatures(query.image,query.points,'SURFSize',128);
[matches, distance] = knnsearch(imageFeatureKDTree,query.featureVectors,'Distance','euclidean','K',3);
indexIntervals = [0, cumsum([imageCollection.featureCount])] + 1;
counts = histc(matches(:, 1), indexIntervals);
counts1=histc(matches(:, 2), indexIntervals);
counts2=histc(matches(:, 3+), indexIntervals);
if max(counts)==0
disp('No Features Matched')
else
for i = 1:numel(imageCollection) % Scale each image
if (counts(i)==max(counts))
name=srcFiles(i).name;
end
if (counts(i)==max(counts1))
name1=srcFiles(i).name;
end
if (counts(i)==max(counts2))
name2=srcFiles(i).name;
end
end
t1 = strcmpi(name(1:10),name1(1:10));t2=strcmpi(name(1:10),name2(1:10));
t3=strcmpi(name1(1:10),name2(1:10));
if((t1==1)||(t2==1)||(t3==1))
Out = insertObjectAnnotation(Out,'rectangle',bbox(k,:),name(1:length(name)-1));
else if (t3==1)
Out = insertObjectAnnotation(Out,'rectangle',bbox(k,:),name1(1:length(name)-1));
else
Out = insertObjectAnnotation(Out,'rectangle',bbox(k,:),'not matched');
end
end
end
end
imshow(Out)