如何从零开始教你写一款车牌识别脚本,用我这个方法非常简单。

      前言

为了感谢每一个关注我的小可爱:每篇文章的项目源码都是无偿分享滴。

最后——如果文章有帮助到你,记得“关注”、“点赞”、“评论”三连哦!

有兴趣的小伙伴儿记得关注我跟我一起学习,开启编程之旅吧!

    随着行业的发展,市场各式各样的需求,市场对车牌识别系统(车牌识别系统)的需求越来越广

泛,今天小编就带大家简单的做一款车牌识别系统小程序啦!!

正文

Show Time

Nov  1)背景环境

开发工具—Python版本:3.6.4

相关模块:cv2模块;numpy模块。

环境搭建:安装Python并添加到环境变量,pip安装需要的相关模块即可。

原理简介:注意这不是车牌号识别,是车牌检测。

因为车牌形状比较单一,所以我参考了一些简单的传统算法实现的,没有使用深度学习。效果比较一般。不适用于复杂环境下的车牌检测。

直接调的OpenCV的函数接口,没有从0开始实现,所以总体技术含量较低。

Nov  2)步骤

其流程为:

Step1:

对图片进行一些预处理,包括灰度化、高斯平滑、中值滤波、Sobel算子边缘检测等等。

Step2:

利用OpenCV对预处理后的图像进行轮廓查找,然后根据一些参数判断该轮廓是否为车牌轮廓。

使用演示:

在cmd窗口运行detect.py文件即可。

使用前请先指定需要检测的图片路径:

图片

代码演示:

# python车牌检测 # Author: Charles  import cv2
import numpy as np

# 形态学处理
def Process(img):
	# 高斯平滑
	gaussian = cv2.GaussianBlur(img, (3, 3), 0, 0, cv2.BORDER_DEFAULT)
	# 中值滤波
	median = cv2.medianBlur(gaussian, 5)
	# Sobel算子
	# 梯度方向: x
	sobel = cv2.Sobel(median, cv2.CV_8U, 1, 0, ksize=3)
	# 二值化
	ret, binary = cv2.threshold(sobel, 170, 255, cv2.THRESH_BINARY)
	# 核函数
	element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
	element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 7))
	# 膨胀
	dilation = cv2.dilate(binary, element2, iterations=1)
	# 腐蚀
	erosion = cv2.erode(dilation, element1, iterations=1)
	# 膨胀
	dilation2 = cv2.dilate(erosion, element2, iterations=3)
	return dilation2
def GetRegion(img):
	regions = []
	# 查找轮廓
	_, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
	for contour in contours:
		area = cv2.contourArea(contour)
		if (area < 2000):
			continue
		eps = 1e-3 * cv2.arcLength(contour, True)
		approx = cv2.approxPolyDP(contour, eps, True)
		rect = cv2.minAreaRect(contour)
		box = cv2.boxPoints(rect)
		box = np.int0(box)
		height = abs(box[0][1] - box[2][1])
		width = abs(box[0][0] - box[2][0])
		ratio =float(width) / float(height)
		if (ratio < 5 and ratio > 1.8):
			regions.append(box)
	return regions


def detect(img):
	# 灰度化
	gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	prc = Process(gray)
	regions = GetRegion(prc)
	print('[INFO]:Detect %d license plates' % len(regions))
	for box in regions:
		cv2.drawContours(img, [box], 0, (0, 255, 0), 2)
	cv2.imshow('Result', img)
	cv2.imwrite('result.jpg', img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()

 Nov  3) ​效果如下

3.1第一组:

 3.2第二组:

 小结

车牌检测一出来就广泛的运用到生活中了,人工智能发展越来越厉害了!

粉丝交流
欢迎关注、收藏、有所收获点赞支持一下!

完整项目源代码点这里即可获取

Guess you like

Origin blog.csdn.net/weixin_43881394/article/details/121557979