日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

神经网络识别车牌字符

發(fā)布時間:2023/12/13 综合教程 26 生活家
生活随笔 收集整理的這篇文章主要介紹了 神经网络识别车牌字符 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Ronny大神曾在OpenCV進階之路:神經網絡識別車牌字符寫過有關用神經網絡識別車牌字符的文章,但無奈僅僅給出了樣本,沒有詳細的代碼,在這里補足這個遺憾。

opencv環(huán)境請自行配置。因為用到了MFC類庫查找文件夾下的文件夾,請選擇使用共享的MFC類庫。

下面是代碼:
#include <afx.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;
}

總結

以上是生活随笔為你收集整理的神经网络识别车牌字符的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。