EasyPR源码剖析(8):字符分割
通过前面的学习,我们已经可以从图像中定位出车牌区域,并且通过SVM模型删除“虚假”车牌,下面我们需要对车牌检测步骤中获取到的车牌图像,进行光学字符识别(OCR),在进行光学字符识别之前,需要对车牌图块进行灰度化,二值化,然后使用一系列算法获取到车牌的每个字符的分割图块。本节主要对该字符分割部分进行详细讨论。
EasyPR中,字符分割部分主要是在类 CCharsSegment 中进行的,字符分割函数为 charsSegment()。
int CCharsSegment::charsSegment(Mat input, vector<Mat>& resultVec, Color color) {
if (!input.data) return 0x01;
Color plateType = color;
Mat input_grey;
cvtColor(input, input_grey, CV_BGR2GRAY);
Mat img_threshold; img_threshold = input_grey.clone();
spatial_ostu(img_threshold, , , plateType); //车牌铆钉 水平线
if (!clearLiuDing(img_threshold)) return 0x02; Mat img_contours;
img_threshold.copyTo(img_contours); vector<vector<Point> > contours;
findContours(img_contours,
contours, // a vector of contours
CV_RETR_EXTERNAL, // retrieve the external contours
CV_CHAIN_APPROX_NONE); // all pixels of each contours vector<vector<Point> >::iterator itc = contours.begin();
vector<Rect> vecRect; while (itc != contours.end()) {
Rect mr = boundingRect(Mat(*itc));
Mat auxRoi(img_threshold, mr); if (verifyCharSizes(auxRoi)) vecRect.push_back(mr);
++itc;
} if (vecRect.size() == ) return 0x03; vector<Rect> sortedRect(vecRect);
std::sort(sortedRect.begin(), sortedRect.end(),
[](const Rect& r1, const Rect& r2) { return r1.x < r2.x; }); size_t specIndex = ; specIndex = GetSpecificRect(sortedRect); Rect chineseRect;
if (specIndex < sortedRect.size())
chineseRect = GetChineseRect(sortedRect[specIndex]);
else
return 0x04; vector<Rect> newSortedRect;
newSortedRect.push_back(chineseRect);
RebuildRect(sortedRect, newSortedRect, specIndex); if (newSortedRect.size() == ) return 0x05; bool useSlideWindow = true;
bool useAdapThreshold = true; for (size_t i = ; i < newSortedRect.size(); i++) {
Rect mr = newSortedRect[i]; // Mat auxRoi(img_threshold, mr);
Mat auxRoi(input_grey, mr);
Mat newRoi; if (i == ) {
if (useSlideWindow) {
float slideLengthRatio = 0.1f;
if (!slideChineseWindow(input_grey, mr, newRoi, plateType, slideLengthRatio, useAdapThreshold))
judgeChinese(auxRoi, newRoi, plateType);
}
else
judgeChinese(auxRoi, newRoi, plateType);
}
else {
if (BLUE == plateType) {
threshold(auxRoi, newRoi, , , CV_THRESH_BINARY + CV_THRESH_OTSU);
}
else if (YELLOW == plateType) {
threshold(auxRoi, newRoi, , , CV_THRESH_BINARY_INV + CV_THRESH_OTSU);
}
else if (WHITE == plateType) {
threshold(auxRoi, newRoi, , , CV_THRESH_OTSU + CV_THRESH_BINARY_INV);
}
else {
threshold(auxRoi, newRoi, , , CV_THRESH_OTSU + CV_THRESH_BINARY);
} newRoi = preprocessChar(newRoi);
}
resultVec.push_back(newRoi);
} return ;
}
下面我们最该字符分割函数中的主要函数进行一个简单的梳理:
- spatial_ostu 空间otsu算法,主要用于处理光照不均匀的图像,对于当前图像,分块分别进行二值化;
- clearLiuDing 处理车牌上铆钉和水平线,因为铆钉和字符连在一起,会影响后面识别的精度。此处有一个特别的乌龙事件,就是铆钉的读音应该是maoding,不读liuding;
- verifyCharSizes 字符大小验证;
- GetSpecificRect 获取特殊字符的位置,主要是车牌中除汉字外的第一个字符,一般位于车牌的 1/7 ~ 2/7宽度处;
- GetChineseRect 获取汉字字符,一般为特殊字符左移字符宽度的1.15倍;
- RebuildRect 从左到右取前7个字符,排除右边边界会出现误判的 I ;
- slideChineseWindow 改进中文字符的识别,在识别中文时,增加一个小型的滑动窗口,以此弥补通过省份字符直接查找中文字符时的定位不精等现象;
- preprocessChar 识别字符前预处理,主要是通过仿射变换,将字符的大小变换为20 *20;
- judgeChinese 中文字符判断,后面字符识别时详细介绍。
spatial_ostu 函数代码如下:
// this spatial_ostu algorithm are robust to
// the plate which has the same light shine, which is that
// the light in the left of the plate is strong than the right.
void spatial_ostu(InputArray _src, int grid_x, int grid_y, Color type) {
Mat src = _src.getMat(); int width = src.cols / grid_x;
int height = src.rows / grid_y; // iterate through grid
for (int i = ; i < grid_y; i++) {
for (int j = ; j < grid_x; j++) {
Mat src_cell = Mat(src, Range(i*height, (i + )*height), Range(j*width, (j + )*width));
if (type == BLUE) {
cv::threshold(src_cell, src_cell, , , CV_THRESH_OTSU + CV_THRESH_BINARY);
}
else if (type == YELLOW) {
cv::threshold(src_cell, src_cell, , , CV_THRESH_OTSU + CV_THRESH_BINARY_INV);
}
else if (type == WHITE) {
cv::threshold(src_cell, src_cell, , , CV_THRESH_OTSU + CV_THRESH_BINARY_INV);
}
else {
cv::threshold(src_cell, src_cell, , , CV_THRESH_OTSU + CV_THRESH_BINARY);
}
}
}
}
spatial_ostu 函数主要是为了应对左右光照不一致的情况,譬如车牌的左边部分光照比右边部分要强烈的多,通过图像分块处理,提高otsu分割的鲁棒性;
clearLiuDing函数代码如下:
bool clearLiuDing(Mat &img) {
std::vector<float> fJump;
int whiteCount = ;
const int x = ;
Mat jump = Mat::zeros(, img.rows, CV_32F);
for (int i = ; i < img.rows; i++) {
int jumpCount = ; for (int j = ; j < img.cols - ; j++) {
if (img.at<char>(i, j) != img.at<char>(i, j + )) jumpCount++; if (img.at<uchar>(i, j) == ) {
whiteCount++;
}
} jump.at<float>(i) = (float) jumpCount;
} int iCount = ;
for (int i = ; i < img.rows; i++) {
fJump.push_back(jump.at<float>(i));
if (jump.at<float>(i) >= && jump.at<float>(i) <= ) { // jump condition
iCount++;
}
} // if not is not plate
if (iCount * 1.0 / img.rows <= 0.40) {
return false;
} if (whiteCount * 1.0 / (img.rows * img.cols) < 0.15 ||
whiteCount * 1.0 / (img.rows * img.cols) > 0.50) {
return false;
} for (int i = ; i < img.rows; i++) {
if (jump.at<float>(i) <= x) {
for (int j = ; j < img.cols; j++) {
img.at<char>(i, j) = ;
}
}
}
return true;
}
清除铆钉对字符识别的影响,基本思路是:依次扫描各行,判断跳变的次数,字符所在行跳变次数会很多,但是铆钉所在行则偏少,将每行中跳变次数少于7的行判定为铆钉,清除影响。
verifyCharSizes函数代码如下:
bool CCharsSegment::verifyCharSizes(Mat r) {
// Char sizes 45x90
float aspect = 45.0f / 90.0f;
float charAspect = (float)r.cols / (float)r.rows;
float error = 0.7f;
float minHeight = .f;
float maxHeight = .f;
// We have a different aspect ratio for number 1, and it can be ~0.2
float minAspect = 0.05f;
float maxAspect = aspect + aspect * error;
// area of pixels
int area = cv::countNonZero(r);
// bb area
int bbArea = r.cols * r.rows;
//% of pixel in area
int percPixels = area / bbArea; if (percPixels <= && charAspect > minAspect && charAspect < maxAspect &&
r.rows >= minHeight && r.rows < maxHeight)
return true;
else
return false;
}
主要是从面积,长宽比和字符的宽度高度等角度进行字符校验。
GetSpecificRect 函数代码如下:
int CCharsSegment::GetSpecificRect(const vector<Rect>& vecRect) {
vector<int> xpositions;
int maxHeight = ;
int maxWidth = ; for (size_t i = ; i < vecRect.size(); i++) {
xpositions.push_back(vecRect[i].x); if (vecRect[i].height > maxHeight) {
maxHeight = vecRect[i].height;
}
if (vecRect[i].width > maxWidth) {
maxWidth = vecRect[i].width;
}
} int specIndex = ;
for (size_t i = ; i < vecRect.size(); i++) {
Rect mr = vecRect[i];
int midx = mr.x + mr.width / ; // use known knowledage to find the specific character
// position in 1/7 and 2/7
if ((mr.width > maxWidth * 0.8 || mr.height > maxHeight * 0.8) &&
(midx < int(m_theMatWidth / ) * &&
midx > int(m_theMatWidth / ) * )) {
specIndex = i;
}
} return specIndex;
}
GetChineseRect函数代码如下:
Rect CCharsSegment::GetChineseRect(const Rect rectSpe) {
int height = rectSpe.height;
float newwidth = rectSpe.width * 1.15f;
int x = rectSpe.x;
int y = rectSpe.y; int newx = x - int(newwidth * 1.15);
newx = newx > ? newx : ; Rect a(newx, y, int(newwidth), height); return a;
}
slideChineseWindow函数代码如下:
bool slideChineseWindow(Mat& image, Rect mr, Mat& newRoi, Color plateType, float slideLengthRatio, bool useAdapThreshold) {
std::vector<CCharacter> charCandidateVec; Rect maxrect = mr;
Point tlPoint = mr.tl(); bool isChinese = true;
int slideLength = int(slideLengthRatio * maxrect.width);
int slideStep = ;
int fromX = ;
fromX = tlPoint.x; for (int slideX = -slideLength; slideX < slideLength; slideX += slideStep) {
float x_slide = ; x_slide = float(fromX + slideX); float y_slide = (float)tlPoint.y;
Point2f p_slide(x_slide, y_slide); //cv::circle(image, p_slide, 2, Scalar(255), 1); int chineseWidth = int(maxrect.width);
int chineseHeight = int(maxrect.height); Rect rect(Point2f(x_slide, y_slide), Size(chineseWidth, chineseHeight)); if (rect.tl().x < || rect.tl().y < || rect.br().x >= image.cols || rect.br().y >= image.rows)
continue; Mat auxRoi = image(rect); Mat roiOstu, roiAdap;
if () {
if (BLUE == plateType) {
threshold(auxRoi, roiOstu, , , CV_THRESH_BINARY + CV_THRESH_OTSU);
}
else if (YELLOW == plateType) {
threshold(auxRoi, roiOstu, , , CV_THRESH_BINARY_INV + CV_THRESH_OTSU);
}
else if (WHITE == plateType) {
threshold(auxRoi, roiOstu, , , CV_THRESH_BINARY_INV + CV_THRESH_OTSU);
}
else {
threshold(auxRoi, roiOstu, , , CV_THRESH_OTSU + CV_THRESH_BINARY);
}
roiOstu = preprocessChar(roiOstu, kChineseSize); CCharacter charCandidateOstu;
charCandidateOstu.setCharacterPos(rect);
charCandidateOstu.setCharacterMat(roiOstu);
charCandidateOstu.setIsChinese(isChinese);
charCandidateVec.push_back(charCandidateOstu);
}
if (useAdapThreshold) {
if (BLUE == plateType) {
adaptiveThreshold(auxRoi, roiAdap, , ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, , );
}
else if (YELLOW == plateType) {
adaptiveThreshold(auxRoi, roiAdap, , ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, , );
}
else if (WHITE == plateType) {
adaptiveThreshold(auxRoi, roiAdap, , ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, , );
}
else {
adaptiveThreshold(auxRoi, roiAdap, , ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, , );
}
roiAdap = preprocessChar(roiAdap, kChineseSize); CCharacter charCandidateAdap;
charCandidateAdap.setCharacterPos(rect);
charCandidateAdap.setCharacterMat(roiAdap);
charCandidateAdap.setIsChinese(isChinese);
charCandidateVec.push_back(charCandidateAdap);
} } CharsIdentify::instance()->classifyChinese(charCandidateVec); double overlapThresh = 0.1;
NMStoCharacter(charCandidateVec, overlapThresh); if (charCandidateVec.size() >= ) {
std::sort(charCandidateVec.begin(), charCandidateVec.end(),
[](const CCharacter& r1, const CCharacter& r2) {
return r1.getCharacterScore() > r2.getCharacterScore();
}); newRoi = charCandidateVec.at().getCharacterMat();
return true;
} return false; }
在对中文字符进行识别时,增加一个小型的滑动窗口,以弥补通过省份字符直接查找中文字符时的定位不精等现象。
preprocessChar函数代码如下:
Mat preprocessChar(Mat in, int char_size) {
// Remap image
int h = in.rows;
int w = in.cols; int charSize = char_size; Mat transformMat = Mat::eye(, , CV_32F);
int m = max(w, h);
transformMat.at<float>(, ) = float(m / - w / );
transformMat.at<float>(, ) = float(m / - h / ); Mat warpImage(m, m, in.type());
warpAffine(in, warpImage, transformMat, warpImage.size(), INTER_LINEAR,
BORDER_CONSTANT, Scalar()); Mat out;
cv::resize(warpImage, out, Size(charSize, charSize)); return out;
}
首先进行仿射变换,将字符统一大小,并归一化到中间,并resize为 20*20,如下图所示:
转化为
judgeChinese 函数用于中文字符判断,后面字符识别时详细介绍。
EasyPR源码剖析(8):字符分割的更多相关文章
- EasyPR源码剖析(1):概述
EasyPR(Easy to do Plate Recognition)是本人在opencv学习过程中接触的一个开源的中文车牌识别系统,项目Git地址为https://github.com/liuru ...
- EasyPR源码剖析(9):字符识别
在上一篇文章的介绍中,我们已经通过相应的字符分割方法,将车牌区域进行分割,得到7个分割字符图块,接下来要做的就是将字符图块放入训练好的神经网络模型,通过模型来预测每个图块所表示的具体字符.神经网络的介 ...
- EasyPR源码剖析(4):车牌定位之Sobel算子定位
一.简介 sobel算子主要是用于获得数字图像的一阶梯度,常见的应用是边缘检测. Ⅰ.水平变化: 将 I 与一个奇数大小的内核进行卷积.比如,当内核大小为3时, 的计算结果为: Ⅱ.垂直变化: 将: ...
- EasyPR源码剖析(3):车牌定位之颜色定位
一.简介 对车牌颜色进行识别,可能大部分人首先想到的是RGB模型, 但是此处RGB模型有一定的局限性,譬如蓝色,其值是255,还需要另外两个分量都为0,不然很有可能你得到的值是白色.黄色更麻烦,它是由 ...
- EasyPR源码剖析(7):车牌判断之SVM
前面的文章中我们主要介绍了车牌定位的相关技术,但是定位出来的相关区域可能并非是真实的车牌区域,EasyPR通过SVM支持向量机,一种机器学习算法来判定截取的图块是否是真的“车牌”,本节主要对相关的技术 ...
- EasyPR源码剖析(6):车牌判断之LBP特征
一.LBP特征 LBP指局部二值模式,英文全称:Local Binary Pattern,是一种用来描述图像局部特征的算子,LBP特征具有灰度不变性和旋转不变性等显著优点. 原始的LBP算子定义在像素 ...
- EasyPR源码剖析(5):车牌定位之偏斜扭转
一.简介 通过颜色定位和Sobel算子定位可以计算出一个个的矩形区域,这些区域都是潜在车牌区域,但是在进行SVM判别是否是车牌之前,还需要进行一定的处理.主要是考虑到以下几个问题: 1.定位区域存在一 ...
- EasyPR源码剖析(2):车牌定位
上一篇主要介绍了车牌识别的整体框架和流程,车牌识别主要划分为了两个过程:即车牌检测和字符识别,而车牌识别的核心环节就是这一节主要介绍的车牌定位,即 Plate Locate.车牌定位主要是将图片中有可 ...
- Apache Spark源码剖析
Apache Spark源码剖析(全面系统介绍Spark源码,提供分析源码的实用技巧和合理的阅读顺序,充分了解Spark的设计思想和运行机理) 许鹏 著 ISBN 978-7-121-25420- ...
随机推荐
- Linux基础(学习过程记录)
常用快捷键:Tab:使用Tab键来进行命令补全,补全目录.补全命令参数Ctrl+c键来强行终止当前程序Ctrl+d 键盘输入结束或退出终端Ctrl+s 暂停当前程序,暂停后按下任意键恢复运行Ctrl+ ...
- python学习之-用scrapy框架来创建爬虫(spider)
scrapy简单说明 scrapy 为一个框架 框架和第三方库的区别: 库可以直接拿来就用, 框架是用来运行,自动帮助开发人员做很多的事,我们只需要填写逻辑就好 命令: 创建一个 项目 : cd 到需 ...
- java-其他-索引
数据结构 设计模式 算法题
- Exp3 免杀原理与实践 20164313
基础 AV厂商检测恶意软件的三种主流方式: 基于特征码的检测 启发式恶意软件检测 基于行为的恶意软件检测 实现免杀就是防止恶意软件被这是三种方式找到.具体手段为: 改变特征码 改变行为 非常规恶意软件 ...
- #include 相关问题
#include <> 和 #include “”的区别是#include <>先从系统默认的搜索路径开始搜索,#include “”是从当前目录开始搜索,如果未搜索到,都会继 ...
- mybatis学习 -每天一记 mybatis insert null 报错
mybatis 插入数据,model的属性存在null,插入报错 在使用mybatis 进行insert时,如果字段值存在null的情况,会出现插入失败的情况,解决方案: 如果使用spring boo ...
- 2018-2019-2 网络对抗技术 20165304 Exp6 信息搜集与漏洞扫描
2018-2019-2 网络对抗技术 20165304 Exp6 信息搜集与漏洞扫描 原理与实践说明 1.实践原理 信息搜集:渗透测试中首先要做的重要事项之一,搜集关于目标机器的一切信息 间接收集 D ...
- 教你快速录制gif动图
我们经常会遇到一些场景,需要你向别人展示一些操作或是效果——例如告诉别人某某软件的配置步骤啊.刚设计出来网站的动画效果怎么样啊.某某电影里面的一个镜头多么经典啊.打得大快人心的NBA绝杀瞬间是怎么回事 ...
- Django2.X报错-------ModuleNotFoundError: No module named 'django.core.urlresolvers'
django2.0 把原来的 django.core.urlresolvers 包 更改为了 django.urls包.所以将导入的包修改为django.urls.
- (转)Android 只开启一个Activity实例
在一个Activity中,多次调用startActivity()来启动另一个Activity,要想只生成一个Activity实例,方法有两种. 方法一:设置起动模式 一个Activity有四种启动模式 ...