Lab Working with Color Spaces

OpenCVOpenCVBeginner
Practice Now

Introduction

Welcome to this tutorial on OpenCV-Python Colorspaces!

A colorspace is a way to represent the color channels in an image. There are several different colorspaces, but the three most common ones are:

  • BGR (Blue, Green, Red): The default colorspace used by OpenCV.
  • Gray: A grayscale representation of the image.
  • HSV (Hue, Saturation, Value): A cylindrical coordinate representation of colors.
    In this tutorial, we'll be focusing on converting images between the BGR, Gray, and HSV colorspaces.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL opencv(("`OpenCV`")) -.-> opencv/BasicOperationsGroup(["`Basic Operations`"]) opencv(("`OpenCV`")) -.-> opencv/ImageProcessingGroup(["`Image Processing`"]) opencv/BasicOperationsGroup -.-> opencv/imread("`Image Read`") opencv/BasicOperationsGroup -.-> opencv/bitwise_and("`Bitwise AND`") opencv/BasicOperationsGroup -.-> opencv/imwrite("`Image Write`") opencv/ImageProcessingGroup -.-> opencv/cvt_color("`Convert Color Space`") opencv/ImageProcessingGroup -.-> opencv/in_range("`Color Space Range`") subgraph Lab Skills opencv/imread -.-> lab-21417{{"`Lab Working with Color Spaces`"}} opencv/bitwise_and -.-> lab-21417{{"`Lab Working with Color Spaces`"}} opencv/imwrite -.-> lab-21417{{"`Lab Working with Color Spaces`"}} opencv/cvt_color -.-> lab-21417{{"`Lab Working with Color Spaces`"}} opencv/in_range -.-> lab-21417{{"`Lab Working with Color Spaces`"}} end

Converting Images Between Colorspaces

To convert images between colorspaces, we use the cv.cvtColor() function. First, let's import the necessary libraries and read an image:

Open the Python Shell

Open the Python shell by typing the following command in the terminal.

python3

Use the cv.imread() function to read the image image.jpg we prepared on the default folder.

import cv2 as cv
import numpy as np

## Read the image
image = cv.imread('image.jpg')

BGR to Gray

To convert the image from BGR to Gray, use the cv.COLOR_BGR2GRAY flag:

## Converting image to grayscale
gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

BGR to HSV

To convert the image from BGR to HSV, use the cv.COLOR_BGR2HSV flag:

## Use HSV colour space. HSV colour space is mainly used for object tracking
hsv_image = cv.cvtColor(image, cv.COLOR_BGR2HSV)

Display the Converted Images

To display the original and converted images, use the cv.imwrite() functions:

## Save the image to the specified file
cv.imwrite('image.jpg', image)
cv.imwrite('gray_image.jpg', gray_image)
cv.imwrite('hsv_image.jpg', hsv_image)

Extracting a Colored Object

To extract a specific colored object from an image, you can use the cv.inRange() function. For this example, we'll extract a blue object from the image.

Define the Color Range

First, define the lower and upper bounds of the blue color in the HSV colorspace:

## Blue is represented in HSV at a hue of around 240 degrees out of 360.
## The Hue range in OpenCV-HSV is 0-180, to store the value in 8 bits.
## Thus, blue is represented in OpenCV-HSV as a value of H around 240 / 2 = 120.
## To detect blue correctly, the following values could be chosen:
blue_lower = np.array([100, 150, 0], np.uint8)
blue_upper = np.array([140, 255, 255], np.uint8)

Threshold the Image

Threshold the HSV image to get only the blue colors:

## mask of blue color
blue_mask = cv.inRange(hsv_image, blue_lower, blue_upper)

Apply the Mask

Apply the mask to the original image to extract the blue object:

## Use the mask to extract the blue object
blue_object = cv.bitwise_and(image, image, mask=blue_mask)

Display the Extracted Object

Display the original image and the extracted blue object:

## Save the image to the specified file
cv.imwrite('blue_object.jpg', blue_object)

Summary

In this tutorial, we covered the basics of OpenCV-Python colorspaces, with this knowledge, you can now work with different colorspaces and apply them to various image processing tasks. You can also experiment with other colorspaces supported by OpenCV, such as LAB or YCrCb.

Other OpenCV Tutorials you may like