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

The following examples show how to use ij.process.ByteProcessor#set() . 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: DistanceTransform5x5FloatTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public final void testDistanceMap_UntilCorners_Borgefors34() 
{
	ByteProcessor image = new ByteProcessor(7, 7);
	image.setValue(255);
	image.fill();
	image.set(4, 4, 0);
	
	float[] weights = ChamferWeights.BORGEFORS.getFloatWeights();
	DistanceTransform5x5Float algo = new DistanceTransform5x5Float(weights, false);
	ImageProcessor result = algo.distanceMap(image);
	
	assertNotNull(result);
	assertEquals(image.getWidth(), result.getWidth());
	assertEquals(image.getHeight(), result.getHeight());
	assertEquals(16, result.getf(0, 0), .01);
	assertEquals(14, result.getf(6, 0), .01);
	assertEquals(14, result.getf(0, 6), .01);
	assertEquals(8, result.getf(6, 6), .01);
	
	assertEquals(13, result.getf(0, 5), .01);
}
 
Example 2
Source File: DistanceTransform5x5FloatTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public final void testDistanceMap_ChessBoard() 
{
	ByteProcessor image = new ByteProcessor(12, 10);
	image.setBackgroundValue(0);
	image.fill();
	for (int y = 2; y < 8; y++) {
		for (int x = 2; x < 10; x++) {
			image.set(x, y, 255);
		}
	}
	
	float[] weights = ChamferWeights.CHESSBOARD.getFloatWeights();
	DistanceTransform5x5Float algo = new DistanceTransform5x5Float(weights, true);
	ImageProcessor result = algo.distanceMap(image);
	
	assertNotNull(result);
	assertEquals(image.getWidth(), result.getWidth());
	assertEquals(image.getHeight(), result.getHeight());
	assertEquals(3, result.getf(4, 4), 1e-12);
}
 
Example 3
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static final ImageProcessor binarize(ImageProcessor image, int label)
{
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();

	ByteProcessor result = new ByteProcessor( sizeX, sizeY );
	for (int y = 0; y < sizeY; y++)
	{
		for (int x = 0; x < sizeX; x++)
		{
			// process only specified label
			int val = (int) image.getf(x, y);
			if (val == label)
			{
				result.set(x, y, 255);
			}
		}
	}
	return result;
}
 
Example 4
Source File: DistanceTransform3x3FloatTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public final void testDistanceMapImageProcessor() 
{
	ByteProcessor image = new ByteProcessor(12, 10);
	image.setBackgroundValue(0);
	image.fill();
	for (int y = 2; y < 8; y++)
	{
		for (int x = 2; x < 10; x++)
		{
			image.set(x, y, 255);
		}
	}

	float[] weights = ChamferWeights.CHESSBOARD.getFloatWeights();
	DistanceTransform3x3Float algo = new DistanceTransform3x3Float(weights, true);
	ImageProcessor result = algo.distanceMap(image);

	assertNotNull(result);
	assertEquals(image.getWidth(), result.getWidth());
	assertEquals(image.getHeight(), result.getHeight());
	assertEquals(3, result.getf(4, 4), 1e-12);
}
 
Example 5
Source File: RunTable.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Write the table at proper offset in provided buffer
 *
 * @param buffer  the buffer to be written to
 * @param xOffset relative buffer abscissa for runTable topLeft corner
 * @param yOffset relative buffer ordinate for runTable topLeft corner
 */
public void write (ByteProcessor buffer,
                   int xOffset,
                   int yOffset)
{
    final boolean isVertical = orientation == Orientation.VERTICAL;

    for (int iSeq = 0, size = getSize(); iSeq < size; iSeq++) {
        for (Iterator<Run> it = iterator(iSeq); it.hasNext();) {
            final Run run = it.next();

            for (int coord = run.getStart(), stop = run.getStop(); coord <= stop; coord++) {
                if (isVertical) {
                    buffer.set(xOffset + iSeq, yOffset + coord, 0);
                } else {
                    buffer.set(xOffset + coord, yOffset + iSeq, 0);
                }
            }
        }
    }
}
 
Example 6
Source File: DistanceTransform5x5ShortTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public final void testDistanceMap_UntilCorners_Weights23() 
{
	ByteProcessor image = new ByteProcessor(7, 7);
	image.setValue(255);
	image.fill();
	image.set(4, 4, 0);
	
	
	short[] weights = ChamferWeights.WEIGHTS_23.getShortWeights();
	DistanceTransform5x5Short algo = new DistanceTransform5x5Short(weights, false);
	ImageProcessor result = algo.distanceMap(image);
	
	assertNotNull(result);
	assertEquals(image.getWidth(), result.getWidth());
	assertEquals(image.getHeight(), result.getHeight());
	assertEquals(12, result.get(0, 0));
	assertEquals(10, result.get(6, 0));
	assertEquals(10, result.get(0, 6));
	assertEquals(6, result.get(6, 6));
	
	assertEquals(9, result.get(0, 5));
}
 
Example 7
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 8
Source File: DistanceTransform3x3ShortTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public final void testDistanceMap_UntilCorners_Borgefors34()
{
	ByteProcessor image = new ByteProcessor(7, 7);
	image.setValue(255);
	image.fill();
	image.set(4, 4, 0);
	
	
	short[] weights = ChamferWeights.BORGEFORS.getShortWeights();
	DistanceTransform3x3Short algo = new DistanceTransform3x3Short(weights, false);
	ImageProcessor result = algo.distanceMap(image);
	
	assertNotNull(result);
	assertEquals(image.getWidth(), result.getWidth());
	assertEquals(image.getHeight(), result.getHeight());
	assertEquals(16, result.get(0, 0));
	assertEquals(14, result.get(6, 0));
	assertEquals(14, result.get(0, 6));
	assertEquals(8, result.get(6, 6));
}
 
Example 9
Source File: DistanceTransform3x3FloatTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public final void testDistanceMap_UntilCorners_Borgefors34() 
{
	ByteProcessor image = new ByteProcessor(7, 7);
	image.setValue(255);
	image.fill();
	image.set(4, 4, 0);

	float[] weights = ChamferWeights.BORGEFORS.getFloatWeights();
	DistanceTransform3x3Float algo = new DistanceTransform3x3Float(weights, false);
	ImageProcessor result = algo.distanceMap(image);

	assertNotNull(result);
	assertEquals(image.getWidth(), result.getWidth());
	assertEquals(image.getHeight(), result.getHeight());
	assertEquals(16, result.getf(0, 0), .01);
	assertEquals(14, result.getf(6, 0), .01);
	assertEquals(14, result.getf(0, 6), .01);
	assertEquals(8, result.getf(6, 6), .01);
}
 
Example 10
Source File: SymbolSample.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
private static ByteProcessor createBuffer (BufferedImage img)
{
    DataBuffer dataBuffer = img.getData().getDataBuffer();
    ByteProcessor buf = new ByteProcessor(img.getWidth(), img.getHeight());

    for (int y = 0, h = img.getHeight(); y < h; y++) {
        for (int x = 0, w = img.getWidth(); x < w; x++) {
            int index = x + (y * w);
            int elem = dataBuffer.getElem(index);

            // ShapeSymbol instances use alpha channel as the pixel level
            // With 0 as totally transparent so background (255)
            // And with 255 as totally opaque so foreground (0)
            int val = 255 - (elem >>> 24);
            buf.set(x, y, val);
        }
    }

    // binarize
    buf.threshold(216);

    return buf;
}
 
Example 11
Source File: SymbolsFilter.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Save the provided pixels as optional glyphs.
 *
 * @param box   the absolute bounding box of inter descriptor (perhaps larger than symbol)
 * @param fores foreground pixels with coordinates relative to descriptor bounding box
 */
private void savePixels (Rectangle box,
                         List<Point> fores)
{
    ByteProcessor buf = new ByteProcessor(box.width, box.height);
    ByteUtil.raz(buf); // buf.invert();

    for (Point p : fores) {
        buf.set(p.x, p.y, 0);
    }

    // Runs
    RunTableFactory factory = new RunTableFactory(SYMBOL_ORIENTATION);
    RunTable runTable = factory.createTable(buf);

    // Glyphs
    List<Glyph> glyphs = GlyphFactory.buildGlyphs(
            runTable,
            new Point(0, 0),
            GlyphGroup.SYMBOL);

    systemWeaks.addAll(glyphs);
}
 
