ZUDN

博客园 首页 新随笔 联系 订阅 管理

#include <cv.h>
#include <highgui.h>
#include <ml.h>

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

#define WIDTH 28
#define HEIGHT 30

int main( /*int argc, char** argv*/ )
{
vector<string> img_path;
vector<int> img_catg;
int nLine = 0;
string buf;
ifstream svm_data( "SVM_DATA.txt" );

while( svm_data )
{
   if( getline( svm_data, buf ) )
   {
    nLine ++;
    if( nLine % 2 == 1 )
    {
     img_catg.push_back( atoi( buf.c_str() ) );
    }
    else
    {
     img_path.push_back( buf );
    }
   }
}
svm_data.close();

CvMat *data_mat, *res_mat;
int nImgNum = nLine / 2;
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;
DWORD 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 );
   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 = (int)*((char*)( sampleImg->imageData + sampleImg->widthStep * ii + jj  )) / 255.0F ; 

     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 );
svm.train( data_mat, res_mat, NULL, NULL, param );
svm.save( "SVM_DATA.xml" );

IplImage *tst, *tst_tmp;
vector<string> img_tst_path;
ifstream img_tst( "SVM_TEST.txt" );
while( img_tst )
{
   if( getline( img_tst, buf ) )
   {
    img_tst_path.push_back( buf );
   }
}
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[i].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 = (int)*((char*)( sampleImg->imageData + sampleImg->widthStep * ii + jj  )) / 255.0F ; 

     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;
}

posted on 2011-06-24 12:43  ZUDN  阅读(2017)  评论(1编辑  收藏  举报