车牌识别之车牌提取

车牌提取

  1. 原理:首先对图片进行一系列的处理,步骤:去噪——sobel滤波——otsu阈值化——闭运算;然后绘制矩形,选择最接近的一个矩形;最后矩形轮廓提取。

#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <map>
using namespace std;
using namespace cv;


bool verify(RotatedRect rect) {
    float error = 0.4;
    const float aspect = 4.7272;
    int min = 15 * aspect * 15; // 面积下限
    int max = 125 * aspect * 125; // 面积上限

    float rmin = aspect - aspect * error; // 宽高比下限
    float rmax = aspect + aspect * error; // 宽高比上限

    int area = rect.size.width * rect.size.height; // 计算面积
    float r = rect.size.width / rect.size.height;  // 计算宽高比
    r = r < 1 ? 1 / r : r;

    return area >= min && area <= max && r >= rmin && r <= rmax;
}


int main()
{
    string in = "D://OPENCV//实践小程序//pictures//车牌切割.jpg";

    Mat image = imread(in, IMREAD_GRAYSCALE);
    Mat image2 = imread(in);
    Mat image3 = imread(in, IMREAD_GRAYSCALE);

    if (image.empty()){
        return -1;
    }
    imshow("【原始图】", image);
    blur(image, image, Size(5, 5));
    imshow("【去噪后】", image);


    Sobel(image, image, CV_8U, 1, 0, 3, 1, 0);
    imshow("【sobel滤波】", image);

    threshold(image, image, 0, 255, CV_THRESH_OTSU);
    imshow("【otsu阈值化】", image);

    Mat element = getStructuringElement(MORPH_RECT, Size(17, 3));
    morphologyEx(image, image, CV_MOP_CLOSE, element);
    imshow("【闭运算】", image);


    vector<vector<Point>> contours;
    findContours(image, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    map<int, RotatedRect> _map;

    for (int i = 0; i < contours.size(); i++) {
        drawContours(image, contours, i, Scalar(255), 1); // 绘制轮廓

        // 绘制矩形
        RotatedRect rect = minAreaRect(contours[i]);
        Point2f vertices[4];
        rect.points(vertices);
        for (int i = 0; i < 4; i++) {
            line(image, vertices[i], vertices[(i + 1) % 4], Scalar(255), 2);
        }

        // 验证
        if (verify(rect)) {
            _map[i] = rect;
        }
    }
    imshow("【轮廓提取】", image);


    // 绘制通过验证的矩形
    int min_diff = 100000;
    int index = 0;
    const float square = 27.75;

    map<int, RotatedRect>::iterator iter;
    iter = _map.begin();
    while (iter != _map.end()) {

        RotatedRect rect = iter->second;
        Point2f vertices[4];
        rect.points(vertices);
        for (int j = 0; j < 4; j++) {
            line(image, vertices[j], vertices[(j + 1) % 4], Scalar(255), 10);
        }

        // 选择最接近的矩形
        int perimeter = arcLength(contours[iter->first], true);
        int area = contourArea(contours[iter->first]);
        if (area != 0) {
            int squareness = perimeter * perimeter / area;

            float diff = abs(squareness - square);
            if (diff < min_diff) {
                min_diff = diff;
                index = iter->first;
            }
        }
        iter++;
    }

    imshow("【通过验证】", image);


    // 绘制最接近的矩形
    RotatedRect rect = _map[index];
    Point2f vertices[4];
    rect.points(vertices);
    for (int i = 0; i < 4; i++) {
        cout << " asdf" << endl;
        line(image2, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0), 10);
    }
    imshow("【最接近的矩形】", image2);


    // 图像切割
    Mat image_crop;

    // 始终保持宽 > 高
    Size rect_size = rect.size;
    if (rect_size.width < rect_size.height) {
        swap(rect_size.width, rect_size.height);
    }
    getRectSubPix(image3, rect.size, rect.center, image_crop);
    imshow("【切割后的车牌】", image_crop);

    waitKey(0);
    return 0;
}

运行效果

这里写图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值