Created
November 9, 2020 10:18
-
-
Save parthsad/c7ccb5e01b3b62d36ca64425e19165c1 to your computer and use it in GitHub Desktop.
Detect lines in image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
image = cv2.imread('input2.png', 0) | |
# find lines by horizontally blurring the image and thresholding | |
blur = cv2.blur(image, (80, 9)) | |
#cv2.imshow('y', blur) | |
#cv2.waitKey(0) | |
b_mean = np.mean(blur, axis=1)/256 | |
#hist, bin_edges = np.histogram(b_mean, bins=100) | |
#threshold = bin_edges[66] | |
#print(hist) | |
#print(threshold) | |
threshold = np.percentile(b_mean, 2) | |
t = b_mean > threshold | |
''' | |
get the image row numbers that has text (non zero) | |
a text line is a consecutive group of image rows that | |
are above the threshold and are defined by the first and | |
last row numbers | |
''' | |
tix = np.where(1-t) | |
tix = tix[0] | |
lines = [] | |
start_ix = tix[0] | |
for ix in range(1, tix.shape[0]-1): | |
if tix[ix] == tix[ix-1] + 1: | |
continue | |
# identified gap between lines, close previous line and start a new one | |
end_ix = tix[ix-1] | |
lines.append([start_ix, end_ix]) | |
start_ix = tix[ix] | |
end_ix = tix[-1] | |
lines.append([start_ix, end_ix]) | |
#l_starts = [] | |
#for line in lines: | |
# xx = 500 | |
# for x in range(0,500): | |
# col = image[line[0]:line[1], x] | |
# print(col) | |
# if np.min(col) <100: | |
# xx = x | |
# break | |
# l_starts.append(xx) | |
#print(l_starts) | |
#median_ls = np.median(l_starts) | |
line_seg = [] | |
for line in lines: | |
line_seg.append(line[1]-line[0]) | |
print(line_seg) | |
median_ls = np.mean(line_seg) | |
print(np.mean(line_seg)) | |
paragraphs = [] | |
p_start = lines[0][0] | |
para = [] | |
inc = line_seg[1] | |
for ix in range(2, len(lines)): | |
if line_seg[ix] > inc and line_seg[ix] > median_ls + 1: | |
inc = line_seg[ix] | |
break | |
dec = inc | |
for ix in range(line_seg.index(dec), len(lines)): | |
if line_seg[ix] < dec and line_seg[ix] < median_ls + 1: | |
dec = line_seg[ix] | |
break | |
#for ix in range(1, len(lines)): | |
# if line_seg[ix] > median_ls+1: | |
# p_end = lines[ix][0] - 10 | |
# paragraphs.append([p_start, p_end]) | |
# p_start = lines[ix][0] | |
inci = line_seg.index(inc) | |
deci = line_seg.index(dec) | |
print(inci) | |
print(deci) | |
paragraphs.append([p_start-10, lines[inci][1]+20]) | |
paragraphs.append([lines[inci][1]+10, lines[deci][1]+20]) | |
paragraphs.append([lines[deci][1]+10, lines[-1][0]]) | |
print(paragraphs) | |
p_img = np.array(image) | |
n_cols = p_img.shape[1] | |
for paragraph in paragraphs: | |
cv2.rectangle(p_img, (5, paragraph[0]), (n_cols - 5, paragraph[1]), (0, 255, 0), 3) | |
cv2.imwrite('paragraphs_out.png', p_img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment