Search in sources :

Example 71 with Shape

use of com.joliciel.jochre.graphics.Shape in project jochre by urieli.

the class OpeningOnBottomFeature method checkInternal.

@Override
public FeatureResult<Boolean> checkInternal(ShapeWrapper shapeWrapper, RuntimeEnvironment env) {
    Shape shape = shapeWrapper.getShape();
    int leftPoint = (int) ((double) shape.getWidth() * (1.0 / 20.0));
    int rightPoint = (int) ((double) shape.getWidth() * (7.0 / 8.0));
    int openingThreshold = shape.getHeight() - (shape.getHeight() / 4) - 1;
    int wallThreshold = shape.getHeight() - (shape.getHeight() / 7) - 1;
    if (LOG.isTraceEnabled()) {
        LOG.trace("leftPoint: " + leftPoint);
        LOG.trace("rightPoint: " + rightPoint);
        LOG.trace("openingThreshold: " + openingThreshold);
        LOG.trace("wallThreshold: " + wallThreshold);
    }
    boolean foundWall = false;
    boolean foundOpening = false;
    boolean foundAnotherWall = false;
    for (int x = rightPoint; x >= leftPoint; x--) {
        for (int y = shape.getHeight(); y >= openingThreshold; y--) {
            if (!foundWall && y < wallThreshold) {
                break;
            } else if (!foundWall && shape.isPixelBlack(x, y, shape.getJochreImage().getBlackThreshold())) {
                foundWall = true;
                if (LOG.isTraceEnabled())
                    LOG.trace("foundWall x=" + x + ", y=" + y);
                break;
            } else if (foundWall && !foundOpening && shape.isPixelBlack(x, y, shape.getJochreImage().getBlackThreshold())) {
                break;
            } else if (foundWall && !foundOpening && y == openingThreshold) {
                foundOpening = true;
                if (LOG.isTraceEnabled())
                    LOG.trace("foundOpening x=" + x + ", y=" + y);
                break;
            } else if (foundOpening && !foundAnotherWall && y <= wallThreshold) {
                break;
            } else if (foundOpening && !foundAnotherWall && shape.isPixelBlack(x, y, shape.getJochreImage().getBlackThreshold())) {
                foundAnotherWall = true;
                if (LOG.isTraceEnabled())
                    LOG.trace("foundAnotherWall x=" + x + ", y=" + y);
                break;
            }
        }
        if (foundAnotherWall)
            break;
    }
    FeatureResult<Boolean> outcome = this.generateResult(foundAnotherWall);
    return outcome;
}
Also used : Shape(com.joliciel.jochre.graphics.Shape)

Example 72 with Shape

use of com.joliciel.jochre.graphics.Shape in project jochre by urieli.

the class RelativeBottomFeature method checkInternal.

@Override
public FeatureResult<Double> checkInternal(ShapeWrapper shapeWrapper, RuntimeEnvironment env) {
    Shape shape = shapeWrapper.getShape();
    int lineHeight = shape.getBaseLine() - shape.getMeanLine();
    int zeroPoint = (shape.getTop() + shape.getMeanLine()) - lineHeight;
    int onePoint = (shape.getTop() + shape.getBaseLine()) + lineHeight;
    double relativeBottom = 0;
    if (shape.getBottom() <= zeroPoint)
        relativeBottom = 0;
    else if (shape.getBottom() >= onePoint)
        relativeBottom = 1;
    else {
        double totalHeight = lineHeight * 3.0;
        relativeBottom = ((double) (shape.getBottom() - zeroPoint) / totalHeight);
    }
    FeatureResult<Double> outcome = this.generateResult(relativeBottom);
    return outcome;
}
Also used : Shape(com.joliciel.jochre.graphics.Shape)

Example 73 with Shape

use of com.joliciel.jochre.graphics.Shape in project jochre by urieli.

the class SectionBrightnessRatioFeature method checkInternal.

@Override
public FeatureResult<Double> checkInternal(ShapeWrapper shapeWrapper, RuntimeEnvironment env) {
    Shape shape = shapeWrapper.getShape();
    double[][] totals = shape.getBrightnessBySection(5, 5, 1, SectionBrightnessMeasurementMethod.RAW);
    double testTotal = 0;
    double fullTotal = 0;
    for (int i = 0; i < totals.length; i++) {
        for (int j = 0; j < totals[0].length; j++) {
            double brightness = totals[i][j];
            if (testSectors[i][j])
                testTotal += brightness;
            fullTotal += brightness;
        }
    }
    double ratio = 0;
    if (fullTotal > 0) {
        ratio = testTotal / fullTotal;
    }
    if (LOG.isDebugEnabled())
        LOG.trace("Test: " + testTotal + "), Total: " + fullTotal + ", Ratio: " + ratio);
    FeatureResult<Double> outcome = this.generateResult(ratio);
    return outcome;
}
Also used : Shape(com.joliciel.jochre.graphics.Shape)

Example 74 with Shape

use of com.joliciel.jochre.graphics.Shape in project jochre by urieli.

the class BorderlineNeighboursFeature method checkInternal.

