title: use python open-cv for segmenting newspaper article
body: <p>I'm using the code below for segmenting the articles from an image of newspaper.</p>
  <pre><code>def segmenter(image_received):
      # Process 1: Lines Detection
  
      img = image_received
      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # convert to binary gray image
      edges = cv2.Canny(gray, 75, 150)  # determine contours
      lines = cv2.HoughLinesP(edges, 0.017, np.pi / 180, 60, minLineLength=100, maxLineGap=0.1)  # houghlines generation
  
      # drawing houghlines
      for line in lines:
          x1, y1, x2, y2 = line[0]
          cv2.line(img, (x1, y1), (x2, y2), (0, 0, 128), 12)  # the houghlines of color (0,0,128) is drawn
  
      # Drawing brown border
      bold = cv2.copyMakeBorder(
          img,  # image source
          5,  # top width
          5,  # bottomm width
          5,  # left width
          5,  # right width
          cv2.BORDER_CONSTANT,
          value=(0, 0, 128)  # brown color value
      )
  
      image = bold
      gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
      thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
  
      horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
      detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
      cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
      cnts = cnts[0] if len(cnts) == 2 else cnts[1]
      for c in cnts:
          if int(len(c) &gt;= 10):
              cv2.drawContours(image, [c], 0, (0, 17, 255), 1)
  
      vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
      detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
      cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
      cnts = cnts[0] if len(cnts) == 2 else cnts[1]
      for c in cnts:
          if int(len(c) &gt;= 10):
              cv2.drawContours(image, [c], 0, (0, 17, 255), 1)
  
      cv2.imwrite(f'tmp/{str(str(uuid.uuid4()))}.jpg', image)
  
  </code></pre>
  <p>for instance
  the input image is
  <a href="https://i.stack.imgur.com/PRY7l.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/PRY7l.jpg" alt="enter image description here" /></a>
  and the output image is :
  <a href="https://i.stack.imgur.com/Oc9YY.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/Oc9YY.jpg" alt="enter image description here" /></a></p>
  <p>There are three problems:</p>
  <ol>
  <li>the output rectangles aren't complete in all cases.</li>
  <li>Images also are segmented inside articles as part of articles. But what I need is to segment only the text of the newspaper and crop all the other things out. Something like this one:
  <a href="https://i.stack.imgur.com/6dleB.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/6dleB.jpg" alt="enter image description here" /></a></li>
  <li>Consider the following image:
  <a href="https://i.stack.imgur.com/JUuOu.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/JUuOu.jpg" alt="enter image description here" /></a>
  The article indicated by borders is not rectangular and is much more complicated. How can I achieve the correct borders using python open-cv or other image processing libraries?</li>
  </ol>
  <p>(the question has an answer <a href="https://stackoverflow.com/questions/63865237/identify-text-areas-on-a-talmud-page">here for matlab</a>. But I need a python code.</p>