opencv4 车牌识别 C++ vs2019

我国的汽车牌照一般由七个字符和一个点组成,车牌字符的高度和宽度是固定的,分别为90mm和45mm,七个字符之间的距离也是固定的12mm,点分割符的直径是10mm。可根据这些特征进行提取。 

#include<opencv2/opencv.hpp>
#include<iostream>
#include<opencv2/highgui/highgui_c.h>
#include<vector>

using namespace cv;
using namespace std;

int main(){
    Mat img2_gauss, img2gray, bin2img, dst_x, abs_X, kernelX, img2thre, kernelY, dst,img;
    vector<vector<Point>> contours;
    vector<Vec4i> vec_4f;
    Mat image = imread("D:\\4.JPG");                //读取图片
    GaussianBlur(image, img2_gauss, Size(5, 5), 15);//高斯滤波
    cvtColor(img2_gauss, img2gray, COLOR_RGB2GRAY); //转为灰度图像
    Sobel(img2gray, dst_x, CV_16S, 1, 0);           //梯度算子
    convertScaleAbs(dst_x, abs_X);                //将CV_16S型的输出图像转变成CV_8U型的图像
    img = abs_X;
    threshold(img, img, 0, 255, THRESH_OTSU);     //用这个函数,我们可以令图像灰度大于阈值 
    //的,为一个值,低于阈值的,为另一个值。这就可以实现图像的二值化
    
    //  闭操作,封闭轮廓
    kernelX = getStructuringElement(MORPH_RECT, Size(17, 5));  //构造一个矩形
    morphologyEx(img, img, MORPH_CLOSE, kernelX);   //用矩形来封闭

    // 形态学处理
    kernelX = getStructuringElement(MORPH_RECT,Size(20, 1));
    kernelY = getStructuringElement(MORPH_RECT,Size(1, 19));
    dilate(img, img, kernelX);                      //图像膨胀
    erode(img, img, kernelX);                       //图像腐蚀
    erode(img, img, kernelY);
    dilate(img, img, kernelY);
    // 平滑去噪处理,使边缘检测更准确
    GaussianBlur(img, img, Size(15, 1), 1);

    // 从二值化后的img图中提取所有轮廓
    findContours(img, contours, vec_4f, RETR_TREE, CHAIN_APPROX_SIMPLE);       
    // 在原图image上绘制所有轮廓(红色)
    drawContours(image, contours, -1, Scalar(0, 0, 255), 1);                   
    
     //    筛选
    for (int i = 0; i < contours.size(); i++) 
    {
         //计算轮廓的垂直边界最小矩形
         Rect rect = boundingRect(contours[i]);                                
         int x = rect.x;
         int y = rect.y;
         if (rect.width > (rect.height * 2))
         {
              Mat chepai = image(Rect(rect.x,rect.y,rect.width,rect.height));  //区域提取
              //将提取出来的区域拿绿色矩形围起来
              rectangle(chepai,Point(rect.x,rect.y),Point(rect.x + rect.width, rect.y + rect.height),Scalar(0,255,0),1);
              imshow("car_num",chepai);
         }
     }
    imshow("test",image);
    waitKey(0);
    destroyAllWindows();
    return 0;
}

       也可以下面这个代码段,提取方法稍有不同,但结果和原理一样的。

#include<opencv2/opencv.hpp>
#include<iostream>
#include<opencv2/highgui/highgui_c.h>
#include<vector>

using namespace cv;
using namespace std;

int main()
{
Mat img = imread("D://4.JPG");
//imshow("1.src",img);
//高斯模糊
Mat Gauss_img;
GaussianBlur(img, Gauss_img, Size(3, 3), 0, 0, BORDER_DEFAULT);
//imshow("2.gauss",Gauss_img);
//转化灰度图
Mat gray_img;
cvtColor(Gauss_img, gray_img, COLOR_BGR2GRAY);
//imshow("3.gray",gray_img);
//Sobel算子
Mat Sobel_x, absX;
Sobel(gray_img, Sobel_x, CV_16S, 1, 0);
convertScaleAbs(Sobel_x, absX);
//imshow("4.Abs",absX);
//二值化
Mat thr_img;
threshold(absX, thr_img, 0, 255, THRESH_OTSU);
//imshow("5.threshold",thr_img);

//闭操作
Mat element, mor_img;
element = getStructuringElement(MORPH_RECT, Size(17, 5));
morphologyEx(thr_img, mor_img, MORPH_CLOSE, element);
//imshow("6.morphologyEx",mor_img);

//膨胀、腐蚀
Mat kernelX, kernelY, ker_img;
kernelX = getStructuringElement(MORPH_RECT, Size(20, 1));
kernelY = getStructuringElement(MORPH_RECT, Size(1, 19));

dilate(mor_img, ker_img, kernelX);
erode(ker_img, ker_img, kernelX);

erode(ker_img, ker_img, kernelY);
dilate(ker_img, ker_img, kernelY);
//imshow("7.dilate and erode",ker_img);

//中值滤波
Mat med_img;
medianBlur(ker_img, med_img, 15);
//imshow("8.medianBlur",med_img);

//查找轮廓
vector<vector<Point>>contours;
findContours(med_img, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
//drawContours(img,contours,-1, Scalar(0, 0, 255), 3);
//imshow("9.result",img);

    //ROI提取
Mat roi_img;
vector<Rect> boundRect(contours.size());
for (int i = 0; i < contours.size(); i++)
{
    boundRect[i] = boundingRect(contours[i]);
    if (boundRect[i].width > boundRect[i].height * 2) //车牌尺寸判断
    {
        //在原图上绘制矩形
        rectangle(img, Rect(boundRect[i].x, boundRect[i].y, boundRect[i].width, boundRect[i].height), Scalar(0, 255, 0), 2);
        //ROI提取
        roi_img = img(Rect(boundRect[i].x, boundRect[i].y, boundRect[i].width, boundRect[i].height));
    }
}
imshow("10.ROI", roi_img);

waitKey(0);
return 0;
}

       该方法车牌识别原理是先对图像进行模糊、转换和图形化处理,使其成为一个二值图,在从该图中进行边缘识别、提取轮廓,然后用矩形圈出绘制在原图上。最后将长度大于二倍宽度的矩形挑出来(不多只有车牌长度大于二倍宽度),即为车牌。

       保存识别出来的车牌图,再次用上述方法可以识别出文字。可能还用其他方法一次识别出车牌文字,如cv::fillContours()函数还有层次结构,本人后续学习了再更改项目。识别结果如下:

  • 8
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值