车牌识别--opencv(python)

这里主要总结一下车牌识别的总体思路,这里车牌区域提取采用的传统的图像处理的方法(转换颜色空间、开运算、闭运算、提取轮廓,包括利用一些启发性信息。。),分类用的是SVM,这个模型需要自己训练,主要是一些数字、字母、省份缩写的汉字,opencv自带svm方法训练即可,然后描述一下具体过程。。

总体流程

(1)训练字符分类器
(2)resize为固定大小
(3)高斯去噪
(4)转化为灰度图
(5)开运算
开运算与闭运算:https://blog.youkuaiyun.com/qq_41332469/article/details/89415429
(6)灰度图与开运算图加权求和
(7)转化为二值图像
(8)后利用Canny算法进行边缘检测
canny算子介绍:https://www.jianshu.com/p/2334bee37de5
(9)先开运算后闭运算使图像变为一个整体
(10)检测矩形框
(11)利用启发性信息过滤矩形框:矩形大小、宽高比、颜色
(12)利用直方图分割字符(阈值设定、边缘干扰、中间的分割点、铆钉的干扰)

训练字符和汉字分类器

	def train_svm(self):
		# 识别英文字母和数字
		self.model = SVM(C=1, gamma=0.5)
		# 识别中文
		self.modelchinese = SVM(C=1, gamma=0.5)
		if os.path.exists("svm.dat"):
			self.model.load("svm.dat")
		else:
			chars_train = []
			chars_label = []

			for root, dirs, files in os.walk("train\\chars2"):
				if len(os.path.basename(root)) > 1:
					continue
				root_int = ord(os.path.basename(root))
				for filename in files:
					filepath = os.path.join(root, filename)
					digit_img = cv2.imread(filepath)
					digit_img = cv2.cvtColor(digit_img, cv2.COLOR_BGR2GRAY)
					chars_train.append(digit_img)
					# chars_label.append(1)
					chars_label.append(root_int)

			chars_train = list(map(deskew, chars_train))
			chars_train = preprocess_hog(chars_train)
			# chars_train = chars_train.reshape(-1, 20, 20).astype(np.float32)
			chars_label = np.array(chars_label)
			print(chars_train.shape)
			self.model.train(chars_train, chars_label)
		if os.path.exists("svmchinese.dat"):
			self.modelchinese.load("svmchinese.dat")
		else:
			chars_train = []
			chars_label = []
			for root, dirs, files in os.walk("train\\charsChinese"):
				if not os.path.basename(root).startswith("zh_"):
					continue
				pinyin = os.path.basename(root)
				index = provinces.index(pinyin) + PROVINCE_START + 1  # 1是拼音对应的汉字
				for filename in files:
					filepath = os.path.join(root, filename)
					digit_img = cv2.imread(filepath)
					digit_img = cv2.cvtColor(digit_img, cv2.COLOR_BGR2GRAY)
					chars_train.append(digit_img)
					# chars_label.append(1)
					chars_label.append(index)
			chars_train = list(map(deskew, chars_train))
			chars_train = preprocess_hog(chars_train)
			# chars_train = chars_train.reshape(-1, 20, 20).astype(np.float32)
			chars_label = np.array(chars_label)
			print(chars_train.shape)
			self.modelchinese.train(chars_train, chars_label)
	# 将训练的模型存入文件
	def save_traindata(self):
		if not os.path.exists("svm.dat"):
			self.model.save("svm.dat")
		if not os.path.exists("svmchinese.dat"):
			self.modelchinese.save("svmchinese.dat")

识别过程

原图:
在这里插入图片描述

一、车牌定位

1、将图片转化为固定大小的尺寸;

if pic_width > MAX_WIDTH:
	resize_rate = MAX_WIDTH / pic_width
	img = cv2.resize(img, (MAX_WIDTH, int(pic_hight * resize_rate
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值