首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >OpenCV项目(21)|mser车牌识别

OpenCV项目(21)|mser车牌识别

作者头像
用户9831583
发布2022-06-16 16:30:12
发布2022-06-16 16:30:12
1.9K0
举报
文章被收录于专栏:码出名企路码出名企路

输入原始车牌信息

输出mser识别结果

具体算法步骤

第1步:mser结构体定义

代码语言:javascript
复制
struct MSERParams
{
    MSERParams(int _delta = 5, int _min_area = 60, int _max_area = 14400,
               double _max_variation = 0.25, double _min_diversity = .2,
               int _max_evolution = 200, double _area_threshold = 1.01,
               double _min_margin = 0.003, int _edge_blur_size = 5)
    {
        delta = _delta;
        minArea = _min_area;
        maxArea = _max_area;
        maxVariation = _max_variation;
        minDiversity = _min_diversity;
        maxEvolution = _max_evolution;
        areaThreshold = _area_threshold;
        minMargin = _min_margin;
        edgeBlurSize = _edge_blur_size;
        pass2Only = false;
    }

    int delta;
    int minArea;
    int maxArea;
    double maxVariation;
    double minDiversity;
    bool pass2Only;

    int maxEvolution;
    double areaThreshold;
    double minMargin;
    int edgeBlurSize;
};

static String Legende(const MSERParams &pAct)
{
    ostringstream ss;
    ss << "Area[" << pAct.minArea << "," << pAct.maxArea << "] ";
    ss << "del. [" << pAct.delta << "] ";
    ss << "var. [" << pAct.maxVariation << "] ";
    ss << "div. [" << (int)pAct.minDiversity << "] ";
    ss << "pas. [" << (int)pAct.pass2Only << "] ";
    ss << "RGb->evo. [" << pAct.maxEvolution << "] ";
    ss << "are. [" << (int)pAct.areaThreshold << "] ";
    ss << "mar. [" << (int)pAct.minMargin << "] ";
    ss << "siz. [" << pAct.edgeBlurSize << "]";

    return ss.str();
}

第2步:输入图像及参数定义

代码语言:javascript
复制
 Mat imgOrig, img;
    Size blurSize(5, 5);
    cv::CommandLineParser parser(argc, argv, "{ help h | | }{ @input | | }");
    if (parser.has("help"))
    {
        return 0;
    }

    string input = parser.get<string>("@input");
    if (!input.empty())
    {
        imgOrig = imread(input, IMREAD_GRAYSCALE);
        blur(imgOrig, img, blurSize);
    }
 
    //mser参数定义
    vector<String> typeDesc;
    vector<MSERParams> pMSER;
    //创建mser对象
    MSERParams params;
    params.delta = 10;
    params.minArea = 100;
    params.maxArea = 5000;
    params.maxVariation = 2;
    params.minDiversity = 0;
    params.pass2Only = true;

    typeDesc.push_back("MSER");
    pMSER.push_back(params);

    params.pass2Only = false;
    typeDesc.push_back("MSER");
    pMSER.push_back(params);

    params.delta = 100;
    typeDesc.push_back("MSER");
    pMSER.push_back(params);

第3步:mser算法检测车牌

代码语言:javascript
复制
vector<MSERParams>::iterator itMSER = pMSER.begin();
    Ptr<Feature2D> b;
    String label;
    vector<String>::iterator itDesc;
    Mat result(img.rows, img.cols, CV_8UC3);
    for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); ++itDesc)
    {
        if (*itDesc == "MSER")
        {
            if (img.type() == CV_8UC3)
            {
                b = MSER::create(itMSER->delta, itMSER->minArea, itMSER->maxArea, itMSER->maxVariation, itMSER->minDiversity, itMSER->maxEvolution,
                                 itMSER->areaThreshold, itMSER->minMargin, itMSER->edgeBlurSize);
                label = Legende(*itMSER);
                ++itMSER;
            }
            else
            {
                b = MSER::create(itMSER->delta, itMSER->minArea, itMSER->maxArea, itMSER->maxVariation, itMSER->minDiversity);
                b.dynamicCast<MSER>()->setPass2Only(itMSER->pass2Only);
                label = Legende(*itMSER);
                ++itMSER;
            }
        }

        if (img.type()==CV_8UC3)
        {
            img.copyTo(result);
        }
        else
        {
            vector<Mat> plan;
            plan.push_back(img);
            plan.push_back(img);
            plan.push_back(img);
            merge(plan,result);
        }
        try
        {
            vector<KeyPoint> keyImg;
            vector<Rect> zone;
            vector<vector <Point> > region;
            Mat desc;

            if (b.dynamicCast<MSER>().get())
            {
                Ptr<MSER> sbd = b.dynamicCast<MSER>();
                //检测Region
                sbd->detectRegions(img, region, zone);
                int nbPixelInMSER=0;
                for (vector<vector <Point> >::iterator itr = region.begin(); itr != region.end(); ++itr)
                {   //根据检测区域点生成mser+结果
                    for (vector <Point>::iterator itp = itr->begin(); itp != itr->end(); ++itp)
                    {
                        // mser找到的区域涂蓝色
                        result.at<Vec3b>(itp->y, itp->x) = Vec3b(128, 0, 0);
                        nbPixelInMSER++;
                    }
                }
                cout << "Number of MSER region: " << region.size() << "; Number of pixels in all MSER region: " << nbPixelInMSER << "\n";
            }

            const string winName = *itDesc + label;
            namedWindow(winName, WINDOW_AUTOSIZE);
            imshow(winName, result);
            imshow("Original", img);
        }
        catch (const Exception& e)
        {
            cout << "Feature: " << *itDesc << "\n";
            cout << e.msg << endl;
        }
        waitKey();
    }

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-01-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码出名企路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档