Example Binary Image

From BoofCV
Jump to: navigation, search

In a binary image each pixel can have a value of 0 or 1. Binary images are easy to compute and fast to process, which makes them popular in many applications. BoofCV contains many operations for creating and manipulating binary images. The example below demonstrates a few of ones contained inside of BinaryImageOps.

Example File: ExampleBinaryOps.java

Concepts:

  • Image Thresholding
  • Morphological Operations
  • Binary Labeling
  • Pixel Math
  • Image Rendering

Relevant Applets:

Basic Example

/**
 * Demonstrates how to create binary images by thresholding, applying binary morphological operations, and
 * then extracting detected features by finding their contours.
 *
 * @see boofcv.examples.segmentation.ExampleThresholding
 *
 * @author Peter Abeles
 */
public class ExampleBinaryOps {
 
	public static void main( String args[] ) {
		// load and convert the image into a usable format
		BufferedImage image = UtilImageIO.loadImage("../data/applet/particles01.jpg");
 
		// convert into a usable format
		ImageFloat32 input = ConvertBufferedImage.convertFromSingle(image, null, ImageFloat32.class);
		ImageUInt8 binary = new ImageUInt8(input.width,input.height);
		ImageSInt32 label = new ImageSInt32(input.width,input.height);
 
		// Select a global threshold using Otsu's method.
		double threshold = GThresholdImageOps.computeOtsu(input, 0, 256);
 
		// Apply the threshold to create a binary image
		ThresholdImageOps.threshold(input,binary,(float)threshold,true);
 
		// remove small blobs through erosion and dilation
		// The null in the input indicates that it should internally declare the work image it needs
		// this is less efficient, but easier to code.
		ImageUInt8 filtered = BinaryImageOps.erode8(binary, 1, null);
		filtered = BinaryImageOps.dilate8(filtered, 1, null);
 
		// Detect blobs inside the image using an 8-connect rule
		List<Contour> contours = BinaryImageOps.contour(filtered, ConnectRule.EIGHT, label);
 
		// colors of contours
		int colorExternal = 0xFFFFFF;
		int colorInternal = 0xFF2020;
 
		// display the results
		BufferedImage visualBinary = VisualizeBinaryData.renderBinary(binary, null);
		BufferedImage visualFiltered = VisualizeBinaryData.renderBinary(filtered, null);
		BufferedImage visualLabel = VisualizeBinaryData.renderLabeledBG(label, contours.size(), null);
		BufferedImage visualContour = VisualizeBinaryData.renderContours(contours,colorExternal,colorInternal,
				input.width,input.height,null);
 
		ShowImages.showWindow(visualBinary,"Binary Original");
		ShowImages.showWindow(visualFiltered,"Binary Filtered");
		ShowImages.showWindow(visualLabel,"Labeled Blobs");
		ShowImages.showWindow(visualContour,"Contours");
	}
 
}