使用opencv进行车牌提取及识别

2023-05-16

商业合作可联系:547691062@qq.com

目录

  • 1车牌提取过程
    • 1.1车辆图像获取
    • 1.2车牌定位
    • 1.3车牌字符分割
  • 2车牌提取
    • 2.1灰度化
    • 2.2Candy边缘检测
    • 2.3形态学(膨胀腐蚀)处理
    • 2.4轮廓处理
    • 2.5自适应二值化处理
  • 3字符提取分割
    • 3.1像素值判断
    • 3.2确认字符位置

 

车牌提取过程

一个典型的车辆牌照识别系统一般包括以下4个部分:车辆图像获取、车牌定位、车牌字符分割和车牌字符识别 。

本篇文章只介绍车牌的提取分割过程,关于如何对分割后的字符进行识别 ,请看我的另一篇WIKI ,opencv基于人工神经网络的字母识别。

车辆图像获取

车辆图像获取是车牌识别的第一步,也是很重要的一步,车辆图像的好坏对后面的工作有很大的影响。如果车辆图像的质量太差,连人眼都没法分辨,那么肯定不会被机器所识别出来。车辆图像都是在实际现场拍摄出来的,实际环境情况比较复杂,图像受天气和光线等环境影响较大,在恶劣的工作条件下系统性能将显著下降。 现有的车辆图像获取方式主要有两种:一种是由彩色摄像机和图像采集卡组成,其工作过程是:当车辆检测器(如地感线圈、红外线等)检测到车辆进入拍摄范围时,向主机发送启动信号,主机通过采集卡采集一幅车辆图像,为了提高系统对天气、环境、光线等的适应性,摄像机一般采用自动对焦和自动光圈的一体化机,同时光照不足时还可以自动补光照明,保证拍摄图片的质量;另一种是由数码照相机构成,其工作过程是:当车辆检测器检测到车辆进入拍摄范围时,直接给数码照相机发送一个信号,数码相机自动拍摄一幅车辆图像,再传到主机上,数码相机的一些技术参数可以通过与数码相机相连的主机进行设置,光照不足时也需要自动开启补光照明,保证拍摄图片的质量。

图(有车牌的图像)

车牌定位

车牌定位的主要工作是从摄入的汽车图像中找到汽车牌照所在位置,并把车牌从该区域中准确地分割出来,供字符分割使用。因此,牌照区域的确定是影响系统性能的重要因素之一,牌照的定位与否直接影响到字符分割和字符识别的准确率。目前车牌定位的方法很多,但总的来说可以分为以下4类:(1)基于颜色的分割方法,这种方法主要利用颜色空间的信息,实现车牌分割,包括彩色边缘算法、颜色距离和相似度算法等;(2)基于纹理的分割方法,这种方法主要利用车牌区域水平方向的纹理特征进行分割,包括小波纹理、水平梯度差分纹理等;(3)基于边缘检测的分割方法;(4)基于数学形态法的分割方法。 本文为了代码实现上的方便,我采用的是基于边缘检测的分割方法。主要是利用水平投影方法和垂直投影方法进行车牌定位。

车牌字符分割

要识别车牌字符,前提是先进行车牌字符的正确分割与提取。字符分割的任务是把多列或多行字符图像中的每个字符从整个图像中切割出来成为单个字符。车牌字符的正确分割对字符的识别是很关键的。传统的字符分割算法可以归纳为以下三类:直接分割法、基于识别基础上的分割法、自适应分割线类聚法。直接分割法简单,但它的局限是分割点的确定需要较高的准确性;基于识别基础上的分割法是把识别和分割结合起来,但是需要识别的高准确性,它根据分类和识别的耦合程度又有不同的划分;自适应分割线聚类法是要建立一个分类器,用它来判断图像的每一列是否是分割线,它是根据训练样本来进行自适应学习的神经网络分类器,但对于粘连字符训练困难。也有直接把字符组成的单词当作一个整体来识别的,诸如运用马尔科夫数学模型等方法进行处理,这些算法主要应用于印刷体文本识别。

车牌提取

灰度化

灰度化的概念就是将一张三通道RGB颜色的图像变成单通道灰度图,为接下来的图像处理做准备。

 //灰度化

 Mat  gray_image;

 cvtColor(image, gray_image, CV_RGB2GRAY);

 imshow("test", gray_image);

图(灰度图)

Candy边缘检测

Canny边缘检测算子的方向性质保证了很好的边缘强度估计,而且能同时产生边缘梯度方向和强度两个信息,即能在一定程度上抗噪声又能保持弱边缘,因此采用以canny算子做边缘检测。

Canny算法步骤:

(1)去噪

任何边缘检测算法都不可能在未经处理的原始数据上很好地處理,所以第一步是对原始数据与高斯 mask 作卷积,得到的图像与原始图像相比有些轻微的模糊(blurred)。这样,单独的一个像素雜訊在经过高斯平滑的图像上变得几乎没有影响。

(2)用一阶偏导的有限差分来计算梯度的幅值和方向。

(3)对梯度幅值进行非极大值抑制。

车牌细定位的目的是为下一步字符的分割做,就是要进一步去掉车牌冗余的部分。在一幅经过适当二值化处理 含有车牌的图像中,车牌区域具有以下三个基本特征:

1.在一个不大的区域内密集包含有多个字符;

2.车牌字符与车牌底色形成强烈对比;

3.车牌区域大小相对固定,区域长度和宽度成固定比例。

   //Candy/sobel 边缘检测:
    Mat candy_image;
   Canny(blur_image, candy_image, 500, 200, 3);
   //imshow("test", candy_image);

