def get_objects(img, _slice, kernel, original_heigth=True): '''return a list of slices ''' or0, oc0, or1, oc1 = slice_to_points(_slice) subgrupo = img[_slice] subgrupo = cv2.morphologyEx(subgrupo, cv2.MORPH_OPEN, kernel) new_image, contours, hierarchy = cv2.findContours(255-subgrupo, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) slices = [] for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) if w > 5 and h > 15: if original_heigth: abs_slice = points_to_slice(or0, oc0 + x, or1, oc0 + x + w) else: abs_slice = points_to_slice(or0 + y, oc0 + x, or0 + y + h, oc0 + x + w) slices.append(abs_slice) slices.sort(key=lambda x: x[1].start + x[0].start) slices = remove_nested_slices(slices) return slices
def get_objects(img, _slice, kernel, original_heigth=True): '''return a list of slices ''' or0, oc0, or1, oc1 = slice_to_points(_slice) subgrupo = img[_slice] subgrupo = cv2.morphologyEx(subgrupo, cv2.MORPH_OPEN, kernel) new_image, contours, hierarchy = cv2.findContours(255 - subgrupo, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) slices = [] for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) if w > 5 and h > 15: if original_heigth: abs_slice = points_to_slice(or0, oc0 + x, or1, oc0 + x + w) else: abs_slice = points_to_slice(or0 + y, oc0 + x, or0 + y + h, oc0 + x + w) slices.append(abs_slice) slices.sort(key=lambda x: x[1].start + x[0].start) slices = remove_nested_slices(slices) return slices
def get_lines(img): '''return a list of slices for every line of text found in the image''' h, w = img.shape _slice = points_to_slice(0, 0, h, w) kernel = np.ones((1, w)) return get_objects(img, _slice, kernel, False)