Example 12
Source File: DistanceTransform5x5FloatTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public final void testDistanceMap_UntilCorners_CityBlock() 
{
	ByteProcessor image = new ByteProcessor(7, 7);
	image.setValue(255);
	image.fill();
	image.set(4, 4, 0);
	
	float[] weights = ChamferWeights.CITY_BLOCK.getFloatWeights();
	DistanceTransform5x5Float algo = new DistanceTransform5x5Float(weights, false);
	ImageProcessor result = algo.distanceMap(image);
	
	assertNotNull(result);
	assertEquals(image.getWidth(), result.getWidth());
	assertEquals(image.getHeight(), result.getHeight());
	assertEquals(8, result.getf(0, 0), .01);
	assertEquals(6, result.getf(6, 0), .01);
	assertEquals(6, result.getf(0, 6), .01);
	assertEquals(4, result.getf(6, 6), .01);
	assertEquals(5, result.getf(0, 5), .01);
}
 
Example 13
Source File: GeodesicReconstructionHybridTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testReconstructByDilationGrayscaleC4() {
	// size of images
	int width = 16;
	int height = 10;

	ByteProcessor mask 		= new ByteProcessor(16, 10);
	ByteProcessor marker 	= new ByteProcessor(16, 10);
	ByteProcessor expected 	= new ByteProcessor(16, 10);

	// initialize mask, marker, and expected images
	int[] maskProfile = {10, 10, 40, 40, 40, 40, 20, 20, 30, 30, 10, 10, 30, 30, 0, 0};
	int[] markerProfile = {0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	int[] expectedProfile = {10, 10, 30, 30, 30, 30, 20, 20, 20, 20, 10, 10, 10, 10, 0, 0};
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			mask.set(x, y, maskProfile[x]);
			marker.set(x, y, markerProfile[x]);
			expected.set(x, y, expectedProfile[x]);
		}
	}

	// Compute geodesic reconstruction by dilation
	GeodesicReconstructionHybrid algo = new GeodesicReconstructionHybrid(
			GeodesicReconstructionType.BY_DILATION, 4);
	ImageProcessor result = algo.applyTo(marker, mask);
	//		printImage(result);

	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			assertEquals(expectedProfile[x], result.get(x, y));
		}
	}

}
 
Example 14
Source File: DistanceTransform3x3FloatTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Test method for {@link inra.ijpb.label.distmap.LabelDistanceTransform3x3Float#distanceMap(ij.process.ImageProcessor)}.
 */
@Test
public final void testDistanceMap_TouchingLabels()
{
	ByteProcessor image = new ByteProcessor(8, 8);
	for (int y = 0; y < 3; y++)
	{
		for (int x = 0; x < 3; x++)
		{
			image.set(x+1, y+1, 1);
			image.set(x+4, y+1, 2);
			image.set(x+1, y+4, 3);
			image.set(x+4, y+4, 4);
		}
	}
	
	DistanceTransform dt = new DistanceTransform3x3Float(ChamferWeights.BORGEFORS, true);
	ImageProcessor distMap = dt.distanceMap(image);

	// value 0 in backgrounf
	assertEquals(0, distMap.getf(0, 0), .1);
	assertEquals(0, distMap.getf(5, 0), .1);
	assertEquals(0, distMap.getf(7, 7), .1);

	// value equal to 2 in the middle of the labels
	assertEquals(2, distMap.getf(2, 2), .1);
	assertEquals(2, distMap.getf(5, 2), .1);
	assertEquals(2, distMap.getf(2, 5), .1);
	assertEquals(2, distMap.getf(5, 5), .1);
	
	// value equal to 1 on the border of the labels
	assertEquals(1, distMap.getf(1, 3), .1);
	assertEquals(1, distMap.getf(3, 3), .1);
	assertEquals(1, distMap.getf(4, 3), .1);
	assertEquals(1, distMap.getf(6, 3), .1);
	assertEquals(1, distMap.getf(1, 6), .1);
	assertEquals(1, distMap.getf(3, 6), .1);
	assertEquals(1, distMap.getf(4, 6), .1);
	assertEquals(1, distMap.getf(6, 6), .1);
}
 
Example 15
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 16
Source File: ReconstructionTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testReconstructByDilationGrayscaleC8() {
	// size of images
	int width = 16;
	int height = 10;

	ByteProcessor mask 		= new ByteProcessor(16, 10);
	ByteProcessor marker 	= new ByteProcessor(16, 10);
	ByteProcessor expected 	= new ByteProcessor(16, 10);

	// initialize mask, marker, and expected images
	int[] maskProfile = {10, 10, 40, 40, 40, 40, 20, 20, 30, 30, 10, 10, 30, 30, 0, 0};
	int[] markerProfile = {0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	int[] expectedProfile = {10, 10, 30, 30, 30, 30, 20, 20, 20, 20, 10, 10, 10, 10, 0, 0};
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			mask.set(x, y, maskProfile[x]);
			marker.set(x, y, markerProfile[x]);
			expected.set(x, y, expectedProfile[x]);
		}
	}

	// Compute geodesic reconstruction by dilation
	ImageProcessor result = Reconstruction.reconstructByDilation(marker, mask, 8);
	//		printImage(result);

	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			assertEquals(expectedProfile[x], result.get(x, y));
		}
	}

}
 
