Skip to content
Related Articles

Related Articles

Mahotas – RGB to Gray Conversion
  • Last Updated : 03 Jul, 2020

In this article we will see how we can covert rgb image to gray in mahotas. An RGB image, sometimes referred to as a truecolor image, is stored in MATLAB as an m-by-n-by-3 data array that defines red, green, and blue color components for each individual pixel.

In this tutorial we will use “lena” image, below is the command to load it.

mahotas.demos.load('lena')

Below is the lena image

In order to do this we will use mahotas.colors.rgb2greymethod

Syntax : mahotas.colors.rgb2gray(img)



Argument :It takes image object as argument

Return : It returns image object

Note : It can convert image to greyscale as using rgb2grey or rgb2gray, both work). This conversion uses a visually realistic method (which weighs the green channel more heavily as human eyes are more sensitive to it)

Below is the implementation




# importing required libraries
import mahotas
import mahotas.demos
from pylab import gray, imshow, show
import numpy as np
   
# loading iamge
img = mahotas.demos.load('lena')
  
   
# showing image
print("Image")
imshow(img)
show()
  
# rgb to gray
new_img = mahotas.colors.rgb2gray(img)
  
# showing new image
print("New Image")
imshow(new_img)
show()

Output :

Image

New Image

Another example




# importing required libraries
import mahotas
import numpy as np
import matplotlib.pyplot as plt
import os
   
# loading iamge
img = mahotas.imread('dog_image.png')
        
# fltering image
img = img[:, :, :3]
  
# showing image
print("Image")
imshow(img)
show()
  
# rgb to gray
new_img = mahotas.colors.rgb2gray(img)
  
# showing new image
print("New Image")
imshow(new_img)
show()

Output :

Image

New Image

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :