gpt4 book ai didi

opencv - 使用OpenCv进行图像特征分类的SVM

转载 作者:行者123 更新时间:2023-12-02 17:54:29 25 4
gpt4 key购买 nike

我的项目范围是通过比较样本图像特征集来识别纸币。在那里,我已经完成了样本图像的特征提取部分。此外,我需要将样本图像功能存储在文本文件或XML文件中,并对其进行分类。
请帮助我通过在OpenCv上使用SVM分类器来进行图像分类

这是我已经完成的特征提取代码。

int main(intargc,char ** argv)
{
/将图像加载为灰度/

//declaring Mat object.This will holds an image(like iplimage in old opencv versions). 

Mat gray_scale_img;


//imread is used to load an image. in here i have load the image as a grayscale image.

gray_scale_img=imread("100.jpg",CV_LOAD_IMAGE_GRAYSCALE);


/*surf detector settings*/

//setting the threshold value.high value will result low number of keypoints.
int hessian=100;

//initializing the surf keypoint detector
SurfFeatureDetectordetector(hessian);


/*detect surf key points*/


//creating vector to store detected keypoints
std::vector<KeyPoint>keypoints;

//detect keypoints
detector.detect(gray_scale_img,keypoints);


/*extract descriptor vectors/feature vectors from each and every keypoints */

SurfDescriptorExtractor extractor;


//this mat object will goinf to hold the extracted descriptors.
Mat descriptors;

//extracting descriptors/features
extractor.compute(gray_scale_img,keypoints,descriptors);

}

最佳答案

OpenCV中的SVM在CvSVM类中实现;

您需要具有矩阵形式的特征 vector (行)。

假设您使用高度,宽度作为特征 vector ,则垫将如下所示(假设您有20个特征 vector ):

Mat FV(20,2, CV_32F);
Mat flagmat(20,1,CV_8U);

/*
code to populate the matrix FV.

Fill the matrix with values so that it will look something as follows:

20 30
30 40
..
..
code to populate the matrix flagmat.
Fill the matrix with labels of each corresponding feature vector in matrix FV. It will look something as follows:
1
-1
1
1
-1
1
1
1
..
*/

CvSVM svm;

svm.train(datamat, flagmat,Mat(),Mat(),CvSVMParams());

Mat testFV(20,2,CV_32F);
Mat sample(1,2,CV_32F);

/* similarly as described above fill testFV matrix*/
float res;// to store result
for(int i =0;i<testFV.rows;i++)
{

sample.at<float>(0,0)=testFV.at<float>(i,0);
sample.at<float>(0,1)=testFV.at<float>(i,1);
float res = svm.predict(sample);
cout<<"predicted label: "<<res<<endl;
}

我假设您可以从特征描述符/ vector 中提取数值,并将其放入上述代码的样本矩阵中。您可以将特征 vector 替换为所使用的任何特征描述符。

关于opencv - 使用OpenCv进行图像特征分类的SVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10041039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com