鱼C论坛

 找回密码
 立即注册
查看: 679|回复: 5

求解析一段python代码~

[复制链接]
发表于 2020-4-11 16:04:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
附在评论里~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-11 16:07:44 | 显示全部楼层
e,代码?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-11 16:09:11 | 显示全部楼层
hello?、、
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-11 16:09:13 | 显示全部楼层
  1. def get_chars(car_plate):
  2.     img_h, img_w = car_plate.shape[:2]
  3.     h_proj_list = []  # 水平投影长度列表
  4.     h_temp_len, v_temp_len = 0, 0
  5.     h_startIndex, h_end_index = 0, 0  # 水平投影记索引
  6.     h_proj_limit = [0.2, 0.8]  # 车牌在水平方向得轮廓长度少于20%或多余80%过滤掉
  7.     char_imgs = []
  8.     cv2.imshow('car_plate', car_plate)
  9.     cv2.waitKey(0)
  10.     # 将二值化的车牌水平投影到Y轴,计算投影后的连续长度,连续投影长度可能不止一段
  11.     h_count = [0 for i in range(img_h)]
  12.     for row in range(img_h):
  13.         temp_cnt = 0
  14.         for col in range(img_w):
  15.             if car_plate[row, col] == 255:
  16.                 temp_cnt += 1  # 计算每一排中像素值为255的数量
  17.         h_count[row] = temp_cnt
  18.         if temp_cnt / img_w < h_proj_limit[0] or temp_cnt / img_w > h_proj_limit[1]:#每一行像素值为255的像素个数/车牌宽度<0.2 或者 每一行像素值为255的像素个数/车牌宽度>0.8
  19.             if h_temp_len != 0:
  20.                 h_end_index = row - 1
  21.                 h_proj_list.append((h_startIndex, h_end_index))
  22.                 h_temp_len = 0
  23.             continue
  24.         if temp_cnt > 0:
  25.             if h_temp_len == 0:
  26.                 h_startIndex = row # 从0.2<(像素值为255的像素个数/img_w)<0.8 的行开始
  27.                 h_temp_len = 1
  28.             else:
  29.                 h_temp_len += 1
  30.         else:
  31.             if h_temp_len > 0:
  32.                 h_end_index = row - 1
  33.                 h_proj_list.append((h_startIndex, h_end_index))
  34.                 h_temp_len = 0

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

  48.     plates = car_plate[chars_top:chars_bottom + 1, :]
  49.     cv2.imwrite('carIdentityData/opencv_output/car.jpg', car_plate)
  50.     cv2.imwrite('carIdentityData/opencv_output/plate.jpg', plates)
  51.     char_addr_list = horizontal_cut_chars(plates)
  52.     for i, addr in enumerate(char_addr_list):
  53.         char_img = car_plate[chars_top:chars_bottom + 1, addr[0]:addr[1]]
  54.         char_img = cv2.resize(char_img, (char_w, char_h))
  55.         char_imgs.append(char_img)
  56.     return char_imgs


  57. def extract_char(car_plate):
  58.     gray_plate = cv2.cvtColor(car_plate, cv2.COLOR_BGR2GRAY)
  59.     # cv2.imshow('gray_plate', gray_plate)
  60.     # cv2.waitKey(0)
  61.     ret, binary_plate = cv2.threshold(gray_plate, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  62.     # print(ret)
  63.     # cv2.imshow('binary_plate', binary_plate)
  64.     # cv2.waitKey(0)
  65.     char_img_list = get_chars(binary_plate)
  66.     return char_img_list
复制代码


传入extract_char函数的是一张汽车牌照的二值图(36*136的黑底白字),知道这两个函数的大概作用是先去除上下汽车牌照的空余部分,再进行切割字符。想着重问问以下这段代码的作用 实在是不太理解
  if temp_cnt / img_w < h_proj_limit[0] or temp_cnt / img_w > h_proj_limit[1]:#每一行像素值为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))
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-11 16:16:27 | 显示全部楼层
emmm代码是可以放在主楼里的。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-11 16:33:04 | 显示全部楼层
墨羽岚 发表于 2020-4-11 16:16
emmm代码是可以放在主楼里的。。

可是我点的时候一直没反应 然后就不得以以回复的形式发了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-12 22:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表