简体   繁体   English

使用SVM进行图像分类-Python

[英]Image classification using SVM - Python

I have a set of images classified as good quality image and bad quality image. 我有一组图像分为高质量图像和劣质图像。 I have to train a classification model so that any new image can be classified as good/bad. 我必须训练一个分类模型,以便可以将任何新图像分类为好/坏。 SVM seems to be the best approach to do it. SVM似乎是最好的方法。 I have done image processing in MATLAB but not in python. 我已经在MATLAB中完成了图像处理,但没有在python中完成。

Can anyone suggest how to do it in python? 谁能建议如何在python中做到这一点? What are the libraries? 什么是图书馆? For SVM scikit is there, what about feature extraction of image and PCA? 对于SVM scikit,图像和PCA的特征提取如何?

I would start reading this simple tutorial and then move into the OpenCV tutorials for Python. 我将开始阅读这个简单的教程,然后进入Python的OpenCV教程。 Also, if you are familiar with the sklearn interface there is Scikit-Image . 另外,如果您熟悉sklearn界面,则可以使用Scikit-Image

I am using opencv 2.4,python 2.7 and pycharm 我正在使用opencv 2.4,python 2.7和pycharm

SVM is a machine learning model for data classification.Opencv2.7 has pca and svm.The steps for building an image classifier using svm is SVM是用于数据分类的机器学习模型.Opencv2.7具有pca和svm。使用svm构建图像分类器的步骤是

  1. Resize each image 调整每个图像的大小
  2. convert to gray scale 转换为灰度
  3. find PCA 找到PCA
  4. flat that and append it to training list 放平并添加到培训列表中
  5. append labels to training labels 将标签附加到训练标签

Sample code is 示例代码为

for file in listing1:
 img = cv2.imread(path1 + file)
 res=cv2.resize(img,(250,250))
 gray_image = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
 xarr=np.squeeze(np.array(gray_image).astype(np.float32))
 m,v=cv2.PCACompute(xarr)
 arr= np.array(v)
 flat_arr= arr.ravel()
 training_set.append(flat_arr)
 training_labels.append(1)

Now Training 现在训练

trainData=np.float32(training_set)
responses=np.float32(training_labels)
svm = cv2.SVM()
svm.train(trainData,responses, params=svm_params)
svm.save('svm_data.dat')

I think this will give you some idea. 我想这会给你一些想法。

Take a look at dlib and opencv . 看一下dlibopencv Both are mature computer vision frameworks implemented in C++ with python bindings. 两者都是使用python绑定在C ++中实现的成熟的计算机视觉框架。 That is important because it means it is relying on compiled code under the hood so it is significantly faster than if it was done in straight python. 这很重要,因为这意味着它依赖引擎盖下的已编译代码,因此比直接使用python进行编译要快得多。 I believe the implementation of the SVM in dlib is based on more resent research at the moment so you may want to take that into consideration as you may get better results using it. 我相信目前dlib中SVM的实现是基于最近的研究,因此您可能需要考虑这一点,因为使用它可能会获得更好的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM