Exemple #1
0
def extract_region_from_image_cc(im, rect):
    pad = 10
    y0, x0, y1, x1 = rect

    local_im = im[y0-pad:y1+pad, x0-pad:x1+pad].copy()
    h, w = local_im.shape

    box = (slice(pad, h - pad, None), slice(pad, w - pad, None))
    binary = convert_to_binary(255 - local_im)
    labels, _ = morph.label(binary)
    objects = morph.find_objects(labels)
    region_objs = []

    for i, b in enumerate(objects):
        if 1.0 * sl.xoverlap(b, box) * sl.yoverlap(b, box) / sl.area(b) > 0.55:
            region_objs.append(b)
        else:
            binary[b][labels[b]==i+1] = 0

    x2 = min([obj[1].start for obj in region_objs] + [pad])
    x3 = max([obj[1].stop for obj in region_objs] + [w - pad])
    y2 = min([obj[0].start for obj in region_objs] + [pad])
    y3 = max([obj[0].stop for obj in region_objs] + [h - pad])

    return convert_binary_to_normal_im(binary[y2:y3, x2:x3]), (pad-x2, x3-w+pad, pad-y2, y3-h+pad)
Exemple #2
0
def compute_checkbox_position(blank_im):
    binary = convert_to_binary(255 - blank_im)
    labels, n = morph.label(binary)
    h, w = binary.shape
    minsize = 40

    # find small dash in img
    sums = measurements.sum(binary, labels, range(n + 1))
    sums = sums[labels]
    good = minimum(binary, 1 - (sums > 0) * (sums < minsize))

    junk_cc = np.bitwise_xor(good, binary)
    # temporary fix: add bottom line
    junk_cc[h-1:, :] = np.ones((1, w))
    junk_cc = morph.r_dilation(junk_cc, (7,7))
    junk_cc = morph.r_closing(junk_cc, (9,9))

    # find hole using morphology
    hole = morph.fill_hole(junk_cc)
    hole = hole - junk_cc

    # locate holes position
    labels, n = morph.label(hole)
    objects = morph.find_objects(labels)
    objects = sorted(objects, key=lambda b: sl.center(b))
    area_thres = 0.4 * (amax([sl.area(b) for b in objects]) if len(objects) > 0 else 0)
    boxes = [[b[0].start, b[1].start, b[0].stop, b[1].stop] for b in objects if sl.area(b) > area_thres]

    return boxes, convert_binary_to_normal_im(hole)
 def remove_hlines(self, binary, scale, maxsize=10):
     labels, _ = morph.label(binary)
     objects = morph.find_objects(labels)
     for i, b in enumerate(objects):
         if sl.width(b) > maxsize * scale:
             labels[b][labels[b] == i + 1] = 0
     return np.array(labels != 0, 'B')
Exemple #4
0
def remove_hlines(binary,scale,maxsize=10):
    labels,_ = morph.label(binary)
    objects = morph.find_objects(labels)
    for i,b in enumerate(objects):
        if sl.width(b)>maxsize*scale:
            labels[b][labels[b]==i+1] = 0
    return array(labels!=0,'B')
Exemple #5
0
def firstAnalyse(binary):
    binaryary = morph.r_closing(binary.astype(bool), (1,1))
    labels,_ = morph.label(binaryary)
    objects = morph.find_objects(labels) ### <<<==== objects here
    bysize = sorted(range(len(objects)), key=lambda k: sl.area(objects[k]))
#     bysize = sorted(objects,key=sl.area)
    scalemap = zeros(binaryary.shape)
    smalldot = zeros(binaryary.shape, dtype=binary.dtype)
    for i in bysize:
        o = objects[i]
        if amax(scalemap[o])>0: 
#             mask = where(labels[o] != (i+1),uint8(255),uint8(0))
#             binary[o] = cv2.bitwise_and(binary[o],binary[o],mask=mask)
            continue
        scalemap[o] = sl.area(o)**0.5
    scale = median(scalemap[(scalemap>3)&(scalemap<100)]) ### <<<==== scale here

    for i,o in enumerate(objects):       
        if (sl.width(o) < scale/2) or (sl.height(o) < scale/2):
            smalldot[o] = binary[o]
        if sl.dim0(o) > 3*scale:
            mask = where(labels[o] != (i+1),uint8(255),uint8(0))
            binary[o] = cv2.bitwise_and(binary[o],binary[o],mask=mask)
            continue
    return objects, smalldot, scale
Exemple #6
0
def text_line_segmentation_NN(image,
                              scale,
                              mask=None,
                              use_binary=False,
                              debug_image=None,
                              offset=(0, 0)):

    h, w = image.shape[:2]

    if debug_image is None:
        debug_image = image
        if len(debug_image.shape) < 3:
            debug_image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

    if len(image.shape) > 2:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    if use_binary:
        image = morph.thresh_sauvola(image, k=0.15) * 255

    if mask is not None:
        image = (image + mask * 255).astype('uint8')

    labels, _ = morph.label(image == 0)
    objects = morph.find_objects(labels)

    height_map = [
        sl.height(o) for o in objects if sl.height(o) > 6
        and sl.height(o) < 100 and sl.aspect_normalized(o) < 8
    ]
    avg_h = max(np.nan_to_num(np.mean(height_map)), scale * 0.6)

    block = Block(image, avg_h)
    words = block.getWordBoundingBoxes()

    lines = filter_and_merge_overlap_boxes(words,
                                           max(avg_h, scale * 1.2) * 0.3)
    lines = filter_and_merge_overlap_boxes(lines,
                                           max(avg_h, scale * 1.2) * 0.3,
                                           use_merge_same_line_only=True)

    offset_x, offset_y = offset

    # filter line by size
    lines = [
        l for l in lines if l[3] - l[1] > avg_h * 0.3 and l[3] - l[1] < avg_h *
        2.5 and l[2] - l[0] > avg_h * 0.5
    ]

    lines = [sl.pad_box(l, 0, (h, w)) for l in lines]
    lines = [[
        l[0] + offset_x, l[1] + offset_y, l[2] + offset_x, l[3] + offset_y
    ] for l in lines]

    debug_image = block.paint(None)

    return lines, debug_image
Exemple #7
0
def remove_vlines(binary, gray, scale, maxsize=10):
    labels, _ = morph.label(binary)
    objects = morph.find_objects(labels)
    for i, b in enumerate(objects):
        if (sl.width(b) <= 20 and sl.height(b) > 200) or (sl.width(b) <= 45 and sl.height(b) > 500):
            gray[b][labels[b] == i + 1] = 140
            # gray[:,b[1].start:b[1].stop]=140
            labels[b][labels[b] == i + 1] = 0
    return array(labels != 0, "B")
Exemple #8
0
def remove_vlines(binary,gray,scale,maxsize=10):
    labels,_ = morph.label(binary)
    objects = morph.find_objects(labels)
    for i,b in enumerate(objects):
        if (sl.width(b)<=20 and sl.height(b)>200) or (sl.width(b)<=45 and sl.height(b)>500):
            gray[b][labels[b]==i+1] = 140
            #gray[:,b[1].start:b[1].stop]=140
            labels[b][labels[b]==i+1] = 0
    return array(labels!=0, 'B')
Exemple #9
0
def simplefirstAnalyse(binary):
    binaryary = morph.r_closing(binary.astype(bool), (1,1))
    labels,_ = morph.label(binaryary)
    objects = morph.find_objects(labels) ### <<<==== objects here
    smalldot = zeros(binaryary.shape, dtype=binary.dtype)
    scale = int(binary.shape[0]*0.7)
    for i,o in enumerate(objects):       
        if (sl.width(o) < scale/2) or (sl.height(o) < scale/2):
            smalldot[o] = binary[o]
        if sl.dim0(o) > 3*scale:
            mask = where(labels[o] != (i+1),uint8(255),uint8(0))
            binary[o] = cv2.bitwise_and(binary[o],binary[o],mask=mask)
            continue
    return objects, smalldot, scale
Exemple #10
0
def remove_small_noise(binary, minsize = 50):
    labels, n = morph.label(binary)
    h, w = binary.shape
    objects = morph.find_objects(labels)
    space_to_edge = 10
    sums = measurements.sum(binary, labels, range(n + 1))
    sums = sums[labels]
    good = minimum(binary, 1 - (sums > 0) * (sums < minsize))

    for i, b in enumerate(objects):
        cy, cx = sl.center(b)
        # if component is small and close to edge
        if (sl.area(b) < minsize * 1.2 and ((cx < space_to_edge or cx > w - space_to_edge) or (cy < space_to_edge or cy > h - space_to_edge))):
            good[b][labels[b] == i+1] = 0

    return good
Exemple #11
0
 def setImageMasked(self,image,mask=None,lo=None,hi=None):
     """Set the image to be iterated over.  This should be an RGB image,
     ndim==3, dtype=='B'.  This picks a subset of the segmentation to iterate
     over, using a mask and lo and hi values.."""
     assert image.dtype==dtype('B') or image.dtype==dtype('i'),"image must be type B or i"
     if image.ndim==3: image = rgb2int(image)
     assert image.ndim==2,"wrong number of dimensions"
     self.image = image
     labels = image
     if lo is not None: labels[labels<lo] = 0
     if hi is not None: labels[labels>hi] = 0
     if mask is not None: labels = bitwise_and(labels,mask)
     labels,correspondence = morph.renumber_labels_ordered(labels,correspondence=1)
     self.labels = labels
     self.correspondence = correspondence
     self.objects = [None]+morph.find_objects(labels)
Exemple #12
0
def compute_boxmap(binary, scale, oriimg, threshold=(.5, 4), dtype='i'):
    labels, n = morph.label(binary)
    objects = morph.find_objects(labels)
    boxmap = zeros(binary.shape, dtype)
    for i, o in enumerate(objects):
        h = sl.dim0(o)
        w = sl.dim1(o)
        ratio = float(h) / w if h > w else float(w) / h
        if h > 2 * scale or h < scale / 3:
            continue
        if ratio > 8: continue
        #         if sl.area(o)**.5<threshold[0]*scale: continue
        #         if sl.area(o)**.5>threshold[1]*scale: continue

        boxmap[o] = 1
    return boxmap
Exemple #13
0
def compute_lines(segmentation,scale):
    """Given a line segmentation map, computes a list
    of tuples consisting of 2D slices and masked images."""
    lobjects = morph.find_objects(segmentation)
    lines = []
    for i,o in enumerate(lobjects):
        if o is None: continue
        if sl.dim1(o)<2*scale or sl.dim0(o)<scale: continue
        mask = (segmentation[o]==i+1)
        if amax(mask)==0: continue
        result = record()
        result.label = i+1
        result.bounds = o
        result.mask = mask
        lines.append(result)
    return lines
Exemple #14
0
def compute_lines(segmentation,scale):
    """Given a line segmentation map, computes a list
    of tuples consisting of 2D slices and masked images."""
    lobjects = morph.find_objects(segmentation)
    lines = []
    for i,o in enumerate(lobjects):
        if o is None: continue
        if sl.dim1(o)<2*scale or sl.dim0(o)<scale: continue
        mask = (segmentation[o]==i+1)
        if np.amax(mask)==0: continue
        result = record()
        result.label = i+1
        result.bounds = o
        result.mask = mask
        lines.append(result)
    return lines
Exemple #15
0
def filter_junk_cc(binary, scale, maxsize):
    junk_cc = np.zeros(binary.shape, dtype='B')
    text_like = np.zeros(binary.shape, dtype='B')

    labels, _ = morph.label(binary)
    objects = morph.find_objects(labels)

    for i, b in enumerate(objects):

        if sl.width(b) > maxsize * scale or sl.area(b) > scale * scale * 8 or \
                        sl.aspect_normalized(b) > 8 or sl.min_dim(b) < scale * 0.35:

            junk_cc[b][labels[b] == i + 1] = 1
        else:
            if sl.width(b) > 0.3 * scale and sl.height(b) > 0.3 * scale:
                text_like[b][labels[b] == i + 1] = 1

    return junk_cc, text_like
