Python | Visualizing image in different color spaces

OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on pictures or videos. It was originally developed by Intel but was later maintained by Willow Garage and is now maintained by Itseez. This library is cross-platform that is it is available on multiple programming languages such as Python, C++ etc.

Let’s discuss different ways to visualize images where we will represent images in different formats like grayscale, RGB scale, Hot_map, edge map, Spectral map, etc.

RGB Image :

RGB image is represented by linear combination of 3 different channels which are R(Red), G(Green) and B(Blue). Pixel intensities in this color space are represented by values ranging from 0 to 255 for single channel. Thus, number of possibilities for one color represented by a pixel is 16 million approximately [255 x 255 x 255 ].

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to read image as RGB
  
# Importing cv2 and matplotlib module
import cv2
import matplotlib.pyplot as plt
  
# reads image as RGB
img = cv2.imread('g4g.png')
  
# shows the image
plt.imshow(img)

chevron_right


Output :

Gray Scale Image :

Grayscale image contains only single channel. Pixel intensities in this color space is represented by values ranging from 0 to 255. Thus, number of possibilities for one color represented by a pixel is 256.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to read image as GrayScale
  
# Importing cv2 module
import cv2
  
# Reads image as gray scale
img = cv2.imread('g4g.png', 0
  
# We can alternatively convert
# image by using cv2color
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
# Shows the image
cv2.imshow('image', img) 
  
cv2.waitKey(0)         
cv2.destroyAllWindows()

chevron_right


Output :

YCrCb Color Space :

Y represents Luminance or Luma component, Cb and Cr are Chroma components. Cb represents the blue-difference (difference of blue component and Luma Component). Cr represents the red-difference (difference of red component and Luma Component).

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to read image
# as YCrCb color space
  
# Import cv2 module
import cv2
  
# Reads the image
img = cv2.imread('g4g.png')
  
# Convert to YCrCb color space
img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
  
# Shows the image
cv2.imshow('image', img) 
  
cv2.waitKey(0)         
cv2.destroyAllWindows()

chevron_right


Output :

HSV color space :

H : Hue represents dominant wavelength.
S : Saturation represents shades of color.
V : Value represents Intensity.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to read image
# as HSV color space
  
# Importing cv2 module
import cv2
  
# Reads the image
img = cv2.imread('g4g.png')
  
# Converts to HSV color space
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  
# Shows the image
cv2.imshow('image', img) 
  
cv2.waitKey(0)         
cv2.destroyAllWindows()

chevron_right


Output :

LAB color space :

L – Represents Lightness.
A – Color component ranging from Green to Magenta.
B – Color component ranging from Blue to Yellow.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to read image
# as LAB color space
  
# Importing cv2 module
import cv2
  
# Reads the image
img = cv2.imread('g4g.png')
  
# Converts to LAB color space
img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
  
# Shows the image
cv2.imshow('image', img) 
  
cv2.waitKey(0)         
cv2.destroyAllWindows()

chevron_right


Output :

Edge map of image :

Edge map can be obtained by various filters like laplacian, sobel, etc. Here, we use Laplacian to generate edge map.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to read image
# as EdgeMap
  
# Importing cv2 module
import cv2
  
# Reads the image
img = cv2.imread('g4g.png')
  
  
laplacian = cv2.Laplacian(img, cv2.CV_64F)
cv2.imshow('EdgeMap', laplacian) 
  
cv2.waitKey(0)         
cv2.destroyAllWindows()

chevron_right


Output :

Heat map of image :

In Heat map representation, individual values contained in a matrix are represented as colors.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to visualize 
# Heat map of image
  
# Importing matplotlib and cv2
import matplotlib.pyplot as plt
import cv2
  
# reads the image
img = cv2.imread('g4g.png')
  
# plot heat map image
plt.imshow(img, cmap ='hot'

chevron_right


Output :

Spectral Image map :

Spectral Image map obtains the spectrum for each pixel in the image of a scene.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to visualize 
# Spectral map of image
  
# Importing matplotlib and cv2
import matplotlib.pyplot as plt
import cv2
  
img = cv2.imread('g4g.png')
plt.imshow(img, cmap ='nipy_spectral'

chevron_right


Output :



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.