@Override
public FeatureResult<Double> checkInternal(ShapePair pair, RuntimeEnvironment env) {
    FeatureResult<Double> result = null;
    FeatureResult<Integer> horizontalToleranceResult = horizontalToleranceFeature.check(pair, env);
    FeatureResult<Integer> verticalToleranceResult = verticalToleranceFeature.check(pair, env);
    if (horizontalToleranceResult != null && verticalToleranceResult != null) {
        int horizontalTolerance = horizontalToleranceResult.getOutcome();
        int verticalTolerance = verticalToleranceResult.getOutcome();
        Shape shape1 = pair.getFirstShape();
        Shape shape2 = pair.getSecondShape();
        JochreImage sourceImage = shape1.getJochreImage();
        // check that the two shapes have dark areas near each other
        Set<Integer> shape1BorderPoints = new HashSet<Integer>();
        int shape1MinBorder = sourceImage.isLeftToRight() ? (shape1.getWidth() - horizontalTolerance) - 1 : 0;
        int shape1MaxBorder = sourceImage.isLeftToRight() ? shape1.getWidth() : horizontalTolerance + 1;
        LOG.trace("shape1MinBorder" + shape1MinBorder);
        LOG.trace("shape1MaxBorder" + shape1MaxBorder);
        StringBuilder sb = new StringBuilder();
        for (int x = shape1MinBorder; x < shape1MaxBorder; x++) {
            for (int y = 0; y < shape1.getHeight(); y++) {
                if (shape1.isPixelBlack(x, y, sourceImage.getBlackThreshold())) {
                    shape1BorderPoints.add(shape1.getTop() + y);
                    sb.append(shape1.getTop() + y);
                    sb.append(',');
                }
            }
        }
        LOG.trace(sb.toString());
        Set<Integer> shape2BorderPoints = new HashSet<Integer>();
        sb = new StringBuilder();
        int shape2MinBorder = sourceImage.isLeftToRight() ? 0 : (shape2.getWidth() - horizontalTolerance) - 1;
        int shape2MaxBorder = sourceImage.isLeftToRight() ? horizontalTolerance + 1 : shape2.getWidth();
        LOG.trace("shape2MinBorder" + shape2MinBorder);
        LOG.trace("shape2MaxBorder" + shape2MaxBorder);
        for (int x = shape2MinBorder; x < shape2MaxBorder; x++) {
            for (int y = 0; y < shape2.getHeight(); y++) {
                if (shape2.isPixelBlack(x, y, sourceImage.getBlackThreshold())) {
                    shape2BorderPoints.add(shape2.getTop() + y);
                    sb.append(shape2.getTop() + y);
                    sb.append(',');
                }
            }
        }
        LOG.trace(sb.toString());
        int numNeighbours1 = 0;
        for (int shape1BorderPoint : shape1BorderPoints) {
            for (int shape2BorderPoint : shape2BorderPoints) {
                if (Math.abs(shape2BorderPoint - shape1BorderPoint) <= verticalTolerance) {
                    numNeighbours1++;
                    break;
                }
            }
        }
        LOG.trace("numNeighbours1: " + numNeighbours1);
        int numNeighbours2 = 0;
        for (int shape2BorderPoint : shape2BorderPoints) {
            for (int shape1BorderPoint : shape1BorderPoints) {
                if (Math.abs(shape1BorderPoint - shape2BorderPoint) <= verticalTolerance) {
                    numNeighbours2++;
                    break;
                }
            }
        }
        LOG.trace("numNeighbours2: " + numNeighbours2);
        LOG.trace("shape1BorderPoints: " + shape1BorderPoints.size());
        LOG.trace("shape2BorderPoints: " + shape2BorderPoints.size());
        double ratio = 0;
        if (shape1BorderPoints.size() + shape2BorderPoints.size() > 0)
            ratio = ((double) numNeighbours1 + numNeighbours2) / (shape1BorderPoints.size() + shape2BorderPoints.size());
        result = this.generateResult(ratio);
    }
    return result;
}
Also used : JochreImage(com.joliciel.jochre.graphics.JochreImage) Shape(com.joliciel.jochre.graphics.Shape) HashSet(java.util.HashSet)

Aggregations

Shape (com.joliciel.jochre.graphics.Shape)74 ArrayList (java.util.ArrayList)22 GroupOfShapes (com.joliciel.jochre.graphics.GroupOfShapes)14 JochreImage (com.joliciel.jochre.graphics.JochreImage)13 Paragraph (com.joliciel.jochre.graphics.Paragraph)9 RowOfShapes (com.joliciel.jochre.graphics.RowOfShapes)9 Decision (com.joliciel.talismane.machineLearning.Decision)8 Test (org.junit.Test)8 JochreSession (com.joliciel.jochre.JochreSession)7 JochrePage (com.joliciel.jochre.doc.JochrePage)7 Config (com.typesafe.config.Config)7 TreeSet (java.util.TreeSet)7 JochreDocument (com.joliciel.jochre.doc.JochreDocument)6 BufferedImage (java.awt.image.BufferedImage)6 ShapeInSequence (com.joliciel.jochre.boundaries.ShapeInSequence)5 ShapeSequence (com.joliciel.jochre.boundaries.ShapeSequence)5 GraphicsDao (com.joliciel.jochre.graphics.GraphicsDao)5 RuntimeEnvironment (com.joliciel.talismane.machineLearning.features.RuntimeEnvironment)5 SplitFeature (com.joliciel.jochre.boundaries.features.SplitFeature)4 JochreException (com.joliciel.jochre.utils.JochreException)4