신경 네트워크가 자동차 번호판 문자를 식별하다

4441 단어 CV
CV 진급의 길: 신경 네트워크로 자동차 문자를 식별하는 데 신경 네트워크로 자동차 문자를 식별하는 글을 썼지만 어쩔 수 없이 샘플만 제시했고 구체적인 코드가 없어서 이 아쉬움을 보완했다.
opencv 환경 설정 참조https://github.com/imistyrain/MRHead
다음은 코드입니다.
 
#include "mrdir.h"
const char*mlpmodel="ann.xml";
//    
const char strCharacters[] = {'0','1','2','3','4','5',\
'6','7','8','9','A','B', 'C', 'D', 'E','F', 'G', 'H', /*   I */\
'J', 'K', 'L', 'M', 'N', /*   O */ 'P', 'Q', 'R', 'S', 'T', \
'U','V', 'W', 'X', 'Y', 'Z'};
void calcGradientFeat(const Mat& imgSrc, vector& feat) 
{ 
   float sumMatValue(const Mat& image); //              
   
   Mat image; 
   cvtColor(imgSrc,image,CV_BGR2GRAY); 
   resize(image,image,Size(8,16)); 
   
   //   x   y       
   float mask[3][3] = { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };


   Mat y_mask = Mat(3, 3, CV_32F, mask) / 8; 
   Mat x_mask = y_mask.t(); //    
   Mat sobelX, sobelY;


   filter2D(image, sobelX, CV_32F, x_mask); 
   filter2D(image, sobelY, CV_32F, y_mask);


   sobelX = abs(sobelX); 
   sobelY = abs(sobelY);


   float totleValueX = sumMatValue(sobelX); 
   float totleValueY = sumMatValue(sobelY);


   //       4*2 8   ,                 
   for (int i = 0; i < image.rows; i = i + 4) 
   { 
       for (int j = 0; j < image.cols; j = j + 4) 
       { 
           Mat subImageX = sobelX(Rect(j, i, 4, 4)); 
           feat.push_back(sumMatValue(subImageX) / totleValueX); 
           Mat subImageY= sobelY(Rect(j, i, 4, 4)); 
           feat.push_back(sumMatValue(subImageY) / totleValueY); 
       } 
   } 
}
 
float sumMatValue(const Mat& image) 
 { 
     float sumValue = 0; 
     int r = image.rows; 
     int c = image.cols; 
     if (image.isContinuous()) 
     { 
         c = r*c; 
         r = 1;    
     } 
     for (int i = 0; i < r; i++) 
     { 
         const uchar* linePtr = image.ptr(i); 
         for (int j = 0; j < c; j++) 
         { 
             sumValue += linePtr[j]; 
         } 
     } 
     return sumValue; 
 }


void getFileFromDir(const char *directorypath,vector &vfiles)
{
CFileFind finder;
string ext2find=directorypath;
ext2find+="\\*.png";
bool bResult=finder.FindFile(ext2find.c_str());
if(!bResult)
{
return ;
}
while (bResult)
{
bResult = finder.FindNextFile();
if (finder.IsDots()||finder.IsDirectory()) 
continue;
string str(finder.GetFilePath().GetBuffer(finder.GetFilePath().GetLength()));
vfiles.push_back(str);
}
}


void readSample(const char *directorypath,Mat &samples,Mat &labels)
{
cout< vfiles;
getFileFromDir(subdir.c_str(),vfiles);
int stotal=0;
for(vector::iterator it=vfiles.begin();it!=vfiles.end();it++)
{
Mat img=imread(*it);
Mat m(img.rows,img.cols,CV_32FC1);
for(int j=0;j(j,k)=img.at(j,k);
resize(m,m,Size(4,8));
m=m.reshape(1,1);
normalize(m,m);
samples.push_back(m);
Mat fl=Mat::zeros(1,34,CV_32FC1);
fl.at(0,i)=1;
labels.push_back(fl);
}
}
cout< LayerSizes;
LayerSizes.push_back(train.cols);    // input layer
LayerSizes.push_back(train.cols+trainLabel.cols);// hidden layer has neurons
LayerSizes.push_back(trainLabel.cols);   // output layer
// Activate function
int ActivateFunc = CvANN_MLP::SIGMOID_SYM;
double Alpha = 1;
double Beta = 1;
// create the network
NeuralNetworks.create(cv::Mat(LayerSizes), ActivateFunc, Alpha, Beta);
// Training Params
CvANN_MLP_TrainParams TrainParams;
TrainParams.train_method = CvANN_MLP_TrainParams::BACKPROP;
TrainParams.bp_dw_scale = 0.0001;
TrainParams.bp_moment_scale = 0;


// iteration number
CvTermCriteria TermCrlt;
TermCrlt.type = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS;
TermCrlt.epsilon = 0.0001f;
TermCrlt.max_iter = 1000;
TrainParams.term_crit = TermCrlt;
// Training the networks
cout<(total);
Point maxLoc;
minMaxLoc(nearest, NULL, NULL, NULL, &maxLoc);
char ret=maxLoc.x;
//char label=testLabel.at(total,0);
char label=0;
for(int i=0;i<34;i++)
{
if(testLabel.at(total,i)==1)
{
label=i;
break;
}
}
if(ret==label)
right++;
else
error++;
total++;
}


cout<

좋은 웹페이지 즐겨찾기