图(边缘检测)

形态学(膨胀腐蚀)处理

膨胀与腐蚀的处理效果就如其名字一样,我们通过膨胀连接相近的图像区域,通过腐蚀去除孤立细小的色块。通过这一步,我们希望将所有的车牌号字符连通起来,这样为我们接下来通过轮廓识别来选取车牌区域做准备。由于字符都是横向排列的,因此要连通这些字符我们只需进行横向的膨胀即可。

进行膨胀腐蚀操作需要注意的是要一次到位,如果一次膨胀没有连通到位,那么再次腐蚀将会将图像回复原装,因此我首先做了2次迭代的膨胀,保证数字区域能连通起来,再进行4次迭代腐蚀,尽可能多的去除小块碎片,随后2次迭代膨胀,保证膨胀次数与腐蚀次数相同,以回复连通区域形态大小。

矩形轮廓查找与筛选经过上一步操作,理论上来说车牌上的字符连通成一个矩形区域,通过轮廓查找我们可以定位该区域。当然,更为准确的说,经过上面的操作,我们将原始图片中在X方向排列紧密的纵向边缘区域连通成了一个矩形区域,出了车牌符合这个特点外,其他一些部分如路间栏杆,车头的纹理等同样符合。因此我们会找到很多这样的区域,这就需要我们进一步根据一些关于车牌特点的先验知识对这些矩形进行进一步筛选。最终,定位车牌所在的矩形区。

        //形态学处理
	//图片膨胀处理
	Mat dilate_image, erode_image;
	//自定义 核进行 x 方向的膨胀腐蚀
	Mat elementX = getStructuringElement(MORPH_RECT, Size(25, 1));
	Mat elementY = getStructuringElement(MORPH_RECT, Size(1, 19));
	Point point(-1, -1);
	dilate(candy_image, dilate_image, elementX, point, 2);
	erode(dilate_image, erode_image, elementX, point, 4);
	dilate(erode_image, dilate_image, elementX, point, 2);

	//自定义 核进行 Y 方向的膨胀腐蚀
	erode(dilate_image, erode_image, elementY, point, 1);
	dilate(erode_image, dilate_image, elementY, point, 2);
	//imshow("test", dilate_image);
	//waitKey(1000);
	imwrite("dilate_image.jpg", dilate_image);
        //噪声处理
	//平滑处理 中值滤波
	Mat blurr_image;
	medianBlur(dilate_image, blurr_image, 15);
	medianBlur(blurr_image, blurr_image, 15);
	imshow("test", blurr_image);

图(形态学处理)

轮廓处理

