opencv data training

#include <the io.h> 
#include < String > 
#include <the iostream> 
#include <opencv2 \ opencv.hpp> 
#include <opencv2 \ ml.hpp>
 the using  namespace CV;
 the using  namespace ml;
 int main () 
{ 
    // ========= ======== reading the image data to create a training // 
    // all unified into a picture size 16 * 8 
    const  int imageRows = 8 ;
     const  int imageCols = 16 ;
     // pictures A total of 10 class 
    const  int classSum = 10 ;
    // for each type of 50 pictures 
    const  int imagesSum = 50 ;
     // every line in a training image for 
    a float trainingData [classSum imagesSum *] [* imageRows imageCols] = {{ 0 }};
     // training sample label 
    a float Labels [classSum * imagesSum] [classSum] = {{ 0 }}; 
    Mat the src, resizeImg, trainImg; 
    for ( int I = 0 ; I <classSum; I ++ ) 
    { 
        // destination folder path 
        STD :: String INPATH = " E: \\ Arabic numerals learning materials \\ \\ " ;
         char the TEMP [256 ];
         int K = 0 ; 
        sprintf_s (TEMP, " % D " , I); 
        INPATH = INPATH + + TEMP " . * \\ PNG " ;
         // Handle for lookup 

        Long handle;
         struct _finddata_t FileInfo;
         // find first 
        handle = _findfirst (inPath.c_str (), & FileInfo);
         IF (handle == - 1 )
             return - 1 ;
         do 
        { 
            // files found in the file name 
            std ::String imgName = " E: / learning materials / digit / " ; 
            imgName = imgName + TEMP + " / " + fileinfo.name; 
            the src = imread (imgName, 0 );
             IF (src.empty ()) 
            { 
                STD :: cout << " CAN not the Load Image \ the n- " << std :: endl;
                 return - 1 ; 
            } 
            // all unified into a picture size 16 * 8 
            of a resize (src, resizeImg, size (imageRows, imageCols), ( 0 ,0), (0, 0), INTER_AREA);
            threshold(resizeImg, trainImg, 0, 255, THRESH_OTSU);
            for (int j = 0; j < imageRows*imageCols; j++)
            {
                trainingData[i*imagesSum + k][j] = (float)trainImg.data[j];
            }
            // 设置标签数据
            for (int j = 0; j < classSum; j++)
            {
                if (j == i)
                    labels[iimagesSum + K *] [J] = . 1 ;
                 the else 
                    Labels [I * imagesSum + K] [J] = 0 ; 
            } 
            K ++ ; 

        } the while (_findfirst (inPath.c_str (), & FileInfo) && K <imagesSum ); 
        labelsMat Mat (classSum * imagesSum, classSum, CV_32FC1, labels); 
    } 
    // training data and corresponding tag 
    MAT trainingDataMat (classSum imagesSum *, * imageRows imageCols, CV_32FC1, trainingData); 
    MAT labelsMat (classSum * imagesSum, classSum, CV_32FC1, Labels);
     //std::cout<<"trainingDataMat: \n"<<trainingDataMat<<"\n"<<std::endl;
    //std::cout<<"labelsMat: \n"<<labelsMat<<"\n"<<std::endl;
    //==========================训练部分==============================//
    Ptr<ANN_MLP>model = ANN_MLP::create();
    Mat layerSizes = (Mat_<int>(1, 5) << imageRows * imageCols, 128, 128, 128, classSum);
    model->setLayerSizes(layerSizes);
    model->setTrainMethod(ANN_MLP::BACKPROP, 0.001, 0.1);
    model-> setActivationFunction (ANN_MLP :: SIGMOID_SYM, 1.0 , 1.0 ); 
    Model -> setTermCriteria (TermCriteria (TermCriteria max_iter :: | :: TermCriteria the EPS, 10000 , from 0.0001 )); 

    the Ptr <TrainData> trainData = TrainData :: Create (trainingDataMat, ROW_SAMPLE, labelsMat); 
    std :: cout << " training Please wait ... " << std :: endl; 
    Model -> train (trainData); 
    std :: cout << " training the end, save the results. .. " << std :: endl;
     // save the training results 
    model->save("E: / learning materials / Arabic numerals /1/MLPModel.xml " );
 return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/hsy1941/p/11495500.html