Python OpenCV ColorMap
In this post, you will learn how to apply the OpenCV colormap (cv2 colormap). Python provides several colormaps for performing visualisation.
OpenCV stands for Open Source Computer Vision Library. It is a free, open source library that is used for computer vision. It provides good support for machine learning, face recognition, deep learning, etc. The OpenCV module is being used for a very wide range of image processing and analysis tasks, like object identification, colour detection, optical character recognition, photo editing, and so on. It provides lots of functions for image processing.
Python OpenCV provides the applyColorMap() function to apply a color map to a given image. It has some predefined constants to apply the colormap. These are the following colormap lists-
- COLORMAP_AUTUMN = 0
- COLORMAP_BONE = 1
- COLORMAP_JET = 2
- COLORMAP_WINTER = 3
- COLORMAP_RAINBOW = 4
- COLORMAP_OCEAN = 5
- COLORMAP_SUMMER = 6
- COLORMAP_SPRING = 7
- COLORMAP_COOL = 8
- COLORMAP_HSV = 9
- COLORMAP_PINK = 10
- COLORMAP_HOT = 11
- COLORMAP_PARULA = 12
Syntax of cv2.applycolormap()
This function accepts three parameters.
applyColorMap(Mat src, Mat dst, int colormap)
The src is an object of the class Mat representing the source image. The dst is an object of the class Mat representing the destination image, and the colormap is a variable of integer type representing the type of the color map to be applied.
The basic use of a colormap is to show different meanings of a grayscale image with different colors. Like we can represent hotter and cooler regions on a map, we can also represent the autumn, the winter, the summer seasons, and so on.
In the given Python program, we are using 'COLORMAP_AUTUMN' and 'COLORMAP_BONE' colormap.
import cv2
import numpy as np
# image path
path = r'nature.jpg'
# using imread()
img = cv2.imread(path)
im1 = cv2.applyColorMap(img, cv2.COLORMAP_AUTUMN)
im2 = cv2.applyColorMap(img, cv2.COLORMAP_BONE)
cv2.imshow('image', np.hstack((im1, im2)))
cv2.waitKey(0);
cv2.destroyAllWindows();
cv2.waitKey(1)
Output of the above code:
In the given Python program, we are using 'COLORMAP_JET' and 'COLORMAP_WINTER' colormap.
import cv2
import numpy as np
# image path
path = r'nature.jpg'
# using imread()
img = cv2.imread(path)
im3 = cv2.applyColorMap(img, cv2.COLORMAP_JET)
im4 = cv2.applyColorMap(img, cv2.COLORMAP_WINTER)
cv2.imshow('image', np.hstack((im3, im4)))
cv2.waitKey(0);
cv2.destroyAllWindows();
cv2.waitKey(1)
Output of the above code:
Related Articles
Color detection OpenCVPython OpenCV Histogram of Grayscale Image
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples
Draw different shapes on image using Python OpenCV
OpenCV Logical Operators- Bitwise AND, OR, NOR, XOR
Python OpenCV Histogram of Color Image
Capture a video in Python OpenCV and save
Contour Detection using Python OpenCV
Python OpenCV Erosion and Dilation
Image Thresholding Python OpenCV
Python OpenCV Histogram Equalization
Human Body Detection Program In Python OpenCV
Adaptive Thresholding in Python OpenCV
Python OpenCV Erosion and Dilation
Eye Detection Program in Python OpenCV
Face Recognition OpenCV Source Code
Python Line Plot Using Matplotlib