/** * Report pixel value at (x, y) location * * @param x abscissa * @param y ordinate * @return pixel value */ public int getPixel (int x, int y) { return buf.get(x, y); }
/** * Check whether the provided source has at least a gray pixel. * * @param source the source to inspect * @return true if at least one pixel is neither black nor white */ private boolean hasGray (ByteProcessor source) { for (int i = source.getPixelCount() - 1; i >= 0; i--) { int val = source.get(i); if ((val != 0) && (val != 255)) { return true; } } return false; }
/** * Count the number of foreground pixels in the provided image. * * @param filter the binary image * @return the number of foreground pixels */ private int getForeCount (ByteProcessor filter) { final int width = filter.getWidth(); final int height = filter.getHeight(); int count = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (filter.get(x, y) == 0) { count++; } } } return count; }
private void initializeToFore (ByteProcessor input, DistanceTable output) { for (int i = (input.getWidth() * input.getHeight()) - 1; i >= 0; i--) { if (input.get(i) == 0) { output.setValue(i, VALUE_TARGET); // reference pixel -> distance=0 } else { output.setValue(i, VALUE_UNKNOWN); // non-reference pixel -> to be computed } } }
@Override public boolean isFore (int x, int y) { return source.get(x, y) <= threshold; } }
private void initializeToBack (ByteProcessor input, DistanceTable output) { for (int y = 0, h = input.getHeight(); y < h; y++) { for (int x = 0, w = input.getWidth(); x < w; x++) { if (input.get(x, y) == 0) { output.setValue(x, y, VALUE_UNKNOWN); // non-reference pixel -> to be computed } else { output.setValue(x, y, VALUE_TARGET); // reference pixel -> distance=0 } } } }
@Override public final boolean isFore (int coord, int pos) { return source.get(coord, pos) == 0; }
/** * Scan the whole image. */ public void scanImage () { for (int x = 1, w = buf.getWidth(); x < w; x++) { for (int y = 1, h = buf.getHeight(); y < h; y++) { int pix = buf.get(x, y); if ((pix == FOREGROUND) // Basic pixel, not yet processed || isJunction(pix)) { // Junction, perhaps not the best checkJunction(x, y); } } } }
@Override public final boolean isFore (int coord, int pos) { return source.get(pos, coord) == 0; }
final static private int[] createHistogram( final int blockRadius, final int bins, final int blockXCenter, final int blockYCenter, final ByteProcessor src ) { final int[] hist = new int[ bins + 1 ]; final int xMin = Math.max( 0, blockXCenter - blockRadius ); final int yMin = Math.max( 0, blockYCenter - blockRadius ); final int xMax = Math.min( src.getWidth(), blockXCenter + blockRadius + 1 ); final int yMax = Math.min( src.getHeight(), blockYCenter + blockRadius + 1 ); for ( int y = yMin; y < yMax; ++y ) { final int row = src.getWidth() * y; for ( int x = xMin; x < xMax; ++x ) { ++hist[ mpicbg.util.Util.roundPos( src.get( row + x ) / 255.0f * bins ) ]; } } return hist; }
@Override public int get (int x, int y) { return source.get(x, y); }
final static private int[] createHistogram( final int blockRadius, final int bins, final int blockXCenter, final int blockYCenter, final ByteProcessor src ) { final int[] hist = new int[ bins + 1 ]; final int xMin = Math.max( 0, blockXCenter - blockRadius ); final int yMin = Math.max( 0, blockYCenter - blockRadius ); final int xMax = Math.min( src.getWidth(), blockXCenter + blockRadius + 1 ); final int yMax = Math.min( src.getHeight(), blockYCenter + blockRadius + 1 ); for ( int y = yMin; y < yMax; ++y ) { final int row = src.getWidth() * y; for ( int x = xMin; x < xMax; ++x ) { ++hist[ mpicbg.util.Util.roundPos( src.get( row + x ) / 255.0f * bins ) ]; } } return hist; }
@Override public void process (int x, int y) { if ((x >= 0) && (x < filterWidth) && (y >= 0) && (y < filterHeight) && (filter.get(x, y) == 0)) { fore.value++; } } }
/** * Print out a ByteProcessor. * * @param title a title for the print * @param buf the input buffer */ public static void dump (String title, ByteProcessor buf) { final int width = buf.getWidth(); final int height = buf.getHeight(); if (title != null) { System.out.println(title); } final String yFormat = printAbscissae(width, height, 4); for (int y = 0; y < height; y++) { System.out.printf(yFormat, y); for (int x = 0; x < width; x++) { System.out.printf("%4d", buf.get(x, y)); } System.out.println(); } }
private boolean isBackground(int x, int y, ByteProcessor proc) { return (proc.get(x, y) == BACKGROUND); }
/** * Count the immediate neighbors in the provided directions. * * @param x center abscissa * @param y center ordinate * @param dirs which directions to scan * @return the number of non-foreground pixels found */ private int dirNeighbors (int x, int y, int[] dirs) { int n = 0; for (int dir : dirs) { int pix = buf.get(x + dxs[dir], y + dys[dir]); if (pix != BACKGROUND) { n++; } } return n; }
@Override public boolean isFore (int x, int y) { double mean = tile.getMean(x, y); double sqrMean = sqrTile.getMean(x, y); double var = Math.abs(sqrMean - (mean * mean)); double stdDev = Math.sqrt(var); double threshold = getThreshold(mean, stdDev); int pixValue = source.get(x, y); boolean isFore = pixValue <= threshold; return isFore; }
@Override public double[] getFeatures (Glyph glyph, int interline) { final ByteProcessor buffer = ScaledBuffer.getBuffer(glyph, interline); buffer.invert(); // 0 for background, 255 for foreground // Layout: row by row final double[] doubles = new double[length()]; int i = 0; for (int y = 0; y < ScaledBuffer.HEIGHT; y++) { for (int x = 0; x < ScaledBuffer.WIDTH; x++) { doubles[i++] = buffer.get(x, y); } } return doubles; }
/** * Compute the gap vector based on foreground pixels found in gap area. * * @param buf the binary buffer * @return the populated gap vector (in which a 1 value indicates a black pixel) */ public int[] computeVector (ByteProcessor buf) { final Rectangle box = area.getBounds(); for (int x = box.x, xBreak = box.x + box.width; x < xBreak; x++) { for (int y = box.y, yBreak = box.y + box.height; y < yBreak; y++) { if (area.contains(x, y)) { if (0 == buf.get(x, y)) { populateVector(x - box.x, y - box.y); } } } } return vector; }
/** * We use the NO_STAFF source of pixels. * * @return the projection on x-axis */ private IntegerFunction getProjection () { // Staff-free pixel source final ByteProcessor source = system.getSheet().getPicture().getSource( Picture.SourceKey.NO_STAFF); final int xMin = roi.x; final int xMax = (roi.x + roi.width) - 1; final IntegerFunction function = new IntegerFunction(xMin, xMax); for (int x = xMin; x <= xMax; x++) { short cumul = 0; for (int y = roi.y, yBreak = roi.y + roi.height; y < yBreak; y++) { if (source.get(x, y) == 0) { cumul++; } } function.setValue(x, cumul); } return function; }