0

I am involved in a project that I think you can help me. I have multiple images that you can see here Images to recognize. The goal here is to extract the numbers between the dashed lines. What is the best approach to do that? The idea that I have from the beginning is to find the coordinates of the dash lines and do the crop function, then is just run OCR software. But is not easy to find those coordinates, can you help me? Or if you have a better approach tell me.

Best regards, Pedro Pimenta

2 Answers2

0

You can use python-tesseract https://code.google.com/p/python-tesseract/ ,it works with your image.What you need to do is to split the result string.I use your https://www.dropbox.com/sh/kcybs1i04w3ao97/u33YGH_Kv6#f:euro9.jpg to test.And source code is below.UPDATE

# -*- coding: utf-8 -*-
from PIL import Image
from PIL import ImageEnhance
import tesseract

im = Image.open('test.jpg')
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(4)
im = im.convert('1')
w, h = im.size

im = im.resize((w * (416 / h), 416))

pix = im.load()

LINE_CR = 0.01
WHITE_HEIGHT_CR = int(h * (20 / 416.0))

status = 0
white_line = []

for i in xrange(h):
    line = []
    for j in xrange(w):
        line.append(pix[(j, i)])
    p = line.count(0) / float(w)
    if not p > LINE_CR:
        white_line.append(i)

wp = None
for i in range(10, len(white_line) - WHITE_HEIGHT_CR):
    k = white_line[i]
    if white_line[i + WHITE_HEIGHT_CR] == k + WHITE_HEIGHT_CR:
        wp = k
        break

result = []
flag = 0

while 1:
    if wp < 0:
        result.append(wp)
        break

    line = []
    for i in xrange(w):
        line.append(pix[(i, wp)])
    p = line.count(0) / float(w)

    if flag == 0 and p > LINE_CR:

        l = []

        for xx in xrange(20):
            l.append(pix[(xx, wp)])
        if l.count(0) > 5:
            break

        l = []
        for xx in xrange(416-1, 416-100-1, -1):
            l.append(pix[(xx, wp)])
        if l.count(0) > 17:
            break

        result.append(wp)
        wp -= 1
        flag = 1
        continue

    if flag == 1 and p < LINE_CR:
        result.append(wp)
        wp -= 1
        flag = 0
        continue

    wp -= 1

result.reverse()
for i in range(1, len(result)):
    if result[i] - result[i - 1] < 15:
        result[i - 1] = -1
result = filter(lambda x: x >= 0, result)
im = im.crop((0, result[0], w, result[-1]))
im.save('test_converted.jpg')

api = tesseract.TessBaseAPI()
api.Init(".","eng",tesseract.OEM_DEFAULT)
api.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyz")
api.SetPageSegMode(tesseract.PSM_AUTO)

mImgFile = "test_converted.jpg"
mBuffer=open(mImgFile,"rb").read()
result = tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api)
print "result(ProcessPagesBuffer)=",result

Depends python 2.7 python-tesseract-win32 python-opencv numpy PIL,and be sure to follow python-tesseract's remember to .

pigletfly
  • 1,051
  • 1
  • 16
  • 32
  • I understand your work, but you crop the image manually by the dash lines. I want to do this crop dynamically because this numbers can be in other coordinates. – Pedro Pimenta Feb 26 '13 at 17:13
0

You may start by looking at more obvious (bigger) objects in your images. The dashed lines are way too small in some images. Searching for the "euros milhoes" logo and the barcode will be easier and it will help you have an idea of the scale and rotation involved.

To find these objects without using match template you can binarize your image (watch out for the background texture) and use the Hu moments on the contours/blobs.

Don't expect a good OCR accuracy on images where the numbers are smaller than 8-10 pixels.

rold2007
  • 1,297
  • 1
  • 12
  • 25
  • Thank you for the hints, but my problem is to crop the images between the dashed lines because OCR get read errors with those dash lines. – Pedro Pimenta Feb 26 '13 at 23:19
  • Once you have the position of the logo and the barcode along with the rotation and scale you can apply [HorizontalRunLengthSmoothing](http://www.aforgenet.com/framework/docs/html/f61451b0-4f33-9f34-ca89-fb95ca040614.htm) and [VerticalRunLengthSmoothing](http://www.aforgenet.com/framework/docs/html/249b205e-b7e5-5a70-0eef-65efa5e61da8.htm) with a small maxGapSize to detect the dash lines and then with a larger maxGapSize to detect the position of the numbers. – rold2007 Feb 27 '13 at 03:55
  • Thank you rold2007, I will try that way. – Pedro Pimenta Feb 27 '13 at 14:12