提取轮廓

        //矩形轮廓查找与筛选:
	Mat contour_image;
	//深拷贝  查找轮廓会改变源图像信息,需要重新 拷贝 图像
	contour_image = blurr_image.clone();
	vector<vector<Point>> contours;
	findContours(contour_image, contours, CV_RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
	//画出轮廓
	drawContours(contour_image, contours, -1, Scalar(255), 1);
         //轮廓表示为一个矩形  车牌提取
	Mat  roi_image;
	vector<Point> rectPoint;
	for (int i = 0; i < contours.size(); i++) {
		Rect r = boundingRect(Mat(contours[i]));
		//RotatedRect r = minAreaRect(Mat(contours[i]));
		cout << "contours " << i << "  height = " << r.height << "  width = " << r.width << "rate = " << ((float)r.width / r.height) << endl;
		if ((float)r.width / r.height >= 2.2 && (float)r.width / r.height <= 3.6) {
			cout << "r.x = " << r.x << "  r.y  = " << r.y << endl;
			rectangle(contour_image, r, Scalar(0, 0, 255), 2);
                        for (int j = 0; j < contours[i].size(); j++) {
				cout << "point = " << contours[i][j] << endl;
			}
			//rectangle(image, r, Scalar(0, 0, 255), 3);
			roi_image = image(r);
		}
	}

图(轮廓)

自适应二值化处理

二值化的处理强化了锐利的边缘,进一步去除图像中无用的信息,使用过程中主要注意阀值的选取,我为了省事儿使用了opencv自带的自适应的的二值化处理,缺点是无用信息有点多,但车牌数字信息也会更为凸显。

cvThreshold(sobel, threshold, 0, 255, CV_THRESH_BINARY| CV_THRESH_OTSU);

最后的参数CV_THRESH_OTSU就是使用自适应算法

        //Candy 边缘检测
	Mat candy_roi_image;
	Canny(roi_gray_image, candy_roi_image, 450, 120, 3);
	imshow("test", candy_roi_image);
        //二值化
	Mat roi_threadhold_image;
	threshold(candy_roi_image, roi_threadhold_image, 50, 255, CV_THRESH_BINARY);
	imshow("test", roi_threadhold_image);

图(二值化)

字符提取分割

像素值判断

这里主要使用水平映射和垂直映射像素进行像素级分割。

分割方法:首先判断每一列的像素值大于0的像素个数超过5个时,认为此列是有数字的,记录每列像素是否大于5,产生一个数组,数组的值有可能是一下。

a[100] = {00000001001110011111111111111100001111111111000000011111111100000000000001111110000110101011110000000000000000。。。。。。。}

这里做一个简单的水平方向的滤波,Size大小是5 ,然后对 每一个像素做处理,处理方法:

        //对数组进行滤波,减少突变概率
	for (int i = 2; i < roi_col - 1 - 2; i++) {
		if ((pixrow[i - 1] + pixrow[i - 2] + pixrow[i + 1] + pixrow[i + 2]) >= 3) {
			pixrow[i] = 1;
		}
		else if((pixrow[i - 1] + pixrow[i - 2] + pixrow[i + 1] + pixrow[i + 2]) <= 1) {
			pixrow[i] = 0;
		}
	}

如果 该像素周围为1的像素大于等于3个,不算自己 那么久认为该像素为 1, 如果 该像素周围 像素为0的个数小于等于1 ,那么就认为该像素为0

确认字符位置

//确认字符位置
	int count = 0;
	bool flage = false;
	for (int i = 0; i < roi_col - 1; i++) {
		pix = pixrow[i];
		if (pix == 1 && !flage) {
			flage = true;
			position1[count] = i;
			continue;
		}
		if (pix == 0 && flage) {
			flage = false;
			position2[count] = i;
			count++;
		}
		if (i == (roi_col - 2) && flage) {
			flage = false;
			position2[count] = i;
			count++;
		}
	}

水平映射以同样的方式完成,最后分割字符

//截取字符
	Mat licenseN = Mat(Scalar(0));
	cout << "countYY = "<< countYY << endl;
	for (int i = 0; i < countYY; i++) {
		Rect rect(licenseX[i], licenseY[i], licenseW[i], licenseH[i]);
		cout << "position = " << licenseX[i] << "  " << licenseY[i] << "  " << licenseW[i] << "  " << licenseH[i] << endl;
		licenseN = roi_threadhold_image(rect);
		imshow("test1"+i, licenseN);
		ostringstream oss;
		oss << "licenseN" << i << ".jpg";
		imwrite(oss.str(), licenseN);
		waitKey(1000);
	}

  图(分割后)

 

源代码

检测分割部分

#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp> 
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
#include<sstream>
using namespace std;
using namespace cv;


int  main_licenseprogress(Mat & image);


//int main() {
// string path = "license/license";
// Mat image;
// for (int i = 2; i <= 2; i++) {
// ostringstream oss;
// oss << path << i << ".jpg";
// cout << "path = " << path << "str = " << oss.str() << endl;
// image = imread(oss.str(), 1);
// main_licenseprogress(image);
// }
// system("pause");
// return 0;
//}




int main_licenseprogress(Mat & image) {
imshow("test", image);
waitKey(1000);
//灰度化
Mat  gray_image;
cvtColor(image, gray_image, CV_RGB2GRAY);
imshow("test", gray_image);
imwrite("license3_gray.jpg", gray_image);

waitKey(1000);


//平滑处理 中值滤波
Mat blur_image;
medianBlur(gray_image, blur_image, 3);
imwrite("blur_image.jpg", blur_image);
//imshow("test", gray_image);
//waitKey(1000);


//自适应二值化处理  由于candy 算子产生的图像已经是二值化图像,所以这里不做处理
//Mat threadhold_image;
//threshold(blur_image, threadhold_image, 200, 255, CV_THRESH_BINARY);
//imshow("test", threadhold_image);
//waitKey(1000);


//Candy/sobel 边缘检测:
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(blur_image, sobelX, CV_32F, x_mask);
filter2D(blur_image, sobelY, CV_32F, y_mask);
sobelX = abs(sobelX);
sobelY = abs(sobelY);


Mat candy_image;
Canny(blur_image, candy_image, 500, 250, 3);
//imshow("test", candy_image);
//waitKey(1000);
//imshow("test", sobelY);
//waitKey(1000);
imwrite("candy_image.jpg", candy_image);


//形态学处理
//图片膨胀处理
Mat dilate_image, erode_image;
//自定义 核进行 x 方向的膨胀腐蚀
Mat elementX = getStructuringElement(MORPH_RECT, Size(25, 1));
Mat elementY = getStructuringElement(MORPH_RECT, Size(1, 19));
Point point(-1, -1);
dilate(candy_image, dilate_image, elementX, point, 2);
imwrite("dilate_image.jpg", dilate_image);
erode(dilate_image, erode_image, elementX, point, 4);
imwrite("erode_image.jpg", erode_image);
dilate(erode_image, dilate_image, elementX, point, 2);
imwrite("dilate_image1.jpg", dilate_image);
//自定义 核进行 Y 方向的膨胀腐蚀
erode(dilate_image, erode_image, elementY, point, 1);
imwrite("erode_image1.jpg", erode_image);
dilate(erode_image, dilate_image, elementY, point, 2);
//imshow("test", dilate_image);
//waitKey(1000);
imwrite("dilate_image2.jpg", dilate_image);
//噪声处理
//平滑处理 中值滤波
Mat blurr_image;
medianBlur(dilate_image, blurr_image, 15);
medianBlur(blurr_image, blurr_image, 15);
imshow("test", blurr_image);
waitKey(1000);


//矩形轮廓查找与筛选:
Mat contour_image;
//深拷贝  查找轮廓会改变源图像信息,需要重新 拷贝 图像
contour_image = blurr_image.clone();
vector<vector<Point>> contours;
findContours(contour_image, contours, CV_RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
//画出轮廓
drawContours(contour_image, contours, -1, Scalar(255), 1);
//imshow("test", contour_image);
//waitKey(1000);


//Mat cannyy_image;
//Canny(contour_image, cannyy_image, 500, 200, 3);


//hough 直线 
//vector<Vec4i> lines;
//HoughLinesP(cannyy_image, lines, 1, CV_PI / 180, 20, 10, 0);
//for (size_t i = 0; i < lines.size(); i++) {
// Vec4i l = lines[i];
// line(image, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 1, LINE_AA);
// cout << "直线:  "<< i <<endl;
//}
//imshow("test", image);
//waitKey(1000);


//轮廓表示为一个矩形  车牌提取
Mat  roi_image;
vector<Point> rectPoint;
for (int i = 0; i < contours.size(); i++) {
Rect r = boundingRect(Mat(contours[i]));
//RotatedRect r = minAreaRect(Mat(contours[i]));
cout << "contours " << i << "  height = " << r.height << "  width = " << r.width << "rate = " << ((float)r.width / r.height) << endl;
if ((float)r.width / r.height >= 2.2 && (float)r.width / r.height <= 3.6) {
cout << "r.x = " << r.x << "  r.y  = " << r.y << endl;
rectangle(contour_image, r, Scalar(0, 0, 255), 2);
imwrite("contour_image.jpg", contour_image);
Point p1, p2, p3, p4;
p1.x = r.x;
p1.y = r.y;
p2.x = r.x + r.width;
p2.x = r.y;
p3.x = r.x + r.width;
p3.y = r.y + r.height;
p4.x = r.x;
p4.y = r.y + r.height;


rectPoint.push_back(p1);
rectPoint.push_back(p2);
rectPoint.push_back(p3);
rectPoint.push_back(p4);


for (int j = 0; j < contours[i].size(); j++) {
cout << "point = " << contours[i][j] << endl;
}
//rectangle(image, r, Scalar(0, 0, 255), 3);
roi_image = image(r);
}
}
imshow("test", roi_image);
waitKey(1000);
imwrite("roi_image.jpg", roi_image);
//图片放大
Mat large_image;
int col = roi_image.cols, row = roi_image.rows;
resize(roi_image, large_image, Size(300, 300 * row / col));
imshow("test", large_image);
waitKey(2000);
//车牌分割
//灰度化 
Mat  roi_gray_image;
cvtColor(large_image, roi_gray_image, CV_RGB2GRAY);
imshow("test", roi_gray_image);
waitKey(1000);


//中值滤波 增强边缘




//Candy 边缘检测
Mat candy_roi_image;
Canny(roi_gray_image, candy_roi_image, 450, 120, 3);
imshow("test", candy_roi_image);
imwrite("candy_roi_image.jpg", candy_roi_image);
waitKey(1000);
//二值化
Mat roi_threadhold_image;
threshold(candy_roi_image, roi_threadhold_image, 50, 255, CV_THRESH_BINARY);
imshow("test", roi_threadhold_image);
waitKey(1000);
imwrite("roi_threadhold_image.jpg", roi_threadhold_image);
//平滑处理 中值滤波
//Mat roi_blurr_image;
//medianBlur(roi_threadhold_image, roi_blurr_image, 1);
//imshow("test", roi_blurr_image);
//waitKey(1000);


//查找轮廓
Mat roi_contours_image;
vector<vector<Point>> roi_contours;
roi_contours_image = roi_threadhold_image.clone();
findContours(roi_contours_image, roi_contours, CV_RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
//画出轮廓
//drawContours(roi_contours_image, roi_contours, -1, Scalar(255), 2);
//imshow("test", roi_contours_image);
//waitKey(1000);


//轮廓表示成矩形
//轮廓表示为一个矩形  车牌提取
vector<Point> roi_rectPoint;
for (int i = 0; i < roi_contours.size(); i++) {
Rect r = boundingRect(Mat(roi_contours[i]));
//RotatedRect r = minAreaRect(Mat(contours[i]));
cout << "contours " << i << "  height = " << r.height << "  width = " << r.width << "rate = " << ((float)r.width / r.height) << endl;
cout << "r.x = " << r.x << "  r.y  = " << r.y << endl;




//rectangle(large_image, r, Scalar(0, 0, 255), 1);
Point p1, p2, p3, p4;
p1.x = r.x;
p1.y = r.y;
p2.x = r.x + r.width;
p2.x = r.y;
p3.x = r.x + r.width;
p3.y = r.y + r.height;
p4.x = r.x;
p4.y = r.y + r.height;


roi_rectPoint.push_back(p1);
roi_rectPoint.push_back(p2);
roi_rectPoint.push_back(p3);
roi_rectPoint.push_back(p4);
for (int j = 0; j < roi_contours[i].size(); j++) {
cout << "point = " << roi_contours[i][j] << endl;
}
}
imshow("test", roi_threadhold_image);
waitKey(1000);


//矩形轮廓特征提取
int contours_height[30], contours_width[30];
for (int i = 0; i < roi_contours.size(); i++) {
Rect r = boundingRect(Mat(roi_contours[i]));
contours_height[i] = r.height;
contours_width[i] = r.width;
cout << "contours_height = " << r.height << " contours_width = " << r.width << endl;
}


//判断字符水平位置
int roi_col = roi_threadhold_image.cols, roi_row = roi_threadhold_image.rows, position1[50], position2[50], roi_width[50];
uchar pix;
//cout << roi_threadhold_image << endl;

//确认为1 的像素
int pixrow[1000];
for (int i = 0; i < roi_col - 1; i++) {
for (int j = 0; j < roi_row - 1; j++) {
pix = roi_threadhold_image.at<uchar>(j, i);
pixrow[i] = 0;
if (pix > 0) {
pixrow[i] = 1;
break;
}
}
}
//对数组进行滤波,减少突变概率
for (int i = 2; i < roi_col - 1 - 2; i++) {
if ((pixrow[i - 1] + pixrow[i - 2] + pixrow[i + 1] + pixrow[i + 2]) >= 3) {
pixrow[i] = 1;
}
else if((pixrow[i - 1] + pixrow[i - 2] + pixrow[i + 1] + pixrow[i + 2]) <= 1) {
pixrow[i] = 0;
}
}
//确认字符位置
int count = 0;
bool flage = false;
for (int i = 0; i < roi_col - 1; i++) {
pix = pixrow[i];
if (pix == 1 && !flage) {
flage = true;
position1[count] = i;
continue;
}
if (pix == 0 && flage) {
flage = false;
position2[count] = i;
count++;
}
if (i == (roi_col - 2) && flage) {
flage = false;
position2[count] = i;
count++;
}
}
//记录所有字符宽度
for (int n = 0; n < count; n++) {
cout << " position1 = " << position2[n] <<" position2 = "<< position2[n] << "distance =" << (position2[n] - position1[n]) << endl;
roi_width[n] = position2[n] - position1[n];
}
// 减去最大值,最小值
int max = roi_width[0], max_index = 0;
int min = roi_width[0], min_index = 0;
for (int n = 1; n < count; n++) {
if (max < roi_width[n]) {
max = roi_width[n];
max_index = n;
}
if (min > roi_width[n]) {
min = roi_width[n];
min_index = n;
}
}
int index = 0;
int new_roi_width[50];
for (int i = 0; i < count; i++) {
if (i == min_index || i == max_index) {


}
else {
new_roi_width[index] = roi_width[i];
index++;
}
}
cout << "count = " << count << endl;
for (int i = 0; i < count - 2; i++) {
cout << "new roi width = " << new_roi_width[i] << endl;
}
//取后面三个值的平均值:
int avgre = (int)((new_roi_width[count - 3] + new_roi_width[count - 4] + new_roi_width[count - 5]) / 3.0);
cout << avgre << endl;
//for (int i = count-3; i < count - 6; i--) {
//if (i < 0) {
// break;
//}
//cout << "count = " << new_roi_width[i] << endl;
//}
//字母位置信息确认
int licenseX[10], licenseW[10], licenseNum = 0;
int countX = 0;
for (int i = 0; i < count; i++) {
if (roi_width[i] > (avgre - 8) && roi_width[i] < (avgre + 8)) {
licenseX[licenseNum] = position1[i];
licenseW[licenseNum] = roi_width[i];
licenseNum++;
cout << "licenseX = " << licenseX[i] << "  roi_width  =" << roi_width[i] << endl;
continue;
}
if (roi_width[i] > (avgre * 2 - 10) && roi_width[i] < (avgre * 2 + 10)) {
licenseX[licenseNum] = position1[i];
licenseW[licenseNum] = roi_width[i];
licenseNum++;
cout << "licenseX = " << licenseX[i] <<"  roi_width  ="<< roi_width[i]<< endl;
}
}


 //判断字符垂直位置
int licenseY[10], licenseH[10];
int position3[10], position4[10];
//确认为1 的像素
int countYY = 0;
int pixcol[1000], row_height[10];
for (int temp = 0; temp < licenseNum; temp++) {
for (int i = 0; i < roi_row - 1; i++) {
for (int j = licenseX[temp]; j < (licenseX[temp]+ licenseW[temp]); j++) {
pix = roi_threadhold_image.at<uchar>(i, j);
pixcol[i] = 0;
if (pix > 0) {
pixcol[i] = 1;
break;
}
}
}
//对数组进行滤波,减少突变概率
for (int i = 2; i < roi_row - 1 - 2; i++) {
if ((pixcol[i - 1] + pixcol[i - 2] + pixcol[i + 1] + pixcol[i + 2]) >= 3) {
pixcol[i] = 1;
}
else if ((pixcol[i - 1] + pixcol[i - 2] + pixcol[i + 1] + pixcol[i + 2]) <= 1) {
pixcol[i] = 0;
}
}
//确认字符位置
int countY = 0;
bool flage2 = false;
for (int i = 0; i < roi_row - 1; i++) {
pix = pixcol[i];
if (pix == 1 && !flage2) {
flage2 = true;
position3[countY] = i;
continue;
}
if (pix == 0 && flage2) {
flage2 = false;
position4[countY] = i;
countY++;
}
}

//记录所有字符宽度
for (int n = 0; n < countY; n++) {
cout << " position3 = " << position3[n] << " position4 = " << position4[n] << "distance =" << (position4[n] - position3[n]) << endl;
row_height[countYY] = position4[n] - position3[n];
licenseY[countYY] = position3[n];
licenseH[countYY] = row_height[countYY];
}
countYY++;
}

//截取字符
Mat licenseN = Mat(Scalar(0));
cout << "countYY = "<< countYY << endl;
for (int i = 0; i < countYY; i++) {
Rect rect(licenseX[i], licenseY[i], licenseW[i], licenseH[i]);
cout << "position = " << licenseX[i] << "  " << licenseY[i] << "  " << licenseW[i] << "  " << licenseH[i] << endl;
licenseN = large_image(rect);
imshow("test1"+i, licenseN);
ostringstream oss;
oss << "licenseN" << i << ".jpg";
imwrite(oss.str(), licenseN);
waitKey(1000);
}



cout << "license plate process" << endl;
return 0;
}





识别部分:

#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include<iostream>
#include<sstream>
using namespace std;
using namespace cv;
using namespace ml;




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 calcGradientFeat(Mat & imgSrc, vector<float> & feat) {
Mat image;
cvtColor(imgSrc, image, CV_BGR2GRAY);
resize(image, image, Size(8, 16));
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);
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);
}
}


Mat img2;
resize(image, img2, Size(4, 8));
int r = img2.rows;
int c = img2.cols;
if (img2.isContinuous()) {
c = r*c;
r = 1;
}
for (int i = 0; i < r; i++) {
const uchar *linePtr = img2.ptr<uchar>(i);
for (int j = 0; j < c; j++) {
feat.push_back(linePtr[j]);
}
}
//       cout<<sobelX<<endl;
//     cout<<sobelY<<endl;
// cout<< x_mask<<endl;
// cout<<img2<<endl;
// for(int i=0; i<feat[num].size(); i++)
//   {
//           cout<<feat[i]<<endl;
//   }
//       imshow("cat", img2);
//       cout<<"sumValue ="<<sumMatValue(image)<<endl;
}


Ptr<StatModel> buildMLPClassifier(Mat & input, Mat & output) {
Ptr<ANN_MLP> model;
//train classifier;
int layer_sz[] = { input.cols, 100 , output.cols };
int nlayers = (int)(sizeof(layer_sz) / sizeof(layer_sz[0]));
Mat layer_sizes(1, nlayers, CV_32S, layer_sz);
int method;
double method_param;
int max_iter;
if (1) {
method = ANN_MLP::BACKPROP;
method_param = 0.01;
max_iter = 100;
}
else {
method = ANN_MLP::RPROP;
method_param = 0.1;
max_iter = 1000;
}
Ptr<TrainData> tData = TrainData::create(input, ROW_SAMPLE, output);
model = ANN_MLP::create();
cout << "create success" << endl;
model->setLayerSizes(layer_sizes);
model->setActivationFunction(ANN_MLP::SIGMOID_SYM, 0, 0);
model->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, max_iter, FLT_EPSILON));
//setIterCondition(max_iter, 0);
model->setTrainMethod(method, method_param);
cout << "train data in process ...."<<endl;
model->train(tData);
cout << "train success" << endl;
model->save("mlp1.xml");
return model;
}
Ptr<StatModel> loadMLPClassifiler() {


Ptr<ANN_MLP> model = Algorithm::load<ANN_MLP>("mlp1.xml");
return model;
}




