Difference between revisions of "Example Thresholding"

From BoofCV
Jump to navigationJump to search
(Created page with "<center> <gallery caption="Variable Lighting" heights=200 widths=600> File:VariableLight_Otsu_Square.jpg|Calibration grid. ''Left:'' original, ''Middle:'' Global Otsu, ''Right...")
 
m
Line 11: Line 11:


Example Code:
Example Code:
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.18/examples/src/boofcv/examples/segmentation/ExampleThresholding.java ExampleThresholding.java]
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.19/examples/src/boofcv/examples/segmentation/ExampleThresholding.java ExampleThresholding.java]


Concepts:
Concepts:
Line 50: Line 50:
// Global Methods
// Global Methods
GThresholdImageOps.threshold(input, binary, ImageStatistics.mean(input), true);
GThresholdImageOps.threshold(input, binary, ImageStatistics.mean(input), true);
gui.addImage(VisualizeBinaryData.renderBinary(binary, null),"Global: Mean");
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Global: Mean");
GThresholdImageOps.threshold(input, binary, GThresholdImageOps.computeOtsu(input, 0, 256), true);
GThresholdImageOps.threshold(input, binary, GThresholdImageOps.computeOtsu(input, 0, 255), true);
gui.addImage(VisualizeBinaryData.renderBinary(binary, null),"Global: Otsu");
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Global: Otsu");
GThresholdImageOps.threshold(input, binary, GThresholdImageOps.computeEntropy(input, 0, 256), true);
GThresholdImageOps.threshold(input, binary, GThresholdImageOps.computeEntropy(input, 0, 255), true);
gui.addImage(VisualizeBinaryData.renderBinary(binary, null),"Global: Entropy");
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Global: Entropy");


// Local method
// Local method
GThresholdImageOps.adaptiveSquare(input, binary, 28, 0, true, null, null);
GThresholdImageOps.adaptiveSquare(input, binary, 28, 0, true, null, null);
gui.addImage(VisualizeBinaryData.renderBinary(binary, null),"Adaptive: Square");
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Adaptive: Square");
GThresholdImageOps.adaptiveGaussian(input, binary, 42, 0, true, null, null);
GThresholdImageOps.adaptiveGaussian(input, binary, 42, 0, true, null, null);
gui.addImage(VisualizeBinaryData.renderBinary(binary, null),"Adaptive: Gaussian");
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Adaptive: Gaussian");
GThresholdImageOps.adaptiveSauvola(input, binary, 5, 0.30f, true);
GThresholdImageOps.adaptiveSauvola(input, binary, 5, 0.30f, true);
gui.addImage(VisualizeBinaryData.renderBinary(binary, null),"Adaptive: Sauvola");
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Adaptive: Sauvola");


// Sauvola is tuned for text image.  Change radius to make it run better in others.
// Sauvola is tuned for text image.  Change radius to make it run better in others.

Revision as of 17:06, 19 September 2015

Thresholding gray scale images is one of the most basic ways to segment an image. It is quick and effective in many situations. BoofCV provides several algorithms for computing both global and local (adaptive) thresholds.

Example Code:

Concepts:

  • Segmentation
  • Thresholding

Relevant Examples/Tutorials:

Example Code

/**
 * Demonstration of different techniques for automatic thresholding an image to create a binary image.  The binary
 * image can then be used for shape analysis and other applications.  Global methods apply the same threshold
 * to the entire image.  Local/adaptive methods compute a local threshold around each pixel and can handle uneven
 * lighting, but produce noisy results in regions with uniform lighting.
 *
 * @see boofcv.examples.imageprocessing.ExampleBinaryOps
 *
 * @author Peter Abeles
 */
public class ExampleThresholding {

	public static void threshold( String imageName ) {
		BufferedImage image = UtilImageIO.loadImage(imageName);

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

		// Display multiple images in the same window
		ListDisplayPanel gui = new ListDisplayPanel();

		// Global Methods
		GThresholdImageOps.threshold(input, binary, ImageStatistics.mean(input), true);
		gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Global: Mean");
		GThresholdImageOps.threshold(input, binary, GThresholdImageOps.computeOtsu(input, 0, 255), true);
		gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Global: Otsu");
		GThresholdImageOps.threshold(input, binary, GThresholdImageOps.computeEntropy(input, 0, 255), true);
		gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Global: Entropy");

		// Local method
		GThresholdImageOps.adaptiveSquare(input, binary, 28, 0, true, null, null);
		gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Adaptive: Square");
		GThresholdImageOps.adaptiveGaussian(input, binary, 42, 0, true, null, null);
		gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Adaptive: Gaussian");
		GThresholdImageOps.adaptiveSauvola(input, binary, 5, 0.30f, true);
		gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Adaptive: Sauvola");

		// Sauvola is tuned for text image.  Change radius to make it run better in others.

		// Show the image image for reference
		gui.addImage(ConvertBufferedImage.convertTo(input,null),"Input Image");

		String fileName =  imageName.substring(imageName.lastIndexOf('/')+1);
		ShowImages.showWindow(gui,fileName);
	}

	public static void main(String[] args) {
		// example in which global thresholding works best
		threshold("../data/applet/particles01.jpg");
		// example in which adaptive/local thresholding works best
		threshold("../data/applet/segment/uneven_lighting_squares.jpg");
		// hand written text with non-uniform stained background
		threshold("../data/applet/segment/stained_handwriting.jpg");
	}
}