#include <cv.h>
#include <highgui.h>
#include <ml.h>
#include<boost/tokenizer.hpp>
#include<boost/lexical_cast.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
using namespace boost;
#define WIDTH 20
#define HEIGHT 20
int main( )
{
vector<string> img_path;
vector<int> img_catg;
string buf;
ifstream svm_data( "/home/dz/data/label/train" );
string spath="/home/dz/data/imageS/";
char_separator<char> sep(";");
while( svm_data )
{
if( getline( svm_data, buf ) )
{
tokenizer<char_separator<char> > tokens(buf,sep);
tokenizer<char_separator<char> >::iterator it;
it=tokens.begin();
img_path.push_back( spath+*it+".jpg" );++it;
img_catg.push_back( lexical_cast<int>(*it) );//atoi将字符串转换成整型,标志(0,1)
}
}
svm_data.close();//关闭文件
CvMat *data_mat, *res_mat;
int nImgNum = img_path.size(); //读入样本数量
样本矩阵,nImgNum:横坐标是样本数量, WIDTH * HEIGHT:样本特征向量,即图像大小
data_mat = cvCreateMat( nImgNum, WIDTH * HEIGHT, CV_32FC1 );
cvSetZero( data_mat );
//类型矩阵,存储每个样本的类型标志
res_mat = cvCreateMat( nImgNum, 1, CV_32FC1 );
cvSetZero( res_mat );
IplImage *srcImg, *sampleImg;
float b;
unsigned n;
for( string::size_type i = 0; i != img_path.size(); i++ )
{
srcImg = cvLoadImage( img_path[i].c_str(), CV_LOAD_IMAGE_GRAYSCALE );
if( srcImg == NULL )
{
cout<<" can not load the image: "<<img_path[i].c_str()<<endl;
continue;
}
cout<<" processing "<<img_path[i].c_str()<<endl;
sampleImg = cvCreateImage( cvSize( WIDTH, HEIGHT ), IPL_DEPTH_8U, 1 );//样本大小(WIDTH, HEIGHT)
cvResize( srcImg, sampleImg );//改变图像大小
cvSmooth( sampleImg, sampleImg ); //降噪
//生成训练数据
n = 0;
for( int ii = 0; ii < sampleImg->height; ii++ )
{
for( int jj = 0; jj < sampleImg->width; jj++, n++ )
{
// b = (float)((int)((uchar)( sampleImg->imageData + sampleImg->widthStep * ii + jj )) / 255.0 );
b=((float*)(sampleImg->imageData+sampleImg->widthStep*ii))[jj];
cvmSet( data_mat, (int)i, n, b );
}
}
cvmSet( res_mat, i, 0, img_catg[i] );
cout<<" end processing "<<img_path[i].c_str()<<" "<<img_catg[i]<<endl;
}
CvSVM svm = CvSVM();
CvSVMParams param;
CvTermCriteria criteria;
criteria = cvTermCriteria( CV_TERMCRIT_EPS, 1000, FLT_EPSILON );
param = CvSVMParams( CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 1.0, 10.0, 0.5, 1.0, NULL, criteria );
//☆☆☆☆☆☆☆☆☆(5)SVM学习☆☆☆☆☆☆☆☆☆☆☆☆
svm.train( data_mat, res_mat, NULL, NULL, param );
//☆☆利用训练数据和确定的学习参数,进行SVM学习☆☆☆☆
svm.save( "SVM_DATA.xml" );
cout<<"adsf "<<endl;
//检测样本
IplImage *tst, *tst_tmp;
vector<string> img_tst_path;
ifstream img_tst( "/home/dz/data/label/test" );
while( img_tst )
{
if( getline( img_tst, buf ) )
{
tokenizer<char_separator<char> > tokens(buf,sep);
tokenizer<char_separator<char> >::iterator it;
it=tokens.begin();
img_tst_path.push_back( spath+*it+".jpg" );
}
}
img_tst.close();
CvMat *tst_mat = cvCreateMat( 1, WIDTH*HEIGHT, CV_32FC1 );
char line[512];
ofstream predict_txt( "SVM_PREDICT.txt" );
for( string::size_type j = 0; j != img_tst_path.size(); j++ )
{
tst = cvLoadImage( img_tst_path[j].c_str(), CV_LOAD_IMAGE_GRAYSCALE );
if( tst == NULL )
{
cout<<" can not load the image: "<<img_tst_path[j].c_str()<<endl;
continue;
}
tst_tmp = cvCreateImage( cvSize( WIDTH, HEIGHT ), IPL_DEPTH_8U, 1 );
cvResize( tst, tst_tmp );
cvSmooth( tst_tmp, tst_tmp );
n = 0;
for(int ii = 0; ii < tst_tmp->height; ii++ )
{
for(int jj = 0; jj < tst_tmp->width; jj++, n++ )
{
//b = (float)(((int)((uchar)tst_tmp->imageData+tst_tmp->widthStep*ii+jj))/255.0);
b=((float*)(tst_tmp->imageData+tst_tmp->widthStep*ii))[jj];
cvmSet( tst_mat, 0, n, (double)b );
}
}
int ret = svm.predict( tst_mat );
sprintf( line, "%s %d\r\n", img_tst_path[j].c_str(), ret );
predict_txt<<line;
}
predict_txt.close();
cvReleaseImage( &srcImg );
cvReleaseImage( &sampleImg );
cvReleaseImage( &tst );
cvReleaseImage( &tst_tmp );
cvReleaseMat( &data_mat );
cvReleaseMat( &res_mat );
return 0;
}
svm图像训练与测试
最新推荐文章于 2023-02-06 11:28:25 发布