使用k均值算法对图像数据集的SURF特征进行聚类 [英] Clustering SURF features of an image dataset using k-means algorithm

查看:697
本文介绍了使用k均值算法对图像数据集的SURF特征进行聚类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MATLAB中实现Bag of Visual Words。首先,我从数据集目录中读取图像,然后检测SURF功能并使用这两个函数 detectSURFFeatures extractFeatures 提取它们。

I want to implement Bag of Visual Words in MATLAB. First I read images from dataset directory and I detect SURF features and extract them using these two functions detectSURFFeatures and extractFeatures.

我将每个特征存储到一个单元格数组中,最后我想使用k-means算法对它们进行聚类,但我无法将这些数据放入k-means函数输入中。如何在MATLAB中将SURF特征插入k-means聚类算法?

I store each feature into a cell array and finally I want to cluster them using the k-means algorithm but I can't fit this data into k-means function input. How can I insert SURF features into the k-means clustering algorithm in MATLAB?

这是我的示例代码,它从文件中读取图像并提取其SURF特征。

Here is my sample code which reads image from files and extracts their SURF features.

clc;
clear;
close all;
folder = 'CarData/TrainImages/cars';
filePattern = fullfile(folder, '*.pgm');
f=dir(filePattern);
files={f.name}; 
for k=1:numel(files)
    fullFileName = fullfile(folder, files{k});
    image=imread(fullFileName);
    temp =  detectSURFFeatures(image);
    [im_features, temp] = extractFeatures(image, temp);
    features{k}= im_features;

end

[centers, assignments] = kmeans(double(features), 100);


推荐答案

kmeans 预计 N x P 输入数据的矩阵,其中 N 是示例总数, P 是总数功能。您正在做的不正确的是将每个要素矩阵放入单元格数组中。你需要做的是将所有图像中的所有特征连接成一个矩阵。

kmeans expects a N x P matrix for the input data where N is the total number of examples and P is the total number of features. What you are doing incorrectly is placing each feature matrix into a cell array. What you have to do instead is to concatenate all of the features from all of the images into a single matrix.

最简单的方法是添加以下内容 kmeans 之前的代码:

The easiest way to do that would be to add the following code before your kmeans call:

features = vertcat(features{:});

函数 vertcat 将给出一个矩阵列表,它们共享相同数量的列,从而将矩阵垂直堆叠在一起。执行功能{:} 提取以逗号分隔的列表,因此它等同于:

The function vertcat will vertically stack matrices together given a list of matrices that all share the same number of columns. Doing features{:} extracts out a comma-separated list so that it is equivalent to doing:

features = vertcat(features{1}, features{2}, ...);

最终效果是,它会将每个图像的所有SURF特征垂直叠加到一起二维矩阵。您使用的是默认版本的SURF,因此每个要素的长度应为64,因此您应该有64列。行数应该是在所有图像上检测到的特征总数。

The final effect is that this will vertically stack all of the SURF features from every single image together into a 2D matrix. You are using the default version of SURF, so each feature should be of length 64, so you should have 64 columns. The number of rows should be the total number of features detected over all images.

因此,绝对清楚:

clc;
clear;
close all;
folder = 'CarData/TrainImages/cars';
filePattern = fullfile(folder, '*.pgm');
f=dir(filePattern);
files={f.name}; 
for k=1:numel(files)
    fullFileName = fullfile(folder, files{k});
    image=imread(fullFileName);
    temp =  detectSURFFeatures(image);
    [im_features, temp] = extractFeatures(image, temp);
    features{k}= im_features;

end

% New code
features = vertcat(features{:});

% Resume old code
[centers, assignments] = kmeans(double(features), 100);

这篇关于使用k均值算法对图像数据集的SURF特征进行聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