use of com.joliciel.jochre.JochreSession in project jochre by urieli.
the class ShapeImplTest method testGetHeight.
@Test
public void testGetHeight() throws Exception {
System.setProperty("config.file", "src/test/resources/test.conf");
ConfigFactory.invalidateCaches();
Config config = ConfigFactory.load();
JochreSession jochreSession = new JochreSession(config);
Shape shape = new Shape(jochreSession);
shape.setTop(10);
shape.setBottom(40);
assertEquals(31, shape.getHeight());
}
use of com.joliciel.jochre.JochreSession in project jochre by urieli.
the class ShapeImplTest method testGetBrightnessTotalsBySectorMidPixelBreaks.
@Test
public void testGetBrightnessTotalsBySectorMidPixelBreaks() throws Exception {
System.setProperty("config.file", "src/test/resources/test.conf");
ConfigFactory.invalidateCaches();
Config config = ConfigFactory.load();
JochreSession jochreSession = new JochreSession(config);
Shape shape = new Shape(jochreSession);
final int top = 0;
final int bottom = 7;
final int left = 0;
final int right = 5;
int[] pixels = new int[] { // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, 245, 245, 245, 245, 245, 245 };
ImagePixelGrabber pixelGrabber = new ImagePixelGrabberMock(pixels, right - left + 1, bottom - top + 1);
final GroupOfShapes group = mock(GroupOfShapes.class);
final RowOfShapes row = mock(RowOfShapes.class);
final Paragraph paragraph = mock(Paragraph.class);
final JochreImage image = mock(JochreImage.class);
final BufferedImage shapeImage = mock(BufferedImage.class);
when(group.getId()).thenReturn(1);
when(group.getRow()).thenReturn(row);
when(row.getParagraph()).thenReturn(paragraph);
when(paragraph.getImage()).thenReturn(image);
when(image.normalize(anyInt())).then(returnsFirstArg());
when(image.isLeftToRight()).thenReturn(true);
shape.setPixelGrabber(pixelGrabber);
shape.setGroup(group);
shape.setTop(0);
shape.setBottom(7);
shape.setLeft(0);
shape.setRight(5);
shape.setMeanLine(1);
shape.setBaseLine(6);
shape.setImage(shapeImage);
double[][] totals = shape.getBrightnessBySection(5, 5, 1, SectionBrightnessMeasurementMethod.RAW);
LOG.debug("Pixels:");
for (int y = 0; y < shape.getHeight(); y++) {
String pixelsStr = "";
for (int x = 0; x < shape.getWidth(); x++) {
pixelsStr += shape.getPixel(x, y) + ",";
}
LOG.debug(pixelsStr);
}
LOG.debug("Brightness totals by sector:");
for (int y = 0; y < totals[0].length; y++) {
String brightnessTotalsStr = "";
for (int x = 0; x < totals.length; x++) {
brightnessTotalsStr += totals[x][y] + ",";
}
LOG.debug(brightnessTotalsStr);
}
for (int y = 0; y < totals[0].length; y++) {
for (int x = 0; x < totals.length; x++) {
double expected = 360.0 / 25.0;
if (y == 0 || y == totals[0].length - 1)
expected = 60.0 / 5.0;
assertEquals("For x=" + x + ",y=" + y + " expected " + expected + " but was " + totals[x][y], expected, totals[x][y], 0.1);
}
}
}
use of com.joliciel.jochre.JochreSession in project jochre by urieli.
the class ShapeImplTest method testGetBrightnessTotalsBySectorWithSquare.
@Test
public void testGetBrightnessTotalsBySectorWithSquare() throws Exception {
System.setProperty("config.file", "src/test/resources/test.conf");
ConfigFactory.invalidateCaches();
Config config = ConfigFactory.load();
JochreSession jochreSession = new JochreSession(config);
Shape shape = new Shape(jochreSession);
final int top = 0;
final int bottom = 5;
final int left = 0;
final int right = 5;
int[] pixels = new int[] { // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245, // row
245 };
ImagePixelGrabber pixelGrabber = new ImagePixelGrabberMock(pixels, right - left + 1, bottom - top + 1);
final GroupOfShapes group = mock(GroupOfShapes.class);
final RowOfShapes row = mock(RowOfShapes.class);
final Paragraph paragraph = mock(Paragraph.class);
final JochreImage image = mock(JochreImage.class);
final BufferedImage shapeImage = mock(BufferedImage.class);
when(group.getId()).thenReturn(1);
when(group.getRow()).thenReturn(row);
when(row.getParagraph()).thenReturn(paragraph);
when(paragraph.getImage()).thenReturn(image);
when(image.normalize(anyInt())).then(returnsFirstArg());
when(image.isLeftToRight()).thenReturn(true);
shape.setPixelGrabber(pixelGrabber);
shape.setGroup(group);
shape.setTop(0);
shape.setBottom(5);
shape.setLeft(0);
shape.setRight(5);
shape.setMeanLine(1);
shape.setBaseLine(4);
shape.setImage(shapeImage);
double[][] totals = shape.getBrightnessBySection(6, 8, 0.5, 0.5, SectionBrightnessMeasurementMethod.RAW);
LOG.debug("Pixels:");
for (int y = 0; y < shape.getHeight(); y++) {
String pixelsStr = "";
for (int x = -1; x < shape.getWidth(); x++) {
pixelsStr += shape.getPixelInShape(x, y) + ",";
}
LOG.debug(pixelsStr);
}
LOG.debug("Brightness totals by sector:");
for (int y = 0; y < totals[0].length; y++) {
String brightnessTotalsStr = "";
for (int x = 0; x < totals.length; x++) {
brightnessTotalsStr += totals[x][y] + ",";
}
LOG.debug(brightnessTotalsStr);
}
for (int y = 0; y < totals[0].length; y++) {
for (int x = 0; x < totals.length; x++) {
double expected = 160.0 / 16.0;
if (y < 1)
expected = 0.0;
else if (y > 6)
expected = 0;
assertEquals("For x=" + x + ",y=" + y + " expected " + expected + " but was " + totals[x][y], expected, totals[x][y], 0.1);
}
}
}
use of com.joliciel.jochre.JochreSession in project jochre by urieli.
the class VectorizerImplTest method testGetLongestLines.
@Test
public void testGetLongestLines() throws Exception {
System.setProperty("config.file", "src/test/resources/test.conf");
ConfigFactory.invalidateCaches();
Config config = ConfigFactory.load();
JochreSession jochreSession = new JochreSession(config);
final int threshold = 100;
final int maxLines = 60;
final int whiteGapFillFactor = 5;
int[] pixels = { // row
0, // row
1, // row
1, // row
0, // row
0, // row
1, // row
1, // row
1, // row
0, // row
1, // row
1, // row
1, // row
0, // row
1, // row
1, // row
1, // row
0, // row
0, // row
1, // row
1, // row
0, // row
0, // row
1, // row
1, // row
0, // row
0, // row
1, // row
1, // row
0, // row
1, // row
1, // row
0, // row
0, // row
0, // row
0, // row
1, // row
1, // row
1, // row
1, // row
0, // row
0, // row
0, // row
0, // row
1, // row
1, // row
1, // row
0, // row
0, // row
0, // row
0, // row
1, // row
1, // row
1, // row
0, // row
0, // row
0, // row
1, // row
1, // row
1, // row
1, // row
1, // row
0, // row
0, // row
0 };
Shape shape = new ShapeMock(pixels, 8, 8, jochreSession);
BitSet outline = new BitSet(64);
int[] outlinePixels = { // row
0, // row
1, // row
1, // row
0, // row
0, // row
1, // row
1, // row
1, // row
0, // row
1, // row
0, // row
1, // row
0, // row
1, // row
0, // row
1, // row
0, // row
0, // row
1, // row
1, // row
0, // row
0, // row
1, // row
1, // row
0, // row
0, // row
1, // row
1, // row
0, // row
1, // row
1, // row
0, // row
0, // row
0, // row
0, // row
1, // row
1, // row
0, // row
1, // row
0, // row
0, // row
0, // row
0, // row
1, // row
0, // row
1, // row
0, // row
0, // row
0, // row
0, // row
1, // row
0, // row
1, // row
0, // row
0, // row
0, // row
1, // row
1, // row
1, // row
1, // row
1, // row
0, // row
0, // row
0 };
for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) {
outline.set(y * 8 + x, outlinePixels[y * 8 + x] == 1);
}
Vectorizer vectorizer = new Vectorizer();
vectorizer.setWhiteGapFillFactor(whiteGapFillFactor);
List<LineSegment> lines = vectorizer.getLongestLines(shape, outline, maxLines, threshold);
assertEquals(maxLines, lines.size());
}
use of com.joliciel.jochre.JochreSession in project jochre by urieli.
the class VectorizerImplTest method testArrayListize.
@Test
public void testArrayListize() throws Exception {
System.setProperty("config.file", "src/test/resources/test.conf");
ConfigFactory.invalidateCaches();
Config config = ConfigFactory.load();
JochreSession jochreSession = new JochreSession(config);
final int threshold = 100;
final int whiteGapFillFactor = 5;
int[] pixels = { // row
0, // row
1, // row
1, // row
0, // row
0, // row
1, // row
1, // row
1, // row
0, // row
1, // row
1, // row
1, // row
0, // row
1, // row
1, // row
1, // row
0, // row
0, // row
1, // row
1, // row
0, // row
0, // row
1, // row
1, // row
0, // row
0, // row
1, // row
1, // row
0, // row
1, // row
1, // row
0, // row
0, // row
0, // row
0, // row
1, // row
1, // row
1, // row
1, // row
0, // row
0, // row
0, // row
0, // row
1, // row
1, // row
1, // row
0, // row
0, // row
0, // row
0, // row
1, // row
1, // row
1, // row
0, // row
0, // row
0, // row
1, // row
1, // row
1, // row
1, // row
1, // row
0, // row
0, // row
0 };
ShapeMock shape = new ShapeMock(pixels, 8, 8, jochreSession);
int[] outlinePixels = { // row 0
0, // row 0
1, // row 0
1, // row 0
0, // row 0
0, // row 0
1, // row 0
1, // row 0
1, // row 1
0, // row 1
1, // row 1
0, // row 1
1, // row 1
0, // row 1
1, // row 1
0, // row 1
1, // row 2
0, // row 2
0, // row 2
1, // row 2
1, // row 2
0, // row 2
0, // row 2
1, // row 2
1, // row 3
0, // row 3
0, // row 3
1, // row 3
1, // row 3
0, // row 3
1, // row 3
1, // row 3
0, // row 4
0, // row 4
0, // row 4
0, // row 4
1, // row 4
1, // row 4
0, // row 4
1, // row 4
0, // row 5
0, // row 5
0, // row 5
0, // row 5
1, // row 5
0, // row 5
1, // row 5
0, // row 5
0, // row 6
0, // row 6
0, // row 6
1, // row 6
0, // row 6
1, // row 6
0, // row 6
0, // row 6
0, // row 7
1, // row 7
1, // row 7
1, // row 7
1, // row 7
1, // row 7
0, // row 7
0, // row 7
0 };
BitSet outline = new BitSet();
for (int i = 0; i < 8 * 8; i++) outline.set(i, outlinePixels[i] == 1);
final JochreImage image = mock(JochreImage.class);
when(image.getBlackThreshold()).thenReturn(threshold);
shape.setOutline(outline);
shape.setJochreImage(image);
Vectorizer vectorizer = new Vectorizer();
vectorizer.setWhiteGapFillFactor(whiteGapFillFactor);
List<LineSegment> lines = vectorizer.vectorize(shape);
int i = 0;
for (LineSegment lineSegment : lines) {
double slope = (double) (lineSegment.getEndY() - lineSegment.getStartY()) / (double) (lineSegment.getEndX() - lineSegment.getStartX());
LOG.debug("Line " + i++ + "(" + lineSegment.getStartX() + "," + lineSegment.getStartY() + ") " + "(" + lineSegment.getEndX() + "," + lineSegment.getEndY() + "). Length = " + lineSegment.getLength() + ", Slope = " + slope);
}
}
Aggregations