Difference between revisions of "Example Binary Image"

From BoofCV
Jump to navigationJump to search
m
m
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Binary Image Processing =
<center>
<gallery caption="Example input and output Images" heights=150 widths=200 >
Image:particles01.jpg|Input TEM particle image.
Image:example_binary.png|Thresholded Binary Image.
Image:example_binary_labeled.png|Labeled Binary Image
Image:example_binary_contour.png|Contour Image
</gallery>
</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/master/examples/src/boofcv/examples/BinaryImageExample.java BinaryImageExample.java]
Example File: [https://github.com/lessthanoptimal/BoofCV/blob/v0.23/examples/src/boofcv/examples/imageprocessing/ExampleBinaryOps.java ExampleBinaryOps.java]


Concepts:
Concepts:
Line 11: Line 18:
* Pixel Math
* Pixel Math
* Image Rendering
* Image Rendering
Relevant Applets:
* [[Applet_Binary_Operations| Binary Operations]]
* [[Applet_Binary_Segmentation| Binary Segmentation]]


= 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">
/**
* 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 {


[[image:example_binary.png|thumb|Output of binary image example.]]
public static void main( String args[] ) {
// load and convert the image into a usable format
BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("particles01.jpg"));


<pre>
public static void binaryExample( BufferedImage image )
{
// convert into a usable format
// convert into a usable format
ImageFloat32 input = ConvertBufferedImage.convertFrom(image,null,ImageFloat32.class);
GrayF32 input = ConvertBufferedImage.convertFromSingle(image, null, GrayF32.class);
ImageUInt8 binary = new ImageUInt8(input.width,input.height);
GrayU8 binary = new GrayU8(input.width,input.height);
 
GrayS32 label = new GrayS32(input.width,input.height);
// the mean pixel value is often a reasonable threshold when creating a binary image
float mean = PixelMath.sum(input)/(input.width*input.height);
 
// create a binary image
ThresholdImageOps.threshold(input,binary,mean,true);


// Render the binary image for output and display it in a window
// Select a global threshold using Otsu's method.
BufferedImage visualBinary = VisualizeBinaryData.renderBinary(binary,null);
double threshold = GThresholdImageOps.computeOtsu(input, 0, 255);
ShowImages.showWindow(visualBinary,"Binary Image");
}
</pre>


= Labeled Example =
// Apply the threshold to create a binary image
Here clustered of blobs are detected and arbitrarily assigned labels.
ThresholdImageOps.threshold(input,binary,(float)threshold,true);
[[image:example_binary_labeled.png|thumb|Output of binary label example.]]
 
<pre>
public static void labeledExample( BufferedImage image )
{
// convert into a usable format
ImageFloat32 input = ConvertBufferedImage.convertFrom(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
float mean = PixelMath.sum(input)/(input.width*input.height);
 
// create a binary image
ThresholdImageOps.threshold(input,binary,mean,true);


// remove small blobs through erosion and dilation
// remove small blobs through erosion and dilation
// 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);
GrayU8 filtered = BinaryImageOps.erode8(binary, 1, null);
binary = BinaryImageOps.dilate8(binary, null);
filtered = BinaryImageOps.dilate8(filtered, 1, null);


// Detect blobs inside the binary image and assign labels to them
// Detect blobs inside the image using an 8-connect rule
int numBlobs = BinaryImageOps.labelBlobs4(binary,blobs);
List<Contour> contours = BinaryImageOps.contour(filtered, ConnectRule.EIGHT, label);


// Render the binary image for output and display it in a window
// colors of contours
BufferedImage visualized = VisualizeBinaryData.renderLabeled(blobs, numBlobs, null);
int colorExternal = 0xFFFFFF;
ShowImages.showWindow(visualized,"Labeled Image");
int colorInternal = 0xFF2020;
 
// display the results
BufferedImage visualBinary = VisualizeBinaryData.renderBinary(binary, false, null);
BufferedImage visualFiltered = VisualizeBinaryData.renderBinary(filtered, false, null);
BufferedImage visualLabel = VisualizeBinaryData.renderLabeledBG(label, contours.size(), null);
BufferedImage visualContour = VisualizeBinaryData.renderContours(contours, colorExternal, colorInternal,
input.width, input.height, null);
 
ListDisplayPanel panel = new ListDisplayPanel();
panel.addImage(visualBinary, "Binary Original");
panel.addImage(visualFiltered, "Binary Filtered");
panel.addImage(visualLabel, "Labeled Blobs");
panel.addImage(visualContour, "Contours");
ShowImages.showWindow(panel,"Binary Operations",true);
}
}
</pre>
 
}
</syntaxhighlight>

Revision as of 20:09, 1 December 2016

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

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(UtilIO.pathExample("particles01.jpg"));

		// convert into a usable format
		GrayF32 input = ConvertBufferedImage.convertFromSingle(image, null, GrayF32.class);
		GrayU8 binary = new GrayU8(input.width,input.height);
		GrayS32 label = new GrayS32(input.width,input.height);

		// Select a global threshold using Otsu's method.
		double threshold = GThresholdImageOps.computeOtsu(input, 0, 255);

		// 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.
		GrayU8 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, false, null);
		BufferedImage visualFiltered = VisualizeBinaryData.renderBinary(filtered, false, null);
		BufferedImage visualLabel = VisualizeBinaryData.renderLabeledBG(label, contours.size(), null);
		BufferedImage visualContour = VisualizeBinaryData.renderContours(contours, colorExternal, colorInternal,
				input.width, input.height, null);

		ListDisplayPanel panel = new ListDisplayPanel();
		panel.addImage(visualBinary, "Binary Original");
		panel.addImage(visualFiltered, "Binary Filtered");
		panel.addImage(visualLabel, "Labeled Blobs");
		panel.addImage(visualContour, "Contours");
		ShowImages.showWindow(panel,"Binary Operations",true);
	}

}