Difference between revisions of "Example Morphological Thinning"

From BoofCV
Jump to navigationJump to search
(Created page with "<center> <gallery widths=240px heights=220px> File:Fingerprint.jpg | Input image of a finger print File:Thinned_binary.png | Binary image File:Thinned_thinned.png | Thinned im...")
 
m
Line 10: Line 10:


Example Code:
Example Code:
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.19/examples/src/boofcv/examples/imageprocessing/ExampleMorphologicalThinning.java ExampleMorphologicalThinning.java]
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.20/examples/src/boofcv/examples/imageprocessing/ExampleMorphologicalThinning.java ExampleMorphologicalThinning.java]


Concepts:
Concepts:
Line 29: Line 29:
public class ExampleMorphologicalThinning {
public class ExampleMorphologicalThinning {
public static void main(String[] args) {
public static void main(String[] args) {
// load and convert the image into a usable format
BufferedImage image = UtilImageIO.loadImage("../data/applet/standard/fingerprint.jpg");


// convert into a usable format
String[] images = new String[]{"drawings/drawing_text.png","standard/fingerprint.jpg","drawings/drawing_face.png"};
ImageFloat32 input = ConvertBufferedImage.convertFromSingle(image, null, ImageFloat32.class);
ImageUInt8 binary = new ImageUInt8(input.width,input.height);


// Adaptive threshold to better handle changes in local image intensity
ListDisplayPanel uberPanel = new ListDisplayPanel();
GThresholdImageOps.adaptiveGaussian(input,binary,10,0,true,null,null);
for( String path : images ) {
// load and convert the image into a usable format
BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample(path));


// Tell it to thin the image until there are no more changes
// convert into a usable format
ImageUInt8 thinned = BinaryImageOps.thin(binary, -1, null);
ImageFloat32 input = ConvertBufferedImage.convertFromSingle(image, null, ImageFloat32.class);
ImageUInt8 binary = new ImageUInt8(input.width, input.height);


// display the results
// Fixed threshold is best for B&W images, but the adaptive would improve results for the finger print
BufferedImage visualBinary = VisualizeBinaryData.renderBinary(binary, false, null);
GThresholdImageOps.threshold(input, binary, 120, true);
BufferedImage visualThinned = VisualizeBinaryData.renderBinary(thinned, false, null);
// GThresholdImageOps.adaptiveSquare(input, binary, 20,0,true,null,null);


ListDisplayPanel panel = new ListDisplayPanel();
// Tell it to thin the image until there are no more changes
panel.addImage(visualThinned, "Thinned");
ImageUInt8 thinned = BinaryImageOps.thin(binary, -1, null);
panel.addImage(visualBinary, "Binary");
panel.addImage(image, "Original");


ShowImages.showWindow(panel,"Thinned/Skeletonalized Image", true);
// display the results
BufferedImage visualBinary = VisualizeBinaryData.renderBinary(binary, false, null);
BufferedImage visualThinned = VisualizeBinaryData.renderBinary(thinned, false, null);
 
ListDisplayPanel panel = new ListDisplayPanel();
panel.addImage(visualThinned, "Thinned");
panel.addImage(visualBinary, "Binary");
panel.addImage(image, "Original");
 
uberPanel.addItem(panel,new File(path).getName());
}
ShowImages.showWindow(uberPanel, "Thinned/Skeletonalized Images", true);
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 07:18, 9 November 2015

Example of how to thin a binary image. The thinned image (or skeleton) is a common preprocessing step in shape analysis.

Example Code:

Concepts:

  • Binary Processing
  • Shapes
  • Thinning

Example Code

/**
 * Simple example showing you how to thin a binary image.  This is also known as skeletonalization.  Thinning
 * discards most of objects foreground (value one) pixels are leaves behind a "skinny" object which still
 * mostly describes the original objects shape.
 *
 * @author Peter Abeles
 */
public class ExampleMorphologicalThinning {
	public static void main(String[] args) {

		String[] images = new String[]{"drawings/drawing_text.png","standard/fingerprint.jpg","drawings/drawing_face.png"};

		ListDisplayPanel uberPanel = new ListDisplayPanel();
		for( String path : images ) {
			// load and convert the image into a usable format
			BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample(path));

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

			// Fixed threshold is best for B&W images, but the adaptive would improve results for the finger print
			GThresholdImageOps.threshold(input, binary, 120, true);
//			GThresholdImageOps.adaptiveSquare(input, binary, 20,0,true,null,null);

			// Tell it to thin the image until there are no more changes
			ImageUInt8 thinned = BinaryImageOps.thin(binary, -1, null);

			// display the results
			BufferedImage visualBinary = VisualizeBinaryData.renderBinary(binary, false, null);
			BufferedImage visualThinned = VisualizeBinaryData.renderBinary(thinned, false, null);

			ListDisplayPanel panel = new ListDisplayPanel();
			panel.addImage(visualThinned, "Thinned");
			panel.addImage(visualBinary, "Binary");
			panel.addImage(image, "Original");

			uberPanel.addItem(panel,new File(path).getName());
		}
		ShowImages.showWindow(uberPanel, "Thinned/Skeletonalized Images", true);
	}
}