int main_num_reconginzed()
{
Mat image;
vector<float>feats;
vector<float>test, test1;
string path = "code/python_image_learn/identfying_code_recognize/charSamples/";
int num = 0;
int classfilternum = 34;
int modlenum = 30;
for (int i = 0; i < classfilternum; i++) {
for (int j = 0; j < modlenum; j++) {
ostringstream oss;
oss << path << i << "/" << j << ".png";
//cout<<oss.str()<<endl;
image = imread(oss.str());
calcGradientFeat(image, feats);
num++;


if (i == 11 && j == 10) {
ostringstream oss;
oss << path << i << "/" << (j + 1) << ".png";
//cout<<oss.str()<<endl;
image = imread(oss.str());
calcGradientFeat(image, test);
}
}
}
Mat input, output;
input = Mat(classfilternum*modlenum, 48, CV_32F);
output = Mat(classfilternum*modlenum, classfilternum, CV_32F, Scalar(0));
int r = input.rows;
int c = input.cols;
if (input.isContinuous()) {
c = r*c;
r = 1;
}
for (int i = 0; i < r; i++) {
float *linePtr = input.ptr<float>(i);
for (int j = 0; j < c; j++) {
linePtr[j] = feats[c*i + j];
}
}
for (int i = 0; i < output.rows; i++) {
float *lineoutput = output.ptr<float>(i);
lineoutput[i / modlenum] = 1;
}


//if(
//Ptr<StatModel> model = buildMLPClassifier(input, output);
Ptr<StatModel> model = loadMLPClassifiler();
float response = model->predict(test, test1);
cout << "response = " << response << endl;
for (int i = 0; i < test1.size(); i++)
{
cout << "test1 = " << test1[i] << endl;
}
//cout<<input<<endl;
//cout<<"rows = "<<input.rows<<"col = "<<input.cols<<endl;
//cout<<output<<endl;
    waitKey(0);                    //等待按键
system("pause");
return 0;
}

