愿你 发表于 2020-4-11 16:04:10

求解析一段python代码~

附在评论里~

永恒的蓝色梦想 发表于 2020-4-11 16:07:44

e,代码?

Hello. 发表于 2020-4-11 16:09:11

hello?、、

愿你 发表于 2020-4-11 16:09:13

def get_chars(car_plate):
    img_h, img_w = car_plate.shape[:2]
    h_proj_list = []# 水平投影长度列表
    h_temp_len, v_temp_len = 0, 0
    h_startIndex, h_end_index = 0, 0# 水平投影记索引
    h_proj_limit = # 车牌在水平方向得轮廓长度少于20%或多余80%过滤掉
    char_imgs = []
    cv2.imshow('car_plate', car_plate)
    cv2.waitKey(0)
    # 将二值化的车牌水平投影到Y轴,计算投影后的连续长度,连续投影长度可能不止一段
    h_count =
    for row in range(img_h):
      temp_cnt = 0
      for col in range(img_w):
            if car_plate == 255:
                temp_cnt += 1# 计算每一排中像素值为255的数量
      h_count = temp_cnt
      if temp_cnt / img_w < h_proj_limit or temp_cnt / img_w > h_proj_limit:#每一行像素值为255的像素个数/车牌宽度<0.2 或者 每一行像素值为255的像素个数/车牌宽度>0.8
            if h_temp_len != 0:
                h_end_index = row - 1
                h_proj_list.append((h_startIndex, h_end_index))
                h_temp_len = 0
            continue
      if temp_cnt > 0:
            if h_temp_len == 0:
                h_startIndex = row # 从0.2<(像素值为255的像素个数/img_w)<0.8 的行开始
                h_temp_len = 1
            else:
                h_temp_len += 1
      else:
            if h_temp_len > 0:
                h_end_index = row - 1
                h_proj_list.append((h_startIndex, h_end_index))
                h_temp_len = 0

    # 手动结束最后得水平投影长度累加
    if h_temp_len != 0:
      h_end_index = img_h - 1
      h_proj_list.append((h_startIndex, h_end_index))
    # 选出最长的投影,该投影长度占整个截取车牌高度的比值必须大于0.5
    h_maxIndex, h_maxHeight = 0, 0
    for i, (start, end) in enumerate(h_proj_list):
      if h_maxHeight < (end - start):
            h_maxHeight = (end - start)
            h_maxIndex = i
    if h_maxHeight / img_h < 0.5:
      return char_imgs
    chars_top, chars_bottom = h_proj_list, h_proj_list

    plates = car_plate
    cv2.imwrite('carIdentityData/opencv_output/car.jpg', car_plate)
    cv2.imwrite('carIdentityData/opencv_output/plate.jpg', plates)
    char_addr_list = horizontal_cut_chars(plates)
    for i, addr in enumerate(char_addr_list):
      char_img = car_plate:addr]
      char_img = cv2.resize(char_img, (char_w, char_h))
      char_imgs.append(char_img)
    return char_imgs


def extract_char(car_plate):
    gray_plate = cv2.cvtColor(car_plate, cv2.COLOR_BGR2GRAY)
    # cv2.imshow('gray_plate', gray_plate)
    # cv2.waitKey(0)
    ret, binary_plate = cv2.threshold(gray_plate, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    # print(ret)
    # cv2.imshow('binary_plate', binary_plate)
    # cv2.waitKey(0)
    char_img_list = get_chars(binary_plate)
    return char_img_list

传入extract_char函数的是一张汽车牌照的二值图(36*136的黑底白字),知道这两个函数的大概作用是先去除上下汽车牌照的空余部分,再进行切割字符。想着重问问以下这段代码的作用 实在是不太理解
if temp_cnt / img_w < h_proj_limit or temp_cnt / img_w > h_proj_limit:#每一行像素值为255的像素个数/车牌宽度<0.2 或者 每一行像素值为255的像素个数/车牌宽度>0.8
            if h_temp_len != 0:
                h_end_index = row - 1
                h_proj_list.append((h_startIndex, h_end_index))
                h_temp_len = 0
            continue
      if temp_cnt > 0:
            if h_temp_len == 0:
                h_startIndex = row # 从0.2<(像素值为255的像素个数/img_w)<0.8 的行开始
                h_temp_len = 1
            else:
                h_temp_len += 1
      else:
            if h_temp_len > 0:
                h_end_index = row - 1
                h_proj_list.append((h_startIndex, h_end_index))
                h_temp_len = 0

    # 手动结束最后得水平投影长度累加
    if h_temp_len != 0:
      h_end_index = img_h - 1
      h_proj_list.append((h_startIndex, h_end_index))

墨羽岚 发表于 2020-4-11 16:16:27

emmm代码是可以放在主楼里的。。

愿你 发表于 2020-4-11 16:33:04

墨羽岚 发表于 2020-4-11 16:16
emmm代码是可以放在主楼里的。。

可是我点的时候一直没反应 然后就不得以以回复的形式发了{:10_285:}
页: [1]
查看完整版本: 求解析一段python代码~