车牌识别---车牌定位及分割

#-*- coding: utf-8 -*-
import cv2
import numpy as np

def Process(img):
    gaussian = cv2.GaussianBlur(img,(3,3),0,0,cv2.BORDER_DEFAULT)       # 高斯平滑
    median = cv2.medianBlur(gaussian,5)         # 中值滤波
    sobel = cv2.Sobel(median,cv2.CV_8U,1,0,ksize=3)         # Sobel算子

    ret,binary = cv2.threshold(sobel,175,255,cv2.THRESH_BINARY)         #灰度值小于175的点置0,灰度值大于175的点置255
	#最后的参数是阈值类型,对应一个公式,一般用这个就可以

    element1 = cv2.getStructuringElement(cv2.MORPH_RECT,(9,1))      # 膨胀和腐蚀操作的核函数
    element2 = cv2.getStructuringElement(cv2.MORPH_RECT,(9,7))

    dilation = cv2.dilate(binary,element2,iterations=1)         # 膨胀一次,让轮廓突出
    erosion = cv2.erode(dilation,element1,iterations=1)         # 腐蚀一次,去掉细小杂点
    dilation2 = cv2.dilate(erosion,element2,iterations=3)       # 再次膨胀,让轮廓更明显

    # 存储中间图片
    cv2.imwrite("binary.png",binary)
    cv2.imwrite("dilation.png",dilation)
    cv2.imwrite("erosion.png",erosion)
    cv2.imwrite("dilation2.png",dilation2)
    return dilation2

def GetRegion(img):
    regions = []
    # 查找轮廓
    _,contours,hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # 筛选面积小的
    for contour in contours:
        # 计算该轮廓的面积
        area = cv2.contourArea(contour)
        # 面积小的都筛选掉
        if (area < 2000):
            continue
        # 轮廓近似,作用很小
        epslion = 1e-3 * cv2.arcLength(contour,True)
        approx = cv2.approxPolyDP(contour,epslion,True)
        # epsilon,是从轮廓到近似轮廓的最大距离。是一个准确率参数,好的epsilon的选择可以得到正确的输出。True决定曲线是否闭合。
        # 找到最小的矩形,该矩形可能有方向
        rect = cv2.minAreaRect(contour)
        # box是四个点的坐标
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        ## 计算高和宽
        height = abs(box[0][1] - box[2][1])
        weight = abs(box[0][0] - box[2][0])
        # 车牌正常情况下长高比在2-5之间
        radio = float(weight) / float(height)
        if (radio > 2 and radio <5.5):
            regions.append(box)
    return regions


def detect(img):
    # 灰度化
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    # 预处理及形态学处理,得到可以查找矩形的图片
    prc = Process(gray)
    # 得到车牌轮廓
    regions = GetRegion(prc)
    print('[INFO]:Detct %d license plates' % len(regions))
    # 用绿线画出这些找到的轮廓
    for box in regions:
        cv2.drawContours(img,[box],0,(0,255,0),3)
        cv2.imshow('Result', img)
        # 保存结果文件名
        cv2.imwrite('result.jpg', img)
        cv2.waitKey(0)

        # 切割图片  将标出的图片切割出来
        x, y, w, h = cv2.boundingRect(box)

        rect = (x, y, w, h)
        mask = np.zeros(img.shape[:2], np.uint8)
        bgdModel = np.zeros((1, 65), np.float64)
        fgdModel = np.zeros((1, 65), np.float64)
        cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)
        mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
        img = img * mask2[:, :, np.newaxis]
        cv2.imshow('l', img)
        # 保存结果文件名
        cv2.imwrite('l.jpg', img)
        cv2.waitKey(0)

if __name__=='__main__':
    # 输入的参数为图片的路径
    img = cv2.imread('/home/xiaoshiguang/PycharmProjects/untitled/venv/1/1.jpg')
    detect(img)

车牌识别---车牌定位及分割_第1张图片车牌识别---车牌定位及分割_第2张图片

你可能感兴趣的:(深度学习,机器学习,opencv)