• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

MATLAB 图像分类 Image Category Classification Using Bag of Features - 爱女王,爱 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

View Post

MATLAB 图像分类 Image Category Classification Using Bag of Features

使用MATLAB实现图像的识别,这是MATLAB官网上面的例子,学习一下。

http://cn.mathworks.com/help/vision/examples/image-category-classification-using-bag-of-features.html

 

这个算法叫做a bag of features approach for image category classification,用于识别小图片里面的是小狗、小猫、还是火车、船等。

首先要下载原材料,用于训练

% Location of the compressed data set
url = \'http://www.vision.caltech.edu/Image_Datasets/Caltech101/101_ObjectCategories.tar.gz\';
% Store the output in a temporary folder
outputFolder = fullfile(tempdir, \'caltech101\'); % define output folde

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\')) ];

imageSet :   Use imageSet class to help you manage the data. Since imageSet operates on image file locations, and therefore does not load all the images into memory, it is safe to use on large image collections.

在控制台用下面的代码查看imgSets的一些属性

{ imgSets.Description } % display all labels on one line
[imgSets.Count]         % show the corresponding count of images

 

对图片集进行预处理

首先,每个集合的图片数量不一致

minSetCount = min([imgSets.Count]); % determine the smallest amount of images in a category

% Use partition method to trim the set.
imgSets = partition(imgSets, minSetCount, \'randomize\');

接下来,将这些图片分为用于训练的和用于检验的,分别是30%和70%

[trainingSets, validationSets] = partition(imgSets, 0.3, \'randomize\');

 

创建一个训练的那个东东——Create a Visual Vocabulary and Train an Image Category Classifier

Bag of words is a technique adapted to computer vision from the world of natural language processing. Since images do not actually contain discrete words, we first construct a "vocabulary" of SURF features representative of each image category.

这个算法基于自然语言处理?什么鬼。。。  应该是和自然语言处理的结合。

分这么两步:

  1. extracts SURF features from all images in all image categories

  2. constructs the visual vocabulary by reducing the number of features through quantization of feature space using K-means clustering

bag = bagOfFeatures(trainingSets);

上面这句话提取图片中的特征,输出这些,每次训练会不一样

Creating Bag-Of-Features from 3 image sets.
--------------------------------------------
* Image set 1: airplanes.
* Image set 2: ferry.
* Image set 3: laptop.

* Extracting SURF features using the Grid selection method.
** The GridStep is [8 8] and the BlockWidth is [32 64 96 128].

* Extracting features from 20 images in image set 1...done. Extracted 86144 features.
* Extracting features from 20 images in image set 2...done. Extracted 70072 features.
* Extracting features from 20 images in image set 3...done. Extracted 97888 features.

* Keeping 80 percent of the strongest features from each image set.

* Balancing the number of features across all image sets to improve clustering.
** Image set 2 has the least number of strongest features: 56058.
** Using the strongest 56058 features from each of the other image sets.

* Using K-Means clustering to create a 500 word visual vocabulary.
* Number of features          : 168174
* Number of clusters (K)      : 500

* Clustering...done.

* Finished creating Bag-Of-Features

可视化,有点搞不懂了。。。到底在做什么

貌似是提取了500个特征,取某一图片,看看这个图片多大程度上有这些特征。

先说结果:

 

下面是代码:

Additionally, the bagOfFeatures object provides an encode method for counting the visual word occurrences in an image. It produced a histogram that becomes a new and reduced representation of an image.

This histogram forms a basis for training a classifier and for the actual image classification. In essence, it encodes an image into a feature vector.

img = read(imgSets(1), 1);
featureVector = encode(bag, img);

% Plot the histogram of visual word occurrences
figure
bar(featureVector)
title(\'Visual word occurrences\')
xlabel(\'Visual word index\')
ylabel(\'Frequency of occurrence\')

 

然后创建图像的分类器

Encoded training images from each category are fed into a classifier training process invoked by the trainImageCategoryClassifier function. Note that this function relies on the multiclass linear SVM classifier from the Statistics and Machine Learning Toolbox™.

categoryClassifier = trainImageCategoryClassifier(trainingSets, bag);

输出这个:

Training an image category classifier for 3 categories.
--------------------------------------------------------
* Category 1: airplanes
* Category 2: ferry
* Category 3: laptop

* Encoding features for category 1...done.
* Encoding features for category 2...done.
* Encoding features for category 3...done.

* Finished training the category classifier. Use evaluate to test the classifier on a test set.

 

评估这个分类器的效果

confMatrix = evaluate(categoryClassifier, trainingSets);

输出这个,效果貌似很不错

Evaluating image category classifier for 3 categories.
-------------------------------------------------------

* Category 1: airplanes
* Category 2: ferry
* Category 3: laptop

* Evaluating 20 images from category 1...done.
* Evaluating 20 images from category 2...done.
* Evaluating 20 images from category 3...done.

* Finished evaluating all the test sets.

* The confusion matrix for this test set is:


                        PREDICTED
KNOWN        | airplanes   ferry   laptop   
--------------------------------------------
airplanes    | 0.95        0.05    0.00     
ferry        | 0.00        1.00    0.00     
laptop       | 0.00        0.00    1.00     

* Average Accuracy is 0.98.

用训练的素材检验当然效果很好,下面用之前分好的检验素材进行检验,默认返回的是“混淆矩阵”,所以要计算平均的分类精度。

confMatrix = evaluate(categoryClassifier, validationSets);

% Compute average accuracy
mean(diag(confMatrix));

输出这个,效果还是很不错!

Evaluating image category classifier for 3 categories.
-------------------------------------------------------

* Category 1: airplanes
* Category 2: ferry
* Category 3: laptop

* Evaluating 47 images from category 1...done.
* Evaluating 47 images from category 2...done.
* Evaluating 47 images from category 3...done.

* Finished evaluating all the test sets.

* The confusion matrix for this test set is:


                        PREDICTED
KNOWN        | airplanes   ferry   laptop   
--------------------------------------------
airplanes    | 0.85        0.13    0.02     
ferry        | 0.02        0.94    0.04     
laptop       | 0.04        0.02    0.94     

* Average Accuracy is 0.91.

 

训练了,检验了,该上战场了!——Try the Newly Trained Classifier on Test Images

img = imread(fullfile(rootFolder, \'airplanes\', \'image_0690.jpg\'));
[labelIdx, scores] = predict(categoryClassifier, img);

% Display the string label
categoryClassifier.Labels(labelIdx)

 

——完——

 

由于不懂算法,所以很多地方看不懂,无法扩展使用这个例子。

扩展阅读:

MATLAB官方文档:bagOfFeatures class

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi线程Timer(TThreadTimer)发布时间:2022-07-18
下一篇:
Delphi-关于错误E2154Type'%s'needsfinalization-notallowedinvariantrecord发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap