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.
这个算法基于自然语言处理?什么鬼。。。 应该是和自然语言处理的结合。
分这么两步:
extracts SURF features from all images in all image categories
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(), );
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 categories.
--------------------------------------------------------
* Category : airplanes
* Category : ferry
* Category : laptop * Encoding features for category ...done.
* Encoding features for category ...done.
* Encoding features for category ...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 categories.
------------------------------------------------------- * Category : airplanes
* Category : ferry
* Category : laptop * Evaluating images from category ...done.
* Evaluating images from category ...done.
* Evaluating images from category ...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 categories.
------------------------------------------------------- * Category : airplanes
* Category : ferry
* Category : laptop * Evaluating images from category ...done.
* Evaluating images from category ...done.
* Evaluating images from category ...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
MATLAB 图像分类 Image Category Classification Using Bag of Features的更多相关文章
- Bag of Words/Bag of Features的Matlab源码发布
2010年11月19日 ⁄ 技术, 科研 ⁄ 共 1296字 ⁄ 评论数 26 ⁄ 被围观 4,150 阅读+ 由于自己以前发过一篇文章讲bow特征的matlab代码的优化的<Bag-Of-Wo ...
- OpenCV探索之路(二十八):Bag of Features(BoF)图像分类实践
在深度学习在图像识别任务上大放异彩之前,词袋模型Bag of Features一直是各类比赛的首选方法.首先我们先来回顾一下PASCAL VOC竞赛历年来的最好成绩来介绍物体分类算法的发展. 从上表我 ...
- Bag of Features (BOF)图像检索算法
1.首先.我们用surf算法生成图像库中每幅图的特征点及描写叙述符. 2.再用k-means算法对图像库中的特征点进行训练,生成类心. 3.生成每幅图像的BOF.详细方法为:推断图像的每一个特征点与哪 ...
- 图像分类之特征学习ECCV-2010 Tutorial: Feature Learning for Image Classification
ECCV-2010 Tutorial: Feature Learning for Image Classification Organizers Kai Yu (NEC Laboratories Am ...
- 浅析 Bag of Feature
Bag of Feature 是一种图像特征提取方法,它借鉴了文本分类的思路(Bag of Words),从图像抽象出很多具有代表性的「关键词」,形成一个字典,再统计每张图片中出现的「关键词」数量,得 ...
- matlab 局部特征检测与提取(问题与特征)
物体识别:SIFT 特征: 人脸识别:LBP 特征: 行人检测:HOG 特征: 0. 常见手工设计的低级别特征 manually designed low-level features 语音:高斯混合 ...
- 基于Tensorflow + Opencv 实现CNN自定义图像分类
摘要:本篇文章主要通过Tensorflow+Opencv实现CNN自定义图像分类案例,它能解决我们现实论文或实践中的图像分类问题,并与机器学习的图像分类算法进行对比实验. 本文分享自华为云社区< ...
- Masked Label Prediction: Unified Message Passing Model for Semi-Supervised Classification
背景 消息传递模型(Message Passing Model)基于拉普拉斯平滑假设(领居是相似的),试图聚合图中的邻居的信息来获取足够的依据,以实现更鲁棒的半监督节点分类. 图神经网络(Graph ...
- Machine Learning – 第2周(Linear Regression with Multiple Variables、Octave/Matlab Tutorial)
Machine Learning – Coursera Octave for Microsoft Windows GNU Octave官网 GNU Octave帮助文档 (有900页的pdf版本) O ...
随机推荐
- redmine设置
接上篇. 1.redmine新版本已经比较强大了,可以定制所有字段(含标准字段和自定义字段)的读写属性.这里为了避免字段过多影响用户感受,希望增加功能将不相关的字段屏蔽,下载插件Workflow Hi ...
- VS2010调试Qt5的相关设置
1.windows环境,下载离线安装包安装: 2.安装Qt5 Visual Studio Add-in并安装: 3.环境变量里设置QTDIR=D:\LIB\Qt\Qt5.3.2\5.3\msvc201 ...
- 虚拟主机无法使用fsockopen操作处理方法
一.如何禁用fsockopen()下面是两种常用的禁用fsockopen的方法.1.修改php.ini,将 disable_functions = 后加入 fsockopen2.修改php.ini,将 ...
- 如何通过CRM评估客户价值和提高客户忠诚度?
随着市场经济的日益繁荣,同行业之间企业的竞争越来越激烈,企业纷纷各出奇招吸引和挖掘客户,力求让自己的品牌成为更多客户的第一选择.那么,我们可以用什么方法来评估客户价值,提高客户忠诚度呢? 在互联网时代 ...
- dotfiles管理
刚刚知道dotfiles这个东西,百度也没发现什么太有价值的讲解,还都是英文,所以自己立志来好好屡屡清楚 1.dotfiles是什么?我自己的理解:linux下(mac下)有各种app,每个人会根据自 ...
- SC.UI
IController using Microsoft.Practices.Prism.Events; using Microsoft.Practices.Prism.Regions; using M ...
- android 布局优化常用技巧
android对多个模块都要是要的UI逻辑的致辞除了fragment之外,没有别的东西可以支持了, include,merge,viewstub只能支持公用的ui,但是这个通用支持不能包含逻辑(jav ...
- python 学习笔记十四 jQuery案例详解(进阶篇)
1.选择器和筛选器 案例1 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- [问题2014A11] 解答
[问题2014A11] 解答 我们需要利用以下关于幂等阵判定的结论,它是复旦高代书第 142 页的例 3.6.4: 结论 设 \(A\) 为 \(n\) 阶方阵, 则 \(A^2=A\) 当且仅当 ...
- FreeSWITCH中文语音包
一.中文语音资源的获取 官方提供的资源:http://files.freeswitch.org/releases/sounds/ 自己录音 实在不行可以@我给你发一份. 二.中文资源的安装 英文资源的 ...