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 = [0.2, 0.8] # 车牌在水平方向得轮廓长度少于20%或多余80%过滤掉
char_imgs = []
cv2.imshow('car_plate', car_plate)
cv2.waitKey(0)
# 将二值化的车牌水平投影到Y轴,计算投影后的连续长度,连续投影长度可能不止一段
h_count = [0 for i in range(img_h)]
for row in range(img_h):
temp_cnt = 0
for col in range(img_w):
if car_plate[row, col] == 255:
temp_cnt += 1 # 计算每一排中像素值为255的数量
h_count[row] = temp_cnt
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))
# 选出最长的投影,该投影长度占整个截取车牌高度的比值必须大于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_maxIndex][0], h_proj_list[h_maxIndex][1]
plates = car_plate[chars_top:chars_bottom + 1, :]
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[chars_top:chars_bottom + 1, addr[0]:addr[1]]
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[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))
|