/
ExampleThresholding.java
96 lines (83 loc) · 4.41 KB
/
ExampleThresholding.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (c) 2011-2018, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package boofcv.examples.segmentation;
import boofcv.alg.filter.binary.GThresholdImageOps;
import boofcv.alg.misc.ImageStatistics;
import boofcv.gui.ListDisplayPanel;
import boofcv.gui.binary.VisualizeBinaryData;
import boofcv.gui.image.ShowImages;
import boofcv.io.UtilIO;
import boofcv.io.image.ConvertBufferedImage;
import boofcv.io.image.UtilImageIO;
import boofcv.struct.ConfigLength;
import boofcv.struct.image.GrayF32;
import boofcv.struct.image.GrayU8;
import java.awt.image.BufferedImage;
/**
* 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 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
GrayF32 input = ConvertBufferedImage.convertFromSingle(image, null, GrayF32.class);
GrayU8 binary = new GrayU8(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.localMean(input, binary, ConfigLength.fixed(57), 1.0, true, null, null);
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Local: Square");
GThresholdImageOps.localBlockMinMax(input, binary, ConfigLength.fixed(21), 1.0, true, 15 );
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Local: Block Min-Max");
GThresholdImageOps.localBlockMean(input, binary, ConfigLength.fixed(21), 1.0, true );
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Local: Block Mean");
GThresholdImageOps.localBlockOtsu(input, binary, false,ConfigLength.fixed(21),0.5, 1.0, true );
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Local: Block Otsu");
GThresholdImageOps.localGaussian(input, binary, ConfigLength.fixed(85), 1.0, true, null, null);
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Local: Gaussian");
GThresholdImageOps.localSauvola(input, binary, ConfigLength.fixed(11), 0.30f, true);
gui.addImage(VisualizeBinaryData.renderBinary(binary, false, null),"Local: 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(UtilIO.pathExample("particles01.jpg"));
// example in which adaptive/local thresholding works best
threshold(UtilIO.pathExample("segment/uneven_lighting_squares.jpg"));
// hand written text with non-uniform stained background
threshold(UtilIO.pathExample("segment/stained_handwriting.jpg"));
}
}