https://blog.csdn.net/jinshengtao/article/details/17883075

商业合作可联系:547691062@qq.com

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用opencv进行车牌提取及识别 的相关文章

  • OpenCV非旋转图像拼接

    我正在 OpenCV 中进行图像拼接 从不同位置拍摄平面场景的照片并尝试构图全景图 我修改了缝合示例以满足我的需要 openCV 拼接管道的问题是 它假设相机纯粹旋转 但对我来说情况并非如此 当拍摄的照片与场景完全正交时 没有相机旋转 只是
  • 有没有办法检测图像是否模糊? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想知道是否有一种方法可以通过分析图像数据来确定图像是否模糊 估计图像清晰度的另一种非常简单的方法是使用拉普拉斯 或 LoG 滤波器并
  • OpenCV:视频录制太快

    我有一个简单的录像机 录制来自网络摄像头的视频 theVideoWriter open filename countAsString ext CV FOURCC X V I D 30 Size 1920 1080 true while re
  • Pyinstaller“无法执行脚本 pyi_rth_pkgres”并且缺少软件包

    这是我第一次在这里发布问题 因为我的大部分问题已经被其他人回答了 我正在 python 中开发 GUI 应用程序 并尝试使用 pyinstaller 将其打包到单个文件夹和 exe 中 以便于移植 目前 我使用 Windows 10 和 a
  • 如何使用 OpenCV 从图像中获取调色板 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想提取图像的调色板 类似于此 来自 我需要它来提取特定的颜色 如黄色 绿色和棕色 并显示该颜色覆盖的区域的百分比 另外 我可以添加更
  • OpenCV:使用 StereoCamera 系统对颜色标记进行 3D 姿态估计

    我有一个立体摄像系统并使用两者正确校准它 cv calibrateCamera and cv stereoCalibrate My reprojection error似乎没问题 凸轮0 0 401427 凸轮1 0 388200 立体声
  • 如何将 opencv mat 图像转换为 gdi 位图

    我想将 openCV Mat 文件转换为 GDI 位图图像 我找不到任何有关如何执行此操作的信息 我认为没有直接的方法可以做到这一点 但我希望它不涉及将其写入文件并读回 http opencv users 1802565 n2 nabble
  • 使用 SURF 在检测到的对象周围绘制矩形

    我正在尝试从涉及冲浪检测器的以下代码中检测对象 我不想绘制匹配项 我想在检测到的对象周围绘制一个矩形 但不知何故我无法获得正确的单应性 请任何人指出在哪里我走错了 include
  • OpenCV Python RTSP 流

    我想使用 RTSP 从 IP 摄像机流式传输视频 但我有一个问题 我已经安装了先决条件 而且我的 RTSP 链接可以在 VlC 播放器上运行 但是当我在编辑器中尝试并运行它时 它说找不到相机 这是我的代码 import cv2 import
  • Android for OpenCV - 打开跟踪文件时出错,UnsatisfiedLinkError

    我对 Android 开发和 OpenCV 都是新手 我从 Android 下载了 OpenCV 库http sourceforge net projects opencvlibrary files opencv android http
  • GrabCut - bgdModel 和 fgdModel 为空 - 断言错误

    我正在尝试使用 OpenCV2 1 C 中的 GrabCut 算法进行图像分割 这是我的代码 Mat rgbWorkImage imread argv 1 Mat mask mask Scalar 0 Mat bgdModel fgdMod
  • 在OpenCV中将YUV转换为BGR或RGB

    我有一个电视采集卡 其输入内容为 YUV 格式 我在这里看到了与此问题类似的其他帖子 并尝试尝试所述的所有可能的方法 但它们都没有提供清晰的图像 目前最好的结果是 OpenCVcvCvtColor scr dst CV YUV2BGR 函数
  • 类型错误:只有长度为 1 的数组可以转换为 Python 标量

    我是 openCV 的初学者 正在尝试分析数独求解器的现有代码 有这一段代码会引发错误 samples np float32 np loadtxt feature vector pixels data responses np float3
  • OpenCv 与 Android studio 1.3+ 使用新的 gradle - 未定义的参考

    我在使用原生 OpenCv 2 4 11 3 0 0 也可以 和 Android Studio 1 3 以及新的 ndk 支持时遇到问题 所有关于 mk 文件的教程 但我想将它与新的实验性 gradle 一起使用 使用 Kiran 答案An
  • 为什么opencv videowriter这么慢?

    你好 stackoverflow 社区 我有一个棘手的问题 我需要你的帮助来了解这里发生了什么 我的程序从视频采集卡 Blackmagic 捕获帧 到目前为止 它工作得很好 同时我用 opencv cv imshow 显示捕获的图像 它也工
  • 通过 cmake 链接作为外部项目包含的 opencv 库[重复]

    这个问题在这里已经有答案了 我对 cmake 比较陌生 经过几天的努力无法弄清楚以下事情 我有一个依赖于 opencv 的项目 它本身就是一个 cmake 项目 我想静态链接 opencv 库 我正在做的是我的项目中有一份 opencv 源
  • OpenCV OpenNI 校准kinect

    我使用 home 通过 kinect 进行捕捉 capture retrieve depthMap CV CAP OPENNI DEPTH MAP capture retrieve bgrImage CV CAP OPENNI BGR IM
  • 如何将本机库链接到 IntelliJ 中的 jar?

    我正在尝试在 IntelliJ 中设置 OpenCV 但是我一直在弄清楚如何告诉 IntelliJ 在哪里可以找到本机库位置 在 Eclipse 中 添加 jar 后 您可以在 Build Config 屏幕中设置 Native 库的位置
  • 如何根据图像中的对象大小(以像素为单位)来测量现实世界中的对象大小(例如英寸、厘米等)?

    我计算了物体的大小pixel来自包含对象的图像 我想测量现实世界中物体的大小 有没有办法找出乘数来测量实际尺寸 我目前正在使用python以便实施 通常 您将使用相机获取图像 该相机通过镜头将 3 维场景投影到 2 维传感器上 垂直 高度
  • 我可以将 OpenCV 的发布配置与我的应用程序的调试配置一起使用吗?

    我正在编写一个通用 Windows 应用程序 它使用 OpenCV 进行相机校准和标签检测等 我希望能够在我自己的 DLL 处于调试模式时使用发布模式 完全优化的 OpenCV DLL 这可能吗 如果是这样 我如何配置 CMake 来实现它