Example 17
Source File: AreaOpeningNaive.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor image, int minArea)
{
	fireStatusChanged(this, "Initialize");
	
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();
	ByteProcessor result = new ByteProcessor(sizeX, sizeY);
	
	fireStatusChanged(this, "Compute thesholds");
	for (int level = 1; level <= 255; level++)
	{
		fireStatusChanged(this, "Threshold: " + level);
		fireProgressChanged(this, level-1, 255);
		
		// threshold
		ImageProcessor binary = Threshold.threshold(image, level, 255);
		
		// keep only components with size larger than minArea
		binary = BinaryImages.areaOpening(binary, minArea);
		
		for (int y = 0; y < sizeY; y++)
		{
			for (int x = 0; x < sizeX; x++)
			{
				if (binary.get(x, y) > 0)
				{
					result.set(x, y, level);
				}
			}
		}
	}
	
	fireStatusChanged(this, "");
	fireProgressChanged(this, 1, 1);
	
	return result;
}
 
Example 18
Source File: DistanceTransform3x3ShortTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Test method for {@link inra.ijpb.label.distmap.LabelDistanceTransform3x3Short#distanceMap(ij.process.ImageProcessor)}.
 */
@Test
public final void testDistanceMap_TouchingLabels()
{
	ByteProcessor image = new ByteProcessor(8, 8);
	for (int y = 0; y < 3; y++)
	{
		for (int x = 0; x < 3; x++)
		{
			image.set(x+1, y+1, 1);
			image.set(x+4, y+1, 2);
			image.set(x+1, y+4, 3);
			image.set(x+4, y+4, 4);
		}
	}
	
	DistanceTransform dt = new DistanceTransform3x3Short(ChamferWeights.BORGEFORS, true);
	ImageProcessor distMap = dt.distanceMap(image);

	// value 0 in backgrounf
	assertEquals(0, distMap.getf(0, 0), .1);
	assertEquals(0, distMap.getf(5, 0), .1);
	assertEquals(0, distMap.getf(7, 7), .1);

	// value equal to 2 in the middle of the labels
	assertEquals(2, distMap.getf(2, 2), .1);
	assertEquals(2, distMap.getf(5, 2), .1);
	assertEquals(2, distMap.getf(2, 5), .1);
	assertEquals(2, distMap.getf(5, 5), .1);
	
	// value equal to 1 on the border of the labels
	assertEquals(1, distMap.getf(1, 3), .1);
	assertEquals(1, distMap.getf(3, 3), .1);
	assertEquals(1, distMap.getf(4, 3), .1);
	assertEquals(1, distMap.getf(6, 3), .1);
	assertEquals(1, distMap.getf(1, 6), .1);
	assertEquals(1, distMap.getf(3, 6), .1);
	assertEquals(1, distMap.getf(4, 6), .1);
	assertEquals(1, distMap.getf(6, 6), .1);
}
 
Example 19
Source File: ChamferDistanceTest.java    From audiveris with GNU Affero General Public License v3.0 4 votes vote down vote up
private ByteProcessor createImage ()
{
    String[] rows = new String[]{
        "                              ",
        "              XXXXXXX         ",
        "           XXXXXXXXXXXXX      ",
        "         XXXXXXXXXXXXXXXX     ",
        "       XXXXXXXXXXXXXXXXXX     ",
        "      XXXXXXXXXXXXXXXXXXX     ",
        "     XXXXXXXXXXXXXXXXXXXXX    ",
        "     XXXXXXXXXXXXXXXXXXXXX    ",
        "     XXXXXXXXXXXXXXXXXXXX     ",
        "     XXXXXXXXXXXXXXXXXXXX     ",
        "    XXXXXXXXXXXXXXXXXXXXX     ",
        "     XXXXXXXXXXXXXXXXXXX      ",
        "     XXXXXXXXXXXXXXXXXX       ",
        "      XXXXXXXXXXXXXXXX        ",
        "         XXXXXXXXXXXXXX       ",
        "         XXXXXXXXXXXXXXX      ",
        "       XXXXXXXXXXXXXXXXXX     ",
        "      XXXXXXXXXXXXXXXXXXX     ",
        "     XXXXXXXXXXXXXXXXXXXXX    ",
        "     XXXXXXXXXXXXXXXXXXXXX    ",
        "    XXXXXXXXXXXXXXXXXXXXXXX   ",
        "    XXXXXXXXXXXXXXXXXXXXXXX   ",
        "     XXXXXXXXXXXXXXXXXXXXXX   ",
        "      XXXXXXXXXXXXXXXXXXXXX   ",
        "       XXXXXXXXXXXXXXXXXXX    ",
        "        XXXXXXXXXXXXXXXXX     ",
        "         XXXXXXXXXXXXXX       ",
        "           XXXXXXXXX          "
    };
    final int width = rows[0].length();
    final int height = rows.length;
    final ByteProcessor img = new ByteProcessor(width, height);

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            char c = rows[y].charAt(x);
            img.set(x, y, (c == 'X') ? 0 : 255);
        }
    }

    return img;
}
 
