示例#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
文件: segment.py 项目: woto/EPC
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
示例#3
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
示例#4
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
示例#5
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]
示例#6
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]
示例#7
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
示例#8
0
def segmentChannel(ch, log=None):
    clone = cv.CloneImage(ch)
    components = cap.findCCs(ch, erasecol=0)
    joiner = lambda rect1, rect2: (cap.rectsIntersectH(rect1, rect2) and cap.distV(rect1, rect2) <= 6) or \
                                  (rect1[2] <= 10 and rect2[2] <= 10 and cap.distH(rect1, rect2) <= 6) or \
                                  ((rect1[2] <= 6 or rect2[2] <= 6)  and cap.distH(rect1, rect2) <= 6)
    components = cap.joinCCs(components, joiner)
    components = filtCCs(components, (15, 15), 200, 0.2)
    if log is not None:
        log.log(cap.drawComponents(clone, components))
    return components
示例#9
0
文件: segment.py 项目: woto/EPC
def segmentChannel(ch, log=None):
    clone = cv.CloneImage(ch)
    components = cap.findCCs(ch, erasecol=0)
    joiner = lambda rect1, rect2: (cap.rectsIntersectH(rect1, rect2) and cap.distV(rect1, rect2) <= 6) or \
                                  (rect1[2] <= 10 and rect2[2] <= 10 and cap.distH(rect1, rect2) <= 6) or \
                                  ((rect1[2] <= 6 or rect2[2] <= 6)  and cap.distH(rect1, rect2) <= 6)
    components = cap.joinCCs(components, joiner)
    components = filtCCs(components, (15, 15), 200, 0.2)
    if log is not None:
        log.log(cap.drawComponents(clone, components))
    return components
示例#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
文件: segment.py 项目: woto/EPC
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