有不对的或更好的方法欢迎交流
一些内容可参照这篇文章:https://blog.csdn.net/m0_64596200/article/details/126748697?spm=1001.2014.3001.5502
训练好的的模型:
车辆识别模型:https://download.csdn.net/download/m0_64596200/86507302?spm=1001.2014.3001.5503
人脸识别模型:https://download.csdn.net/download/m0_64596200/86509158?spm=1001.2014.3001.5503
如需讲解如何训练级联分类器模型请留言
人脸识别在最后面
车辆识别
1、为了提高检测效率首先进行灰度处理
cvtColor(frame,gray,CV_RGB2GRAY);
//视频画面可能过于高清
//再将灰度图再缩小一半
Mat smalling(cvRound(frame.rows/scale),cvRound(frame.cols/scale),CV_8UC1);
resize(gray,smalling,smalling.size(),0,0,INTER_LINEAR);
2、直方图均值化:将灰度后的图片黑白分明
equalizeHist(smalling,smalling);
3、级联分类器检测出车辆进行保存再容器中
vector<Rect>cars;
//以矩形形状保存
cascade.detectMultiScale(smalling,cars,1.1,2,0|CV_HAAR_SCALE_IMAGE,Size(30,20));
4、绘制级联分类器识别出的物体
vector<Rect>::const_iterator iter;
for(iter=cars.begin();iter!=cars.end();iter++)
{//前面灰度是对图像进行了缩小,现在进行放大绘制矩形rectangle(frame,cvPoint(cvRound(iter->x*scale),cvRound(iter->y*scale)),cvPoint(cvRound((iter->x+iter->width)*scale),cvRound((iter->x+iter->height)*scale)),Scalar(250,200,500),2,8);
}
结果:
总结:绘制上可能需要小改。识别没有非常准确,只有look like,数据量不够,车辆的样式等不够全。级联分类器的正样本和负样本和训练时间、数据量的多少对准确度的影响
代码:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void datectCarDaw(Mat &frame,CascadeClassifier cascade,double scale)
{Mat gray;cvtColor(frame,gray,CV_RGB2GRAY);//灰度图再缩小一半Mat smalling(cvRound(frame.rows/scale),cvRound(frame.cols/scale),CV_8UC1);//INTER_LINEAR线性变化resize(gray,smalling,smalling.size(),0,0,INTER_LINEAR);imshow("hui",smalling);//直方图均值化:将缩小的灰度图黑白分明//原图和保存对象equalizeHist(smalling,smalling);//检测级联分类器检测物体vector<Rect>cars;//以矩形形状保存cascade.detectMultiScale(smalling,cars,1.1,2,0|CV_HAAR_SCALE_IMAGE,Size(30,20));imshow("zhi",smalling);//绘制vector<Rect>::const_iterator iter;for(iter=cars.begin();iter!=cars.end();iter++){//前面灰度是对图像进行了缩小,现在进行放大绘制矩形rectangle(frame,cvPoint(cvRound(iter->x*scale),cvRound(iter->y*scale)),cvPoint(cvRound((iter->x+iter->width)*scale),cvRound((iter->x+iter->height)*scale)),Scalar(250,200,500),2,8);}imshow("frame",frame);
}int main()
{//级联分类器对象CascadeClassifier cascade;cascade.load("D:/QQ/cars.xml");Mat frame;VideoCapture cap("./carMove.mp4");while(cap.read(frame)){imshow("res",frame);datectCarDaw(frame,cascade,2);waitKey(25);}return 0;
}
人脸识别:
1、将路径cascade.load(“”);改成人脸识别级联分类器的路径
2、将vediocapture的参数数该为0(打开本机摄像头即可)
结果: