OpenCV: Changing Colorspaces

| OpenCV 3.4.2 Open Source Computer Vision

Changing Colorspaces

## Goal

- In this tutorial, you will learn how to convert images from one color-space to another, like RGB \(\leftrightarrow\) Gray, RGB \(\leftrightarrow\) HSV etc.
- You will learn following functions : cv.cvtColor(), cv.inRange() etc.

## cvtColor

There are more than 150 color-space conversion methods available in OpenCV. But we will look into the most widely used one: RGB \(\leftrightarrow\) Gray.

We use the function: cv.cvtColor (src, dst, code, dstCn = 0)

Parameters

  src | input image.
  dst | output image of the same size and depth as src
  code | color space conversion code(see cv.ColorConversionCodes).
  dstCn | number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code.

For RGB \(\rightarrow\) Gray conversion we use the code cv.COLOR_RGBA2GRAY.

## Try it

## inRange

Checks if array elements lie between the elements of two other arrays.

We use the function: cv.inRange (src, lowerb, upperb, dst)

Parameters

  src | first input image.
  lowerb | inclusive lower boundary Mat of the same size as src.
  upperb | inclusive upper boundary Mat of the same size as src.
  dst | output image of the same size as src and cv.CV_8U type.

## Try it