Caltech是常用的图像识别数据库之一,加州理工有一个在Caltech上的图像检索工具包image-search ,提供了很多图像检索需要的功能,其中包括基于BOW的图像检索。该工具包的使用有一定难度,并且涉及函数很多。在Matlab2016中也有Caltech101的图像分类和识别程序,代码数量很少。
% 数据库下载地址,第一次执行需要联网下载
url ='http://www.vision.caltech.edu/Image_Datasets/Caltech101/101_ObjectCategories.tar.gz';
outputFolder = fullfile(tempdir, 'caltech101');
if ~exist(outputFolder, 'dir') % download only once
disp('Downloading 126MB Caltech101 data set...');
untar(url, outputFolder);
end
rootFolder = fullfile(outputFolder, '101_ObjectCategories');
%为加快速度只取三类图像
imgSets = [ imageSet(fullfile(rootFolder, 'airplanes')), ...
imageSet(fullfile(rootFolder, 'ferry')), ...
imageSet(fullfile(rootFolder, 'laptop')) ];
%取各类图像数目最小值
minSetCount = min([imgSets.Count]);
%图像集分割
[trainingSets, validationSets] = partition(imgSets, 0.3, 'randomize');
bag = bagOfFeatures(trainingSets);
img = read(imgSets(1), 1);
%使用图像词包(bag)对每幅图像进行编码,得出一个定长的向量
featureVector = encode(bag, img);
figure;
%图像向量分布图
bar(featureVector);
title('Visual word occurrences');
xlabel('Visual word index');
ylabel('Frequency of occurrence');
%训练图像类别分类器
categoryClassifier = trainImageCategoryClassifier(trainingSets, bag);
%计算训练集上的混淆矩阵
confMatrix = evaluate(categoryClassifier, trainingSets);
confMatrix = evaluate(categoryClassifier, validationSets);
% 计算平均准确度
mean(diag(confMatrix));
img = imread(fullfile(rootFolder, 'airplanes', 'image_0690.jpg'));
%对指定图像进行识别
[labelIdx, scores] = predict(categoryClassifier, img);
该示例关键代码也只有几行,除了计算图象集词包,还利用encode函数对图像编码,生成了特征向量,也有人叫做视觉向量。该示例中,仅画出了视觉向量的分布图,没有它用。事实上,该向量可作为图像特征进一步完成图像检索、分类、识别等任务,相比直接使用提取词袋的局部特征,可大大减少计算复杂度和内存消耗。