随机推荐

  • 图深度学习 Deep Learning on Graph

    深度学习在图上的应用 引言图神经网络图卷积网络卷积操作谱方法Efficiency AspectMultiple Graphs Aspect框架 Readout 操作改进与讨论注意力机制残差和跳跃连接 Residual and Jumping
  • Ubuntu16.04中文输入法安装初战

    最近刚给笔记本装了Ubuntu 43 win10双系统 xff0c 但是ubuntu16 04没有自带中文输入法 xff0c 所以经过网上的一些经验搜索整合 xff0c 分享一下安装中文输入法的心得 本文主要介绍了谷歌拼音跟ibus中文输入
  • GoDaddy与Namecheap域名注册商对比分析

    默默鸟已经有几天没有更新博客 xff0c 博客更新的少是因为我必须在更新之前想好了围绕博客的主题更新 xff0c 而不是想到哪个就更新哪些内容 xff0c 一来可能不符合博客的中心 xff0c 二来对于用户群也有影响 xff0c 同事显得不
  • ubuntu18安装vnc远程桌面服务

    安装 vnc4server xff0c xfce4 sudo apt install vnc4server xfce4 xfce4 goodies 安装完成后配置VNC登录密码 vncpasswd 启动VNC server vncserve
  • 国内国外常用的10个云服务器可视化管理面板

    如今无论是搭建网站 xff0c 还是部署小程序 xff0c 甚至一些企业应用都会用到云服务求或者独立服务器 对于很多希望利用网站 网络创业的 xff0c 也会用到服务器 xff0c 不过在使用服务器过程中 xff0c 我们对于服务器环境的配
  • 几个Windows强力卸载工具软件推荐

    对于我们有在使用Windows系统的时候 xff0c 是不是会主动或者被动的安装一些软件和插件工具 殊不知日积月累之后 xff0c 系统中的软件会越来越多 xff0c 甚至有很多我们安装几个月甚至几年都不会用到的 这些软件 xff0c 其实
  • 几款值得选的SSH客户端软件

    对于服务器运维工作来说 xff0c 我们少不了SSH远程客户端管理工具 我们在用哪款呢 xff1f 比如常见用的包括PuTTY XShell WindTerm等 xff0c 有很多的付费或者免费的 xff0c 既然有这么多免费且好用的为什么
  • 原生态Ubuntu部署LAMP环境 PHP8.1+MySQL+Apache

    如果我们部署WEB环境用于网站项目 xff0c 我们还是建议用成熟的一键包或者可视化面板这种 xff0c 毕竟软件的部署和后续的运维方便很多 但是 xff0c 如果我们有需要学习Linux环境的原理 xff0c 那还是要学会原生态部署软件的
  • Passwork适合多人协作团队的自建密码管理器

    如今互联网已经深入我们的工作和生活中 xff0c 从办公 购物 学习每天都会用到各种网站平台 各种APP客户端 各种软件账户 xff0c 这就离不开对各个平台账户的管理 我们应该也知道 xff0c 账户的安全是至关重要的 xff0c 如果账
  • 完整利用Rsync实现服务器/网站数据增量同步备份

    我们在选择VPS 服务器架设项目之后 xff0c 所有的项目 网站数据都需要我们自行备份和维护 xff0c 即便有些服务商有提供管理型服务器 xff0c 但是数据自行备份和管理才是较为靠谱的 无论是网站 xff0c 还是其他项目 xff0c
  • 整理Nginx/Apache服务器配置HTTPS SSL证书示范教程

    昨天我们看到百度发布 34 百度烽火算法升级 34 xff0c 提到网站如果被劫持或者拦截可能会降低网站的权重和排名等问题 这使得我们网站需要使用HTTPS SSL证书来减少被拦截劫持的风险 其实在早些时候我们已经看到很多浏览器都强制要求网
  • 6个免费DNS解析服务商评测分析 适用于网站域名解析应用

    这几天我们很多网友应该知道CloudXNS DNS解析服务商预计7月15日会宣布停止提供免费解析服务而主营商业服务 虽然网络上提供免费DNS解析服务商很多 xff0c 但是毕竟这么多年CloudXNS域名解析稳定性还是不错的 xff0c 而
  • 两种方法修改数据库myslq密码

    搞了很久终于修改数据库密码成功了 命令行修改root密码 xff1a mysql gt UPDATE mysql user SET password 61 PASSWORD 新密码 WHERE User 61 root mysql gt F
  • 关于学生课堂行为识别算法

    目前基于针对学校做了一款考生行为识别算法 xff0c 算法可以在服务器部署 xff0c 也可以在前端设备如Jetson RK等边缘设备运行 xff0c 目前算法已经投入使用 xff0c 算法效果如下 目前算法在 2080Ti 服务器运行效率
  • 获取imagefield 类型图片的路径

    绝对路径 request build absolute uri 图片 url 相对路径 图片 url
  • mmdetection 常用命令

    1 多卡训练 CUDA VISIBLE DEVICES 61 0 1 2 3 PORT 61 15200 tool dist train py configs py 4 2 普通测试 python tools test py configs
  • yolov5 导出onnx 忽略检测层

    def forward self x z 61 inference output for i in range self nl x i 61 self m i x i conv bs ny nx 61 x i shape x bs 255
  • python opencv 添加运动模糊

    在训练过程中增加 运动模糊 class MotionBlur object def init self p 61 0 5 degree 61 5 angle 61 45 self p 61 p self degree 61 degree s
  • pth 多类模型改成一类模型

    import torch import copy def change pth input pth out pth model dir 61 input pth checkpoint 61 torch load model dir mode
  • 使用opencv进行车牌提取及识别

    商业合作可联系 xff1a 547691062 64 qq com 目录 1车牌提取过程 1 1车辆图像获取1 2车牌定位1 3车牌字符分割2车牌提取 2 1灰度化2 2Candy边缘检测2 3形态学 xff08 膨胀腐蚀 xff09 处理