Пример #1
0
def segment(image, addr, extras):
    log = cap.logger(extras, image)
    components = findComponents(image, log)
    log.log(cap.drawComponents(image, components))
    angles = map(lambda comp: findAngle(comp, log), components)
    angles = filter(lambda angle: angle != 0.0 and abs(angle) <= 27.0, angles)
    if len(angles) != 0:
        angle = fsum(angles) / len(angles)
        image = cap.doRotate(image,
                             -angle,
                             fillval=255,
                             interpolation=cv.CV_INTER_NN)
        log.log(image)
        components = findComponents(image)
    assert (len(components) <= nSegs)
    log.log(cap.drawComponents(image, components))
    #components = cap.spltCCs(components, cap.partsFromnSegs5(components), projRadius=5, thresh=2)
    #components = filtCCs(components, (15, 15), 200, 0.2)
    components = map(lambda comp: addBackground(comp, image, bgcolor=96),
                     components)
    segments = map(lambda comp: comp[3], components)
    segments = map(removeBadBackground, segments)
    log.log(cap.joinImagesH(segments))
    segments = map(lambda seg: cap.smoothNoise1(seg, bgcolor=0), segments)
    log.log(cap.joinImagesH(segments))
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_SEGMENT)
    return segments
Пример #2
0
def recognise(image, addr, extras):
    result = ""
    x = image.width - 1
    channels = getChannels(image)
    bestBounds = []
    #cv.NamedWindow("pic", 1)
    #cv.NamedWindow("cols", 0)
    while len(result) < nSegs and x >= minW:
        x = cap.getBound(image, cap.CAP_BOUND_RIGHT, start=x)
        ratings = []
        for w in xrange(minW, min(maxW + 1, x)):
            bounds = findBounds(image, x, w)
            subImage = cap.getSubImage(image, bounds)
            flags = findColors(subImage)
            for index, flag in enumerate(flags):
                if not flag: continue
                seg = getSegment(channels[index], image, bounds)
                seg = cap.flattenImage(adjustSize(seg, segSize))
                guesses = ann.run(seg)
                charIndex = cap.argmax(guesses)
                ratings.append(
                    (guesses[charIndex], charIndex, index, bounds, seg))
        best = max(ratings, key=itemgetter(0))
        result += charset[best[1]]
        bestChannel = channels[best[2]]
        cv.SetImageROI(bestChannel, best[3])
        cv.Set(bestChannel, 96, bestChannel)
        cv.ResetImageROI(bestChannel)
        bestBounds.append(best[3])
        bestW = best[3][2]
        x -= bestW
        #print ann.run(best[4])
    cap.processExtras([cap.drawComponents(image, bestBounds)], addr, extras,
                      cap.CAP_STAGE_RECOGNISE)
    return result[::-1]
Пример #3
0
def recognise(image, addr, extras):
    result = ""
    x = image.width - 1
    channels = getChannels(image)
    bestBounds = []
    #cv.NamedWindow("pic", 1)
    #cv.NamedWindow("cols", 0)
    while len(result) < nSegs and x >= minW:
        x = cap.getBound(image, cap.CAP_BOUND_RIGHT, start=x)
        ratings = []
        for w in xrange(minW, min(maxW + 1, x)):
            bounds = findBounds(image, x, w)
            subImage = cap.getSubImage(image, bounds)
            flags = findColors(subImage)
            for index, flag in enumerate(flags):
                if not flag: continue
                seg = getSegment(channels[index], image, bounds)
                seg = cap.flattenImage(adjustSize(seg, segSize))
                guesses = ann.run(seg)
                charIndex = cap.argmax(guesses)
                ratings.append((guesses[charIndex], charIndex, index, bounds, seg))
        best = max(ratings, key=itemgetter(0))
        result += charset[best[1]]
        bestChannel = channels[best[2]]
        cv.SetImageROI(bestChannel, best[3])
        cv.Set(bestChannel, 96, bestChannel)
        cv.ResetImageROI(bestChannel)
        bestBounds.append(best[3])
        bestW = best[3][2]
        x -= bestW
        #print ann.run(best[4])
    cap.processExtras([cap.drawComponents(image, bestBounds)], addr, extras, cap.CAP_STAGE_RECOGNISE)
    return result[::-1]
Пример #4
0
def segment(image, addr, extras):
    clone = cv.CloneImage(image)
    log = cap.logger(extras, image)
    components = cap.findCCs(image, erasecol=0, doContinue=None, doSkip=lambda comp: comp[0] <= 25)
    log.log(cap.drawComponents(clone, components))
    if len(components) != nSegs:
        components = cap.joinCCs(components, cap.rectsIntersectH)
        log.log(cap.drawComponents(clone, components))
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_SEGMENT)
    assert(len(components) == nSegs)
    segments = map(lambda comp: comp[3], components)
    return segments
Пример #5
0
def preprocess(image, addr, extras):
    log = cap.logger(extras, image)
    image = cap.doResize(image, factor, cv.CV_INTER_CUBIC)
    log.log(image)
    image = undistort(image)
    log.log(image)
    cv.Threshold(image, image, 200, 255, cv.CV_THRESH_BINARY)
    log.log(image)
    image = cap.repaintCCs(image, doRepaint=lambda comp, col: comp[0] <= 10 and col <= 128)
    log.log(image)
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_PREPROCESS)
    return image
Пример #6
0
def preprocess(image, addr, extras):
    log = cap.logger(extras, image)
    image = cap.doResize(image, factor, cv.CV_INTER_CUBIC)
    log.log(image)
    image = undistort(image)
    log.log(image)
    cv.Threshold(image, image, 200, 255, cv.CV_THRESH_BINARY)
    log.log(image)
    image = cap.repaintCCs(
        image, doRepaint=lambda comp, col: comp[0] <= 10 and col <= 128)
    log.log(image)
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_PREPROCESS)
    return image
Пример #7
0
def preprocess(image, addr, extras):
    log = cap.logger(extras, image)
    alpha, dark = rotAngle(image)
    log.log(dark, False)
    clear = clearNoise(image)
    log.log(clear, False)
    straight = cap.doRotate(clear, -alpha, fillval=0, resize=False, interpolation=cv.CV_INTER_NN)
    #cv.Threshold(straight, straight, 128, 255, cv.CV_THRESH_BINARY)
    log.log(straight)
    cv.Dilate(straight, straight)
    cv.Erode(straight, straight)
    log.log(straight)
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_PREPROCESS)
    return straight
Пример #8
0
def segment(image, addr, extras):
    clone = cv.CloneImage(image)
    log = cap.logger(extras, image)
    components = cap.findCCs(image,
                             erasecol=0,
                             doContinue=None,
                             doSkip=lambda comp: comp[0] <= 25)
    log.log(cap.drawComponents(clone, components))
    if len(components) != nSegs:
        components = cap.joinCCs(components, cap.rectsIntersectH)
        log.log(cap.drawComponents(clone, components))
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_SEGMENT)
    assert (len(components) == nSegs)
    segments = map(lambda comp: comp[3], components)
    return segments
Пример #9
0
def segment(image, addr, extras):
    log = cap.logger(extras, image)
    clone = cv.CloneImage(image)
    components = cap.findCCs(image, 0, lambda col: col <= 128, lambda comp: comp[0] < 10)
    log.log(cap.drawComponents(clone, components), False)
    components = cap.joinCCs(components, cap.rectsIntersectH)
    log.log(cap.drawComponents(clone, components), False)
    assert(len(components) <= nSegs)
    parts = cap.partsFromnSegs5(components)
    components = cap.spltCCs(components, parts, projRadius=4, thresh=1)
    log.log(cap.drawComponents(clone, components), False)
    segments = map(lambda comp: comp[3], components)
    log.log(cap.joinImagesH(segments), False)
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_SEGMENT)
    return segments
Пример #10
0
def segment(image, addr, extras):
    clone = cv.CloneImage(image)
    log = cap.logger(extras, image)
    components = cap.findCCs(image, erasecol=255, doContinue=lambda col: col == 255, doSkip=lambda comp: comp[0] <= 20)
    #components = cap.spltCCs(components, cap.partsFromSegW(components, 20), projRadius=5, thresh=1)
    log.log(cap.drawComponents(clone, components))
    while len(components) > nSegs:
        smallest = cap.argmin(components, itemgetter(0))
        del(components[smallest])
    assert(len(components) == nSegs)
    log.log(cap.drawComponents(clone, components))
    segments = map(lambda comp: comp[3], components)
    log.log(cap.joinImagesH(segments))
    segments = map(deRotate, segments)
    log.log(cap.joinImagesH(segments))
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_SEGMENT)
    return segments
Пример #11
0
def preprocess(image, addr, extras):
    log = cap.logger(extras, image)
    alpha, dark = rotAngle(image)
    log.log(dark, False)
    clear = clearNoise(image)
    log.log(clear, False)
    straight = cap.doRotate(clear,
                            -alpha,
                            fillval=0,
                            resize=False,
                            interpolation=cv.CV_INTER_NN)
    #cv.Threshold(straight, straight, 128, 255, cv.CV_THRESH_BINARY)
    log.log(straight)
    cv.Dilate(straight, straight)
    cv.Erode(straight, straight)
    log.log(straight)
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_PREPROCESS)
    return straight
