答案 0 :(得分:1)
由于您提到您使用的是Accord.NET,我相信您可能需要查看the examples for Bag-of-Visual-Words。更具体地说,由于您提到了纹理识别,您可能应该考虑使用Haralick特征描述符从图像中提取纹理特征的BoVW示例。这个例子如下:
// Ensure results are reproducible
Accord.Math.Random.Generator.Seed = 0;
// The Bag-of-Visual-Words model converts images of arbitrary
// size into fixed-length feature vectors. In this example, we
// will be setting the codebook size to 3. This means all feature
// vectors that will be generated will have the same length of 3.
// By default, the BoW object will use the sparse SURF as the
// feature extractor and K-means as the clustering algorithm.
// In this example, we will use the Haralick feature extractor.
// Create a new Bag-of-Visual-Words (BoW) model using Haralick features
var bow = BagOfVisualWords.Create(new Haralick()
{
CellSize = 256, // divide images in cells of 256x256 pixels
Mode = HaralickMode.AverageWithRange,
}, new KMeans(3));
// Generate some training images. Haralick is best for classifying
// textures, so we will be generating examples of wood and clouds:
var woodenGenerator = new WoodTexture();
var cloudsGenerator = new CloudsTexture();
Bitmap[] images = new[]
{
woodenGenerator.Generate(512, 512).ToBitmap(),
woodenGenerator.Generate(512, 512).ToBitmap(),
woodenGenerator.Generate(512, 512).ToBitmap(),
cloudsGenerator.Generate(512, 512).ToBitmap(),
cloudsGenerator.Generate(512, 512).ToBitmap(),
cloudsGenerator.Generate(512, 512).ToBitmap()
};
// Compute the model
bow.Learn(images);
bow.ParallelOptions.MaxDegreeOfParallelism = 1;
// After this point, we will be able to translate
// images into double[] feature vectors using
double[][] features = bow.Transform(images);
为了将此示例应用于您的问题,您可以使用images
和wood
纹理生成器创建cloud
变量,而不是从您自己的图像数据库中检索它们。稍后,在为数据集中的每个图像提取特征表示后,可以使用这些表示来学习任何机器学习分类器,例如支持向量机,使用类似于以下的代码:
// Now, the features can be used to train any classification
// algorithm as if they were the images themselves. For example,
// let's assume the first three images belong to a class and
// the second three to another class. We can train an SVM using
int[] labels = { -1, -1, -1, +1, +1, +1 };
// Create the SMO algorithm to learn a Linear kernel SVM
var teacher = new SequentialMinimalOptimization<Linear>()
{
Complexity = 100 // make a hard margin SVM
};
// Obtain a learned machine
var svm = teacher.Learn(features, labels);
// Use the machine to classify the features
bool[] output = svm.Decide(features);
// Compute the error between the expected and predicted labels
double error = new ZeroOneLoss(labels).Loss(output); // should be 0
P.S。:如果您考虑使用ChiSquare kernel而不是线性创建SVM,则可能会在分类问题中获得更好的性能。