Exemple #16
0
def calc_typo_metric(binline):
    labels,n = morph.label(binline)
    objects = morph.find_objects(labels)
    filtered = []
    max_h = 0
    for o in objects:
        h = sl.dim0(o)
        w = sl.dim1(o)
        if h > binline.shape[0]*0.98: continue
        if h < 3 or w < 3: continue
        if (h > binline.shape[0]*0.2 and w > binline.shape[0]*0.2) or \
                (o[0].start > binline.shape[0]/2 and o[1].stop > binline.shape[1]/4 and o[1].stop < 3*binline.shape[1]/4 and o[0].stop < binline.shape[0]*0.98):
            filtered.append(o)
            if h > max_h:
                max_h = h
    filtered.sort(key=lambda x:x[1].start)
    prech = None
    zoomforsee = 4
    infoheight=50
    info = np.zeros((infoheight*2+binline.shape[0]*zoomforsee,binline.shape[1]*zoomforsee))
    for ch in filtered:
        h = sl.dim0(ch)
        w = sl.dim1(ch)
        if prech is not None and ch[1].start < (prech[1].start + prech[1].stop)/2: continue
        cv2.putText(info,'{:3.2f}'.format(1.0*w/max_h),\
                    ((ch[1].start)*zoomforsee, int(infoheight*0.4)), cv2.FONT_HERSHEY_SIMPLEX, \
                    0.5,1.0,1)
        if prech is None:
            cv2.putText(info,'{:3d}'.format(max_h),\
                        ((ch[1].start)*zoomforsee, int(infoheight*0.9)), cv2.FONT_HERSHEY_SIMPLEX, \
                        0.5,1.0,1)           
        else:    
            space = ch[1].start - prech[1].stop
            dist = ch[1].stop - prech[1].stop
            cv2.putText(info,'{:3.2f}'.format(1.0*space/max_h),\
                        ((prech[1].stop)*zoomforsee, int(infoheight*0.9)), cv2.FONT_HERSHEY_SIMPLEX, \
                        0.5,1.0,1)
            cv2.putText(info,'({:3.2f})'.format(1.0*dist/max_h),\
                        ((prech[1].stop)*zoomforsee, int(infoheight*1.4)), cv2.FONT_HERSHEY_SIMPLEX, \
                        0.5,1.0,1)
        prech = ch
    info[infoheight*2:,:] = cv2.resize(binline, (zoomforsee*binline.shape[1], zoomforsee*binline.shape[0]))
    return (info*250).astype(np.uint8), filtered
