opencv车牌照识别
生活随笔
收集整理的這篇文章主要介紹了
opencv车牌照识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
所用圖片:
代碼:01.py
代碼2:02.py
import cv2 import cv2 as cv import numpy as np from utilsW.utils import cvShowif __name__ == '__main__':#1, load imagesrc = cv.imread("test1.png")cvShow("src", src)#2, gauss to dry and to graygauss = cv.GaussianBlur(src, (3, 3), 0)gray = cv.cvtColor(gauss, cv2.COLOR_BGR2GRAY)cvShow("gray", gray)#3, process for threshold thresholdImg = cv.threshold(gray, 0, 255, cv.THRESH_OTSU)[1]cvShow("thresholdImg", thresholdImg)#4, process for thresholdarea_white, area_black = 0, 0h, w = thresholdImg.shapeprint(thresholdImg.shape)for i in range(h):for j in range(w):if thresholdImg[i, j] == 255:area_white +=1else:area_black +=1if area_white > area_black:thresholdImg = cv.threshold(gray, 0, 255, cv.THRESH_OTSU | cv.THRESH_BINARY_INV)[1]cvShow("thresholdImg", thresholdImg)#5, process for morph cvetkernel = cv.getStructuringElement(cv.MORPH_RECT, (2,2))dilateImg = cv.dilate(thresholdImg, kernel)cvShow("dilateImg", dilateImg)#6, find contourscontours = cv.findContours(dilateImg, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)[0]srcCopy = src.copy()cv.drawContours(srcCopy, contours, -1, (0,0,255),2)cvShow("drawContours", srcCopy)# finf every sigal character postionwords = []for c in contours:word = []rect = list(cv2.boundingRect(c))words.append(rect)words = sorted(words, key = lambda s:s[0], reverse=False)print(words)i = 0for word in words:if(word[3] > word[2] * 1.8) and (word[3] < (word[2] * 3.5)):i = i + 1iamge = src[word[1]:word[1]+word[3], word[0]:word[0]+word[2]]cvShow("12", iamge)其中工具函數代碼:utils.py
# @time: 2022/1/6 11:06 # @Author: wangshubo # @File: utilsW.py # @description: 封裝的工具函數 # @author_email: '971490321@qq.com' import cv2 as cv import numpy as npdef cvShow(name, img):cv.imshow(name, img)cv.waitKey(0)cv.destroyAllWindows()#計算兩點距離之和 def CalDistance(pt1, pt2):x1, y1, x2, y2 = pt1[0], pt1[1], pt2[0], pt2[1]distance = np.sqrt(((y2 - y1) ** 2) + ((x2 - x1) ** 2))return distance# 計算列表中元素之和 def listSum(list):total = 0ele = 0while (ele < len(list)):total = total + list[ele]ele += 1return total#畫矩形,第一個參數必須是3通道的 def drawRect(image, x, y, w, h):pt1, pt2 = (x, y), (x + w, y + h)cv.rectangle(image, pt1, pt2, (0, 0, 255), 2)cvShow("image", image)其中01.py保存的圖片用于02.py
總結:傳統圖像處理步驟:
1,導入圖像src
2,濾波去噪,灰度化
3,閾值函數進行處理
4,進行形態學操作,好找輪廓
5,找到輪廓,進行輪廓排序
6,畫上輪廓
7,挑選輪廓
8,找到輪廓坐標,然后定位原圖像src其中的具體位置。
9,進行matchTemplate比較
總結
以上是生活随笔為你收集整理的opencv车牌照识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js 车牌号判断
- 下一篇: vue在开发环境中配置本地hosts修改