溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python實(shí)現(xiàn)車牌識(shí)別的示例代碼

發(fā)布時(shí)間:2020-08-27 11:39:35 來(lái)源:腳本之家 閱讀:215 作者:Halosec_Wei 欄目:開(kāi)發(fā)技術(shù)

某天回家之時(shí),聽(tīng)到有個(gè)朋友說(shuō)起他正在做一個(gè)車牌識(shí)別的項(xiàng)目

于是對(duì)其定位車牌的位置算法頗有興趣,今日有空得以研究,事實(shí)上車牌識(shí)別算是比較成熟的技術(shù)了,

這里我只是簡(jiǎn)單實(shí)現(xiàn)。

我的思路為:

對(duì)圖片進(jìn)行一些預(yù)處理,包括灰度化、高斯平滑、中值濾波、Sobel算子邊緣檢測(cè)等等。

利用OpenCV對(duì)預(yù)處理后的圖像進(jìn)行輪廓查找,然后根據(jù)一些參數(shù)判斷該輪廓是否為車牌輪廓。

效果如下:

test1:

python實(shí)現(xiàn)車牌識(shí)別的示例代碼

python實(shí)現(xiàn)車牌識(shí)別的示例代碼

test2

python實(shí)現(xiàn)車牌識(shí)別的示例代碼

python實(shí)現(xiàn)車牌識(shí)別的示例代碼

實(shí)現(xiàn)代碼如下(對(duì)圖像預(yù)處理(濾波器等)的原理比較簡(jiǎn)單,這里只是對(duì)一些函數(shù)進(jìn)行調(diào)包):

import cv2
import numpy as np
 
 
# 形態(tài)學(xué)處理
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)
	# 核函數(shù)
	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)
  #保存結(jié)果文件名
	cv2.imwrite('result2.jpg', img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
 
 
if __name__ == '__main__':
  #輸入的參數(shù)為圖片的路徑
	img = cv2.imread('test2.jpg')
	detect(img)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI