初學圖像處理,作了一個車牌提取項目,本博客僅僅是爲了記錄一下學習過程,該項目只具有初級功能,還有待改善
app
第一部分:車牌傾斜矯正ide
# 導入所需模塊 import cv2 import math from matplotlib import pyplot as plt # 顯示圖片 def cv_show(name,img): cv2.imshow(name,img) cv2.waitKey() cv2.destroyAllWindows() # 調整圖片大小 def resize(image, width=None, height=None, inter=cv2.INTER_AREA): dim = None (h, w) = image.shape[:2] if width is None and height is None: return image if width is None: r = height / float(h) dim = (int(w * r), height) else: r = width / float(w) dim = (width, int(h * r)) resized = cv2.resize(image, dim, interpolation=inter) return resized # 加載圖片 origin_Image = cv2.imread("./images/car_09.jpg") rawImage = resize(origin_Image,height=500) # 高斯去噪 image = cv2.GaussianBlur(rawImage, (3, 3), 0) # 灰度處理 gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # sobel算子邊緣檢測(作了一個y方向的檢測) Sobel_x = cv2.Sobel(gray_image, cv2.CV_16S, 1, 0) absX = cv2.convertScaleAbs(Sobel_x) # 轉回uint8 image = absX # 自適應閾值處理 ret, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU) # 閉運算,是白色部分練成總體 kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (14, 5)) image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernelX,iterations = 1) # 去除一些小的白點 kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1)) kernelY = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 19)) # 膨脹,腐蝕 image = cv2.dilate(image, kernelX) image = cv2.erode(image, kernelX) # 腐蝕,膨脹 image = cv2.erode(image, kernelY) image = cv2.dilate(image, kernelY) # 中值濾波去除噪點 image = cv2.medianBlur(image, 15) # 輪廓檢測 # cv2.RETR_EXTERNAL表示只檢測外輪廓 # cv2.CHAIN_APPROX_SIMPLE壓縮水平方向,垂直方向,對角線方向的元素,只保留該方向的終點座標,例如一個矩形輪廓只需4個點來保存輪廓信息 thresh_, contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 繪製輪廓 image1 = rawImage.copy() cv2.drawContours(image1, contours, -1, (0, 255, 0), 5) cv_show('image1',image1) # 篩選出車牌位置的輪廓 # 這裏我只作了一個車牌的長寬比在3:1到4:1之間這樣一個判斷 for i,item in enumerate(contours): # enumerate() 函數用於將一個可遍歷的數據對象(如列表、元組或字符串)組合爲一個索引序列,同時列出數據和數據下標,通常用在for循環當中 # cv2.boundingRect用一個最小的矩形,把找到的形狀包起來 rect = cv2.boundingRect(item) x = rect[0] y = rect[1] weight = rect[2] height = rect[3] if (weight > (height * 1.5)) and (weight < (height * 4)) and height>50: index = i image2 = rawImage.copy() cv2.drawContours(image2, contours, index, (0, 0, 255), 3) cv_show('image2',image2) # 參數: # http://www.javashuo.com/article/p-ddgnxgzh-gx.html # InputArray Points: 待擬合的直線的集合,必須是矩陣形式; # distType: 距離類型。fitline爲距離最小化函數,擬合直線時,要使輸入點到擬合直線的距離和最小化。這裏的 距離的類型有如下幾種: # cv2.DIST_USER : User defined distance # cv2.DIST_L1: distance = |x1-x2| + |y1-y2| # cv2.DIST_L2: 歐式距離,此時與最小二乘法相同 # cv2.DIST_C:distance = max(|x1-x2|,|y1-y2|) # cv2.DIST_L12:L1-L2 metric: distance = 2(sqrt(1+x*x/2) - 1)) # cv2.DIST_FAIR:distance = c^2(|x|/c-log(1+|x|/c)), c = 1.3998 # cv2.DIST_WELSCH: distance = c2/2(1-exp(-(x/c)2)), c = 2.9846 # cv2.DIST_HUBER:distance = |x|<c ? x^2/2 : c(|x|-c/2), c=1.345 # param: 距離參數,跟所選的距離類型有關,值能夠設置爲0。 # # reps, aeps: 第5/6個參數用於表示擬合直線所須要的徑向和角度精度,一般狀況下兩個值均被設定爲1e-2. # output : # # 對於二維直線,輸出output爲4維,前兩維表明擬合出的直線的方向,後兩位表明直線上的一點。(即一般說的點斜式直線) # 其中(vx, vy) 是直線的方向向量,(x, y) 是直線上的一個點。 # 斜率k = vy / vx # 截距b = y - k * x # 直線擬合找斜率 cnt = contours[index] image3 = rawImage.copy() h, w = image3.shape[:2] [vx, vy, x, y] = cv2.fitLine(cnt, cv2.DIST_L2, 0, 0.01, 0.01) k = vy/vx b = y-k*x lefty = b righty = k*w+b img = cv2.line(image3, (w, righty), (0, lefty), (0, 255, 0), 2) cv_show('img',img) a = math.atan(k) a = math.degrees(a) image4 = origin_Image.copy() # 圖像旋轉 h,w = image4.shape[:2] print(h,w) #第一個參數旋轉中心,第二個參數旋轉角度,第三個參數:縮放比例 M = cv2.getRotationMatrix2D((w/2,h/2),a,1) #第三個參數:變換後的圖像大小 dst = cv2.warpAffine(image4,M,(int(w*1),int(h*1))) cv_show('dst',dst) cv2.imwrite('car_09.jpg',dst)
第二部分:車牌號碼提取函數
1 # 導入所需模塊 2 import cv2 3 from matplotlib import pyplot as plt 4 import os 5 import numpy as np 6 7 # 顯示圖片 8 def cv_show(name,img): 9 cv2.imshow(name,img) 10 cv2.waitKey() 11 cv2.destroyAllWindows() 12 13 def resize(image, width=None, height=None, inter=cv2.INTER_AREA): 14 dim = None 15 (h, w) = image.shape[:2] 16 if width is None and height is None: 17 return image 18 if width is None: 19 r = height / float(h) 20 dim = (int(w * r), height) 21 else: 22 r = width / float(w) 23 dim = (width, int(h * r)) 24 resized = cv2.resize(image, dim, interpolation=inter) 25 return resized 26 27 #讀取待檢測圖片 28 origin_image = cv2.imread('./car_09.jpg') 29 resize_image = resize(origin_image,height=600) 30 ratio = origin_image.shape[0]/600 31 print(ratio) 32 cv_show('resize_image',resize_image) 33 34 35 #高斯濾波,灰度化 36 image = cv2.GaussianBlur(resize_image, (3, 3), 0) 37 gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) 38 cv_show('gray_image',gray_image) 39 40 #梯度化 41 Sobel_x = cv2.Sobel(gray_image, cv2.CV_16S, 1, 0) 42 absX = cv2.convertScaleAbs(Sobel_x) 43 image = absX 44 cv_show('image',image) 45 46 #閉操做 47 ret, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU) 48 kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 1)) 49 image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernelX, iterations=2) 50 cv_show('image',image) 51 kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 1)) 52 kernelY = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1)) 53 image = cv2.dilate(image, kernelX) 54 image = cv2.erode(image, kernelX) 55 image = cv2.erode(image, kernelY) 56 image = cv2.dilate(image, kernelY) 57 image = cv2.medianBlur(image, 9) 58 cv_show('image',image) 59 60 #繪製輪廓 61 thresh, contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 62 print(type(contours)) 63 print(len(contours)) 64 65 cur_img = resize_image.copy() 66 cv2.drawContours(cur_img,contours,-1,(0,0,255),3) 67 cv_show('img',cur_img) 68 69 for item in contours: 70 rect = cv2.boundingRect(item) 71 x = rect[0] 72 y = rect[1] 73 weight = rect[2] 74 height = rect[3] 75 if (weight > (height * 2.5)) and (weight < (height * 4)): 76 if height > 40 and height < 80: 77 image = origin_image[int(y*ratio): int((y + height)*ratio), int(x*ratio) : int((x + weight)*ratio)] 78 cv_show('image',image) 79 cv2.imwrite('chepai_09.jpg',image)
第三部分:車牌號碼分割:學習
1 # 導入所需模塊 2 import cv2 3 from matplotlib import pyplot as plt 4 import os 5 import numpy as np 6 7 # 顯示圖片 8 def cv_show(name,img): 9 cv2.imshow(name,img) 10 cv2.waitKey() 11 cv2.destroyAllWindows() 12 13 #車牌灰度化 14 chepai_image = cv2.imread('chepai_09.jpg') 15 image = cv2.GaussianBlur(chepai_image, (3, 3), 0) 16 gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) 17 cv_show('gray_image',gray_image) 18 19 ret, image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_OTSU) 20 cv_show('image',image) 21 22 #計算二值圖像黑白點的個數,處理綠牌照問題,讓車牌號碼始終爲白色 23 area_white = 0 24 area_black = 0 25 height, width = image.shape 26 for i in range(height): 27 for j in range(width): 28 if image[i, j] == 255: 29 area_white += 1 30 else: 31 area_black += 1 32 print(area_black,area_white) 33 if area_white > area_black: 34 ret, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV) 35 cv_show('image',image) 36 37 #繪製輪廓 38 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 5)) 39 image = cv2.dilate(image, kernel) 40 cv_show('image', image) 41 thresh, contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 42 cur_img = chepai_image.copy() 43 cv2.drawContours(cur_img,contours,-1,(0,0,255),3) 44 cv_show('img',cur_img) 45 words = [] 46 word_images = [] 47 print(len(contours)) 48 for item in contours: 49 word = [] 50 rect = cv2.boundingRect(item) 51 x = rect[0] 52 y = rect[1] 53 weight = rect[2] 54 height = rect[3] 55 word.append(x) 56 word.append(y) 57 word.append(weight) 58 word.append(height) 59 words.append(word) 60 words = sorted(words, key=lambda s:s[0], reverse=False) 61 print(words) 62 i = 0 63 for word in words: 64 if (word[3] > (word[2] * 1)) and (word[3] < (word[2] * 5)): 65 i = i + 1 66 splite_image = chepai_image[word[1]:word[1] + word[3], word[0]:word[0] + word[2]] 67 word_images.append(splite_image) 68 69 for i, j in enumerate(word_images): 70 cv_show('word_images[i]',word_images[i]) 71 cv2.imwrite("./chepai_09/0{}.png".format(i),word_images[i])
第四部分:字符匹配:ui
1 # 導入所需模塊 2 import cv2 3 from matplotlib import pyplot as plt 4 import os 5 import numpy as np 6 7 # 準備模板 8 template = ['0','1','2','3','4','5','6','7','8','9', 9 'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z', 10 '藏','川','鄂','甘','贛','貴','桂','黑','滬','吉','冀','津','晉','京','遼','魯','蒙','閩','寧', 11 '青','瓊','陝','蘇','皖','湘','新','渝','豫','粵','雲','浙'] 12 13 # 顯示圖片 14 def cv_show(name,img): 15 cv2.imshow(name,img) 16 cv2.waitKey() 17 cv2.destroyAllWindows() 18 19 # 讀取一個文件夾下的全部圖片,輸入參數是文件名,返回文件地址列表 20 def read_directory(directory_name): 21 referImg_list = [] 22 for filename in os.listdir(directory_name): 23 referImg_list.append(directory_name + "/" + filename) 24 return referImg_list 25 26 # 中文模板列表(只匹配車牌的第一個字符) 27 def get_chinese_words_list(): 28 chinese_words_list = [] 29 for i in range(34,64): 30 c_word = read_directory('./refer1/'+ template[i]) 31 chinese_words_list.append(c_word) 32 return chinese_words_list 33 34 #英文模板列表(只匹配車牌的第二個字符) 35 def get_english_words_list(): 36 eng_words_list = [] 37 for i in range(10,34): 38 e_word = read_directory('./refer1/'+ template[i]) 39 eng_words_list.append(e_word) 40 return eng_words_list 41 42 # 英文數字模板列表(匹配車牌後面的字符) 43 def get_eng_num_words_list(): 44 eng_num_words_list = [] 45 for i in range(0,34): 46 word = read_directory('./refer1/'+ template[i]) 47 eng_num_words_list.append(word) 48 return eng_num_words_list 49 50 #模版匹配 51 def template_matching(words_list): 52 if words_list == 'chinese_words_list': 53 template_words_list = chinese_words_list 54 first_num = 34 55 elif words_list == 'english_words_list': 56 template_words_list = english_words_list 57 first_num = 10 58 else: 59 template_words_list = eng_num_words_list 60 first_num = 0 61 best_score = [] 62 for template_word in template_words_list: 63 score = [] 64 for word in template_word: 65 template_img = cv2.imdecode(np.fromfile(word, dtype=np.uint8), 1) 66 template_img = cv2.cvtColor(template_img, cv2.COLOR_RGB2GRAY) 67 ret, template_img = cv2.threshold(template_img, 0, 255, cv2.THRESH_OTSU) 68 height, width = template_img.shape 69 image = image_.copy() 70 image = cv2.resize(image, (width, height)) 71 result = cv2.matchTemplate(image, template_img, cv2.TM_CCOEFF) 72 score.append(result[0][0]) 73 best_score.append(max(score)) 74 return template[first_num + best_score.index(max(best_score))] 75 76 referImg_list = read_directory("chepai_13") 77 chinese_words_list = get_chinese_words_list() 78 english_words_list = get_english_words_list() 79 eng_num_words_list = get_eng_num_words_list() 80 chepai_num = [] 81 82 #匹配第一個漢字 83 img = cv2.imread(referImg_list[0]) 84 # 高斯去噪 85 image = cv2.GaussianBlur(img, (3, 3), 0) 86 # 灰度處理 87 gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) 88 # 自適應閾值處理 89 ret, image_ = cv2.threshold(gray_image, 0, 255, cv2.THRESH_OTSU) 90 #第一個漢字匹配 91 chepai_num.append(template_matching('chinese_words_list')) 92 print(chepai_num[0]) 93 94 #匹配第二個英文字母 95 img = cv2.imread(referImg_list[1]) 96 # 高斯去噪 97 image = cv2.GaussianBlur(img, (3, 3), 0) 98 # 灰度處理 99 gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) 100 # 自適應閾值處理 101 ret, image_ = cv2.threshold(gray_image, 0, 255, cv2.THRESH_OTSU) 102 #第二個英文字母匹配 103 chepai_num.append(template_matching('english_words_list')) 104 print(chepai_num[1]) 105 106 #匹配其他5個字母,數字 107 for i in range(2,7): 108 img = cv2.imread(referImg_list[i]) 109 # 高斯去噪 110 image = cv2.GaussianBlur(img, (3, 3), 0) 111 # 灰度處理 112 gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) 113 # 自適應閾值處理 114 ret, image_ = cv2.threshold(gray_image, 0, 255, cv2.THRESH_OTSU) 115 #其他字母匹配 116 chepai_num.append(template_matching('eng_num_words_list')) 117 print(chepai_num[i]) 118 print(chepai_num)