article extraction from newspaper image in python

2020-05-29 02:24发布

问题:

I tried extracting articles from the newspaper image, but headings are being separated with rlsa algorithm horizontal and vertical of some pixel value in the first image. If I tried with more pixel value, articles are merging which is showed in second image. Can anyone suggest the best method to separate articles from the image in python and opencv?

This loop is for run-length-smoothing-algorithm-horizontal on the image

    for i in range(1,a):
        c = 1
        for j in range(1, b):
            if im_bw[i, j] == 0:
                if (j-c) <= 10:
                    im_bw[i, c:j] = 0

                c = j


        if (b - c) <= 10:
            im_bw[i, c:b] = 0

This loop is for run-length-smoothing-algorithm-vertical on the image

    for i in range(1, b):
        c = 1
        for j in range(1, a):
            if im_bw[j, i] == 0:
                if (j-c) <= 9:
                    im_bw[c:j, i] = 0

                c = j


        if (b - c) <= 9:
            im_bw[c:b, i] = 0

a is number of rows b is number of columns of an binary image

How algorithm worked on binary image and red mark shows the merging of articles

回答1:

I have an approach worked for most of the images.

  1. Binary conversion of color/gray scale images using PIL/Opencv.
  2. Remove pictures from image as contours with largest area compared to average area of all the contours present in the image.
  3. Remove lines using canny edge filter and houghlines
  4. Use RLSA(run length smoothing algorithm) on this binary image. Description and Code for this RLSA can be found on this repository https://github.com/Vasistareddy/python-rlsa

Removing lines helps because some e-papers keeps lines as article separator. We can achieve better results with more processing of the images. Heuristics like average width, average height, average area can be implemented on the contours left on the image after applying above steps to achieve better results.

Coming to the above question, the articles always with the white background. Without white background are clearly "Ads" or "pictures" or "miscellaneous" stuff. Removing pictures from the above 4 mentioned steps clears solves this issue.

PS: Choosing a value for RLSA horizontal and vertical is always mystery. Since the gap of article varies from edition to edition.



标签: python opencv