车牌识别大概步骤可分为:车牌定位,字符分割,字符识别三个步骤。
细分点可以有以下几个步骤:
(1)将图片灰度化与二值化
(2)去噪,然后切割成一个一个的字符
(3)提取每一个字符的特征,生成特征矢量或特征矩阵
(4)分类与学习。将特征矢量或特征矩阵与样本库进行比对,挑选出相似的那类样本,将这类样本的值作为输出结果。
车牌定位可以参考这里,网上也有很多关于这个问题的讨论,理论上觉得可行,但实际上没有操作过,等有空再去搞搞。
http://blog.csdn.net/sing_sing/archive/2010/10/13/5937725.aspx
这几天研究了一下车牌字符分割的问题,前提是已经进行了车牌定位,角度校正等预处理。
用到的主要知识有:二值化,形态学操作,轮廓查找等。
字符分割网上资料比较少,本人接触opencv一段时间,自己瞎搞了一下,以此抛砖引玉,希望与各位交流一下。
以下为全部源代码:
//==============================================
//write by sing
//2010-10-10
//==============================================
#include "stdafx.h"
//找出含车牌文字的最左端
void findX(IplImage* img, int* min, int* max)
{
int found = 0;
CvScalar maxVal = cvRealScalar(img->width * 255);
CvScalar val = cvRealScalar(0);
CvMat data;
int minCount = img->width * 255 / 5;
int count = 0;
for (int i = 0; i < img->width; i++) {
cvGetCol(img, &data, i);
val = cvSum(&data);
if (val.val[0] < maxVal.val[0]) {
count = val.val[0];
if (count > minCount && count < img->width * 255) {
*max = i;
if (found == 0) {
*min = i;
found = 1;
}
}
}
}
}
//找出含车牌文字的最上端,排除两颗螺丝的位置
void findY(IplImage* img, int* min, int* max)
{
int found = 0;
CvScalar maxVal = cvRealScalar(img->height * 255);
CvScalar val = cvRealScalar(0);
CvMat data;
int minCount = img->width * 255 / 5;
int count = 0;
for (int i = 0; i < img->height; i++) {
cvGetRow(img, &data, i);
val = cvSum(&data);
if (val.val[0] < maxVal.val[0]) {
count = val.val[0];
if (count > minCount && count < img->height * 255) {
*max = i;
if (found == 0) {
*min = i;
found = 1;
}
}
}
}
}
//车牌字符的最小区域
CvRect findArea(IplImage* img)
{
int minX, maxX;
int minY, maxY;
findX(img, &minX, &maxX);
findY(img, &minY, &maxY);
CvRect rc = cvRect(minX, minY, maxX - minX, maxY - minY);
return rc;
}
int main(int argc, char* argv[])
{
IplImage* imgSrc = cvLoadImage("cp.jpg", CV_LOAD_IMAGE_COLOR);
IplImage* img_gray = cvCreateImage(cvGetSize(imgSrc), IPL_DEPTH_8U, 1);
cvCvtColor(imgSrc, img_gray, CV_BGR2GRAY);
cvThreshold(img_gray, img_gray, 100, 255, CV_THRESH_BINARY);
//寻找最小区域,并截取
CvRect rc = findArea(img_gray);
cvSetImageROI(img_gray, rc);
IplImage* img_gray2 = cvCreateImage(cvSize(rc.width, rc.height), IPL_DEPTH_8U, 1);
cvCopyImage(img_gray, img_gray2);
cvResetImageROI(img_gray);
IplImage* imgSrc2 = cvCreateImage(cvSize(rc.width, rc.height), IPL_DEPTH_8U, 3);
cvSetImageROI(imgSrc, rc);
cvCopyImage(imgSrc, imgSrc2);
cvResetImageROI(imgSrc);
//形态学
cvMorphologyEx(img_gray2, img_gray2, NULL, NULL, CV_MOP_CLOSE);
CvSeq* contours = NULL;
CvMemStorage* storage = cvCreateMemStorage(0);
int count = cvFindContours(img_gray2, storage, &contours,
sizeof(CvContour), CV_RETR_EXTERNAL);
int idx = 0;
char szName[56] = {0};
for (CvSeq* c = contours; c != NULL; c = c->h_next) {
//cvDrawContours(imgSrc2, c, CV_RGB(255, 0, 0), CV_RGB(255, 255, 0), 100);
CvRect rc = cvBoundingRect(c);
cvDrawRect(imgSrc2, cvPoint(rc.x, rc.y), cvPoint(rc.x + rc.width, rc.y + rc.height), CV_RGB(255, 0, 0));
if (rc.width < imgSrc2->width / 10 && rc.height < imgSrc2->height / 5) {
continue;
}
IplImage* imgNo = cvCreateImage(cvSize(rc.width, rc.height), IPL_DEPTH_8U, 3);
cvSetImageROI(imgSrc2, rc);
cvCopyImage(imgSrc2, imgNo);
cvResetImageROI(imgSrc2);
sprintf(szName, "wnd_%d", idx++);
cvNamedWindow(szName);
cvShowImage(szName, imgNo);
cvReleaseImage(&imgNo);
}
cvNamedWindow("src");
cvShowImage("src", imgSrc2);
cvWaitKey(0);
cvReleaseMemStorage(&storage);
cvReleaseImage(&imgSrc);
cvReleaseImage(&imgSrc2);
cvReleaseImage(&img_gray);
cvReleaseImage(&img_gray2);
cvDestroyAllWindows();
return 0;
}
输入图像:
运行结果截图如下: