本程序的目的是为了识别行驶过程中的车牌,并且通过.png文件将车牌照片保存在对应路径中。首先分类器导入对应xml文件接着借助分类器的detectMultiScale函数来检测车牌。
具体代码如下:
#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
VideoCapture cap(0);
Mat img;
CascadeClassifier platecascades;
string path = "resources/haarcascade_russian_plate_number.xml";
platecascades.load(path); // 载入俄罗斯车牌识别xml
if (platecascades.empty() == true) // 判别xml文件是否为空
cout << "XML file not loaded." << endl;
vector<Rect> plates; // 以便显示被检测对象的边框
while (true)
{
cap.read(img);
platecascades.detectMultiScale(img, plates);
for (int i = 0; i < plates.size(); i++)
{
Mat imgCrop = img(plates[i]); // 裁剪图像
rectangle(img, plates[i], Scalar(0, 255, 0), 2);
//imshow(to_string(i), imgCrop); im