Demo entry 6745441

detect_roughly

   

Submitted by anonymous on May 27, 2018 at 05:28
Language: Python 3. Code size: 2.1 kB.

def detect_roughly(img, h_scaled=720, scale_factor=1.08, v_padding_rate=0.05):
    """车牌粗定位"""
    if v_padding_rate > 0.2:
        print("error:v_padding_rate > 0.2:", v_padding_rate)
        exit(1)
    # 高和宽
    h_, w_ = img.shape[0], img.shape[1]
    aspect_ratio = w_ / h_  # 尺度:宽高比
    w_scaled = int(aspect_ratio*h_scaled)  # 缩放后的宽度
    padding = int(h_*v_padding_rate)  # 填充高度
    # 按宽高比等比例放缩到 宽:720。比例必须为整数之比
    img_scaled = cv2.resize(img, (w_scaled, h_scaled))
    cv2.imwrite(dd+"img_scaled.jpg", img_scaled)
    # 裁剪上下边界:上: 下, 左: 右(左右边界不变)
    cropped0 = img_scaled[padding : h_scaled-padding, 0 : w_]
    cv2.imwrite(dd+"cropped0.jpg", cropped0)
    # 灰度转换
    cropped0_g = cv2.cvtColor(cropped0, cv2.COLOR_RGB2GRAY)  
    cv2.imwrite(dd+"cropped0_g.jpg", cropped0_g)
    # 分类器
    classfier = cv2.CascadeClassifier("model/cascade.xml")
    # scale_factor 图像缩放比例
    # minNeighbors:对特征检测点周边多少有效点同时检测,这样可避免因选取的特征检测点太小而导致遗漏
    # minSize:特征检测点的最小值
    detecting = classfier.detectMultiScale(cropped0_g, scale_factor, minNeighbors=2, minSize=(36, 9), maxSize=(36*40, 9*40))
    # detecting 的结果是所有满足条件被筛选出来的区域图片组成的
    cropped_images = []
    for (x, y, w, h) in detecting:
        cropped = crop_img(cropped0, (int(x), int(y), int(w), int(h)))
        print('Candidate region: x={}, y={}, w={}, h={}'.format(x,y,w,h))
        # 保存各区域图像
        cv2.imwrite(dd+"{}.jpg".format(str(uuid1())), cropped)
        # 对粗定位出来的车牌区域进行拓展
        x -= w * 0.14
        w += w * 0.28
        y -= h * 0.15
        h += h * 0.3
        # 对原彩色图像中符合条件区域进行裁剪
        extended = crop_img(cropped0, (int(x), int(y), int(w), int(h)))
        print('Extended region: x={}, y={}, w={}, h={}'.format(x,y,w,h))
        # 保存各区域图像
        cv2.imwrite(dd+"{}.jpg".format(str(uuid1())), extended)
        # 以每处区域的图像数组和图像裁剪坐标信息为一数组,添加至列表
        cv2.rectangle(cropped0, (int(x),int(y)), (int(x+w),int(y+h)), (0, 0, 255), 2, cv2.LINE_AA)
        cropped_images.append([extended,[x, y+padding, w, h]])
    cv2.imwrite(dd+"rected.jpg", cropped0)
    return cropped_images

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).