Difference between revisions of "Example Binary Image"
From BoofCV
Jump to navigationJump to searchm  | 
				|||
| Line 11: | Line 11: | ||
Binary images are images where each pixel can take on two values, typically represented by 0 or 1.  Binary images are easy to compute and fast to process, which makes them popular in many applications.  | Binary images are images where each pixel can take on two values, typically represented by 0 or 1.  Binary images are easy to compute and fast to process, which makes them popular in many applications.  | ||
Example File: [https://github.com/lessthanoptimal/BoofCV/blob/v0.  | Example File: [https://github.com/lessthanoptimal/BoofCV/blob/v0.12/examples/src/boofcv/examples/ExampleBinaryImage.java ExampleBinaryImage.java]  | ||
Concepts:  | Concepts:  | ||
| Line 35: | Line 35: | ||
		// the mean pixel value is often a reasonable threshold when creating a binary image  | 		// the mean pixel value is often a reasonable threshold when creating a binary image  | ||
		float mean =   | 		float mean = (float)ImageStatistics.mean(input);  | ||
		// create a binary image  | 		// create a binary image  | ||
| Line 58: | Line 58: | ||
		// the mean pixel value is often a reasonable threshold when creating a binary image  | 		// the mean pixel value is often a reasonable threshold when creating a binary image  | ||
		double mean = ImageStatistics.mean(input);  | |||
		// create a binary image  | 		// create a binary image  | ||
		ThresholdImageOps.threshold(input,binary,mean,true);  | 		ThresholdImageOps.threshold(input,binary,(float)mean,true);  | ||
		// remove small blobs through erosion and dilation  | 		// remove small blobs through erosion and dilation  | ||
Revision as of 04:39, 5 December 2012
Binary Image Processing
- Example input and output Images
 
Binary images are images where each pixel can take on two values, typically represented by 0 or 1. Binary images are easy to compute and fast to process, which makes them popular in many applications.
Example File: ExampleBinaryImage.java
Concepts:
- Image Thresholding
 - Morphological Operations
 - Binary Labeling
 - Pixel Math
 - Image Rendering
 
Relevant Applets:
Basic Example
In this example a threshold is computed for the input image dynamically and the resulting binary image shown.
	public static void binaryExample( BufferedImage image )
	{
		// convert into a usable format
		ImageFloat32 input = ConvertBufferedImage.convertFromSingle(image, null, ImageFloat32.class);
		ImageUInt8 binary = new ImageUInt8(input.width,input.height);
		// the mean pixel value is often a reasonable threshold when creating a binary image
		float mean = (float)ImageStatistics.mean(input);
		// create a binary image
		ThresholdImageOps.threshold(input,binary,mean,true);
		// Render the binary image for output and display it in a window
		BufferedImage visualBinary = VisualizeBinaryData.renderBinary(binary,null);
		ShowImages.showWindow(visualBinary,"Binary Image");
	}
Labeled Example
Here clustered of blobs are detected and arbitrarily assigned labels. Noise is reduced through morphological image operations.
	public static void labeledExample( BufferedImage image )
	{
		// convert into a usable format
		ImageFloat32 input = ConvertBufferedImage.convertFromSingle(image, null, ImageFloat32.class);
		ImageUInt8 binary = new ImageUInt8(input.width,input.height);
		ImageSInt32 blobs = new ImageSInt32(input.width,input.height);
		// the mean pixel value is often a reasonable threshold when creating a binary image
		double mean = ImageStatistics.mean(input);
		// create a binary image
		ThresholdImageOps.threshold(input,binary,(float)mean,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.
		binary = BinaryImageOps.erode8(binary,null);
		binary = BinaryImageOps.dilate8(binary, null);
		// Detect blobs inside the binary image and assign labels to them
		int numBlobs = BinaryImageOps.labelBlobs4(binary,blobs);
		// Render the binary image for output and display it in a window
		BufferedImage visualized = VisualizeBinaryData.renderLabeled(blobs, numBlobs, null);
		ShowImages.showWindow(visualized,"Labeled Image");
	}