opencv实现车牌识别之字符识别

来源:互联网 发布:老司机 知乎 收藏夹 编辑:程序博客网 时间:2024/05/18 00:05

简介

  在前面已经讲了车牌的定位和对车牌字符的分割,这里继续是最后对车牌的识别。


字符识别

  主要是用了两张办法,第一个是前面 <opencv实现车牌识别之失败经验> 这一篇中用到过的,opencv自带的getPSNR函数,这里不再对它进行讲解。另一种方法是将分割出来的字符和模板字符都分割成9宫格形式,对比比较每个块中,像素占的比例来匹配分辨出字符。  具体代码如下:
double proCale(Mat& mat1, int width_1, int height_1, int width_2, int height_2){int number = 0, sum = 0;IplImage pI = mat1;CvScalar s;double result;int i, j; for(i=width_1; i<=width_2; i++){for(j=height_1; j<=height_2; j++){s = cvGet2D(&pI,i,j);if(s.val[0] > 0.0){number += 1;}sum += 1;}}result = (double)number / (double)sum; return result;} int getPro(Mat& mat1){Mat mat2;int i, j, k;int hArr[4] = {0, 19, 39, 59};int wArr[4] = {0, 29, 69, 119};int wArrWidth = 3, wArrHeight = 3, sum;double tmp, tmp1, tmp2, all = 0;double *allPro; allPro = (double*)malloc(match_detect * sizeof(double));for(k=0; k< match_detect; k++){all = 0;mat2 = cv::imread(match_pic[k], 0);for(i=0; i< wArrWidth; i++){for(j=0; j< wArrHeight; j++){tmp1 = proCale(mat1, wArr[i], hArr[j], wArr[i+1], hArr[j+1]);tmp2 = proCale(mat2, wArr[i], hArr[j], wArr[i+1], hArr[j+1]);tmp = tmp1 - tmp2;if(tmp < 0){tmp = -tmp;}all += tmp;}}allPro[k] = all;} tmp = allPro[0];sum = 0;for(i=0; i<match_detect; i++){//printf("allPro[%d]:%lf\n", i, allPro[i]);if(tmp > allPro[i]){tmp = allPro[i];sum = i;    }}//printf("sum:%d, %lf\n\n\n\n", sum, allPro[sum]);tmp = allPro[0];sum = 0;for(i=0; i<match_detect; i++){if(tmp > allPro[i]){tmp = allPro[i];sum = i;    }}return sum;} int main(int argc, char** argv){      ...............      carCard_Resize(img_5, img_w, wWidth, wHeight);        pic_Thresholding(img_w, 50);         proSum = getPro(img_w);//      proSum = match_ok(img_w);                                                                                                            match_result(proSum);        sprintf(str, "%d", i+3);        namedWindow(str);        imshow(str, img_w);    }    printf("\n");     waitKey(0);    return 0;}
  函数proCale用来计算出传入图片的255像素所占的比例,然后getPro函数中将传入的目标图面mat1和模板图片,都分成九宫格的块,依次proCale计算,计算出9块中所有的像素比例差。通过循环,将目标图片mat1和所有的模板都匹配计算出像素比例总的差值,找到其中差值最小的模板,则它就是该目标字符。注意,这里没有匹配汉字。


结果演示

  结果演示如下:                                                   
    代码下载地址:http://download.csdn.net/detail/u011630458/8444711
2 0
原创粉丝点击