Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active August 7, 2021 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/fdfaa4f8414fb753527a39f837ccee00 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/fdfaa4f8414fb753527a39f837ccee00 to your computer and use it in GitHub Desktop.
Convert RGB Image to Grayscale using Java
// Load an image in an instance of Image
try (Image image = Image.load("aspose-logo.jpg"))
{
// Cast the image to RasterCachedImage and Check if image is cached
RasterCachedImage rasterCachedImage = (RasterCachedImage)image;
if (!rasterCachedImage.IsCached)
{
// Cache image if not already cached
rasterCachedImage.CacheData();
}
// Binarize image with predefined fixed threshold and Save the resultant image
rasterCachedImage.binarizeFixed((byte) 100);
rasterCachedImage.save("BinarizationWithFixedThreshold_out.jpg");
}
// Load an image in an instance of Image
try (Image image = Image.load("aspose-logo.jpg"))
{
// Cast the image to RasterCachedImage and Check if image is cached
RasterCachedImage rasterCachedImage = (RasterCachedImage) image;
if (!rasterCachedImage.isCached())
{
// Cache image if not already cached
rasterCachedImage.cacheData();
}
// Binarize image with Otsu Thresholding
rasterCachedImage.binarizeOtsu();
// Save the resultant image
rasterCachedImage.save("BinarizationWithOtsuThreshold_out.jpg");
}
// Load an image in an instance of Image
try (Image image = Image.load("aspose-logo.jpg"))
{
// 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();
}
// Transform image to its grayscale representation
rasterCachedImage.grayscale();
// Save the resultant image
rasterCachedImage.save("Grayscaling_out.jpg");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment