Java Code Examples for ij.process.ByteProcessor#getWidth()

The following examples show how to use ij.process.ByteProcessor#getWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SheetDiff.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: TableUtil.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 3
Source File: Patch.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
/** Must call updateMipMaps() afterwards. Set it to null to remove it.
 * @return true if the alpha mask file was written successfully. */
public synchronized boolean setAlphaMask(final ByteProcessor bp) throws IllegalArgumentException {
	if (null == bp) {
		alpha_mask_id = 0;
		return true;
	}

	// Check that the alpha mask represented by argument bp
	// has the appropriate dimensions:
	if (o_width != bp.getWidth() || o_height != bp.getHeight()) {
		throw new IllegalArgumentException("Need a mask of identical dimensions as the original image.");
	}

	final long amID = project.getLoader().getNextBlobId();
	if (writeAlphaMask(bp, amID)) {
		this.alpha_mask_id = amID;
		return true;
	} else {
		Utils.log("Could NOT write the alpha mask file for patch #" + id);
	}

	return false;
}
 
Example 4
Source File: GlobalFilter.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public ByteProcessor filteredImage ()
{
    ByteProcessor ip = new ByteProcessor(source.getWidth(), source.getHeight());

    for (int y = 0, h = ip.getHeight(); y < h; y++) {
        for (int x = 0, w = ip.getWidth(); x < w; x++) {
            if (isFore(x, y)) {
                ip.set(x, y, FOREGROUND);
            } else {
                ip.set(x, y, BACKGROUND);
            }
        }
    }

    return ip;
}
 
Example 5
Source File: Utils.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
static public final BufferedImage convertToBufferedImage(final ByteProcessor bp) {
	bp.setMinAndMax(0, 255); // TODO what is this doing here? The ByteProcessor.setMinAndMax is destructive, it expands the pixel values to the desired range.
	final Image img = bp.createImage();
	if (img instanceof BufferedImage) return (BufferedImage)img;
	//else:
	final BufferedImage bi = new BufferedImage(bp.getWidth(), bp.getHeight(), BufferedImage.TYPE_BYTE_INDEXED, Loader.GRAY_LUT);
	bi.createGraphics().drawImage(img, 0, 0, null);
	return bi;
}
 
Example 6
Source File: Utils.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
/** A method that circumvents the findMinAndMax when creating a float processor from an existing processor.  Ignores color calibrations and does no scaling at all. */
static public final FloatProcessor fastConvertToFloat(final ByteProcessor ip) {
	final byte[] pix = (byte[])ip.getPixels();
	final float[] data = new float[pix.length];
	for (int i=0; i<pix.length; i++) data[i] = pix[i]&0xff;
	final FloatProcessor fp = new FloatProcessorT2(ip.getWidth(), ip.getHeight(), data, ip.getColorModel(), ip.getMin(), ip.getMax());
	return fp;
}
 
Example 7
Source File: DownsamplerTest.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
final private static void testOutside( ByteProcessor a )
	{
		while( a.getWidth() > 32 )
		{
			a = Downsampler.downsampleOutside( a );
//			new ImagePlus( "alpha " + ba.a.getWidth(), ba.a ).show();
//			new ImagePlus( "outside " + ba.b.getWidth(), ba.b ).show();
		}
	}
 
Example 8
Source File: WatershedGrayLevelTest.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
private void merge (ByteProcessor img,
                    boolean[][] lines)
{
    for (int y = 0, h = img.getHeight(); y < h; y++) {
        for (int x = 0, w = img.getWidth(); x < w; x++) {
            if (lines[x][y]) {
                img.set(x, y, PixelSource.BACKGROUND);
            }
        }
    }
}
 
Example 9
Source File: RunTable.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Report a drawing of the table.
 *
 * @return a drawing of the table
 */
public String dumpOf ()
{
    StringBuilder sb = new StringBuilder();

    sb.append(String.format("%s%n", this));

    // Prepare output buffer
    ByteProcessor buffer = getBuffer();

    // Print the buffer
    sb.append('+');

    for (int c = 0; c < width; c++) {
        sb.append('=');
    }

    sb.append(String.format("+%n"));

    for (int row = 0; row < height; row++) {
        sb.append('|');

        for (int col = 0; col < buffer.getWidth(); col++) {
            sb.append((buffer.get(col, row) == BACKGROUND) ? '-' : 'X');
        }

        sb.append(String.format("|%n"));
    }

    sb.append('+');

    for (int c = 0; c < width; c++) {
        sb.append('=');
    }

    sb.append(String.format("+"));

    return sb.toString();
}
 
Example 10
Source File: RunTableFactory.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Report the RunTable created with the runs retrieved from the provided source.
 *
 * @param source the source to read runs from.
 * @return a populated RunTable
 */
public RunTable createTable (ByteProcessor source)
{
    // ROI is defined as the whole source
    final Rectangle roi = new Rectangle(0, 0, source.getWidth(), source.getHeight());

    return createTable(source, roi);
}
 
Example 11
Source File: Template.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Collect the image foreground pixels located under the template foreground areas.
 *
 * @param box   absolute positioning of template box in global image
 * @param image global image to be read
 * @return the collection of foreground pixels, relative to template box.
 */
public List<Point> getForegroundPixels (Rectangle box,
                                        ByteProcessor image)
{
    final int imgWidth = image.getWidth();
    final int imgHeight = image.getHeight();
    final List<Point> fores = new ArrayList<>();

    for (PixelDistance pix : keyPoints) {
        if (pix.d != 0) {
            continue;
        }

        int nx = box.x + pix.x;
        int ny = box.y + pix.y;

        if ((nx >= 0) && (nx < imgWidth) && (ny >= 0) && (ny < imgHeight)) {
            // Check if we have some image foreground there
            int val = image.get(nx, ny);

            if (val == 0) {
                fores.add(new Point(pix.x, pix.y));
            }
        }
    }

    return fores;
}
 
Example 12
Source File: AbstractGrayFilter.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Apply this filter on a given input image.
 *
 * @param input the input image
 * @return the filtered image
 */
public ByteProcessor filter (final ByteProcessor input)
{
    final ByteProcessor output = new ByteProcessor(input.getWidth(), input.getHeight());
    StopWatch watch = new StopWatch(getClass().getSimpleName());
    watch.start("filter");

    filter(input, output);

    ///watch.print();
    return output;
}
 
Example 13
Source File: AreaMask.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
ForeCounter (ByteProcessor filter,
             Wrapper<Integer> fore)
{
    this.filter = filter;
    this.fore = fore;
    filterWidth = filter.getWidth();
    filterHeight = filter.getHeight();
}
 
Example 14
Source File: ChamferDistance.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
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
        }
    }
}
 
Example 15
Source File: ChamferDistance.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
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
            }
        }
    }
}
 
Example 16
Source File: MedianGrayFilter.java    From audiveris with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void filter (final ByteProcessor input,
                    final ByteProcessor output)
{
    final int width = input.getWidth();
    final int height = input.getHeight();
    final int[] histogram = new int[256];
    Arrays.fill(histogram, 0);

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // To address specific behavior at image boundaries,
            // reduce radius to not use pixels outside the image.
            int rad = radius;

            if ((x - rad) < 0) {
                rad = x;
            }

            if ((y - rad) < 0) {
                rad = y;
            }

            if ((x + rad) >= width) {
                rad = width - 1 - x;
            }

            if ((y + rad) >= height) {
                rad = height - 1 - y;
            }

            Arrays.fill(histogram, 0); // Brute force!

            for (int i = x - rad; i <= (x + rad); i++) {
                for (int j = y - rad; j <= (y + rad); j++) {
                    int val = input.get(i, j);
                    histogram[val]++;
                }
            }

            // Pick up the median value
            final int side = (2 * rad) + 1;
            final int medianCount = ((side * side) + 1) / 2;
            int median = 255;
            int sum = 0;

            while (sum < medianCount) {
                sum += histogram[median];
                median--;
            }

            output.set(x, y, median + 1);
        }
    }
}
 
Example 17
Source File: DownsamplerMipMaps.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
static private final ImageBytes asBytes(final ByteProcessor bp, final ByteProcessor mask) {
	 return new ImageBytes(new byte[][]{(byte[])bp.getPixels(), (byte[])mask.getPixels()}, bp.getWidth(), bp.getHeight());
}
 
Example 18
Source File: DownsamplerMipMaps.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
static private final ImageBytes asBytes(final ByteProcessor bp) {
	 return new ImageBytes(new byte[][]{(byte[])bp.getPixels()}, bp.getWidth(), bp.getHeight());
}
 
Example 19
Source File: IntegralImageMipMaps.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings({ "unused", "unchecked", "null" })
private static final BufferedImage[] createGRAY8(
		final Patch patch,
		final ByteProcessor ip,
		final ByteProcessor mask) {
	final int w = ip.getWidth();
	final int h = ip.getHeight();
	final int[] dims = new int[]{w, h};
	final ScaleAreaAveraging2d<LongType, UnsignedByteType> saai, saam;
	{
		// Integral of the image
		final IntegralImage<UnsignedByteType, LongType> oa =
				new IntegralImage<UnsignedByteType, LongType>(
						wrap((byte[])ip.getPixels(), dims),
						new LongType(), new IntegerTypeConverter<UnsignedByteType, LongType>());
		oa.process();
		saai = new ScaleAreaAveraging2d<LongType, UnsignedByteType>(
				oa.getResult(), new UnsignedByteType(), dims);

		// Integral of the mask, if any
		if (null != mask) {
			final IntegralImage<UnsignedByteType, LongType> ma = new IntegralImage<UnsignedByteType, LongType>(
					wrap((byte[])mask.getPixels(), dims),
					new LongType(), new IntegerTypeConverter<UnsignedByteType, LongType>());
			ma.process();
			saam = new ScaleAreaAveraging2d<LongType, UnsignedByteType>(ma.getResult(), new UnsignedByteType(), dims);
		} else {
			saam = null;
		}
	}
	
	// Generate images
	final BufferedImage[] bis = new BufferedImage[Loader.getHighestMipMapLevel(patch) + 1];
	//
	if (null == saam) { // mask is null
		// Save images as grayscale
		bis[0] = ImageSaver.createGrayImage((byte[])ip.getPixels(), w, h); // sharing the byte[]
		for (int i=1; i<bis.length; i++) {
			final int K = (int) Math.pow(2, i),
		          wk = w / K,
		          hk = h / K;
			// An image of the scaled size
			saai.setOutputDimensions(wk, hk);
			saai.process();
			bis[i] = ImageSaver.createGrayImage(((Array<UnsignedByteType,ByteArray>) saai.getResult().getContainer()).update(null).getCurrentStorageArray(), wk, hk);
		}
	} else {
		// Save images as RGBA, where all 3 color channels are the same
		bis[0] = ImageSaver.createARGBImage(blend((byte[])ip.getPixels(), (byte[])mask.getPixels()), w, h);
		for (int i=1; i<bis.length; i++) {
			final int K = (int) Math.pow(2, i),
		          wk = w / K,
		          hk = h / K;
			// An image of the scaled size
			saai.setOutputDimensions(wk, hk);
			saai.process();
			// A mask of the scaled size
			saam.setOutputDimensions(wk, hk);
			saam.process();
			//
			bis[i] = ImageSaver.createARGBImage(
				blend(
					((Array<UnsignedByteType,ByteArray>) saai.getResult().getContainer()).update(null).getCurrentStorageArray(),
					((Array<UnsignedByteType,ByteArray>) saam.getResult().getContainer()).update(null).getCurrentStorageArray()),
				wk, hk);
		}
	}
	
	return bis;
}
 
Example 20
Source File: IntegralImageMipMaps.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
private static final ImageBytes[] fastCreateGRAY8(
		final Patch patch,
		final ByteProcessor ip,
		final ByteProcessor mask) {
	final int w = ip.getWidth();
	final int h = ip.getHeight();
	
	// Integrals
	final long[] ii = FastIntegralImage.longIntegralImage((byte[])ip.getPixels(), w, h);
	final long[] mi = null == mask ? null : FastIntegralImage.longIntegralImage((byte[])mask.getPixels(), w, h);
	
	// Generate images
	final ImageBytes[] bis = new ImageBytes[Loader.getHighestMipMapLevel(patch) + 1];
	//
	if (null == mask) { // mask is null
		// Save images as grayscale
		bis[0] = new ImageBytes(new byte[][]{(byte[])ip.getPixels()}, ip.getWidth(), ip.getHeight());
		for (int i=1; i<bis.length; i++) {
			final int K = (int) Math.pow(2, i),
		              wk = w / K,
		              hk = h / K;
			// An image of the scaled size
			bis[i] = new ImageBytes(new byte[][]{FastIntegralImage.scaleAreaAverage(ii, w+1, h+1, wk, hk)}, wk, hk);
		}
	} else {
		// Save images as RGBA, where all 3 color channels are the same
		bis[0] = new ImageBytes(new byte[][]{(byte[])ip.getPixels(), (byte[])mask.getPixels()}, ip.getWidth(), ip.getHeight());
		for (int i=1; i<bis.length; i++) {
			final int K = (int) Math.pow(2, i),
		              wk = w / K,
		              hk = h / K;
			//
			bis[i] = new ImageBytes(
					new byte[][]{FastIntegralImage.scaleAreaAverage(ii, w+1, h+1, wk, hk),
					             FastIntegralImage.scaleAreaAverage(mi, w+1, h+1, wk, hk)},
					wk, hk);
		}
	}
	
	return bis;
}