指定提取的SURF点的大小

时间:2015-12-27 22:21:53

标签: matlab image-processing matlab-cvst surf

我正在尝试提取SURF点,以便在输入图像和训练图像集之间进行模式匹配。我修改了MATLAB的HOG检测代码。但是,我收到错误,因为不同图像的SURF功能大小不同。这在CellSize的HOG检测器中得到了解决,但SURF点没有这样的参数。

有没有办法确保所有图像的SURF功能大小相同?

错误: 下标分配尺寸不匹配。 SURF2出错         features(i,:) = extractFeatures(img,points);

%1.  Load Image Sets 
imgSets = [ imageSet(fullfile('Patterns', 'Cat1')), ...
            imageSet(fullfile('Patterns', 'Cat2')), ...
            imageSet(fullfile('Patterns', 'Cat3'))...
            imageSet(fullfile('Patterns', 'Cat4'))];
{imgSets.Description } % display all labels on one line
[imgSets.Count]         % show the corresponding count of images

%2.  Prepare Training and Validation Image Sets
%2.1 Balance number of each training set 
% determine the smallest amount of images in a category
minSetCount = min([imgSets.Count]);
% Use partition method to trim the set.
imgSets = partition(imgSets, minSetCount, 'randomize');
% Notice that each set now has exactly the same number of images.
[imgSets.Count]

%2.2  Separate sets into training and validation data. 30% of the images
%for training data and the remainder 70% for validation data
[trainingSets, validationSets] = partition(imgSets, 0.3, 'randomize');


img = read(trainingSets(3), 4);
img = rgb2gray(img);
%3.   Detect SURF Points
points =detectSURFFeatures(img);
points = points.selectStrongest(15);
[feats,vPoints] = extractFeatures(img,points);
SURFFeatureSize = length(feats);


trainingFeatures = [];
trainingLabels   = [];

for digit = 1:numel(trainingSets)

    numImages1 = trainingSets(digit).Count;           
    features = zeros(numImages1,SURFFeatureSize,'single');

    for i = 1:numImages1

        img = read(trainingSets(digit), i);
        img = rgb2gray(img);
        points = detectSURFFeatures(img);
        features(i, :) = extractFeatures(img,points);
    end

    if digit== 1 %plaid = 5
    label = [trainingSets(digit).Description,blanks(6)];
    end

    if digit== 2 %patternless = 11
    label = [trainingSets(digit).Description];
    end

    if digit== 3 %striped = 7
    label = [trainingSets(digit).Description,blanks(4)];
    end

    if digit== 4 %irregular = 9
    label = [trainingSets(digit).Description,blanks(2)];
    end

    % Use the imageSet Description as the training labels. .
    labels = repmat(label, numImages1, 1);

    trainingFeatures = [trainingFeatures; features];   %#ok<AGROW>
    trainingLabels   = [trainingLabels;   labels  ];   %#ok<AGROW>

end

谢谢。

2 个答案:

答案 0 :(得分:1)

根据@rayryeng提供的评论,我已将检测到的特征数组填充到设定的预期数组大小,如下所示:

img = read(trainingSets(3), 4);
img = rgb2gray(img);
%3.   Detect SURF Points
points =detectSURFFeatures(img);
points = points.selectStrongest(15);
[feats,vPoints] = extractFeatures(img,points);
currentSize = size(feats);
rowSize = currentSize (:,1);
expectedRowSize = 15;
differenceRowSize = abs(expectedRowSize- rowSize);

if rowSize ~= expectedRowSize
    z =zeros(differenceRowSize,64,'single');
    feats= vertcat(feats,z);
end

还有其他方法可以填充数组(例如使用padarray),但我对这种方法比较熟悉。

请随意提供其他方法以使他人受益。

答案 1 :(得分:1)

您不能像使用HOG一样直接使用SURF。 extractHOGFeatures计算描述整个图像的单个直方图,而extractSURFFeatures采用一组点,并计算每个点周围的描述符,因此它返回多个向量。 extractHOGFeatures也可以计算点描述符,但extractSURFFeatures无法计算全局图像描述符。

如果要将SURF用于图像分类(例如数字识别),则需要将一组SURF描述符转换为单个矢量。一种方法是使用bag-of-features approach