Пример #12
0
def segment(image, addr, extras):
    clone = cv.CloneImage(image)
    log = cap.logger(extras, image)
    components = cap.findCCs(image,
                             erasecol=255,
                             doContinue=lambda col: col == 255,
                             doSkip=lambda comp: comp[0] <= 20)
    #components = cap.spltCCs(components, cap.partsFromSegW(components, 20), projRadius=5, thresh=1)
    log.log(cap.drawComponents(clone, components))
    while len(components) > nSegs:
        smallest = cap.argmin(components, itemgetter(0))
        del (components[smallest])
    assert (len(components) == nSegs)
    log.log(cap.drawComponents(clone, components))
    segments = map(lambda comp: comp[3], components)
    log.log(cap.joinImagesH(segments))
    segments = map(deRotate, segments)
    log.log(cap.joinImagesH(segments))
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_SEGMENT)
    return segments
Пример #13
0
def preprocess(image, addr, extras):
    log = cap.logger(extras, image)
    image = removeLightColors(image)
    log.log(image)
    image = remapColors(image)
    log.log(image)
    image = smoothNoise2(image)
    log.log(image)
    image = cap.smoothNoise1(image)
    log.log(image)
    mask = getNoiseMask(image, 15, 4)
    cv.Or(mask, findColor(image, myunkn), mask)
    log.log(mask)
    image = doInpaint(image, mask)
    log.log(image)
    image = sharpenColors(image)
    log.log(image)
    image = cap.repaintCCs(image, doRepaint=lambda comp, col: comp[0] <= 5 or comp[2][2] <= 2 or comp[2][3] <= 2)
    log.log(image)
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_PREPROCESS)
    return image
Пример #14
0
def segment(image, addr, extras):
    log = cap.logger(extras, image)
    components = findComponents(image, log)
    log.log(cap.drawComponents(image, components))
    angles = map(lambda comp: findAngle(comp, log), components)
    angles = filter(lambda angle: angle != 0.0 and abs(angle) <= 27.0, angles)
    if len(angles) != 0:
        angle = fsum(angles) / len(angles)
        image = cap.doRotate(image, -angle, fillval=255, interpolation=cv.CV_INTER_NN)
        log.log(image)
        components = findComponents(image)
    assert(len(components) <= nSegs)
    log.log(cap.drawComponents(image, components))
    #components = cap.spltCCs(components, cap.partsFromnSegs5(components), projRadius=5, thresh=2)
    #components = filtCCs(components, (15, 15), 200, 0.2)
    components = map(lambda comp: addBackground(comp, image, bgcolor=96), components)
    segments = map(lambda comp: comp[3], components)
    segments = map(removeBadBackground, segments)
    log.log(cap.joinImagesH(segments))
    segments = map(lambda seg: cap.smoothNoise1(seg, bgcolor=0), segments)
    log.log(cap.joinImagesH(segments))
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_SEGMENT)
    return segments
Пример #15
0
def preprocess(image, addr, extras):
    log = cap.logger(extras, image)
    image = removeLightColors(image)
    log.log(image)
    image = remapColors(image)
    log.log(image)
    image = smoothNoise2(image)
    log.log(image)
    image = cap.smoothNoise1(image)
    log.log(image)
    mask = getNoiseMask(image, 15, 4)
    cv.Or(mask, findColor(image, myunkn), mask)
    log.log(mask)
    image = doInpaint(image, mask)
    log.log(image)
    image = sharpenColors(image)
    log.log(image)
    image = cap.repaintCCs(image,
                           doRepaint=lambda comp, col: comp[0] <= 5 or comp[2][
                               2] <= 2 or comp[2][3] <= 2)
    log.log(image)
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_PREPROCESS)
    return image
Пример #16
0
def preprocess(image, addr, extras):
    log = cap.logger(extras, image)
    image = cutLetters(image, 116, log)
    log.log(image)
    cap.processExtras(log.steps, addr, extras, cap.CAP_STAGE_PREPROCESS)
    return image
Пример #17
0
def recognise(segments, addr, extras):
    allParts = cap.partsFromnSegs5(segments)
    result = splitRecogniseAll(segments, allParts, shiftRadius=4)
    cap.processExtras([cap.joinImagesH(segments)], addr, extras, cap.CAP_STAGE_RECOGNISE)
    return result
Пример #18
0
def recognise(segments, addr, extras):
    allParts = cap.partsFromnSegs5(segments)
    result = splitRecogniseAll(segments, allParts, shiftRadius=4)
    cap.processExtras([cap.joinImagesH(segments)], addr, extras,
                      cap.CAP_STAGE_RECOGNISE)
    return result