需求分析:
車牌識別技術(shù)主要應(yīng)用領(lǐng)域有停車場收費(fèi)管理,交通流量控制指標(biāo)測量,車輛定位,汽車防盜,高速公路超速自動化監(jiān)管、闖紅燈電子警察、公路收費(fèi)站等等功能。對于維護(hù)交通安全和城市治安,防止交通堵塞,實(shí)現(xiàn)交通自動化管理有著現(xiàn)實(shí)的意義。
車牌自動識別系統(tǒng)應(yīng)用的主要技術(shù)
- 圖像處理相關(guān)技術(shù)
- 一維條形碼相關(guān)技術(shù)
- 二維數(shù)字識別碼相關(guān)技術(shù)
- 三維立體識別碼相關(guān)技術(shù)以及IC卡識別相關(guān)技術(shù)
車牌識別的技術(shù)難點(diǎn)
誤定位一直是汽車牌照定位方面的研究重點(diǎn)。汽車牌照分割方面的研究方向一直是如何獲得良好的汽車牌照分割,以及如何快速地實(shí)現(xiàn)汽車牌照傾斜度校正的基礎(chǔ)。
車牌定位技術(shù)點(diǎn)分析
車牌特征概述
- 字符特征
- 形狀特征
- 顏色特征
- 灰度變化特征
車牌定位方法
- 基于邊緣檢測的車牌定位方法
- 基于遺傳算法的車牌定位方法
- 基于紋理特征的車牌定位方法
- 基于形態(tài)學(xué)的車牌定義方法
- 基于小波分析和變換的車牌定位方法
- 基于神經(jīng)網(wǎng)絡(luò)的車牌定位方法
車牌圖像預(yù)處理
之所以要進(jìn)行預(yù)處理,主要是為了得到一個比較準(zhǔn)確清晰的圖像,以便于后期相關(guān)分析工作在圖像中進(jìn)行。
車牌圖像的灰度化
- 分量法
- 最大值法
- 平均值法
- 加權(quán)平均法
生成的灰度圖
車牌圖像的直方圖均衡化
在汽車牌照定位之前,可以通過采用直方圖均衡化的方法來實(shí)現(xiàn)車牌圖像亮度接近化的轉(zhuǎn)換處理。
車牌定位技術(shù)
車牌圖像的濾波
通常情況下,首先,圖像會因?yàn)椴杉瘯r大氣湍流、相對運(yùn)動以及光學(xué)系統(tǒng)失真等因素的影響而變得模糊。
圖像增強(qiáng)處理的一種常見的方法是圖像濾波。
操作模板的形式為:
車牌圖像的二值化
確定一個合適閾值,是對圖像進(jìn)行二值化處理的主要目的,待研究的區(qū)域經(jīng)過二值化處理后將被分為背景以及前景兩個部分。
車牌圖像的邊緣檢測
兩個灰度值不同的相鄰區(qū)域之間總會存在邊緣,也就是圖像中亮度函數(shù)發(fā)生急劇變化的位置,邊緣檢測是圖像分割、紋理和形狀特征提取等圖像分析的基礎(chǔ)。
車牌圖像的灰度映射
灰度映射是一種對像素點(diǎn)的操作,即根據(jù)原始圖像中每個像素的灰度值,按照某種映射規(guī)則將其轉(zhuǎn)化為另外一個灰度值,通過對原始圖像中每個像素點(diǎn)賦予一個新的灰度值,來達(dá)到增強(qiáng)圖像的目的。
車牌圖像的改進(jìn)型投影法定位
車牌字符分割技術(shù)
車牌字符切割方法
基于識別基礎(chǔ)的車牌字符分割法
垂直投影分割方法
自適應(yīng)分割線聚類法
車牌傾斜問題
通常汽車牌照圖像的傾斜校正主要分為垂直傾斜校正和水平傾斜校正兩種,如此劃分的依據(jù)是汽車牌照圖像傾斜的類型。
車牌傾斜度檢測方法
我們采用基于行掃描的灰度值跳變點(diǎn)數(shù)目變化率來判斷車牌是否水平。
車牌傾斜的矯正方法
該方法主要是運(yùn)用兩個坐標(biāo)系之間的變換來實(shí)現(xiàn)的;對于傾斜角度在整幅圖像上的設(shè)定,該方法主要是通過對圖像中直線的傾斜角度進(jìn)行判斷來實(shí)現(xiàn)的。
車牌邊框和鉚釘?shù)娜コ?/h4>
對車牌圖像進(jìn)行逐行掃描,當(dāng)掃描到某一行白色像素點(diǎn)寬度大于某一個閾值時,可以認(rèn)為是字符邊沿處,去除這一行以下或以上的所有行就可以消除鉚釘和邊框的干擾。
車牌字符分割技術(shù)
整個字符分割過程需要考慮如下問題:
- 初步垂直切分后的切分結(jié)果和字符尺寸應(yīng)該基本一致。
- 字符拆分
- 字符合并
- 估計(jì)字間距、字符中心距離等信息
基于垂直投影和先驗(yàn)知識的車牌字符分割
-
計(jì)算垂直投影
-
初步垂直切分
在初步垂直切分中,字符的斷裂和粘連現(xiàn)象先不被考慮,進(jìn)行提取的只是在投影圖中已經(jīng)分離的那些區(qū)域。
粘連車牌字符的分割
在低質(zhì)量車牌圖像中,二值化處理后出現(xiàn)的字符粘連現(xiàn)象有時無法被任何一種分割方法所消除,有時利用垂直投影也無法分割那些粘連的字符,造成這種狀況的主要原因是有時存在大量的噪聲。
斷裂車牌字符的合并
先將每兩個相鄰塊之間的距離計(jì)算出來,在一定的閾值大于這個距離的情況下,可以認(rèn)為這兩個塊屬于同一個字符并執(zhí)行塊合并過程。
對車牌字符的切分結(jié)果進(jìn)行確認(rèn)
字符結(jié)果確認(rèn)機(jī)制的具體實(shí)現(xiàn)步驟:
- 搜索連通字符區(qū)域
- 對字符的垂直切分進(jìn)行更新和確認(rèn)
車牌字符識別技術(shù)
模式識別
影響并導(dǎo)致汽車牌照內(nèi)字符出現(xiàn)缺損、污染、模糊等情況的常見因素有照相機(jī)的性能、采集車輛圖像時光照的差異、汽車牌照的清潔度。
模式識別流程
模式就是一種對某種對象(一些敏感的客體)結(jié)構(gòu)或者定量的描述,是一種集合(由具有某些共同特定性質(zhì)的模式構(gòu)成)。
模式識別方法
目前模式識別主要有4種方法:
- 基于神經(jīng)網(wǎng)絡(luò)的識別方法、
- 基于句法模式的識別方法、
- 基于統(tǒng)計(jì)模式的識別方法和
- 基于模糊模式的識別方法。
字符識別
- 基本原理:對字符圖像進(jìn)行預(yù)處理、模式表達(dá)、判別和字典學(xué)習(xí)
- 階段:初級階段、對基礎(chǔ)理論進(jìn)行相關(guān)研究的階段、發(fā)展階段
字符識別方法:
基于神經(jīng)網(wǎng)絡(luò)的識別方法
基于特征分析的匹配方法
基于模版的匹配方法
目前,小波識別法、模板匹配法與神經(jīng)網(wǎng)絡(luò)法等常被作為汽車牌照字符識別的主要方法。
漢字識別
相較于數(shù)字和英文字符的識別,汽車牌照中的漢字字符識別的難度更大,主要原因有以下4個方面:
字符筆畫因切分誤差導(dǎo)致非筆畫或筆畫流失。
汽車牌照被污染導(dǎo)致字符上出現(xiàn)污垢。
采集所得車輛圖像分辨率低下導(dǎo)致多筆畫的漢字較難分辨。
車輛圖像采集時所受光照影響的差異導(dǎo)致筆畫較淡。
代碼示例:車牌定位
import cv2
import numpy as np
def stretch(img):
'''
圖像拉伸函數(shù)
'''
maxi = float(img.max())
mini = float(img.min())
for i in range(img.shape[0]):
for j in range(img.shape[1]):
img[i, j] = (255 / (maxi - mini) * img[i, j] - (255 * mini) / (maxi - mini))
return img
def dobinaryzation(img):
'''
二值化處理函數(shù)
'''
maxi = float(img.max())
mini = float(img.min())
x = maxi - ((maxi - mini) / 2)
# 二值化,返回閾值ret 和 二值化操作后的圖像thresh
ret, thresh = cv2.threshold(img, x, 255, cv2.THRESH_BINARY)
# 返回二值化后的黑白圖像
return thresh
def find_rectangle(contour):
'''
尋找矩形輪廓
'''
y, x = [], []
for p in contour:
y.append(p[0][0])
x.append(p[0][1])
return [min(y), min(x), max(y), max(x)]
def locate_license(img, afterimg):
'''
定位車牌號
'''
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 找出最大的三個區(qū)域
block = []
for c in contours:
# 找出輪廓的左上點(diǎn)和右下點(diǎn),由此計(jì)算它的面積和長度比
r = find_rectangle(c)
a = (r[2] - r[0]) * (r[3] - r[1]) # 面積
s = (r[2] - r[0]) * (r[3] - r[1]) # 長度比
block.append([r, a, s])
# 選出面積最大的3個區(qū)域
block = sorted(block, key=lambda b: b[1])[-3:]
# 使用顏色識別判斷找出最像車牌的區(qū)域
maxweight, maxindex = 0, -1
for i in range(len(block)):
b = afterimg[block[i][0][1]:block[i][0][3], block[i][0][0]:block[i][0][2]]
# BGR轉(zhuǎn)HSV
hsv = cv2.cvtColor(b, cv2.COLOR_BGR2HSV)
# 藍(lán)色車牌的范圍
lower = np.array([100, 50, 50])
upper = np.array([140, 255, 255])
# 根據(jù)閾值構(gòu)建掩膜
mask = cv2.inRange(hsv, lower, upper)
# 統(tǒng)計(jì)權(quán)值
w1 = 0
for m in mask:
w1 += m / 255
w2 = 0
for n in w1:
w2 += n
# 選出最大權(quán)值的區(qū)域
if w2 > maxweight:
maxindex = i
maxweight = w2
return block[maxindex][0]
def find_license(img):
'''
預(yù)處理函數(shù)
'''
m = 400 * img.shape[0] / img.shape[1]
# 壓縮圖像
img = cv2.resize(img, (400, int(m)), interpolation=cv2.INTER_CUBIC)
# BGR轉(zhuǎn)換為灰度圖像
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 灰度拉伸
stretchedimg = stretch(gray_img)
'''進(jìn)行開運(yùn)算,用來去除噪聲'''
r = 16
h = w = r * 2 + 1
kernel = np.zeros((h, w), np.uint8)
cv2.circle(kernel, (r, r), r, 1, -1)
# 開運(yùn)算
openingimg = cv2.morphologyEx(stretchedimg, cv2.MORPH_OPEN, kernel)
# 獲取差分圖,兩幅圖像做差 cv2.absdiff('圖像1','圖像2')
strtimg = cv2.absdiff(stretchedimg, openingimg)
# 圖像二值化
binaryimg = dobinaryzation(strtimg)
# canny邊緣檢測
canny = cv2.Canny(binaryimg, binaryimg.shape[0], binaryimg.shape[1])
'''消除小的區(qū)域,保留大塊的區(qū)域,從而定位車牌'''
# 進(jìn)行閉運(yùn)算
kernel = np.ones((5, 19), np.uint8)
closingimg = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel)
# 進(jìn)行開運(yùn)算
openingimg = cv2.morphologyEx(closingimg, cv2.MORPH_OPEN, kernel)
# 再次進(jìn)行開運(yùn)算
kernel = np.ones((11, 5), np.uint8)
openingimg = cv2.morphologyEx(openingimg, cv2.MORPH_OPEN, kernel)
# 消除小區(qū)域,定位車牌位置
rect = locate_license(openingimg, img)
return rect, img
def cut_license(afterimg, rect):
'''
圖像分割函數(shù)
'''
# 轉(zhuǎn)換為寬度和高度
rect[2] = rect[2] - rect[0]
rect[3] = rect[3] - rect[1]
rect_copy = tuple(rect.copy())
rect = [0, 0, 0, 0]
# 創(chuàng)建掩膜
mask = np.zeros(afterimg.shape[:2], np.uint8)
# 創(chuàng)建背景模型 大小只能為13*5,行數(shù)只能為1,單通道浮點(diǎn)型
bgdModel = np.zeros((1, 65), np.float64)
# 創(chuàng)建前景模型
fgdModel = np.zeros((1, 65), np.float64)
# 分割圖像
cv2.grabCut(afterimg, mask, rect_copy, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
img_show = afterimg * mask2[:, :, np.newaxis]
return img_show
def deal_license(licenseimg):
'''
車牌圖片二值化
'''
# 車牌變?yōu)榛叶葓D像
gray_img = cv2.cvtColor(licenseimg, cv2.COLOR_BGR2GRAY)
# 均值濾波 去除噪聲
kernel = np.ones((3, 3), np.float32) / 9
gray_img = cv2.filter2D(gray_img, -1, kernel)
# 二值化處理
ret, thresh = cv2.threshold(gray_img, 120, 255, cv2.THRESH_BINARY)
return thresh
def find_end(start, arg, black, white, width, black_max, white_max):
end = start + 1
for m in range(start + 1, width - 1):
if (black[m] if arg else white[m]) > (0.98 * black_max if arg else 0.98 * white_max):
end = m
break
return end
if __name__ == '__main__':
img = cv2.imread('car.jpg', cv2.IMREAD_COLOR)
# 預(yù)處理圖像
rect, afterimg = find_license(img)
# 框出車牌號
cv2.rectangle(afterimg, (rect[0], rect[1]), (rect[2], rect[3]), (0, 255, 0), 2)
cv2.imshow('afterimg', afterimg)
# 分割車牌與背景
cutimg = cut_license(afterimg, rect)
cv2.imshow('cutimg', cutimg)
# 二值化生成黑白圖
thresh = deal_license(cutimg)
cv2.imshow('thresh', thresh)
cv2.imwrite("cp.jpg", thresh)
cv2.waitKey(0)
# 分割字符
'''
判斷底色和字色
'''
# 記錄黑白像素總和
white = []
black = []
height = thresh.shape[0] # 263
width = thresh.shape[1] # 400
# print('height',height)
# print('width',width)
white_max = 0
black_max = 0
# 計(jì)算每一列的黑白像素總和
for i in range(width):
line_white = 0
line_black = 0
for j in range(height):
if thresh[j][i] == 255:
line_white += 1
if thresh[j][i] == 0:
line_black += 1
white_max = max(white_max, line_white)
black_max = max(black_max, line_black)
white.append(line_white)
black.append(line_black)
print('white', white)
print('black', black)
# arg為true表示黑底白字,F(xiàn)alse為白底黑字
arg = True
if black_max < white_max:
arg = False
n = 1
start = 1
end = 2
while n < width - 2:
n += 1
# 判斷是白底黑字還是黑底白字 0.05參數(shù)對應(yīng)上面的0.95 可作調(diào)整
if (white[n] if arg else black[n]) > (0.02 * white_max if arg else 0.02 * black_max):
start = n
end = find_end(start, arg, black, white, width, black_max, white_max)
n = end
if end - start > 5:
cj = thresh[1:height, start:end]
cv2.imshow('cutlicense', cj)
cv2.waitKey(0)
cv2.waitKey(0)
cv2.destroyAllWindows()
運(yùn)行效果:
可見車牌定位成功,下一步是識別字符。
代碼示例:基于tesseract 識別車牌字符
-
安裝步驟:
Tesseract Ocr 工具安裝,下面是下載地址,下載后 用管理員安裝,安裝的時候注意 記住自己的安裝路徑。
https://www.aliyundrive.com/s/xHi1Y28LGjv -
安裝python pytesseract 庫包
pip install pytesseract
pytesseract 配置。
找到pytesseract 剛才的安裝路徑,如下
然后打開這個文件,
這里填上你自己的安裝路徑地址,至此,字符識別開發(fā)環(huán)境就準(zhǔn)備好了。
import cv2 as cv
from PIL import Image
import pytesseract as tess
def recoginse_text(image):
"""
步驟:
1、灰度,二值化處理
2、形態(tài)學(xué)操作去噪
3、識別
:param image:
:return:
"""
# 灰度 二值化
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# 如果是白底黑字 建議 _INV
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
# 形態(tài)學(xué)操作 (根據(jù)需要設(shè)置參數(shù)(1,2))
kernel = cv.getStructuringElement(cv.MORPH_RECT, (1, 2)) # 去除橫向細(xì)線
morph1 = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (2, 1)) # 去除縱向細(xì)線
morph2 = cv.morphologyEx(morph1, cv.MORPH_OPEN, kernel)
cv.imshow("Morph", morph2)
# 黑底白字取非,變?yōu)榘椎缀谧郑ū阌趐ytesseract 識別)
cv.bitwise_not(morph2, morph2)
textImage = Image.fromarray(morph2)
# 圖片轉(zhuǎn)文字
text = tess.image_to_string(textImage)
n = 10 # 根據(jù)不同國家車牌固定數(shù)目進(jìn)行設(shè)置
print("識別結(jié)果:")
print(text[1:n])
def main():
# 讀取需要識別的數(shù)字字母圖片,并顯示讀到的原圖
src = cv.imread("cp.jpg")
cv.imshow("src", src)
# 識別
recoginse_text(src)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == "__main__":
main()
運(yùn)行結(jié)果:
識別結(jié)果:
0226 F By
漢字識別
向校于數(shù)字和英文字符的識別,汽車車牌中的漢子字符識別的 難度更大。
- 預(yù)處理原始圖像
- 對字符的原始特征進(jìn)行提取
- 讀取獲取所得的最終特征
代碼示例:中文車牌的識別
import tkinter as tk
from tkinter.filedialog import *
from tkinter import ttk
import tkinter.messagebox as mBox
#代碼在下一個文件中
import predict
import cv2
from PIL import Image, ImageTk
import threading
import time
class Surface(ttk.Frame):
pic_path = ""
viewhigh = 600
viewwide = 600
update_time = 0
thread = None
thread_run = False
camera = None
color_transform = {"green": ("綠牌", "#55FF55"), "yello": ("黃牌", "#FFFF00"), "blue": ("藍(lán)牌", "#6666FF")}
def __init__(self, win):
ttk.Frame.__init__(self, win)
frame_left = ttk.Frame(self)
frame_right1 = ttk.Frame(self)
frame_right2 = ttk.Frame(self)
win.title("車牌識別")
win.state("zoomed")
self.pack(fill=tk.BOTH, expand=tk.YES, padx="5", pady="5")
frame_left.pack(side=tk.LEFT, expand=1, fill=tk.BOTH)
frame_right1.pack(side=tk.TOP, expand=1, fill=tk.Y)
frame_right2.pack(side=tk.RIGHT, expand=0)
ttk.Label(frame_left, text='原圖:').pack(anchor="nw")
ttk.Label(frame_right1, text='車牌位置:').grid(column=0, row=0, sticky=tk.W)
from_pic_ctl = ttk.Button(frame_right2, text="來自圖片", width=20, command=self.from_pic)
from_vedio_ctl = ttk.Button(frame_right2, text="來自攝像頭", width=20, command=self.from_vedio)
self.image_ctl = ttk.Label(frame_left)
self.image_ctl.pack(anchor="nw")
self.roi_ctl = ttk.Label(frame_right1)
self.roi_ctl.grid(column=0, row=1, sticky=tk.W)
ttk.Label(frame_right1, text='識別結(jié)果:').grid(column=0, row=2, sticky=tk.W)
self.r_ctl = ttk.Label(frame_right1, text="")
self.r_ctl.grid(column=0, row=3, sticky=tk.W)
self.color_ctl = ttk.Label(frame_right1, text="", width="20")
self.color_ctl.grid(column=0, row=4, sticky=tk.W)
from_vedio_ctl.pack(anchor="se", pady="5")
from_pic_ctl.pack(anchor="se", pady="5")
self.predictor = predict.CardPredictor()
self.predictor.train_svm()
def get_imgtk(self, img_bgr):
img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
im = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=im)
wide = imgtk.width()
high = imgtk.height()
if wide > self.viewwide or high > self.viewhigh:
wide_factor = self.viewwide / wide
high_factor = self.viewhigh / high
factor = min(wide_factor, high_factor)
wide = int(wide * factor)
if wide <= 0: wide = 1
high = int(high * factor)
if high <= 0: high = 1
im = im.resize((wide, high), Image.LANCZOS) # 在pillow的10.0.0版本中,ANTIALIAS方法被刪除了,使用新的方法即可:
imgtk = ImageTk.PhotoImage(image=im)
return imgtk
def show_roi(self, r, roi, color):
if r:
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
roi = Image.fromarray(roi)
self.imgtk_roi = ImageTk.PhotoImage(image=roi)
self.roi_ctl.configure(image=self.imgtk_roi, state='enable')
self.r_ctl.configure(text=str(r))
self.update_time = time.time()
try:
c = self.color_transform[color]
self.color_ctl.configure(text=c[0], background=c[1], state='enable')
except:
self.color_ctl.configure(state='disabled')
elif self.update_time + 8 < time.time():
self.roi_ctl.configure(state='disabled')
self.r_ctl.configure(text="")
self.color_ctl.configure(state='disabled')
def from_vedio(self):
if self.thread_run:
return
if self.camera is None:
self.camera = cv2.VideoCapture(0)
if not self.camera.isOpened():
mBox.showwarning('警告', '攝像頭打開失??!')
self.camera = None
return
self.thread = threading.Thread(target=self.vedio_thread, args=(self,))
self.thread.setDaemon(True)
self.thread.start()
self.thread_run = True
def from_pic(self):
self.thread_run = False
self.pic_path = askopenfilename(title="選擇識別圖片", filetypes=[("jpg圖片", "*.jpg")])
if self.pic_path:
img_bgr = predict.imreadex(self.pic_path)
self.imgtk = self.get_imgtk(img_bgr)
self.image_ctl.configure(image=self.imgtk)
resize_rates = (1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4)
for resize_rate in resize_rates:
print("resize_rate:", resize_rate)
try:
r, roi, color = self.predictor.predict(img_bgr, resize_rate)
except:
continue
if r:
break
# r, roi, color = self.predictor.predict(img_bgr, 1)
self.show_roi(r, roi, color)
@staticmethod
def vedio_thread(self):
self.thread_run = True
predict_time = time.time()
while self.thread_run:
_, img_bgr = self.camera.read()
self.imgtk = self.get_imgtk(img_bgr)
self.image_ctl.configure(image=self.imgtk)
if time.time() - predict_time > 2:
r, roi, color = self.predictor.predict(img_bgr)
self.show_roi(r, roi, color)
predict_time = time.time()
print("run end")
def close_window():
print("destroy")
if surface.thread_run:
surface.thread_run = False
surface.thread.join(2.0)
win.destroy()
if __name__ == '__main__':
win = tk.Tk()
surface = Surface(win)
win.protocol('WM_DELETE_WINDOW', close_window)
win.mainloop()
在上述代碼中,先使用圖像邊緣和車牌顏色定位車牌再識別字符。車牌字符識別使用的算法是Opencv 的SVM,車牌字符識別在predict方法(perdict.py )中實(shí)現(xiàn),代碼如下:
import cv2
import numpy as np
from numpy.linalg import norm
import sys
import os
import json
SZ = 20 #訓(xùn)練圖片長寬
MAX_WIDTH = 1000 #原始圖片最大寬度
Min_Area = 2000 #車牌區(qū)域允許最大面積
PROVINCE_START = 1000
#讀取圖片文件
def imreadex(filename):
return cv2.imdecode(np.fromfile(filename, dtype=np.uint8), cv2.IMREAD_COLOR)
def point_limit(point):
if point[0] < 0:
point[0] = 0
if point[1] < 0:
point[1] = 0
#根據(jù)設(shè)定的閾值和圖片直方圖,找出波峰,用于分隔字符
def find_waves(threshold, histogram):
up_point = -1#上升點(diǎn)
is_peak = False
if histogram[0] > threshold:
up_point = 0
is_peak = True
wave_peaks = []
for i,x in enumerate(histogram):
if is_peak and x < threshold:
if i - up_point > 2:
is_peak = False
wave_peaks.append((up_point, i))
elif not is_peak and x >= threshold:
is_peak = True
up_point = i
if is_peak and up_point != -1 and i - up_point > 4:
wave_peaks.append((up_point, i))
return wave_peaks
#根據(jù)找出的波峰,分隔圖片,從而得到逐個字符圖片
def seperate_card(img, waves):
part_cards = []
for wave in waves:
part_cards.append(img[:, wave[0]:wave[1]])
return part_cards
#來自opencv的sample,用于svm訓(xùn)練
def deskew(img):
m = cv2.moments(img)
if abs(m['mu02']) < 1e-2:
return img.copy()
skew = m['mu11']/m['mu02']
M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]])
img = cv2.warpAffine(img, M, (SZ, SZ), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR)
return img
#來自opencv的sample,用于svm訓(xùn)練
def preprocess_hog(digits):
samples = []
for img in digits:
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bin_n = 16
bin = np.int32(bin_n*ang/(2*np.pi))
bin_cells = bin[:10,:10], bin[10:,:10], bin[:10,10:], bin[10:,10:]
mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
# transform to Hellinger kernel
eps = 1e-7
hist /= hist.sum() + eps
hist = np.sqrt(hist)
hist /= norm(hist) + eps
samples.append(hist)
return np.float32(samples)
#不能保證包括所有省份
provinces = [
"zh_cuan", "川",
"zh_e", "鄂",
"zh_gan", "贛",
"zh_gan1", "甘",
"zh_gui", "貴",
"zh_gui1", "桂",
"zh_hei", "黑",
"zh_hu", "滬",
"zh_ji", "冀",
"zh_jin", "津",
"zh_jing", "京",
"zh_jl", "吉",
"zh_liao", "遼",
"zh_lu", "魯",
"zh_meng", "蒙",
"zh_min", "閩",
"zh_ning", "寧",
"zh_qing", "靑",
"zh_qiong", "瓊",
"zh_shan", "陜",
"zh_su", "蘇",
"zh_sx", "晉",
"zh_wan", "皖",
"zh_xiang", "湘",
"zh_xin", "新",
"zh_yu", "豫",
"zh_yu1", "渝",
"zh_yue", "粵",
"zh_yun", "云",
"zh_zang", "藏",
"zh_zhe", "浙"
]
class StatModel(object):
def load(self, fn):
self.model = self.model.load(fn)
def save(self, fn):
self.model.save(fn)
class SVM(StatModel):
def __init__(self, C = 1, gamma = 0.5):
self.model = cv2.ml.SVM_create()
self.model.setGamma(gamma)
self.model.setC(C)
self.model.setKernel(cv2.ml.SVM_RBF)
self.model.setType(cv2.ml.SVM_C_SVC)
#訓(xùn)練svm
def train(self, samples, responses):
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)
#字符識別
def predict(self, samples):
r = self.model.predict(samples)
return r[1].ravel()
class CardPredictor:
def __init__(self):
#車牌識別的部分參數(shù)保存在js中,便于根據(jù)圖片分辨率做調(diào)整
f = open('config.js')
j = json.load(f)
for c in j["config"]:
if c["open"]:
self.cfg = c.copy()
break
else:
raise RuntimeError('沒有設(shè)置有效配置參數(shù)')
def __del__(self):
self.save_traindata()
def train_svm(self):
#識別英文字母和數(shù)字
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)
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是拼音對應(yīng)的漢字
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")
def accurate_place(self, card_img_hsv, limit1, limit2, color):
row_num, col_num = card_img_hsv.shape[:2]
xl = col_num
xr = 0
yh = 0
yl = row_num
#col_num_limit = self.cfg["col_num_limit"]
row_num_limit = self.cfg["row_num_limit"]
col_num_limit = col_num * 0.8 if color != "green" else col_num * 0.5#綠色有漸變
for i in range(row_num):
count = 0
for j in range(col_num):
H = card_img_hsv.item(i, j, 0)
S = card_img_hsv.item(i, j, 1)
V = card_img_hsv.item(i, j, 2)
if limit1 < H <= limit2 and 34 < S and 46 < V:
count += 1
if count > col_num_limit:
if yl > i:
yl = i
if yh < i:
yh = i
for j in range(col_num):
count = 0
for i in range(row_num):
H = card_img_hsv.item(i, j, 0)
S = card_img_hsv.item(i, j, 1)
V = card_img_hsv.item(i, j, 2)
if limit1 < H <= limit2 and 34 < S and 46 < V:
count += 1
if count > row_num - row_num_limit:
if xl > j:
xl = j
if xr < j:
xr = j
return xl, xr, yh, yl
def predict(self, car_pic, resize_rate=1):
if type(car_pic) == type(""):
img = imreadex(car_pic)
else:
img = car_pic
pic_hight, pic_width = img.shape[:2]
if pic_width > MAX_WIDTH:
pic_rate = MAX_WIDTH / pic_width
img = cv2.resize(img, (MAX_WIDTH, int(pic_hight*pic_rate)), interpolation=cv2.INTER_LANCZOS4)
pic_hight, pic_width = img.shape[:2]
if resize_rate != 1:
img = cv2.resize(img, (int(pic_width*resize_rate), int(pic_hight*resize_rate)), interpolation=cv2.INTER_LANCZOS4)
pic_hight, pic_width = img.shape[:2]
print("h,w:", pic_hight, pic_width)
blur = self.cfg["blur"]
#高斯去噪
if blur > 0:
img = cv2.GaussianBlur(img, (blur, blur), 0)#圖片分辨率調(diào)整
oldimg = img
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#equ = cv2.equalizeHist(img)
#img = np.hstack((img, equ))
#去掉圖像中不會是車牌的區(qū)域
kernel = np.ones((20, 20), np.uint8)
img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
img_opening = cv2.addWeighted(img, 1, img_opening, -1, 0);
#找到圖像邊緣
ret, img_thresh = cv2.threshold(img_opening, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
img_edge = cv2.Canny(img_thresh, 100, 200)
#使用開運(yùn)算和閉運(yùn)算讓圖像邊緣成為一個整體
kernel = np.ones((self.cfg["morphologyr"], self.cfg["morphologyc"]), np.uint8)
img_edge1 = cv2.morphologyEx(img_edge, cv2.MORPH_CLOSE, kernel)
img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, kernel)
#查找圖像邊緣整體形成的矩形區(qū)域,可能有很多,車牌就在其中一個矩形區(qū)域中
try:
contours, hierarchy = cv2.findContours(img_edge2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
except ValueError:
image, contours, hierarchy = cv2.findContours(img_edge2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = [cnt for cnt in contours if cv2.contourArea(cnt) > Min_Area]
print('len(contours)', len(contours))
#一一排除不是車牌的矩形區(qū)域
car_contours = []
for cnt in contours:
rect = cv2.minAreaRect(cnt)
area_width, area_height = rect[1]
if area_width < area_height:
area_width, area_height = area_height, area_width
wh_ratio = area_width / area_height
#print(wh_ratio)
#要求矩形區(qū)域長寬比在2到5.5之間,2到5.5是車牌的長寬比,其余的矩形排除
if wh_ratio > 2 and wh_ratio < 5.5:
car_contours.append(rect)
box = cv2.boxPoints(rect)
box = np.int0(box)
#oldimg = cv2.drawContours(oldimg, [box], 0, (0, 0, 255), 2)
#cv2.imshow("edge4", oldimg)
#cv2.waitKey(0)
print(len(car_contours))
print("精確定位")
card_imgs = []
#矩形區(qū)域可能是傾斜的矩形,需要矯正,以便使用顏色定位
for rect in car_contours:
if rect[2] > -1 and rect[2] < 1:#創(chuàng)造角度,使得左、高、右、低拿到正確的值
angle = 1
else:
angle = rect[2]
rect = (rect[0], (rect[1][0]+5, rect[1][1]+5), angle)#擴(kuò)大范圍,避免車牌邊緣被排除
box = cv2.boxPoints(rect)
heigth_point = right_point = [0, 0]
left_point = low_point = [pic_width, pic_hight]
for point in box:
if left_point[0] > point[0]:
left_point = point
if low_point[1] > point[1]:
low_point = point
if heigth_point[1] < point[1]:
heigth_point = point
if right_point[0] < point[0]:
right_point = point
if left_point[1] <= right_point[1]:#正角度
new_right_point = [right_point[0], heigth_point[1]]
pts2 = np.float32([left_point, heigth_point, new_right_point])#字符只是高度需要改變
pts1 = np.float32([left_point, heigth_point, right_point])
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(oldimg, M, (pic_width, pic_hight))
point_limit(new_right_point)
point_limit(heigth_point)
point_limit(left_point)
card_img = dst[int(left_point[1]):int(heigth_point[1]), int(left_point[0]):int(new_right_point[0])]
card_imgs.append(card_img)
#cv2.imshow("card", card_img)
#cv2.waitKey(0)
elif left_point[1] > right_point[1]:#負(fù)角度
new_left_point = [left_point[0], heigth_point[1]]
pts2 = np.float32([new_left_point, heigth_point, right_point])#字符只是高度需要改變
pts1 = np.float32([left_point, heigth_point, right_point])
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(oldimg, M, (pic_width, pic_hight))
point_limit(right_point)
point_limit(heigth_point)
point_limit(new_left_point)
card_img = dst[int(right_point[1]):int(heigth_point[1]), int(new_left_point[0]):int(right_point[0])]
card_imgs.append(card_img)
#cv2.imshow("card", card_img)
#cv2.waitKey(0)
#開始使用顏色定位,排除不是車牌的矩形,目前只識別藍(lán)、綠、黃車牌
colors = []
for card_index,card_img in enumerate(card_imgs):
green = yello = blue = black = white = 0
card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV)
#有轉(zhuǎn)換失敗的可能,原因來自于上面矯正矩形出錯
if card_img_hsv is None:
continue
row_num, col_num= card_img_hsv.shape[:2]
card_img_count = row_num * col_num
for i in range(row_num):
for j in range(col_num):
H = card_img_hsv.item(i, j, 0)
S = card_img_hsv.item(i, j, 1)
V = card_img_hsv.item(i, j, 2)
if 11 < H <= 34 and S > 34:#圖片分辨率調(diào)整
yello += 1
elif 35 < H <= 99 and S > 34:#圖片分辨率調(diào)整
green += 1
elif 99 < H <= 124 and S > 34:#圖片分辨率調(diào)整
blue += 1
if 0 < H <180 and 0 < S < 255 and 0 < V < 46:
black += 1
elif 0 < H <180 and 0 < S < 43 and 221 < V < 225:
white += 1
color = "no"
limit1 = limit2 = 0
if yello*2 >= card_img_count:
color = "yello"
limit1 = 11
limit2 = 34#有的圖片有色偏偏綠
elif green*2 >= card_img_count:
color = "green"
limit1 = 35
limit2 = 99
elif blue*2 >= card_img_count:
color = "blue"
limit1 = 100
limit2 = 124#有的圖片有色偏偏紫
elif black + white >= card_img_count*0.7:#TODO
color = "bw"
print(color)
colors.append(color)
print(blue, green, yello, black, white, card_img_count)
#cv2.imshow("color", card_img)
#cv2.waitKey(0)
if limit1 == 0:
continue
#以上為確定車牌顏色
#以下為根據(jù)車牌顏色再定位,縮小邊緣非車牌邊界
xl, xr, yh, yl = self.accurate_place(card_img_hsv, limit1, limit2, color)
if yl == yh and xl == xr:
continue
need_accurate = False
if yl >= yh:
yl = 0
yh = row_num
need_accurate = True
if xl >= xr:
xl = 0
xr = col_num
need_accurate = True
card_imgs[card_index] = card_img[yl:yh, xl:xr] if color != "green" or yl < (yh-yl)//4 else card_img[yl-(yh-yl)//4:yh, xl:xr]
if need_accurate:#可能x或y方向未縮小,需要再試一次
card_img = card_imgs[card_index]
card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV)
xl, xr, yh, yl = self.accurate_place(card_img_hsv, limit1, limit2, color)
if yl == yh and xl == xr:
continue
if yl >= yh:
yl = 0
yh = row_num
if xl >= xr:
xl = 0
xr = col_num
card_imgs[card_index] = card_img[yl:yh, xl:xr] if color != "green" or yl < (yh-yl)//4 else card_img[yl-(yh-yl)//4:yh, xl:xr]
#以上為車牌定位
#以下為識別車牌中的字符
predict_result = []
roi = None
card_color = None
for i, color in enumerate(colors):
if color in ("blue", "yello", "green"):
card_img = card_imgs[i]
gray_img = cv2.cvtColor(card_img, cv2.COLOR_BGR2GRAY)
#黃、綠車牌字符比背景暗、與藍(lán)車牌剛好相反,所以黃、綠車牌需要反向
if color == "green" or color == "yello":
gray_img = cv2.bitwise_not(gray_img)
ret, gray_img = cv2.threshold(gray_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
#查找水平直方圖波峰
x_histogram = np.sum(gray_img, axis=1)
x_min = np.min(x_histogram)
x_average = np.sum(x_histogram)/x_histogram.shape[0]
x_threshold = (x_min + x_average)/2
wave_peaks = find_waves(x_threshold, x_histogram)
if len(wave_peaks) == 0:
print("peak less 0:")
continue
#認(rèn)為水平方向,最大的波峰為車牌區(qū)域
wave = max(wave_peaks, key=lambda x:x[1]-x[0])
gray_img = gray_img[wave[0]:wave[1]]
#查找垂直直方圖波峰
row_num, col_num= gray_img.shape[:2]
#去掉車牌上下邊緣1個像素,避免白邊影響閾值判斷
gray_img = gray_img[1:row_num-1]
y_histogram = np.sum(gray_img, axis=0)
y_min = np.min(y_histogram)
y_average = np.sum(y_histogram)/y_histogram.shape[0]
y_threshold = (y_min + y_average)/5#U和0要求閾值偏小,否則U和0會被分成兩半
wave_peaks = find_waves(y_threshold, y_histogram)
#for wave in wave_peaks:
# cv2.line(card_img, pt1=(wave[0], 5), pt2=(wave[1], 5), color=(0, 0, 255), thickness=2)
#車牌字符數(shù)應(yīng)大于6
if len(wave_peaks) <= 6:
print("peak less 1:", len(wave_peaks))
continue
wave = max(wave_peaks, key=lambda x:x[1]-x[0])
max_wave_dis = wave[1] - wave[0]
#判斷是否是左側(cè)車牌邊緣
if wave_peaks[0][1] - wave_peaks[0][0] < max_wave_dis/3 and wave_peaks[0][0] == 0:
wave_peaks.pop(0)
#組合分離漢字
cur_dis = 0
for i,wave in enumerate(wave_peaks):
if wave[1] - wave[0] + cur_dis > max_wave_dis * 0.6:
break
else:
cur_dis += wave[1] - wave[0]
if i > 0:
wave = (wave_peaks[0][0], wave_peaks[i][1])
wave_peaks = wave_peaks[i+1:]
wave_peaks.insert(0, wave)
#去除車牌上的分隔點(diǎn)
point = wave_peaks[2]
if point[1] - point[0] < max_wave_dis/3:
point_img = gray_img[:,point[0]:point[1]]
if np.mean(point_img) < 255/5:
wave_peaks.pop(2)
if len(wave_peaks) <= 6:
print("peak less 2:", len(wave_peaks))
continue
part_cards = seperate_card(gray_img, wave_peaks)
for i, part_card in enumerate(part_cards):
#可能是固定車牌的鉚釘
if np.mean(part_card) < 255/5:
print("a point")
continue
part_card_old = part_card
#w = abs(part_card.shape[1] - SZ)//2
w = part_card.shape[1] // 3
part_card = cv2.copyMakeBorder(part_card, 0, 0, w, w, cv2.BORDER_CONSTANT, value = [0,0,0])
part_card = cv2.resize(part_card, (SZ, SZ), interpolation=cv2.INTER_AREA)
#cv2.imshow("part", part_card_old)
#cv2.waitKey(0)
#cv2.imwrite("u.jpg", part_card)
#part_card = deskew(part_card)
part_card = preprocess_hog([part_card])
if i == 0:
resp = self.modelchinese.predict(part_card)
charactor = provinces[int(resp[0]) - PROVINCE_START]
else:
resp = self.model.predict(part_card)
charactor = chr(resp[0])
#判斷最后一個數(shù)是否是車牌邊緣,假設(shè)車牌邊緣被認(rèn)為是1
if charactor == "1" and i == len(part_cards)-1:
if part_card_old.shape[0]/part_card_old.shape[1] >= 8:#1太細(xì),認(rèn)為是邊緣
print(part_card_old.shape)
continue
predict_result.append(charactor)
roi = card_img
card_color = color
break
return predict_result, roi, card_color#識別到的字符、定位的車牌圖像、車牌顏色
if __name__ == '__main__':
c = CardPredictor()
c.train_svm()
r, roi, color = c.predict("car2.jpg")
print(r)
運(yùn)行第一個文件會彈出一個選擇框;
選個圖片
運(yùn)行結(jié)果:
['京', 'A', 'D', '7', 'Z', '9', '7', '2']
看來還是需要在訓(xùn)練下模型。后續(xù)補(bǔ)充新模型驗(yàn)證測試。文章來源:http://www.zghlxwxcb.cn/news/detail-674115.html
代碼中涉及的文件下載地址
下載
https://download.csdn.net/download/hai411741962/88244933文章來源地址http://www.zghlxwxcb.cn/news/detail-674115.html
到了這里,關(guān)于opencv 案例實(shí)戰(zhàn)01-停車場車牌識別實(shí)戰(zhàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!