选中车牌代码opencv

#include <iostream>
#include <string>
#include <cxcore.h>
#include <cv.h>
#include <highgui.h>
#include <opencv2/opencv.hpp>
#include <opencv2/video.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp> //包含imread, imshow等
#include <opencv2/imgproc/imgproc.hpp> //包含cvtColor等

using namespace std;
using namespace cv;

string window_name_2 = "license_plate_detect";
string window_name_3 = "video_license_plate_detect";

// 使用形态学梯度检测或sobel边缘检测(y方向)来检测出垂直边缘
// 首先进行边缘检测,检测垂直边缘,使用形态学梯度、或者Sobel边缘检测的垂直方向
// 对边缘实现二值化
// 使用闭运算进行区域填充,填补空洞
// 使用轮廓检测findContours,找到车牌区域的轮廓
// 对找到的轮廓进行遍历,根据车牌的特征(宽高比、面积比、像素等)进行筛选,输出
Mat get_license_plate(int width, int height, Mat srcGray) {
	Mat result;
	//形态学梯度边缘检测,形态学梯度即膨胀图与腐蚀图之差,提取物体边缘
	//用Mat(1,2) ,用来检测出垂直的边缘,尽量减少横向的边缘连通车牌区域
	morphologyEx(srcGray, result, MORPH_GRADIENT, Mat(1, 2, CV_8U, Scalar(1)));
	//或用Sobel边缘检测,求y方向的Sobel边缘
	//GaussianBlur(srcGray, srcGray, Size(3, 3),2);
	//Sobel(srcGray, edgeYMat, CV_16S, 2, 0, 3, 1, 0, BORDER_DEFAULT);
	//线性变换,转换输入数组元素为8位无符号整形
	//convertScaleAbs(edgeYMat, result);
	//这是垂直方向边缘检测的结果,尽量减少横向的边缘连通车牌区域
	//imshow(window_name_2, result); 
	//图像二值化
	threshold(result, result, 255 * 0.1, 255, THRESH_BINARY);
	//二值化后结果
	//imshow(window_name_2, result);
	//开运算: 先腐蚀,再膨胀,可清除一些小东西(亮的),放大局部低亮度的区域
	//闭运算:先膨胀,再腐蚀,可清除小黑点
	//水平方向闭运算
	//闭运算:填补空洞
	//检测目标尺寸400到600使用的闭运算算子为(1x25)水平方向,(8x1)垂直方向
	if (width >= 400 && width < 600) {
		morphologyEx(result, result, MORPH_CLOSE, Mat(1, 25, CV_8U, Scalar(1)));
	}
	//检测目标尺寸200到300使用的闭运算算子为(1x20)水平方向,(6x1)垂直方向	
	else if (width >= 200 && width < 300) {
		morphologyEx(result, result, MORPH_CLOSE, Mat(1, 20, CV_8U, Scalar(1)));
	}
	//检测目标尺寸大于600使用的闭运算算子为(1x28)水平方向,(6x1)垂直方向	
	else if (width >= 600) {
		morphologyEx(result, result, MORPH_CLOSE, Mat(1, 28, CV_8U, Scalar(1)));
	}
	//其余尺寸使用的闭运算算子为(1x15)水平方向,(4x1)垂直方向	
	else {
		morphologyEx(result, result, MORPH_CLOSE, Mat(1, 15, CV_8U, Scalar(1)));
	}
	//水平方向闭运算后的结果
	//imshow(window_name_2, result);
	//垂直方向闭运算
	if (width >= 400 && width < 600) {
		morphologyEx(result, result, MORPH_CLOSE, Mat(8, 1, CV_8U, Scalar(1)));
	}
	else if (width >= 200 && width < 300) {
		morphologyEx(result, result, MORPH_CLOSE, Mat(6, 1, CV_8U, Scalar(1)));
	}
	else if (width >= 600) {
		morphologyEx(result, result, MORPH_CLOSE, Mat(10, 1, CV_8U, Scalar(1)));
	}
	else {
		morphologyEx(result, result, MORPH_CLOSE, Mat(4, 15, CV_8U, Scalar(1)));
	}
	//垂直方向闭运算后的结果
	//imshow(window_name_2, result);
	return result;
}

// 对图片进行车牌检测,请输入彩色图像矩阵
Mat license_plate_detect(Mat pScr) {
	Mat graypScr;
	cvtColor(pScr, graypScr, CV_BGR2GRAY);
	// 车牌轮廓识别(得到闭运算后的结果,有许多白色的区域(候选车牌区域)
	Mat result = get_license_plate(400, 300, graypScr);
	//连通域检测
	vector<vector<Point>> blue_contours;
	vector<Rect>blue_rect;
	//FindContours从二值图像中检索轮廓,并返回检测到的轮廓的个数
	findContours(result.clone(), blue_contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
	//遍历检测到的轮廓,进行车牌目标提取
	for (size_t i = 0; i < blue_contours.size(); i++) {
		// 找到一个最小的矩形把轮廓包起来
		Rect rect = boundingRect(blue_contours[i]);
		//矩形区域宽高比
		double wh_ratio = double(rect.width) / rect.height;
		//非零像素点数,即白色像素点数(白色为255)
		int sub = countNonZero(result(rect));
		//白色像素占比
		double ratio = double(sub) / rect.area();
		//车牌特征,条件判断,宽高比大于2且小于8,高度大于12且宽度大于60且白色像素占比大于0.4
		if (wh_ratio > 2 && wh_ratio < 8 && rect.height>6 &&
			rect.width > 30 && ratio > 0.6) {
			// 在彩色图pScr上画框框出车牌
			rectangle(pScr, rect, Scalar(0, 0, 255), 2, 8, 0);
			//只显示graypScr中rect区域,也就是车牌所在区域
			//imshow(window_name_2, pScr(rect));
			//waitKey(0);
		}
	}
	//imshow(window_name_2, pScr);
	//waitKey(0);
	//destroyAllWindows();
	return pScr;
}

// 视频中进行车牌目标检测
void video_license_plate_detect(string load_path) {
	VideoCapture capture;
	Mat frame, result;
	if (load_path == "")
		frame = capture.open(0);
	else
		frame = capture.open(load_path);
	if (capture.isOpened()) {
		while (1) {
			capture >> frame;
			if (frame.empty())
				break;
			if (waitKey(10) >= 0)
				break;
			result = license_plate_detect(frame);
			namedWindow(window_name_3, WINDOW_AUTOSIZE);
			imshow(window_name_3, frame);
		}
	}
}

int main() {
	string load_path_car = "D:/motuo.png";
	string load_path_license_plate_video = "D:\\独山-G105国道-独山路口方向.avi";
	Mat pScr = imread(load_path_car, 1);
	Mat result = license_plate_detect(pScr);
	video_license_plate_detect(load_path_license_plate_video);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

物联网小镇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值