# How can I detect blocks of text from scanned document images

ORIGINAL IMAGE:

GOAL:

I want to separate texts into individual paragraphs by placing bounding boxes over them (as shown above).

I tried it do this via traditional computer vision approach using opencv.

1. I plotted character level bounding box
2. Next, I gray-scaled the image, binarized it.
3. Applied dilation
4. And finally placed bbox over the dilated image.

This is what I get:

> #Morphological Transformation

kernel = np.ones((3,4),np.int8)

dilation = cv2.dilate(im_bw, kernel)

cv2.imwrite('dilated.png', dilation)


# Plotting rectangular box

ret,thresh = cv2.threshold(im_bw, 127,255,0)
image, contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE )

for c in contours:
rect = cv2.boundingRect(c)
if rect[2] < 50 or rect[3] < 50 : continue

print (cv2.contourArea(c))
x,y,w,h = rect
cv2.rectangle(im_new,(x,y),(x+w,y+h),(0,255,0),2)

cv2.imwrite('sample_res_inner.jpg',im_new)


Since the image is scanned image plus the line-spaces between them are small I couldn't able to segment them based on paragraphs.

How can I get my desired result?

• May you post the original image without any processing? Mar 14 '19 at 21:28
• @Edmund It's done now.
– DGS
Mar 15 '19 at 4:18
• I think you should refer to the following link: getting-paragraph-sections Nov 8 '19 at 19:45
• It is a year old post.. wondering if the solution was found. Can you post here please Apr 2 '20 at 4:24

There are two options :

1. Scan the images with a higher DPI. This should accentuate vertical separation between paragraphs.
2. Train a Deep learning model for Text Detection in scene. Examples : https://github.com/qjadud1994/CRNN-Keras and https://github.com/mvoelk/ssd_detectors
• Well, the above mentioned methods are scene text detectors. It will detect the texts word by word. It doesn't perform segmentation by paragraphs which is want.
– DGS
Mar 15 '19 at 9:54
• youtube.com/watch?v=qIs1SUH_3Lw&feature=youtu.be Mar 15 '19 at 10:38
• Video mentioned above is a demo of model that extends the approach to paragraphs. arxiv.org/abs/1604.03286 . It starts with word boundaries and trains an "attention" layer to identify paragraph boundaries. Mar 15 '19 at 10:39
• I just want to detect paragraphs and i don't want to recognize it now. Will this method help to detect the paragraphs or group of text that lies together as mentioned under GOAL section. If so, can u please provide the implementation of this paper
– DGS
Mar 19 '19 at 5:31

Have you looked into Tesseract (and its Python wrapper/interface: pytesseract)? I don't guarantee that it will solve your problems entirely, but it offers bounding box and OCR features.

On this Tesseract site it lists possible page segmentation modes that you could play around with.