def drop_over(over_canny, over_width): over_canny_width = np.shape(over_canny)[1] over_left = over_width / 2 over_right = over_width - over_left eliminate_pic_01 = fn.deal_pixel_01(over_canny) shadow_x = fn.picshadow_x(eliminate_pic_01) shadow_y = fn.picshadow_y(eliminate_pic_01) # cv2.imshow('canny' + str(over_left), over_canny) for i in range(over_canny_width): if shadow_x[i] > height - 4: over_canny[:, i] = 0 for i in range(height): if shadow_y[i] > over_canny_width - 1: over_canny[i, :] = 0 eliminate_pic_01 = fn.deal_pixel_01(over_canny) shadow_x = fn.picshadow_x(eliminate_pic_01) # print(int(np.sum(shadow_x[: over_width])), int(np.sum(shadow_x[-over_width :]))) # print((height - 1) * over_width) if np.sum(shadow_x) > (height - 1.5) * over_canny_width: # 去除线段后接近黑图 return 0 if int(np.sum(shadow_x[:over_width])) < (height - 1) * over_width and int( np.sum(shadow_x[-over_width:])) > (height - 2) * over_width: # 右边黑色像素远大出左边 return 1 if int(np.sum(shadow_x[-over_width:])) < (height - 1) * over_width and int( np.sum(shadow_x[:over_width])) > (height - 2) * over_width: # 左边黑色像素远大出右边 return 2 if over_canny_width > 1.5 * thre: eliminate_pic_01 = fn.deal_pixel_01(over_canny) again_list = etp.find_singledigital(shadow_x, over_canny_width) return again_list else: return 3
def drop_little(eliminate_pic): eliminate_pic_01 = fn.deal_pixel_01(eliminate_pic) shadow_x = fn.picshadow_x(eliminate_pic_01) thre = height - 3 for i in range(width): if shadow_x[i] > thre: eliminate_pic[:, i] = 0 return eliminate_pic
if shadow_pic[i] < height: single_start = i is_start = False else: if shadow_pic[i] == height: single_end = i is_start = True if single_end - single_start > thre: single_digital_list.append([single_start, single_end]) return single_digital_list num_img2gray = cv2.cvtColor(num_img, cv2.COLOR_RGB2GRAY) threshold = cv2.adaptiveThreshold(num_img2gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 5) for i in range(np.shape(threshold)[1]): for j in range(np.shape(threshold)[0]): if threshold[j, i] == 0: threshold[j, i] = 255 else: threshold[j, i] = 0 cv2.imshow('threshold', threshold) drop_num = drop_little(threshold) cv2.imshow('drop_num', drop_num) eliminate_pic_01 = fn.deal_pixel_01(drop_num) shadow_x = fn.picshadow_x(eliminate_pic_01) digital_list = find_singledigital(shadow_x) cv2.waitKey()