Read the complete article on converting RGB images to grayscale using Java: https://blog.aspose.com/2021/08/06/convert-rgb-images-to-grayscale-in-java/
Last active
August 7, 2021 01:41
-
-
Save aspose-com-gists/fdfaa4f8414fb753527a39f837ccee00 to your computer and use it in GitHub Desktop.
Convert RGB Image to Grayscale using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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