So far I have been unable to find any detailed documentation regarding binarizeFixed method. What I have found is the following:
"Parameters:
threshold - Threshold value. If corresponding gray value of a pixel is greater than threshold, a value of 255 will be assigned to it, 0 otherwise."
I understand this to mean any pixel below 100 will be black (0) and any pixel above this number will be white (255) however, when running the following line of code:
rasterCachedImage.binarizeFixed((byte) 127);
That is not the case. There are still a lot of gray pixels left in the image result.
Can you please explain how this function works in more detail and recommend how I can get an image to convert to a true black and white image using a threshold value like 127?
Here is the code we are testing with:
try {
// Load an image in an instance of Image
Image image = Image.load("PATH TO INPUT FILE");
double scale = 0;
int width = image.getWidth();
scale = 50 / (double) width;
//Reduce image size for analysis
image.resizeWidthProportionally((int)(width * scale), ResizeType.LanczosResample);
// Cast the image to RasterCachedImage
RasterCachedImage rasterCachedImage = (RasterCachedImage) image;
// Check if image is cached
if (!rasterCachedImage.isCached()) {
// Cache image if not already cached
rasterCachedImage.cacheData();
}
// Binarize image with pre defined fixed threshold
rasterCachedImage.binarizeFixed((byte) 127);
// Save the resultant image
rasterCachedImage.save("PATH TO OUTPUT FILE");
} catch (Exception e) {
e.printStackTrace();
}
Original File:
wolverine.jpg (83.1 KB)
Output File:
wolverine_small.jpg (1.9 KB)