某天回家之時(shí),聽到有個(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:
test2
實(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)
網(wǎng)頁(yè)名稱:python實(shí)現(xiàn)車牌識(shí)別的示例代碼-創(chuàng)新互聯(lián)
URL地址:http://www.chinadenli.net/article24/diocje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、微信小程序、網(wǎng)站導(dǎo)航、網(wǎng)站收錄、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容