神经网络识别车牌字符

原创 2015年07月07日 09:16:36

OpenCV进阶之路:神经网络识别车牌字符写过有关用神经网络识别车牌字符的文章,但无奈只给出了样本,没有具体的代码,在这里补足这个遗憾。

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<float>& 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<uchar>(i); 
         for (int j = 0; j < c; j++) 
         { 
             sumValue += linePtr[j]; 
         } 
     } 
     return sumValue; 
 }


void getFileFromDir(const char *directorypath,vector<string> &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<<"start reading characters:"<<endl; 
for(int i=0;i<sizeof(strCharacters);i++)
{
// cout<<"reading:"<<strCharacters[i]<<endl;
string subdir=directorypath;
subdir=subdir+"\\"+strCharacters[i];
vector<string> vfiles;
getFileFromDir(subdir.c_str(),vfiles);
int stotal=0;
for(vector<string>::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<img.rows;j++)
for(int k=0;k<img.cols;k++)
m.at<float>(j,k)=img.at<uchar>(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<float>(0,i)=1;
labels.push_back(fl);
}
}
cout<<"good,reading characters finished!"<<endl;
}
void MLPTrain(Mat &train,Mat &trainLabel)
{
CvANN_MLP NeuralNetworks;
std::vector<int> 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<<"starting mlp training"<<endl;
NeuralNetworks.train(train,trainLabel, cv::Mat(), cv::Mat(), TrainParams);
NeuralNetworks.save(mlpmodel);
cout<<"mlp train finished"<<endl;
}


void MLPTest(Mat &test,Mat &testLabel)
{
CvANN_MLP NeuralNetworks;
NeuralNetworks.load(mlpmodel);
int total=0;
int right=0,error=0;
while((bool)(total<test.rows))
{
Mat m=test.row(total);
Mat nearest(1, 10, CV_32FC1, Scalar(0));
NeuralNetworks.predict(m, nearest);
//char label=testLabel.at<char>(total);
Point maxLoc;
minMaxLoc(nearest, NULL, NULL, NULL, &maxLoc);
char ret=maxLoc.x;
//char label=testLabel.at<float>(total,0);
char label=0;
for(int i=0;i<34;i++)
{
if(testLabel.at<float>(total,i)==1)
{
label=i;
break;
}
}
if(ret==label)
right++;
else
error++;
total++;
}


cout<<"precision"<<right*1.0/total<<endl;
}
int main()
{
Mat Sameples,Labels;
readSample("charSamples",Sameples,Labels);
// MLPTrain(Sameples,Labels);
MLPTest(Sameples,Labels);
return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

车牌识别-模板匹配-BP神经网络-卷积神经网络[深度学习]

文章来源:http://blog.csdn.net/fighting_dreamer/article/details/56285738 1 车牌识别的步骤 车牌定位车牌分割字符识别...

车牌识别系统Matlab算法实现

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   % 字符分...

字符特征训练

typedef cv::Mat (*FEATURE_EXTRACTOR)(cv::Mat); // Label 生成函数: 输入子文件夹名称,输出label typedef double (*LABE...

OpenCV进阶之路:神经网络识别车牌字符

1. 关于OpenCV进阶之路 前段时间写过一些关于OpenCV基础知识方面的系列文章,主要内容是面向OpenCV初学者,介绍OpenCV中一些常用的函数的接口和调用方法,相关的内容在OpenC...

基于EasyPR的车牌识别android实现

EasyPR4Android,基于EasyPR1.5beta的android实现 更新: 添加编译好的安装文件 采用android studio2.3.3大幅简化ndk开发的配置 mrcar ...

YOLOv2论文理解

本文是对YOLOv2论文的学习研究,主要针对Better,Faster

移植开源EasyPR的车牌识别源码到Android工程

基于开源EasyPR的车牌识别Android工程源码

MRLabeler:一款先进的VOC格式标注工具

https://github.com/imistyrain/MRLabeler/blob/master/MRLabeler%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E.pd...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)