4

I have a number of images from Chinese genealogies, and I would like to be able to programatically categorize them. Generally speaking, one type of image has primarily line-by-line text, while the other type may be in a grid or chart format.

Example photos

Question: Is there a (relatively) simple way to do this? I have experience with Python, but little knowledge of image processing. Direction to other resources is appreciated as well.

Thanks!

DevinRB
  • 107
  • 2
  • 8
  • [**This question**](http://stackoverflow.com/questions/7227074/horizontal-line-detection-with-opencv) seems related. – Junuxx Oct 30 '12 at 16:03

2 Answers2

3

Assuming that at least some of the grid lines are exactly or almost exactly vertical, a fairly simple approach might work.

I used PIL to find all the columns in the image where more than half of the pixels were darker than some threshold value.

Code

import Image, ImageDraw # PIL modules

withlines = Image.open('withgrid.jpg')
nolines   = Image.open('nogrid.jpg')

def findlines(image):
    w,h, = image.size
    s = w*h
    im = image.point(lambda i: 255 * (i < 60))   # threshold
    d = im.getdata()      # faster than per-pixel operations

    linecolumns = []

    for col in range(w):
        black = sum( (d[x] for x in range(col, s, w)) )//255
        if black > 450:
            linecolumns += [col]

    # return an image showing the detected lines
    im2 = image.convert('RGB')
    draw = ImageDraw.Draw(im2)        
    for col in linecolumns:
        draw.line( (col,0,col,h-1), fill='#f00', width = 1)

    return im2

findlines(withlines).show()
findlines(nolines).show()

Results

showing detected vertical lines in red for illustration

image 1enter image description here

As you can see, four of the grid lines are detected, and, with some processing to ignore the left and right sides and the center of the book, there should be no false positives on the desired type.

This means that you could use the above code to detect black columns, discard those that are near to the edge or the center. If any black columns remain, classify it as the "other" undesired class of pictures.

Junuxx
  • 14,011
  • 5
  • 41
  • 71
0

AFAIK, there is no easy way to solve this. You will need a decent amount of image processing and some basic machine learning to classify these kinds of images (and even than it probably won't be 100% successful)

Another note:

While this can be solved by only using machine learning techniques, I would advice you to start searching for some image processing techniques first and try to convert your image to a form that has a decent difference for both images. For this you best start reading about the fft. After that have a look at some digital image processing techniques. When you feel comfortable that you have a decent understanding of these, you can read up on pattern recognition.

This is only one suggested approach though, there are more ways to achieve this.

Minion91
  • 1,911
  • 12
  • 19