基于python+OpenCV的车牌号码识别

基于python+OpenCV的车牌号码识别

车牌识别行业已具备一定的市场规模,在电子警察、公路卡口、停车场、商业管理、汽修服务等领域已取得了部分应用。一个典型的车辆牌照识别系统一般包括以下4个部分:车辆图像获取、车牌定位、车牌字符分割和车牌字符识别

1、车牌定位的主要工作是从获取的车辆图像中找到汽车牌照所在位置,并把车牌从该区域中准确地分割出来
这里所采用的是利用车牌的颜色(黄色、蓝色、绿色) 来进行定位

#定位车牌
def color_position(img,output_path):
    colors = [([26,43,46], [34,255,255]),  # 黄色
              ([100,43,46], [124,255,255]),  # 蓝色
              ([35, 43, 46], [77, 255, 255])  # 绿色
              ]
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    for (lower, upper) in colors:
        lower = np.array(lower, dtype="uint8")  # 颜色下限
        upper = np.array(upper, dtype="uint8")  # 颜色上限

        # 根据阈值找到对应的颜色
        mask = cv2.inRange(hsv, lowerb=lower, upperb=upper)
        output = cv2.bitwise_and(img, img, mask=mask)
        k = mark_zone_color(output,output_path)
        if k==1:
            return 1
        # 展示图片
        #cv2.imshow("image", img)
        #cv2.imshow("image-color", output)
        #cv2.waitKey(0)
    return 0

基于python+OpenCV的车牌号码识别_第1张图片

2、将车牌提取出来

def mark_zone_color(src_img,output_img):
    #根据颜色在原始图像上标记
    #转灰度
    gray = cv2.cvtColor(src_img,cv2.COLOR_BGR2GRAY)

    #图像二值化
    ret,binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY)
    #轮廓检测
    x,contours,hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    #drawing = img
    #cv2.drawContours(drawing, contours, -1, (0, 0, 255), 3)  # 填充轮廓颜色
    #cv2.imshow('drawing', drawing)
    #cv2.waitKey(0)
    #print(contours)
    
    temp_contours = []  # 存储合理的轮廓
    car_plates=[]
    if len(contours)>0:
        for contour in contours:
            if cv2.contourArea(contour) > Min_Area:
                temp_contours.append(contour)
            car_plates = []
            for temp_contour in temp_contours:
                rect_tupple = cv2.minAreaRect(temp_contour)
                rect_width, rect_height = rect_tupple[1]
                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:
                    car_plates.append(temp_contour)
                    rect_vertices = cv2.boxPoints(rect_tupple)
                    rect_vertices = np.int0(rect_vertices)
            if len(car_plates)==1:
                oldimg = cv2.drawContours(img, [rect_vertices], -1, (0, 0, 255), 2)
                #cv2.imshow("che pai ding wei", oldimg)
                # print(rect_tupple)
                break

    #把车牌号截取出来
    if len(car_plates)==1:
        for car_plate in car_plates:
            row_min,col_min = np.min(car_plate[:,0,:],axis=0)
            row_max,col_max = np.max(car_plate[:,0,:],axis=0)
            cv2.rectangle(img,(row_min,col_min),(row_max,col_max),(0,255,0),2)
            card_img = img[col_min:col_max,row_min:row_max,:]
            cv2.imshow("img",img)
        cv2.imwrite(output_img + '/' + 'card_img' + '.jpg',card_img)
        cv2.imshow("card_img.",card_img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        return 1
    return 0

基于python+OpenCV的车牌号码识别_第2张图片

你可能感兴趣的:(python,OpenCV,车牌定位)