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;
}
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;
}
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;
}
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;
}
Aggregations