Exemple #17
0
def cut_dash_line(im, num_cells):
    binary = convert_to_binary(255-im, thres=0.5)
    labels, _ = morph.label(binary)
    objects = morph.find_objects(labels)

    scale = int(round(1.0 * binary.shape[1] / num_cells + 0.2))
    h = binary.shape[0] - 1
    # list to store objects for each cell
    cells = [[] for _ in range(num_cells)]
    cell_ims = []

    for i, b in enumerate(objects):
        # only process object with width < 2 x scale
        if sl.width(b) < 2 * scale:
            x1, x2 = b[1].start, b[1].stop
            mid_x = (x1 + x2) // 2
            cell_index = np.median([x1 // scale, x2 // scale, mid_x // scale]).astype(int)
            #print(cell_index)
            # handle case where digit from 2 cells connected
            if x2 - (cell_index + 1) * scale > 0.3 * scale:
                temp_b = (b[0], slice(b[1].start, (cell_index + 1) * (scale + 1), None))
                print("2 char connected!!!")
            else:
                temp_b = b
            cells[cell_index].append(temp_b)

    for i, c in enumerate(cells):
        if len(c) > 0:
            x1 = min([obj[1].start for obj in c])
            x2 = max([obj[1].stop for obj in c])
            cell_ims.append(normalize_cell_img(im[0:h, x1:x2]))
        else:
            blank = np.zeros((h, scale))
            cell_ims.append(normalize_cell_img(convert_binary_to_normal_im(blank)))

    return cell_ims
Exemple #18
0
def binary_objects(binary):
    labels,n = morph.label(binary)
    objects = morph.find_objects(labels)
    return objects
Exemple #19
0
def detect_table(image, scale, maxsize=10, debug_path=None):
    h, w = image.shape[:2]
    if len(image.shape) > 2 and image.shape[2] >= 3:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    else:
        gray = image

    # kernel = np.ones((5,5),np.uint8)
    # gray = 255-cv2.morphologyEx(255-gray, cv2.MORPH_CLOSE, kernel)

    binary = 1 - morph.thresh_sauvola(gray, k=0.05)
    junk_cc, _ = filter_junk_cc(binary, scale, maxsize)

    junk_cc = morph.r_closing(junk_cc, (5, 5))

    print('calculating combine sep...')
    combine_sep = compute_combine_seps(junk_cc, scale)
    # using closing morphology to connect disconnected edges
    close_thes = int(scale * 0.15)
    closed_sep = morph.r_closing(combine_sep, (close_thes, close_thes))

    if debug_path is not None:
        cv2.imwrite(filename[:-4] + '_bin.png',
                    ((1 - junk_cc) * 255).astype('uint8'))
        cv2.imwrite(filename[:-4] + '_sep.png',
                    (closed_sep * 255).astype('uint8'))

    labels, _ = morph.label(closed_sep)
    objects = morph.find_objects(labels)

    # result table list
    boxes = []

    for i, b in enumerate(objects):
        if sl.width(b) > maxsize * scale or sl.area(
                b) > scale * scale * 10 or (sl.aspect_normalized(b) > 6
                                            and sl.max_dim(b) > scale * 1.5):

            density = np.sum(combine_sep[b])
            density = density / sl.area(b)

            if (sl.area(b) > scale * scale * 10 and sl.min_dim(b) > scale * 1.0
                    and sl.max_dim(b) > scale * 8 and density < 0.4):
                # calculate projection to determine table border
                w = sl.width(b)
                h = sl.height(b)

                region = (labels[b] == i + 1).astype('uint8')

                border_pad = max(w, h)
                border_thres = scale * 2

                proj_x = np.sum(region, axis=0)
                proj_y = np.sum(region, axis=1)

                proj_x[3:] += proj_x[:-3]
                proj_y[3:] += proj_y[:-3]

                sep_x = np.sort([j[0] for j in np.argwhere(proj_x > 0.75 * h)])
                sep_y = np.sort([j[0] for j in np.argwhere(proj_y > 0.4 * w)])

                # skip if sep count < 2
                if len(sep_x) < 1 or len(sep_y) < 1: continue

                border_left, border_right, border_top, border_bottom = None, None, None, None

                if sep_x[0] < border_pad:
                    border_left = sep_x[0]
                if sep_x[-1] > w - border_pad:
                    border_right = sep_x[-1]
                if sep_y[0] < border_pad:
                    border_top = sep_y[0]
                if sep_y[-1] > h - border_pad:
                    border_bottom = sep_y[-1]

                # print_info(border_top, border_bottom, border_left, border_right)

                if all([
                        j is not None for j in
                    [border_top, border_bottom, border_left, border_right]
                ]):
                    border_right = b[1].stop - b[1].start
                    boxes.append([
                        b[1].start + border_left, b[0].start + border_top,
                        b[1].start + border_right, b[0].start + border_bottom
                    ])
                    # boxes.append(([b[1].start, b[0].start, b[1].stop, b[0].stop]))

    return boxes
Exemple #20
0
def remove_small_noise_and_seps(im, num_cells, minsize = 30):
    im = 255 - im
    binary = np.array(im>0.5*(np.amin(im)+np.amax(im))).astype('uint8')  # invert
    h, w = im.shape
    scale = int(w / num_cells)

    labels, n = morph.label(binary)
    objects = morph.find_objects(labels)

    # remove small noise using connected components
    sums = measurements.sum(binary, labels, range(n + 1))
    sums = sums[labels]
    good = minimum(binary, 1 - (sums > 0) * (sums < minsize))

    # calculate sep bar positions from junk cc
    junk_cc = np.bitwise_xor(binary, good)

    # remove long connected component (solid separators)
    proj_x = np.sum(binary, axis=0)
    mask_x = np.tile((proj_x > h * 0.8).astype('B'), h)
    solid_sep_pos = [j[0] for j in np.argwhere(proj_x > h * 0.6)]
    good[mask_x] = 0
    '''for i, b in enumerate(objects):
            if sl.width(b) < 6 and sl.height(b) > h * 0.9:
                good[b][labels[b] == i + 1] = 0
        '''

    if np.sum(junk_cc) > 140:
        # only detect sep bars if has enough pixels
        proj_x = np.sum(junk_cc, axis=0)
        mask_x = proj_x > np.amax(proj_x) * 0.2
        sep_pos = np.array([i[0] for i in np.argwhere(mask_x)])
        start = [True] + [True if abs(sep_pos[i] - sep_pos[i-1] - scale) < 5 or abs(sep_pos[i] - sep_pos[i-1] - 2 * scale) < 5 else False for i in range(1,len(sep_pos))]
    else:
        sep_pos = []

    if len(sep_pos) > 0:
        start_sep_pos = sep_pos[start]
        #print(start_sep_pos)

        # fill-in missing pos
        '''for i in range(1,len(start_sep_pos)):
            if start_sep_pos[i] - start_sep_pos[i-1] > scale + 4:
                mid = (start_sep_pos[i] + start_sep_pos[i-1]) // 2
                good[0:h, mid:mid + 5] = 0
        '''

        # fill seps start from begin sep with scale space
        if len(start_sep_pos) > 0 and len(solid_sep_pos) > 0:
            pos_x = start_sep_pos[0]
            scale = int(round(1.0 * w / num_cells) + 0.1)
            while pos_x < w:
                if any(x in solid_sep_pos for x in range(pos_x-3,pos_x+4)):
                    pos_x = min([x for x in range(pos_x-3,pos_x+4) if x in solid_sep_pos])
                    good[0:h,pos_x:pos_x+5] = 0
                pos_x += scale
        else:
            # handle special case for 2 cells
            if w / scale > 1.5 and w / scale < 2.6:
                mid = w // 2
                good[0:h, mid:mid + 5] = 0

    else:
        # fill seps start from solid sep with scale space
        proj_x = np.sum(good, axis=0)
        mask_x = proj_x > h * 0.9
        sep_pos = np.array([i[0] for i in np.argwhere(mask_x)])
        pos_x = scale if len(sep_pos) == 0 else sep_pos[0]
        while pos_x < w:
            good[0:h, pos_x:pos_x + 5] = 0
            pos_x += scale + 1

    return np.array((1-good) * 255).astype('uint8')
def extractLines(imgpath, param):
    img_grey = ocrolib.read_image_gray(imgpath)
    (h, w) = img_grey.shape[:2]
    img00 = cv2.resize(img_grey[h / 4:3 * h / 4, w / 4:3 * w / 4],
                       None,
                       fx=0.5,
                       fy=0.5)
    angle = estimate_skew_angle(img00, linspace(-5, 5, 42))
    print 'goc', angle

    rotM = cv2.getRotationMatrix2D((w / 2, h / 2), angle, 1)
    img_grey = cv2.warpAffine(img_grey, rotM, (w, h))

    h, w = img_grey.shape
    img_grey = cv2.normalize(img_grey.astype(float32), None, 0.0, 0.999,
                             cv2.NORM_MINMAX)
    binary = sauvola(img_grey,
                     w=param.w,
                     k=param.k,
                     scaledown=0.2,
                     reverse=True)  ### PARAM
    binary = morph.r_closing(binary.astype(bool), (args.connect, 1))
    binaryary = binary[h / 4:3 * h / 4, w / 4:3 * w / 4]
    binary = binary.astype(np.uint8)
    labels, n = morph.label(binaryary)
    objects = morph.find_objects(labels)

    bysize = sorted(objects, key=sl.area)
    scalemap = zeros(binaryary.shape)
    for o in bysize:
        if amax(scalemap[o]) > 0: continue
        scalemap[o] = sl.area(o)**0.5
    scale = median(scalemap[(scalemap > 3) & (scalemap < 100)])
    objects = psegutils.binary_objects(binary)
    boxmap = zeros(binary.shape, dtype=np.uint8)

    imgwidth = binary.shape[1]
    imgheight = binary.shape[0]
    cellwidth = 6 * scale
    cellheight = 2.5 * scale
    N_x = int(round(imgwidth / cellwidth))
    cellwidth = int(round(imgwidth / N_x))
    N_y = int(round(imgheight / cellheight))
    cellheight = int(round(imgheight / N_y))
    cells_list = [{}, {}, {}, {}]

    def pixel2cell2id(pixel_x, pixel_y, CELLTYPE):
        dx = 0
        dy = 0
        if CELLTYPE == 3:
            pixel_x -= cellwidth / 2
            pixel_y -= cellheight / 2
            dx = cellwidth / 2
            dy = cellheight / 2
        if CELLTYPE == 2:
            pixel_x -= cellwidth / 2
            dx = cellwidth / 2
        if CELLTYPE == 1:
            pixel_y -= cellheight / 2
            dy = cellheight / 2
        if pixel_x <= 0 or pixel_y <= 0: return None, None
        cellcoord = (pixel_x / cellwidth, pixel_y / cellheight)
        cellid = cellcoord[0] + cellcoord[1] * N_x
        cellcoord = (cellcoord[0] * cellwidth + dx,
                     cellcoord[1] * cellheight + dy)
        return cellcoord, cellid

    def id2cell2pixel(cellid, x, y, CELLTYPE):
        cellcoord = (cellid % N_x, cellid / N_x)
        pixel_x = cellcoord[0] * cellwidth + x
        pixel_y = cellcoord[1] * cellheight + y
        if CELLTYPE == 3:
            pixel_x += cellwidth / 2
            pixel_y += cellheight / 2
        return cellcoord, pixel_x, pixel_y

    img_grey = (cv2.cvtColor(img_grey, cv2.COLOR_GRAY2BGR) * 255).astype(
        np.uint8)

    for o in objects:
        h = sl.dim0(o)
        w = sl.dim1(o)
        ratio = float(w) / h
        ### Dirty cheat
        if ratio > 1 and ratio < 6:
            recommended_width = max(int(0.6 * (o[0].stop - o[0].start)),
                                    int(scale * 0.6), 5)
            for pos in range(o[1].start + recommended_width, o[1].stop,
                             recommended_width):
                binary[o[0].start:o[0].stop, pos:pos + 1] = np.uint8(0)
    objects = psegutils.binary_objects(binary)

    for o in objects:
        h = sl.dim0(o)
        w = sl.dim1(o)
        a = h * w
        #         black = float(sum(binary[o]))/a
        #         if sl.area(o)**.5<threshold[0]*scale: continue
        #         if sl.area(o)**.5>threshold[1]*scale: continue
        if h > 5 * scale: continue
        #         if h < 0.4*scale: continue
        if w > 4 * scale and (h > 2 * scale or h < 0.5 * scale): continue
        if a < 0.25 * scale * scale: continue
        if float(h) / w > 10: continue
        ratio = float(w) / h
        if ratio > 10: continue

        ### Add object as candidate character
        pixel_x, pixel_y = (o[1].start + o[1].stop) / 2, o[0].stop
        for celltype in range(4):
            cellcoord, cellid = pixel2cell2id(pixel_x,
                                              pixel_y,
                                              CELLTYPE=celltype)
            if cellcoord is None or cellid is None: continue
            cellbound = slice(cellcoord[1], cellcoord[1] + cellheight,
                              None), slice(cellcoord[0],
                                           cellcoord[0] + cellwidth, None)
            if cellid not in cells_list[celltype]:
                cells_list[celltype][cellid] = SubLineFinder(
                    window_size=max(3, scale / 6),
                    cellbound=cellbound,
                    initChar=o)
            else:
                cells_list[celltype][cellid].addChar(o)

        y0 = o[0].start
        y1 = o[0].stop - 3 if o[0].stop - o[0].start > 8 else o[0].start + 5
        x0 = o[1].start
        x1 = o[1].stop - 3 if o[1].stop - o[1].start > 8 else o[1].start + 5
        boxmap[y0:y1, x0:x1] = 1

    for celltype in range(4):
        if celltype == 0: col = (255, 0, 0)
        if celltype == 1: col = (0, 255, 0)
        if celltype == 2: col = (255, 255, 0)
        if celltype == 3: col = (0, 0, 255)
        for cellid, subline in cells_list[celltype].iteritems():
            #             cv2.rectangle(img_grey, (subline.cellbound[1].start+celltype, subline.cellbound[0].start+celltype), (subline.cellbound[1].stop+celltype, subline.cellbound[0].stop+celltype), col,1)
            line = subline.subline()
            if line is not None:
                pos1 = (int(line[0][0]), int(line[0][1]))
                pos2 = (int(line[1][0]), int(line[1][1]))
                #                 print cellid, pos1, pos2
                cv2.line(img_grey, pos1, pos2, col, 1)
    ### illustrate/debug first round

    return binary, cv2.add(img_grey, (boxmap[:, :, np.newaxis] *
                                      np.array([0, 50, 50])).astype(np.uint8))