If you have ever worked on a Computer Vision project, you might know that using augmentations to diversify the dataset is the best practice. On this page, we will:
Сover the To gray augmentation;
Check out its parameters;
See how To gray affects an image;
And check out how to work with To gray using Python through the Albumentations library.
Let’s jump in.
To define the term, To gray is an augmentation that converts the input color image to grayscale. If you are unfamiliar with the topic, for the grayscale image, each pixel contains only information about the amount of light within it. In other words, it carries only the intensity information. That is why grayscale images are monochrome and composed of shades of gray (from black at the pixel with the weakest intensity to white at the strongest).
As for the exact algorithm behind the To gray augmentation, it is straightforward. Any color image is composed of three channels (red, green, and blue - RGB), whereas each channel contains information about the intensity of a particular color in a specific pixel. The idea is to remove all the color information leaving only the luminance values. With that done, your image will become monochrome composed of shades of gray. Yes, it is as simple as that.
For Data Scientists, To gray is a valuable augmentation. With its help, researchers can make the model less focused on color as a signal. And in some cases, it might be an intelligent decision. Still, if you are trying to detect an object that is always of the same color, To gray is not the best fit for you. So, please analyze your case before using To gray as your augmentation.
Probability of applying transform - defines the likelihood of applying To gray to an image;
Hello, thank you for using the code provided by Hasty. Please note that some code blocks might not be 100% complete and ready to be run as is. This is done intentionally as we focus on implementing only the most challenging parts that might be tough to pick up from scratch. View our code block as a LEGO block - you can’t use it as a standalone solution, but you can take it and add to your system to complement it. If you have questions about using the tool, please get in touch with us to get direct help from the Hasty team.
import albumentations as albu
from PIL import Image
import numpy as np
transform = albu.augmentations.transforms.ToGray()
image = np.array(Image.open('/some/random/image.png'))
augmented_image = transform(image=image)['image']
# We have the transformed image in augmented_image.
Only 13% of vision AI projects make it to production, with Hasty we boost that number to 100%.
Start for free Request a demo