Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

skimage: Why does rgb2gray from skimage.color result in a colored image?

When I tried to convert the image to gray scale using:

from skimage.io import imread
from skimage.color import rgb2gray
mountain_r = rgb2gray(imread(os.getcwd() + '/mountain.jpg'))

#Plot
import matplotlib.pyplot as plt
plt.figure(0)
plt.imshow(mountain_r)
plt.show()

I got a weird colored image instead of a gray scale.

Manually implementing the function also gives me the same result. The custom function is:

def rgb2grey(rgb):
    if len(rgb.shape) is 3:
        return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

    else:
        print 'Current image is already in grayscale.'
        return rgb

Original

Coloured image that is not in greyscale. gray

Why doesn't the function convert the image to greyscale?

like image 226
kwotsin Avatar asked Oct 01 '16 10:10

kwotsin


People also ask

How do I convert an image from RGB to grayscale in Python?

Convert an Image to Grayscale in Python Using the Conversion Formula and the Matplotlib Library. We can also convert an image to grayscale using the standard RGB to grayscale conversion formula that is imgGray = 0.2989 * R + 0.5870 * G + 0.1140 * B .

How do I convert an image to grayscale in OpenCV?

Step 1: Import OpenCV. Step 2: Read the original image using imread(). Step 3: Convert to grayscale using cv2. cvtcolor() function.

How do I convert RGB to grayscale?

You just have to take the average of three colors. Since its an RGB image, so it means that you have add r with g with b and then divide it by 3 to get your desired grayscale image. Its done in this way.

How do you convert an image to grayscale in Matlab?

I = rgb2gray( RGB ) converts the truecolor image RGB to the grayscale image I . The rgb2gray function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance. If you have Parallel Computing Toolbox™ installed, rgb2gray can perform this conversion on a GPU.


1 Answers

The resulting image is in grayscale. However, imshow, by default, uses a kind of heatmap (called viridis) to display the image intensities. Just specify the grayscale colormap as shown below:

plt.imshow(mountain_r, cmap="gray")

For all the possible colormaps, have a look at the colormap reference.

like image 53
Tony Power Avatar answered Oct 05 '22 11:10

Tony Power