# 车牌识别
import cv2 as cv
import numpy as np
import pytesseract as tess
from PIL import Image
def license_prepation(image):
image_hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV) # 将图像转换为hsv色彩空间
low_hsv = np.array([108, 43, 46]) # 设置颜色
high_hsv = np.array([124, 255, 255])
mask = cv.inRange(image_hsv, lowerb=low_hsv, upperb=high_hsv) # cv.inrange 函数用来追踪image
image_dst = cv.bitwise_and(image, image, mask=mask) # 取frame与mask中不为0的相与,mask=mask必须有
# cv.imshow('license_dst', image_dst)
image_blur = cv.GaussianBlur(image_dst, (7, 7), 0)
# cv.imshow('license_blur',image_blur)
image_gray = cv.cvtColor(image_blur, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(image_gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
# cv.imshow('binary', binary)
kernel1 = cv.getStructuringElement(cv.MORPH_RECT, (4, 6))
image_opened = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel1)
# cv.imshow('license_opened', image_opened)
kernel2 = cv.getStructuringElement(cv.MORPH_RECT, (7, 7))
image_closed = cv.morphologyEx(image_opened, cv.MORPH_CLOSE, kernel2)
# cv.imshow('license_closed', image_closed)
return image_closed
def choose_license_area(contours, Min_Area):
temp_contours = []
for contour in contours:
if cv.contourArea(contour) > Min_Area: # 面积大于MIN_AREA的区域保留
temp_contours.append(contour)
license_area = []
for temp_contour in temp_contours:
rect_tupple = cv.minAreaRect(temp_contour)
# print(rect_tupple)
rect_width, rect_height = rect_tupple[1] # 0为中心点,1为长和宽,2为角度
if rect_width < rect_height:
rect_width, rect_height = rect_height, rect_width
aspect_ratio = rect_width / rect_height
# 车牌正常情况下宽高比在2 - 5.5之间
if aspect_ratio > 2 and aspect_ratio < 5.5:
license_area.append(temp_contour)
rect_vertices = cv.boxPoints(rect_tupple)
rect_vertices = np.int0(rect_vertices)
return license_area
def license_segment(license_area):
if (len(license_area)) == 1:
for car_plate in license_area:
# print(car_plate.shape)
# print(car_plate)
row_min, col_min = np.min(car_plate[:, 0, :], axis=0) # 行是row 列是col
row_max, col_max = np.max(car_plate[:, 0, :], axis=0)
# cv.rectangle(license, (row_min, col_min), (row_max, col_max), (0, 255, 0),2)
card_img = license[col_min:col_max, row_min:row_max, :]
# cv.imshow("card_img", card_img)
cv.imwrite("card_img.jpg", card_img)
return card_img
def recognize_text(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 120, 255, cv.THRESH_BINARY_INV)
cv.imshow('bin', binary) # 白底黑字
bin1 = cv.resize(binary, (370, 82))
kernel1 = cv.getStructuringElement(cv.MORPH_RECT, (2, 5))
dilated = cv.dilate(bin1, kernel1) # 白色膨胀
text = tess.image_to_string(dilated, lang='chi_sim')
print('识别的结果为:%s' % text)
src = cv.imread("D:/opencv/image/license.png") # 打开要识别的照片,不能有中文路径
# print(src.shape)
license = cv.resize(src, (800, int(800 * src.shape[0] / src.shape[1])))
# print(license.shape)
cv.namedWindow('inputImage', 0)
cv.imshow('inputImage', license)
license_prepared = license_prepation(license)
# cv.imshow('license_prepared', license_prepared)
contours, hierarchy = cv.findContours(license_prepared, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
Min_Area = 2000
license_area = choose_license_area(contours, Min_Area)
result = license_segment(license_area)
cv.imshow('result', result)
recognize_text(result)
cv.waitKey(0)
cv.destroyAllWindows()
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
license recognization.rar (3个子文件)
CSDN
license.png 5.29MB
license recognition_2.py 4KB
chi_sim.traineddata 50.22MB
共 3 条
- 1
资源评论
啥都不会的小王
- 粉丝: 14
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功