Difference between revisions of "Example Binary Image"

From BoofCV
Jump to navigationJump to search
m
(Updated for latest version)
Line 4: Line 4:
Image:example_binary.png|Thresholded Binary Image.
Image:example_binary.png|Thresholded Binary Image.
Image:example_binary_labeled.png|Labeled Binary Image
Image:example_binary_labeled.png|Labeled Binary Image
Image:example_binary_contour.png|Contour Image
</gallery>
</gallery>
</center>
</center>


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.
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: [https://github.com/lessthanoptimal/BoofCV/blob/v0.12/examples/src/boofcv/examples/ExampleBinaryImage.java ExampleBinaryImage.java]
Example File: [https://github.com/lessthanoptimal/BoofCV/blob/v0.14/examples/src/boofcv/examples/ExampleBinaryOps.java ExampleBinaryOps.java]


Concepts:
Concepts:
Line 23: Line 24:


= Basic Example =
= Basic Example =
In this example a threshold is computed for the input image dynamically and the resulting binary image shown.
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
public static void binaryExample( BufferedImage image )
/**
{
* Demonstrates how to create binary images by thresholding, applying binary morphological operations, and
// convert into a usable format
* then extracting detected features by finding their contours.
ImageFloat32 input = ConvertBufferedImage.convertFromSingle(image, null, ImageFloat32.class);
*
ImageUInt8 binary = new ImageUInt8(input.width,input.height);
* @author Peter Abeles
*/
public class ExampleBinaryOps {


// the mean pixel value is often a reasonable threshold when creating a binary image
public static void main( String args[] ) {
float mean = (float)ImageStatistics.mean(input);
// load and convert the image into a usable format
BufferedImage image = UtilImageIO.loadImage("../data/applet/particles01.jpg");


// 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");
}
</syntaxhighlight>
= Labeled Example =
Here clustered of blobs are detected and arbitrarily assigned labels.  Noise is reduced through morphological image operations.
<syntaxhighlight lang="java">
public static void labeledExample( BufferedImage image )
{
// convert into a usable format
// convert into a usable format
ImageFloat32 input = ConvertBufferedImage.convertFromSingle(image, null, ImageFloat32.class);
ImageFloat32 input = ConvertBufferedImage.convertFromSingle(image, null, ImageFloat32.class);
ImageUInt8 binary = new ImageUInt8(input.width,input.height);
ImageUInt8 binary = new ImageUInt8(input.width,input.height);
ImageSInt32 blobs = new ImageSInt32(input.width,input.height);
ImageSInt32 label = new ImageSInt32(input.width,input.height);


// 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);
double mean = ImageStatistics.mean(input);


// create a binary image
// create a binary image by thresholding
ThresholdImageOps.threshold(input,binary,(float)mean,true);
ThresholdImageOps.threshold(input,binary,(float)mean,true);


Line 64: Line 51:
// The null in the input indicates that it should internally declare the work image it needs
// The null in the input indicates that it should internally declare the work image it needs
// this is less efficient, but easier to code.
// this is less efficient, but easier to code.
binary = BinaryImageOps.erode8(binary,null);
ImageUInt8 filtered = BinaryImageOps.erode8(binary,null);
binary = BinaryImageOps.dilate8(binary, null);
filtered = BinaryImageOps.dilate8(filtered, null);
 
// Detect blobs inside the image using an 8-connect rule
List<Contour> contours = BinaryImageOps.contour(filtered, 8, label);


// Detect blobs inside the binary image and assign labels to them
// colors of contours
int numBlobs = BinaryImageOps.labelBlobs4(binary,blobs);
int colorExternal = 0xFFFFFF;
int colorInternal = 0xFF2020;


// Render the binary image for output and display it in a window
// display the results
BufferedImage visualized = VisualizeBinaryData.renderLabeled(blobs, numBlobs, null);
BufferedImage visualBinary = VisualizeBinaryData.renderBinary(binary, null);
ShowImages.showWindow(visualized,"Labeled Image");
BufferedImage visualFiltered = VisualizeBinaryData.renderBinary(filtered, null);
BufferedImage visualLabel = VisualizeBinaryData.renderLabeled(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");
}
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 10:14, 14 April 2013

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.
 *
 * @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);

		// the mean pixel value is often a reasonable threshold when creating a binary image
		double mean = ImageStatistics.mean(input);

		// create a binary image by thresholding
		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.
		ImageUInt8 filtered = BinaryImageOps.erode8(binary,null);
		filtered = BinaryImageOps.dilate8(filtered, null);

		// Detect blobs inside the image using an 8-connect rule
		List<Contour> contours = BinaryImageOps.contour(filtered, 8, 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.renderLabeled(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");
	}

}