Here we're going to segment one image ( with a bimodal histogram ), using Otsu's Algorithm, which tries to maximize inter class variance, by minimizing intra class variance. Finally, we obtain suitable intensity value ( to be used as threshold ), for that intensity value which is having highest inter class variance.
Pixel intensity classes defined by selecting a threshold ∈ [0, 255]
- Intensity < threshold :> class one
- Intensity >= threshold :> class two
Details available here.
Image to be segmented needs to be bi-modal, only then this algorithm performs well.
- Make sure you've JDK installed & added
in.itzmeanjan.filterit.jar
to your Java project. - Now in
Main.java
, put following code snippet.
// this is Main.java
// assuming weaver.jpg is present in current working
// directory or put image file name as you wish
import in.itzmeanjan.filterit.ImportExportImage;
import in.itzmeanjan.filterit.segmentation.AutomaticThresholding;
public class Main {
public static void main(String[] args) {
AutomaticThresholding automaticThresholding = new AutomaticThresholding();
System.out.println(
ImportExportImage.exportImage(
automaticThresholding.segment("./weaver.jpg"),
"segmentedUsingAutomaticThresholding.jpg"));
}
}
- Compile
Main.java
using following bash command.
$ javac -cp ".:in.itzmeanjan.filterit.jar" Main.java
- Run it with following command
$ java -cp ".:in.itzmeanjan.filterit.jar" Main
- And here's your result.
Original Image | Segmented Image |
---|---|
Thanking you :)