Difference between revisions of "Example Binary Image"

From BoofCV
Jump to navigationJump to search
(Updated for v0.16)
m
 
(8 intermediate revisions by the same user not shown)
Line 10: Line 10:
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.
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.16/examples/src/boofcv/examples/imageprocessing/ExampleBinaryOps.java ExampleBinaryOps.java]
Example File: [https://github.com/lessthanoptimal/BoofCV/blob/v0.40/examples/src/main/java/boofcv/examples/imageprocessing/ExampleBinaryOps.java ExampleBinaryOps.java]


Concepts:
Concepts:
Line 18: 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 =
Line 30: Line 26:
  *
  *
  * @author Peter Abeles
  * @author Peter Abeles
* @see boofcv.examples.segmentation.ExampleThresholding
  */
  */
public class ExampleBinaryOps {
public class ExampleBinaryOps {
 
public static void main( String[] args ) {
public static void main( String args[] ) {
// load and convert the image into a usable format
// load and convert the image into a usable format
BufferedImage image = UtilImageIO.loadImage("../data/applet/particles01.jpg");
BufferedImage image = UtilImageIO.loadImageNotNull(UtilIO.pathExample("particles01.jpg"));


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


// the mean pixel value is often a reasonable threshold when creating a binary image
// Select a global threshold using Otsu's method.
double mean = ImageStatistics.mean(input);
double threshold = GThresholdImageOps.computeOtsu(input, 0, 255);


// create a binary image by thresholding
// Apply the threshold to create a binary image
ThresholdImageOps.threshold(input,binary,(float)mean,true);
ThresholdImageOps.threshold(input, binary, (float)threshold, 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.
ImageUInt8 filtered = BinaryImageOps.erode8(binary,null);
GrayU8 filtered = BinaryImageOps.erode8(binary, 1, null);
filtered = BinaryImageOps.dilate8(filtered, null);
filtered = BinaryImageOps.dilate8(filtered, 1, null);


// Detect blobs inside the image using an 8-connect rule
// Detect blobs inside the image using an 8-connect rule
List<Contour> contours = BinaryImageOps.contour(filtered, 8, label);
List<Contour> contours = BinaryImageOps.contour(filtered, ConnectRule.EIGHT, label);


// colors of contours
// colors of contours
Line 62: Line 58:


// display the results
// display the results
BufferedImage visualBinary = VisualizeBinaryData.renderBinary(binary, null);
BufferedImage visualBinary = VisualizeBinaryData.renderBinary(binary, false, null);
BufferedImage visualFiltered = VisualizeBinaryData.renderBinary(filtered, null);
BufferedImage visualFiltered = VisualizeBinaryData.renderBinary(filtered, false, null);
BufferedImage visualLabel = VisualizeBinaryData.renderLabeled(label, contours.size(), null);
BufferedImage visualLabel = VisualizeBinaryData.renderLabeledBG(label, contours.size(), null);
BufferedImage visualContour = VisualizeBinaryData.renderContours(contours,colorExternal,colorInternal,
BufferedImage visualContour = VisualizeBinaryData.renderContours(contours, colorExternal, colorInternal,
input.width,input.height,null);
input.width, input.height, null);


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

Latest revision as of 14:57, 17 January 2022

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.
 *
 * @author Peter Abeles
 * @see boofcv.examples.segmentation.ExampleThresholding
 */
public class ExampleBinaryOps {
	public static void main( String[] args ) {
		// load and convert the image into a usable format
		BufferedImage image = UtilImageIO.loadImageNotNull(UtilIO.pathExample("particles01.jpg"));

		// convert into a usable format
		GrayF32 input = ConvertBufferedImage.convertFromSingle(image, null, GrayF32.class);
		var binary = new GrayU8(input.width, input.height);
		var 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);

		var 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);
	}
}