赞
踩
进行车牌识别和定位首先要了解车牌的特征以及分类。
车牌主要包括以下几种:
蓝牌白字:普通小型车(其中包括政府机关专用号段、政法部门警车以外的行政用车)的牌照
黄牌黑字:大型车辆、摩托车、驾校教练车牌照
黑牌白字:涉外车辆牌照,式样和蓝牌基本相同
白牌:政法部门(公安、法院、检察院、国安、司法)警车、武警部队车辆、解放军军车的牌照都是白牌
警车:公安警车的牌照样式为[某·A1234警],除“警”为红字外其他的都是黑字,一共4位数字,含义与普通牌照相同
车牌定位的算法分为三类,一类是基于边缘的,一类是基于颜色的,一类是基于机器学习的,这三种方法我都做过实验,基于边缘的最简单也最有效,如果对于收费站和小区的应用,做到99%以上的检测率不是件难事,但如果场景复杂一点,误检会比较多,但并不会漏掉真正的车牌,只是虚警率高点,可以通过先验知识、颜色和后面的分割加以去除,误检不是重点。基于颜色的定位算法,从根本上讲也可以算是基于边缘的一种,无非是利用彩色边缘或者灰度图像边缘和颜色一起来定位,基于颜色的车牌定位算法用于高清图片效果不错,对于一般的场景我认为没必要用颜色进行定位,但初期用颜色先去除一些明显不是车牌的区域还是比较有效的。基于机器学习的算法进行车牌定位或者说检测,关键是找到好的特征和好的训练方法,不少人利用adaboost+haar特征进行车牌检测,从我的实验结果来看,检测率也能达到99%以上,但同时虚警率也非常高,会出现很多误检,而且很难把车牌的区域完整的检测出来,所以如果单独要用机器学习的算法还是不太可行,不过可以先利用边缘信息找到候选区域,然后用adaboost去去除非车牌区域,这个效果还是蛮不错的。
对于边缘的检测,如果车牌在图像中占的比例不是很小,普通的差分和全局二值化就可以达到很好的效果,如果对于高清图像(譬如要检测几个车道)或者场景很复杂,导致车牌所占图像的比例很小,还有就是车牌处于比较暗的地方,而整个场景很亮,这个时候差分得到的边缘就不会很丰富,如果利用全局二值化就可能导致车牌区域检测不到边缘,解决办法一就是对图像进行灰度拉伸或增强,解决办法二就是换边缘检测的方法(譬如sobel),解决办法三就是改进二值化的方法。对于图像增强的方法我要特别提一下直方图均衡化,很多论文上都会说对输入图片先进行直方图均衡化,但我的实验发现,晚上的图片如果进行直方图均衡化操作后会导致噪点特别多,而且可能会导致车牌区域检测不到边缘,总之图像增强是一把双刃剑,需要慎重考虑。
如果利用边缘进行定位,关键是要想办法一定要检测出车牌区域的边缘。
车牌定位,利用边缘是王道,可以先粗检再精检,颜色可以用于精定位和去除误检,机器学习如果想要好的结果得需要好的特征,但目前好像还没有。我个人认为车牌定位的难点不在于找到车牌区域,而在于怎么对车牌区域进行更精确的定位,而精定位的难点在于左右精定位,以便于后面的分割算法。
这里给出一份基于颜色和形状的车牌识别的源代码,基本思路是提取车牌的特征然后根据特征进行筛选。
识别车牌用到的特征:颜色特征、形状特征、大小特征
a 颜色特征
每种车牌的底色具有各自特殊的RGB值,例如程序中使用的蓝底车牌的典型RGB值为 R = 28,G = 63, B = 138;
当RGB值接近时则认为可能是车牌,将该点像素赋值为255,否则0;利用颜色特征可以获取二值图像,可以去除大部分的其他物体,但是会有很多的干扰。
b、大小特征
对二值图像进行膨胀处理和腐蚀处理之后,可以去掉很小的噪点。对于灰度图获取轮廓边缘,轮廓具有一定的面积。车牌应该具有一定的大小,如果面积过小则认为不是车牌。此法可以去除大部分的小面积的干扰物。
颜色特征和大小特征是初选。形状特征是精选。
c、形状特征
矩形度:提取轮廓之后,轮廓包围的面积和轮廓最小外接矩形的面积之比称为矩形度,值越接近1,则是矩形的概率越大。
长宽比:正常车牌的长宽比为3:1,最小外接矩形的长宽比越接近1则认为是车牌的概率最大。
如果三个筛选条件都符合,则是车牌的概率非常大。
//主程序lpr.cpp
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include<cv.h>
- #include<highgui.h>
- #include <cmath>
- #include "image.h"
- using namespace std;
- int main(){
- char imageName[12] = "1.jpg";
- char imageDstName[12] = "1_dst.jpg";
- char imageBwName[12] = "1_bw.jpg";
- IplImage* srcImage = NULL,*image = NULL,*bwImage = NULL;
-
- cvNamedWindow("srcImage",1);
- cvNamedWindow("bwImage",1);
- //cvShowImage("srcImage",srcImage);
- int imageWidth,imageHeight;
- int maxDif = 50;
- //找到蓝色区域
- int i= 0,j = 0;
- unsigned char * pPixel = NULL;
- unsigned char pixelR = 0,pixelG = 0,pixelB = 0;
- unsigned char R = 28,G = 63, B = 138;
-
- double length,area,rectArea;
- double rectDegree = 0.0; //矩形度
- double long2Short = 1.0; //体态比
- //计算边界序列的参数 长度 面积 矩形 最小矩形
- //并输出每个边界的参数
- CvRect rect;
- CvBox2D box;
- int imageCnt = 1;
- double axisLong = 0.0, axisShort = 0.0;
- double temp;
-
- while ((srcImage = cvLoadImage(imageName,1)) != NULL)
- {
- cvShowImage("srcImage",srcImage);
- cout<<imageName<<": "<<endl;
- imageWidth = srcImage->width;
- imageHeight = srcImage->height;
-
-
- image = cvCreateImage(cvSize(imageWidth,imageHeight),8,3);
- //image = cvCloneImage(srcImage);
- Image::cloneImage(srcImage,image);
-
- bwImage = cvCreateImage(cvGetSize(srcImage),srcImage->depth,1);
- //cvZero(bwImage);
- Image::ZerosImage(bwImage);
-
-
- for (i = 0; i< imageHeight;i++)
- {
- for (j = 0;j<imageWidth;j++)
- {
- pPixel = (unsigned char*)srcImage->imageData + i*srcImage->widthStep + j*3;
- pixelB = pPixel[0];
- pixelG = pPixel[1];
- pixelR = pPixel[2];
-
- if (abs(pixelB - B) < maxDif && abs(pixelG - G)< maxDif && abs(pixelR - R)< maxDif)
- {
- *((unsigned char*)bwImage->imageData + i*bwImage->widthStep + j) = 255;
- }else {
- *((unsigned char*)bwImage->imageData + i*bwImage->widthStep + j) = 0;
- }
- }
- }
- cvShowImage("bwImage",bwImage);
- cvSaveImage(imageBwName,bwImage);
- //cvWaitKey(0);
- //膨胀
- //cvDilate(bwImage,bwImage,0,3);
- Image::dilateImage(bwImage,bwImage);
- Image::dilateImage(bwImage,bwImage);
- Image::dilateImage(bwImage,bwImage);
-
- //cvErode (bwImage,bwImage,0,3);
- Image::erodeImage(bwImage,bwImage);
- Image::erodeImage(bwImage,bwImage);
- Image::erodeImage(bwImage,bwImage);
-
- cvShowImage("bwImage",bwImage);
- //cvWaitKey(0);
-
- //新图,将轮廓绘制到dst
- IplImage *dst = cvCreateImage(cvGetSize(srcImage),8,3);
- //dst = cvCloneImage(srcImage);//赋值为0
- Image::cloneImage(srcImage,dst);
- //寻找轮廓
- CvMemStorage *storage = cvCreateMemStorage(0);
- CvSeq * seq = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), storage);
- CvSeq * tempSeq = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), storage);
- int cnt = cvFindContours(bwImage,storage,&seq);//返回轮廓的数目
- cout<<"number of contours "<<cnt<<endl;
- cvShowImage("bwImage",bwImage); //难道使用cvFindContours会改变源图像?需要实现保存一下
-
- for (tempSeq = seq;tempSeq != NULL; tempSeq = tempSeq->h_next)
- {
-
- length = cvArcLength(tempSeq);
- area = cvContourArea(tempSeq);
- //筛选面积比较大的区域
- if (area > 1000 && area < 50000)
- {
-
- //cout<<"Points: "<<tempSeq->total<<endl;
- //外接矩形
- rect = cvBoundingRect(tempSeq,1);
- //绘制轮廓和外接矩形
- //cvDrawContours(dst,tempSeq,CV_RGB(255,0,0),CV_RGB(255,0,0),0);
- //cvRectangleR(dst,rect,CV_RGB(0,255,0));
- //cvShowImage("dst",dst);
-
- //绘制外接最小矩形
- CvPoint2D32f pt[4];
- box = cvMinAreaRect2(tempSeq,0);
- cvBoxPoints(box,pt);
- //下面开始分析图形的形状特征
- //长轴 短轴
- axisLong = sqrt(pow(pt[1].x -pt[0].x,2) + pow(pt[1].y -pt[0].y,2));
- axisShort = sqrt(pow(pt[2].x -pt[1].x,2) + pow(pt[2].y -pt[1].y,2));
-
- if (axisShort > axisLong)
- {
- temp = axisLong;
- axisLong = axisShort;
- axisShort= temp;
- }
- rectArea = axisLong*axisShort;
- rectDegree = area/rectArea;
- //体态比or长宽比 最下外接矩形的长轴和短轴的比值
- long2Short = axisLong/axisShort;
-
- if (long2Short> 2.2 && long2Short < 3.8 && rectDegree > 0.63 && rectArea > 3000 && rectArea <50000)
- {
- cout<<"Length: "<<length<<endl;
- cout<<"Area : "<<area<<endl;
- cout<<"long axis :"<<axisLong<<endl;
- cout<<"short axis: "<<axisShort<<endl;
- cout<<"long2Short: "<<long2Short<<endl;
- cout<<"rectArea: "<<rectArea<<endl;
- cout<<"rectDegree: "<<rectDegree<<endl;
-
- for(int i = 0;i<4;++i){
- cvLine(dst,cvPointFrom32f(pt[i]),cvPointFrom32f(pt[((i+1)%4)?(i+1):0]),CV_RGB(255,0,0));
- }
- }
- //cvShowImage("dst",dst);
- //cvWaitKey();
- }
- }
- cvShowImage("dst",dst);
- cvSaveImage(imageDstName,dst);
- //cvWaitKey(0);
-
- imageCnt++;
- sprintf(imageName,"%d.jpg",imageCnt);
- sprintf(imageBwName,"%d_bw.jpg",imageCnt);
- sprintf(imageDstName,"%d_dst.jpg",imageCnt);
- cout<<"\n\n";
- }
-
- return 0;
- }
-
//image类
//image.h
- #pragma once
- #include "cv.h"
- #include<highgui.h>
- class Image
- {
- public:
- Image(void);
- ~Image(void);
- static void cloneImage(IplImage *src,IplImage*dst);
- static void ZerosImage(IplImage * src);
- static void dilateImage(IplImage* src, IplImage *dst);
- static void erodeImage(IplImage* src,IplImage*dst);
- static void rgb2gray(IplImage* src, IplImage* dst);
- static void gray2bw(IplImage* src, IplImage* dst,unsigned char value);
- };
- #include "Image.h"
-
-
- Image::Image(void)
- {
- }
-
-
- Image::~Image(void)
- {
- }
-
- void Image::cloneImage(IplImage *src,IplImage*dst){
- int nChannels = src->nChannels;
- int imageHeight = src->height;
- int imageWidth = src->width;
- unsigned char *pPixel= NULL;
- unsigned char *pPixel2= NULL;
- if (nChannels == 1)
- {
- for (int i = 0; i< imageHeight;i++)
- {
- for (int j = 0; j< imageWidth ;j++)
- {
- pPixel = (unsigned char *)src->imageData + i*src->widthStep+j;
- pPixel2 = (unsigned char *)dst->imageData + i*dst->widthStep+j;
- pPixel2[0] = pPixel[0];
- }
- }
- }else if (nChannels == 3)
- {
- for (int i = 0; i< imageHeight;i++)
- {
- for (int j = 0; j< imageWidth ;j++)
- {
- pPixel = (unsigned char *)src->imageData + i*src->widthStep+3*j;
- pPixel2 = (unsigned char *)dst->imageData + i*dst->widthStep+3*j;
- pPixel2[0] = pPixel[0];
- pPixel2[1] = pPixel[1];
- pPixel2[2] = pPixel[2];
- }
- }
- }
- }
- void Image::ZerosImage(IplImage * src){
- int nChannels = src->nChannels;
- int imageHeight = src->height;
- int imageWidth = src->width;
- unsigned char *pPixel= NULL;
- unsigned char *pPixel2= NULL;
- if (nChannels == 1)
- {
- for (int i = 0; i< imageHeight;i++)
- {
- for (int j = 0; j< imageWidth ;j++)
- {
- pPixel = (unsigned char *)src->imageData + i*src->widthStep+j;
- pPixel[0] = 0;
- }
- }
- }else if (nChannels == 3)
- {
- for (int i = 0; i< imageHeight;i++)
- {
- for (int j = 0; j< imageWidth ;j++)
- {
- pPixel = (unsigned char *)src->imageData + i*src->widthStep + 3*j;
- pPixel[0] = 0;
- pPixel[1] = 0;
- pPixel[2] = 0;
- }
- }
- }
-
- }
- //膨胀
- void Image::dilateImage(IplImage* src, IplImage *dst){
- int nChannels = src->nChannels;
- int imageHeight = src->height;
- int imageWidth = src->width;
- IplImage * tmpDst = cvCreateImage(cvSize(imageWidth,imageHeight),src->depth,src->nChannels);
- ZerosImage(tmpDst);
- unsigned char *pPixel= NULL;
- unsigned char *pPixel2= NULL;
- CvSize windowSize = cvSize(3,3);
- int SW2 = windowSize.width/2;
- int SH2 = windowSize.height/2;
-
- if (nChannels == 1)
- {
- for (int i = 1; i< imageHeight - 1;i++)
- {
- for (int j = 1; j< imageWidth - 1 ;j++)
- {
- pPixel = (unsigned char *)tmpDst->imageData + i*tmpDst->widthStep+j;
- for (int m = i - SH2 ; m <= i+SH2; m++)
- {
- for (int n = j - SW2; n <= j+SW2;n++)
- {
- pPixel2 = (unsigned char *)src->imageData + m*src->widthStep + n;
- if (pPixel2[0] == 255)
- {
- pPixel[0] = 255;
- }
- }
- }
- }
- }
- }else if (nChannels == 3)
- {
- //不是灰度图像,报错
- }
-
- //拷贝图像
- cloneImage(tmpDst,dst);
- //cvShowImage("dst",dst);
- //cvWaitKey(0);
- }
- void Image::erodeImage(IplImage* src,IplImage*dst){
- int nChannels = src->nChannels;
- int imageHeight = src->height;
- int imageWidth = src->width;
- IplImage * tmpDst = cvCreateImage(cvSize(imageWidth,imageHeight),src->depth,src->nChannels);
- ZerosImage(tmpDst);
- unsigned char *pPixel= NULL;
- unsigned char *pPixel2= NULL;
- CvSize windowSize = cvSize(3,3);
- int SW2 = windowSize.width/2;
- int SH2 = windowSize.height/2;
- int flag = 0;
-
- if (nChannels == 1)
- {
- for (int i = 1; i< imageHeight - 1;i++)
- {
- for (int j = 1; j< imageWidth - 1 ;j++)
- {
- pPixel = (unsigned char *)tmpDst->imageData + i*tmpDst->widthStep+j;
- flag = 0;
- for (int m = i - SH2 ; m <= i+SH2; m++)
- {
- for (int n = j - SW2; n <= j+SW2;n++)
- {
- pPixel2 = (unsigned char *)src->imageData + m*src->widthStep + n;
- if (pPixel2[0] == 0)
- {
- flag = 1;
- }
- }
- if (flag == 0)
- {
- //
- pPixel[0] = 255;
- }else {
- pPixel[0] = 0;
- }
-
- }
- }
- }
- }else if (nChannels == 3)
- {
- //不是灰度图像,报错
- }
-
- //拷贝图像
- cloneImage(tmpDst,dst);
- //cvShowImage("dst",dst);
- //cvWaitKey(0);
- }
-
- void Image::rgb2gray(IplImage* src, IplImage* dst){
- //Gray = 0.29900 * R + 0.58700 * G + 0.11400 * B
- int nChannels = src->nChannels;
- int imageHeight = src->height;
- int imageWidth = src->width;
- unsigned char *pPixel= NULL;
- unsigned char *pPixel2= NULL;
- unsigned char R ,G, B;
- unsigned char grayPixle = 0;
-
- if (nChannels == 3)
- {
- for (int i = 0; i< imageHeight;i++)
- {
- for (int j = 0; j< imageWidth ;j++)
- {
- pPixel = (unsigned char *)src->imageData + i*src->widthStep+j*3;
- pPixel2 = (unsigned char *)dst->imageData + i*dst->widthStep+j;
- B = pPixel[0];
- G = pPixel[1];
- R = pPixel[2];
- grayPixle = (unsigned char)(0.29900 * R + 0.58700 * G + 0.11400 * B);
- pPixel2[0] = grayPixle;
- }
- }
- }else if (nChannels == 1)
- {
- //不是RGB图像,报错
- }
- }
- void Image::gray2bw(IplImage* src, IplImage* dst,unsigned char value){
- int nChannels = src->nChannels;
- int imageHeight = src->height;
- int imageWidth = src->width;
- unsigned char *pPixel= NULL;
- unsigned char *pPixel2= NULL;
- if (nChannels == 1)
- {
- for (int i = 0; i< imageHeight;i++)
- {
- for (int j = 0; j< imageWidth ;j++)
- {
- pPixel = (unsigned char *)src->imageData + i*src->widthStep+j;
- pPixel2 = (unsigned char *)dst->imageData + i*dst->widthStep+j;
- if (pPixel[0] > value)
- {
- pPixel2[0] = 255;
- }else {
- pPixel2[0] = 0;
- }
-
- }
- }
- }else if (nChannels == 3)
- {
- //不是灰度图像,报错
- }
- }
源码以及测试图片下载地址:http://download.csdn.net/detail/renshengrumenglibing/5073845
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。