最近上课有一门课需要做一个大作业,用三种方法实现一个任务。分别是基于特征的方法、基于传统的机器学习方法和基于深度学习的方法
由于本人水平有限,只能从简单的车牌识别任务入手。
三种方法都是博采众长得来,不过很多网上的代码有些问题不能运行,于是本人就做了些修改,也方便以后的人查询
车牌识别首先就是车牌区域的分割
[https://blog.csdn.net/u011808673/article/details/78510692]
首先是字符分割的代码:
int segmentation()
{
Mat origin_image = imread("2.jpg");
Mat gray_image;
cvtColor(origin_image, gray_image, CV_RGB2GRAY);
Mat canny_image;
Canny(gray_image, canny_image, 100, 200, 3);
Mat dilate_image, erode_image;
Mat elementX = getStructuringElement(MORPH_RECT, Size(19, 1));
Mat elementY = getStructuringElement(MORPH_RECT, Size(1, 20));
Point point(-1, -1);
dilate(canny_image, dilate_image, elementX, point, 2);
erode(dilate_image, erode_image, elementX, point, 4);
dilate(erode_image, dilate_image, elementX, point, 2);
erode(dilate_image, erode_image, elementY, point, 2);
dilate(erode_image, dilate_image, elementY, point, 2);
Mat blurr_image;
medianBlur(dilate_image, blurr_image, 15);
medianBlur(blurr_image, blurr_image, 15);
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, 0, 0), 1);
Mat roi_image;
vector<Point> rectPoint;
for (int i = 0; i < contours.size(); i++)
{
Rect r = boundingRect(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 >= 3 && (float)r.width / r.height <= 5)
{
cout << "r.x = " << r.x << " r.y = " << r.y << endl;
rectangle(contour_image, r, Scalar(0, 0, 255), 2);
Point p1, p2, p3, p4;
p1.x = r.x;
p1.y = r.y;
p2.x = r.x + r.width;
p2.y = 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;
}
roi_image = origin_image(r);
}
}
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));
Mat roi_gray_image;
cvtColor(large_image, roi_gray_image, CV_RGB2GRAY);
Mat canny_roi_image;
Canny(roi_gray_image, canny_roi_image, 100, 200, 3);
imwrite("canny_roi_image.jpg", canny_roi_image);
Mat roi_contours_image;
vector<vector<Point>> roi_contours;
roi_contours_image = canny_roi_image.clone();
findContours(roi_contours_image, roi_contours, CV_RETR_EXTERNAL,
CHAIN_APPROX_SIMPLE);
vector<Point> roi_rectPoint;
for (int i = 0; i < roi_contours.size(); i++)
{
Rect r = boundingRect(Mat(roi_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;
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;
}
}
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;
conto