日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

opencv车牌照识别

發布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 opencv车牌照识别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

所用圖片:

代碼:01.py

import cv2 import numpy as np from matplotlib import pyplot as plt from utilsW.utils import cvShow, drawRectdef plt_show0(img):b, g, r = cv2.split(img)img = cv2.merge([r, g, b])plt.imshow(img)plt.show()def plt_show(img):plt.imshow(img)plt.show()if __name__ == '__main__':# 1, load imagesrc = cv2.imread("carcode.jpg")cvShow("7", src)# drawRect(src, 215, 222, 152, 50)# 2,高斯去噪gaussImg = cv2.GaussianBlur(src, (3, 3), 0)cvShow("gaussImg", gaussImg)#3,灰度化gray = cv2.cvtColor(gaussImg, cv2.COLOR_BGR2GRAY)#4,邊緣檢測sobel_x = cv2.Sobel(gray, cv2.CV_16S, 1, 0)absX = cv2.convertScaleAbs(sobel_x)image = absXcvShow("2", image)#5,自適應閾值處理ret, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU)cvShow("3",image)#6, 形態學手段,閉運算,讓白色部分凝練成整體kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (17,5))print(kernelX)image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernelX, iterations=3)cvShow("4",image)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, KernelY)image = cv2.erode(image, kernelX)image = cv2.dilate(image, KernelY)cvShow("5", image)image = cv2.medianBlur(image, 15)cvShow("6", image)#7,輪廓檢測contours = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]new = src.copy()#繪制輪廓cv2.drawContours(new, contours, -1, (0, 0, 255), 2)cvShow("7", new)#篩選出車牌位置的輪廓:for item in contours:rect = cv2.boundingRect(item)x = rect[0]y = rect[1]w = rect[2]h = rect[3]if y >=215 and y <250:image = src[y:y+h, x:x+w]cvShow("10", image)cv2.imwrite("test1.png", image)pass

代碼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车牌照识别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。