Example 20
Source File: ColorImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Splits the channels of the 3D color image into three new instances of
 * ImageStack containing ByteProcessors.
 * 
 * @param image
 *            the original image, assumed to be a ColorProcessor
 * @return a collection containing the red, green and blue channels
 */
public static final HashMap<String, ImageStack> mapChannels(ImageStack image) 
{
	if (!(image.getProcessor(1) instanceof ColorProcessor)) 
	{
		throw new IllegalArgumentException("Requires a Stack containing instances of ColorProcessor");
	}
	
	// size of input image
	int width = image.getWidth();
	int height = image.getHeight();
	int depth = image.getSize();

	// create byte stacks
	ImageStack redStack = ImageStack.create(width, height, depth, 8);
	ImageStack greenStack = ImageStack.create(width, height, depth, 8);
	ImageStack blueStack = ImageStack.create(width, height, depth, 8);
	
	for (int z = 1; z <= depth; z++)
	{
		// extract the current RGB slice
		ColorProcessor rgb = (ColorProcessor) image.getProcessor(z);

		// extract the current slice of each channel
		ByteProcessor red 	= (ByteProcessor) redStack.getProcessor(z);
		ByteProcessor green = (ByteProcessor) greenStack.getProcessor(z);
		ByteProcessor blue 	= (ByteProcessor) blueStack.getProcessor(z);
		
		// convert int codes to color components
		for (int y = 0; y < height; y++)
		{
			for (int x = 0; x < width; x++)
			{
				int intCode = rgb.get(x, y);
				red.set(x, y, (intCode >> 16) & 0x00FF);
				green.set(x, y, (intCode >> 8) & 0x00FF);
				blue.set(x, y, intCode & 0x00FF);
			}
		}

		// set slices (should not be necessary, but more secure)
		redStack.setProcessor(red, z);
		greenStack.setProcessor(green, z);
		blueStack.setProcessor(blue, z);
	}
	
	// concatenate channels into a new collection
	HashMap<String, ImageStack> map = new HashMap<String, ImageStack>(3);
	map.put("red", redStack);
	map.put("green", greenStack);
	map.put("blue", blueStack);
	
	return map;
}