传统方法实现车牌检测

数据准备

refer2数据集

环境配置

opencv-python                4.10.0.84

代码实现

第一部分(实现车牌处理)

具体流程:  转灰度    边缘算法     腐蚀膨胀     边框判断

import cv2

old_img = cv2.imread('che1.png')
temp_img = old_img.copy()
# cv2.imshow('img',old_img)
# cv2.waitKey(1000)
img = old_img.copy()
img = cv2.GaussianBlur(img,(3,3),0)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

sobel_x = cv2.Sobel(img_gray,cv2.CV_16S,1,0)
img_edge = cv2.convertScaleAbs(sobel_x)

_, img = cv2.threshold(img_edge,0,255,cv2.THRESH_OTSU)
kernelX = cv2.getStructuringElement(cv2.MORPH_RECT,(30,10))
img = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernelX,iterations=1)

kernelX = cv2.getStructuringElement(cv2.MORPH_RECT,(50,1))
kernelY = cv2.getStructuringElement(cv2.MORPH_RECT,(1,20))
img = cv2.dilate(img,kernelX)
img = cv2.erode(img,kernelX)
img = cv2.erode(img,kernelY)
img = cv2.dilate(img,kernelY)
img = cv2.medianBlur(img,21)

contours, _ = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# for cnt in contours:
#     print(cnt)
#     for cntt in cnt:
#         x,y = cntt[0]
#         cv2.circle(old_img,(x,y),5,(0,0,255),-1)
boxes = []
for contour in contours:
    rect = cv2.boundingRect(contour)
    x,y,w,h = rect
    if w*h < 100: continue
    if (w>h*2) and (w<h*4):
        boxes.append([x,y,w,h])
        cv2.rectangle(old_img, (x,y), (x+w, y+h), (255, 0, 0), 2)
boxes.sort(key=lambda i:i[1], reverse=True)
cv2.imshow('img',old_img)
cv2.waitKey()
x,y,w,h = boxes[0]
img_crop = temp_img[y:y+h,x:x+w]
old_crop = img_crop.copy()
backup_crop = img_crop.copy()
cv2.imshow('img',img_crop)
cv2.waitKey()
img_crop = cv2.GaussianBlur(img_crop,(3,3),0)
crop_gray = cv2.cvtColor(img_crop, cv2.COLOR_BGR2GRAY)
_, crop_bin = cv2.threshold(crop_gray,0,255,cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(2,2))
crop_bin = cv2.dilate(crop_bin,kernel)
cv2.imshow('img',crop_bin)
cv2.waitKey()
contours, _ = cv2.findContours(crop_bin,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
zi_box = []
for contour in contours:
    rect = cv2.boundingRect(contour)
    x,y,w,h = rect
    # if w*h < 200 or w*h>3000: continue
    if (h>1.5*w) and (h<3*w):
        zi_box.append([x,y,w,h])
        cv2.rectangle(old_crop, (x,y), (x+w, y+h), (0, 0, 255), 2)
cv2.imshow('img',old_crop)
cv2.waitKey()
zi_box.sort(key=lambda i:i[0],reverse=False)
print(zi_box)

x,y,w,h= zi_box[3]
zi_crop = backup_crop[y:y+h,x:x+w]
print(zi_crop.shape)
cv2.imwrite('9.jpg',zi_crop)
cv2.imshow('img',zi_crop)
cv2.waitKey(5000)
cv2.destroyAllWindows()

第二部分(实现模版匹配)

import os
import cv2

template_dir = './refer2'
template_body = []
for cls_name in os.listdir(template_dir):
    template_body.extend([[cls_name,template_dir+'/'+cls_name+'/'+i] for i in os.listdir(template_dir+'/'+cls_name)])
print(template_body[0])
img = cv2.imread('H.jpg',cv2.COLOR_BGR2GRAY)
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
w,h = img.shape
for index,item in enumerate(template_body):
    template = cv2.imread(item[1])
    template = cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)
    template = cv2.resize(template,(h,w))
    score = cv2.matchTemplate(img,template,cv2.TM_CCOEFF)[0,0]
    template_body[index].insert(0,score)
#     print(template)
# print(template_body[0])
template_body.sort(key=lambda i:i[0],reverse=True)
print(template_body[0:3])

实现流程图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值