Color Spaces

Learn about different color spaces and conversion from one color space to another.

What is a color space?

A color space is a specific organization of colors. It’s essentially a system of representing an array of pixel colors. There are several color spaces, such as BGR, grayscale, HSV, Lab, and many more. Here, we’ll learn to convert our image from one color space to another.

Converting from BGR to grayscale

OpenCV reads images in BGR format. To convert an image to grayscale, we have to use the cvtColor() method of the OpenCV library. We need to give three parameters to this function:

  • The first parameter is the original image.
  • The second parameter is the Mat class object that stores the converted image.
  • The third parameter is the color code. It directs the change of color spaces of the image. For example, color code COLOR_BGR2GRAY changes the color space from BGR to grayscale.

Get hands-on with 1200+ tech skills courses.