Hu矩SVM训练及检测-----OpenCV
关键词:Hu矩,SVM,OpenCV
在图像中进行目标物识别,涉及到特定区域内是否存在目标物,SVM可在样本量较少情况下对正负样本(图片中前景背景)做出良好区分,图片基本特征包括诸如HOG、LBP、HAAR等,在具体进行物体检测时考虑结合待检测物特点利用或设计新特征进行训练并分类。本文以几何不变矩为例说明OpenCV中SVM分类器的一般使用过程,下面依次简述Hu矩函数、SVM参数设置及实例演示。
1.Hu求解
double M[7];//Hu矩输出
Moments mo; //矩变量
src=imread(path, IMREAD_GRAYSCALE);//获取图像
canny_output=preDispose(src);//原始图像初步处理
resize(canny_output, imageNewSize, sampleSize, CV_INTER_LINEAR);//尺寸归一化
//计算Hu矩
mo=moments(imageNewSize);
HuMoments(mo, M);
2.SVM训练过程
1)训练矩阵变量
Mat trainData(a,b,CV_32FC1);//待训练样本集 a:样本总数 b:特征个数
Mat labels(a,1,CV_32FC1);//与训练数据相应的标签 a:样本总数
2)SVM参数设置
CvSVMParams SVM_params; // CvSVMParams结构用于定义基本参数
SVM_params.svm_type = CvSVM::C_SVC; // SVM类型
SVM_params.kernel_type = CvSVM::LINEAR; // 不做映射
SVM_params.degree = 0;
SVM_params.gamma = 1;
SVM_params.coef0 = 0;
SVM_params.C = 1;
SVM_params.nu = 0;
SVM_params.p = 0;
SVM_params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, 0.01);
3)SVM训练及保存
CvSVM svm;
svm.train(trainData, labels, Mat(), Mat(), SVM_params);
svm.save("svm_para.xml");
4)SVM样本检测
CvSVM svm;
svm.load("svm_para.xml");
float response = svm.predict(test);//test:待检测样本特征
3.训练及检测实例
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/ml/ml.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Mat preDispose(Mat src);
Size sampleSize(160,160);//样本的大小
int main()
{
Mat trainData(20,7,CV_32FC1);//待训练样本集
Mat labels(20,1,CV_32FC1);//与训练数据相应的标签
Mat canny_output;
Mat imageNewSize;
char path[90];//图片路径
Mat src;//输入图片
double M[7];//Hu矩
Moments mo; //矩变量
float* p; //data行变量
int train_samples=10;
for(int i=0;i<2;++i)
{
for(int j=0;j<10;++j)
{
if(i==0)
sprintf_s(path, "negtive/%d.jpg", j);
else
sprintf_s(path, "positive/%d.jpg", j);
src=imread(path, IMREAD_GRAYSCALE);
canny_output=preDispose(src);
resize(canny_output, imageNewSize, sampleSize, CV_INTER_LINEAR);
//计算Hu矩
mo=moments(imageNewSize);
HuMoments(mo, M);
//训练样本集赋值
Mat C = (Mat_<double>(1,7) << M[0],M[1],M[2],M[3],M[4],M[5], M[6]);
C.convertTo(trainData(Range(i*train_samples + j, i*train_samples + j + 1), Range(0, trainData.cols)), CV_32FC1);
//标签赋值
labels.at<float>(i*train_samples + j,0) = i;
}
}
CvSVMParams SVM_params; // CvSVMParams结构用于定义基本参数
SVM_params.svm_type = CvSVM::C_SVC; // SVM类型
SVM_params.kernel_type = CvSVM::LINEAR; // 不做映射
SVM_params.degree = 0;
SVM_params.gamma = 1;
SVM_params.coef0 = 0;
SVM_params.C = 1;
SVM_params.nu = 0;
SVM_params.p = 0;
SVM_params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, 0.01);
CvSVM svm;
svm.train(trainData, labels, Mat(), Mat(), SVM_params);
SVM.save("svm_para.xml");
//CvSVM svm;
//svm.load("svm_para.xml");
for(int i=0;i<8;++i)
{
sprintf_s(path, "test/%d.jpg", i);
src=imread(path, IMREAD_GRAYSCALE);
canny_output=preDispose(src);
resize(canny_output, imageNewSize, sampleSize, CV_INTER_LINEAR);
imshow("canny",imageNewSize);
//计算Hu矩
mo=moments(imageNewSize);
HuMoments(mo, M);
//样本赋值
Mat test(1,7,CV_32FC1);
Mat C = (Mat_<double>(1,7) << M[0],M[1],M[2],M[3],M[4],M[5], M[6]);
C.convertTo(test(Range(0, 1), Range(0, 7)), CV_32FC1);
float response = svm.predict(test);
cout<<response<<endl;
}
waitKey(0);
return EXIT_SUCCESS;
}
资源:
https://share.weiyun.com/5LRwSxS
https://share.weiyun.com/5kLvDQS