def template_match(image, path): template = cv2.imread(path, cv2.COLOR_BGR2GRAY) template = convert_to_binary_and_invert(template) if (image.shape[0] < template.shape[0] or image.shape[1] < template.shape[1]): return [], 0 img = image.copy() w, h = template.shape[::-1] recv2.lines = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED) threshold = 0.65 loc = np.where(res >= threshold) # print("template width", template.shape[1]) points = [] for pt in zip(*loc[::-1]): if (len(points) > 0): if (pt[0] - points[-1] < template.shape[1]): print("here") continue # cv2.line(img, (pt[0], 0), (pt[0], img.shape[0]), (255, 255, 255), 1) # cv2.line(img, (pt[0] + template.shape[1], 0), (pt[0] + template.shape[1], img.shape[0]), (255, 255, 255), 1) # noqa # cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (255, 255, 255), 2) points.append(pt[0]) # display_image('res.png', img) return points, template.shape[1]
def process_image(line_segmets_path, input_path, f, acc_char_map, train): image = cv2.imread(os.path.join(input_path, f)) # display_image("source", image) start_time = time.time() processed_image = convert_to_binary_and_invert(image) processed_image = deskew(processed_image) # display_image("after deskew", processed_image) # cv2.imwrite("binary.png", processed_image) line_segmets_path = os.path.join(line_segmets_path, f[:-4]) lines = segment_lines(processed_image, line_segmets_path, 0) curr_ww, curr_tw, acc_char_map, end_time = segment_words( lines, line_segmets_path, f, input_path, train, acc_char_map) # noqa if (train): print(f'we got {curr_ww} wrong out of {curr_tw}') return curr_ww, curr_tw, acc_char_map, end_time - start_time
default="./segmented_lines") ap.add_argument("-i", "--input-path", required=False, help="path to line segments file", default="./inputs") args = vars(ap.parse_args()) print(args) input_path = args["input_path"] line_segmets_path = args["line_segments_path"] image = cv2.imread("./segmented_lines/csep1622/segment_1/segment_41.png") display_image("source", image) processed_image = convert_to_binary_and_invert(image) # cv2.imwrite("binary.png", processed_image) image = processed_image.copy() # original copy of the image img_line = processed_image.copy() edged = processed_image # cv2.imwrite("img_cnt.png", edged) hp = get_horizontal_projection(edged) baseline = get_baseline_y_coord(get_horizontal_projection(processed_image)) print("now baseline is: ", baseline) h, w = processed_image.shape cv2.line(img_line, (0, baseline), (w, baseline), (255, 255, 255), 1) # cv2.imwrite("baseline.png", img_line)
def segment_words(path): """ this function keeps the list of word separatation points in word_separation list but segments into sub words and saves the sub words segements in their designated directory """ files = [ f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) ] image = cv2.imread(os.path.join(path, files[0])) directory_name = path + "/" + files[0][:-4] if os.path.exists(directory_name): shutil.rmtree(directory_name) os.makedirs(directory_name) original_image = image.copy() image = convert_to_binary_and_invert(image) # image_with_line = image.copy() (h, w) = image.shape # image_with_line = cv2.dilate(image, np.ones((2, 2), np.uint8), iterations=1) # needs some tuning horizontal_projection = get_horizontal_projection(image) baseline_y_coord = get_baseline_y_coord(horizontal_projection) # cv2.line(image_with_line, (0, baseline_y_coord), (w, baseline_y_coord), (255, 255, 255), 1) # display_image("image without dotting", image_without_dotting) vertical_projection = get_vertical_projection(image) print("shape of vertical projections is: ", len(vertical_projection)) x, count = 0, 0 is_space = False xcoords = [] distances = [] for i in range(w): if not is_space: if vertical_projection[i] == 0: is_space = True count = 1 x = i else: if vertical_projection[i] > 0: is_space = False xcoords.append(x / count) distances.append(count) else: x += i count += 1 distance = get_distance_between_words(distances) previous_width = 0 word_separation = xcoords.copy() print(len(word_separation)) for i in range(len(xcoords)): if i == 0: previous_width = int(xcoords[i]) continue if distances[i - 1] >= distance: pass # cv2.line(image, (previous_width, 0), (previous_width, h), (255, 255, 255), 1) else: # print(i) word_separation[i - 1] = -1 sub_word = original_image[:, previous_width:int(xcoords[i])] cv2.imwrite(directory_name + "/" + "segment_" + str(i) + ".png", sub_word) # display_image("sub word", sub_word) previous_width = int(xcoords[i]) if distances[-2] < distance: word_separation[-2] = -1 sub_word = original_image[:, int(xcoords[-1]):w] cv2.imwrite(directory_name + "/" + "segment_" + str(len(xcoords)) + ".png", sub_word) display_image("sub word", sub_word) print("word sep: ", word_separation) previous_width = 0 sub_seg_points = [] # word_separation = list(filter(lambda a: a != -1, word_separation)) flag = False for i in range(len(word_separation)): if word_separation[i] == -1 and flag == False: flag = True sub_seg_points = [] sub_seg_points.append(xcoords[i - 1]) # sub_seg_points.append(xcoords[i]) if word_separation[i] == -1 and flag: sub_seg_points.append(xcoords[i]) if word_separation[i] != -1 and flag: sub_seg_points.append(xcoords[i]) flag = False print("sub seg: ", sub_seg_points) sub_image = image[:, int(sub_seg_points[0]):int(sub_seg_points[-1])] display_image("duh", sub_image) for i in range(1, len(sub_seg_points) - 1): cv2.line(image, (int(sub_seg_points[i]), 0), (int(sub_seg_points[i]), h), (255, 255, 255), 1) display_image("display", image) previous_width = int(word_separation[i]) cv2.line(image, (int(xcoords[-1]), 0), (int(xcoords[-1]), h), (255, 255, 255), 1) print("word: ", word_separation) print("xcoord: ", xcoords) display_image("final output", image) cv2.imwrite("dis.png", image)