enter image description here


enter image description here

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:

enter image description here

> #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


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?

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

2 Answers 2


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
  • $\begingroup$ 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. $\endgroup$
    – DGS
    Commented Mar 15, 2019 at 9:54
  • $\begingroup$ youtube.com/watch?v=qIs1SUH_3Lw&feature=youtu.be $\endgroup$ Commented Mar 15, 2019 at 10:38
  • $\begingroup$ 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. $\endgroup$ Commented Mar 15, 2019 at 10:39
  • 1
    $\begingroup$ 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 $\endgroup$
    – DGS
    Commented Mar 19, 2019 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.

There is also this page that provides some quality improvement suggestions.

There are many questions/answers on Stack Overflow about specific usage cases. In this answer, for example, there is a recommendation to use OSM mode to detect multiple columns.

And there is this SO answer that offers a way to break text into paragraphs.


Not the answer you're looking for? Browse other questions tagged or ask your own question.