How to binarize an image
How to binarize an image
The simple binarization method with a fixed threshold BinarizeFixed is using only one parameter - a threshold with value from 0 to 255. All pixels in this case that have an intensity greater than the defined threshold will be set as a black color, or if less than the threshold, then to a white color. You can use a more precise binarize method `Bradly` BinarizeBradley to improve binarization quality. Method `Bradly` use the threshold as an average estimation intensity of the surrounding area with size `s x s`. In this case, the resulting picture will have smooth edges. Also, you can use `Otsu` method BinarizeOtsu with automatic threshold:
import com.aspose.imaging.IMultipageImage; | |
import com.aspose.imaging.Image; | |
import com.aspose.imaging.RasterImage; | |
import com.aspose.imaging.imageoptions.PngOptions; | |
import java.io.File; | |
import java.util.Arrays; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.function.Consumer; | |
binarizeFixed(); | |
public static void binarizeOtsu() | |
{ | |
// https://apireference.aspose.com/imaging/java/com.aspose.imaging/RasterImage#binarizeOtsu-- | |
filterImages(RasterImage::binarizeOtsu, "binarizeotsu"); | |
} | |
public static void binarizeBradley() | |
{ | |
filterImages(image -> | |
{ | |
// https://apireference.aspose.com/imaging/java/com.aspose.imaging/RasterImage#binarizeBradley-double- | |
image.binarizeBradley(0.5); | |
}, "binarizebradley"); | |
} | |
public static void binarizeFixed() | |
{ | |
filterImages(image -> | |
{ | |
// https://apireference.aspose.com/imaging/java/com.aspose.imaging/RasterImage#binarizeFixed-byte- | |
image.binarizeFixed((byte)70); | |
}, "binarizefixed"); | |
} | |
static String templatesFolder = "D:\\"; | |
public static void filterImages(Consumer<RasterImage> doFilter, String filterName) | |
{ | |
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng", "dicom", | |
"jp2", "j2k", "tga", "webp", "tif", "gif", "ico"); | |
List<String> vectorFormats = Arrays.asList("svg", "otg", "odg", "eps", "wmf", "emf", "wmz", "emz", "cmx", "cdr"); | |
List<String> allFormats = new LinkedList<>(rasterFormats); | |
allFormats.addAll(vectorFormats); | |
allFormats.forEach( | |
formatExt -> | |
{ | |
String inputFile = templatesFolder + "template." + formatExt; | |
boolean isVectorFormat = vectorFormats.contains(formatExt); | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = rasterizeVectorImage(formatExt, inputFile); | |
} | |
String outputFile = templatesFolder + String.format("%s_%s.png", filterName, formatExt); | |
System.out.println("Processing " + formatExt); | |
try (RasterImage image = (RasterImage) Image.load(inputFile)) | |
{ | |
doFilter.accept(image); | |
//If image is multipage save each page to png to demonstrate results | |
if (image instanceof IMultipageImage && ((IMultipageImage) image).getPageCount() > 1) | |
{ | |
IMultipageImage multiPage = (IMultipageImage) image; | |
final int pageCount = multiPage.getPageCount(); | |
final Image[] pages = multiPage.getPages(); | |
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) | |
{ | |
String fileName = String.format("%s_page%d_%s.png", filterName, pageIndex, formatExt); | |
pages[pageIndex].save(fileName, new PngOptions()); | |
} | |
} | |
else | |
{ | |
image.save(outputFile, new PngOptions()); | |
} | |
} | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
new File(inputFile).delete(); | |
} | |
} | |
); | |
} | |
private static String rasterizeVectorImage(String formatExt, String inputFile) | |
{ | |
String outputFile = templatesFolder + "rasterized."+ formatExt + ".png"; | |
try (Image image = Image.load(inputFile)) | |
{ | |
image.save(outputFile, new PngOptions()); | |
} | |
return outputFile; | |
} |