def digit_ocr_by_line_crossing(image, cell_corners, debug, image_drawn): points = adjust_cell_corners(image, cell_corners) if debug: for point in points: imageproc.draw_point(image_drawn, point) plu, pru, pld, prd = points points_left = interpolate_line(plu, pld, param_cross_num_lines) points_right = interpolate_line(pru, prd, param_cross_num_lines) points_up = interpolate_line(plu, pru, param_cross_num_lines) points_down = interpolate_line(pld, prd, param_cross_num_lines) hcrossings = [] vcrossings = [] for i in range(0, param_cross_num_lines): h = float(i) / (param_cross_num_lines - 1) hcrossings.append(crossings(image, points_left[i], points_right[i], h, debug, image_drawn)) vcrossings.append(crossings(image, points_up[i], points_down[i], h, debug, image_drawn)) if debug: print hcrossings print vcrossings return decide_digit(hcrossings, vcrossings, debug)
def crossings(image, p0, p1, h, debug = False, image_drawn = None): pixels = [] crossings = [] for x, y in walk_line(p0, p1): pixels.append(image[y, x] > 0) if debug: if image[y, x] > 0: color = (255, 255, 0, 0) else: color = (200, 200, 200, 0) imageproc.draw_point(image_drawn, (x, y), color, 0) # Filter the value sequence for i in range(1, len(pixels) - 1): if not pixels[i - 1] and not pixels[i + 1]: pixels[i] = False elif pixels[i - 1] and pixels[i + 1]: pixels[i] = True # detect crossings begin = None for i, value in enumerate(pixels): if begin is None: if value: begin = i else: if not value or i == len(pixels) - 1: end = i if value else i - 1 if end - begin > 0: n = len(pixels) begin_rel = float(begin) / n end_rel = float(end) / n center_rel = (begin_rel + end_rel) / 2 length = end - begin + 1 crossings.append((begin_rel, end_rel, center_rel, length, h)) begin = None return crossings