Mahotas - RGB to Gray Conversion



RGB to grayscale conversion in image processing transforms a colored image in the RGB color space to a grayscale image.

  • RGB images are composed of three color channels− red, green, and blue. Each pixel in an RGB image is represented by a combination of intensity values for these three channels, resulting in a wide range of colors.
  • On the other hand, grayscale images are single−channel images (contain only shades of (gray) where each pixel represents the intensity of the corresponding location in the original image.
  • The intensity values range from black (0) to white (255), with intermediate shades of gray.

RGB to Gray Conversion in Mahotas

In Mahotas, we can convert an RGB image to grayscale image using the colors.rgb2gray() function.

The function calculates the grayscale intensity of each pixel based on the weighted average of its RGB values.

The weights reflect the human perception of colors, with red having the highest weight, followed by green, and then blue.

The mahotas.colors.rgb2gray() Function

The mahotas.colors.rgb2gray() function takes an RGB image as input and returns the grayscale version of the image.

The resulting grayscale image retains the structure and overall content of the original RGB image but lacks color information.

Syntax

Following is the basic syntax of the rgb2gray() function in mahotas −

mahotas.colors.rgb2gray(rgb_image, dtype=float)

where,

  • rgb_image − It is the input image in RGB color space.

  • dtype (optional) − It is the data type of the returned image (default is float).

Example

In the following example, we are converting an RGB image to a grayscale image using the mh.colors.rgb2gray() function −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
gray_image = mh.colors.rgb2gray(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the grayscale image
axes[1].imshow(gray_image, cmap='gray')
axes[1].set_title('Grayscale Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
Output

Following is the output of the above code −

rgb2gray Color

Using Average of RGB Channels

We can also convert an RGB image to grayscale image using average of RGB channels.

The intensity of red, green, and blue color of each pixel is summed and divided by three to obtain the average intensity value. We can achieve this using the mean() function of the numpy library.

The resulting image will be a grayscale image having a single channel where each pixel represents the average intensity across the RGB channels, where each color channel contributes equally to the overall grayscale intensity.

Example

The following example shows conversion of an RGB image to grayscale using average of RGB channels −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
gray_image = np.mean(image, axis=2).astype(np.uint8)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the grayscale image
axes[1].imshow(gray_image, cmap='gray')
axes[1].set_title('Grayscale Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

Output

Output of the above code is as follows −

Average RGB Channels

Using Luminosity

Luminosity is a term used to describe the perceived brightness or lightness of an object or image.

In image processing, luminosity assigns specific weights to the red, green, and blue color channels of each pixel and combines them to calculate the grayscale intensity value.

The luminosity in the RGB color space can be calculated using the following formula −

Luminosity = 0.2989 * R + 0.5870 * G + 0.1140 * B

Where, the values 0.2989, 0.5870, and 0.1140 are the weights assigned to the red, green, and blue channels, respectively. These weights are derived from the standard Rec.709 color space used for digital displays.

This method produces better results compared to simply averaging the RGB channels, as it better captures the visual perception of the original image.

Example

Here, we have defined the luminosity of RGB to convert a colored image to its equivalent grayscale image −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to gray
gray_image = np.dot(image[..., :3], [0.2989, 0.5870, 0.1140]).astype(np.uint8)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the grayscale image
axes[1].imshow(gray_image, cmap='gray')
axes[1].set_title('Grayscale Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

Output

After executing the above code, we get the following output −

Luminosity Image
Advertisements