Color Conversions in OpenCV

OpenCV allows us to work with various colorspaces. Colorspaces have their importance for various applications. We can choose a colorspace according to our application requirements. Fortunately, OpenCV provides more than 150 color-space conversion methods. In the scope of this book, we will cover only few of them. However, once you learn about the color conversion for some of them, you can definitely work for rest of them.

OpenCV provides cvtColor() function to convert an image from one color space to another. Let us look at the parameters of the function which are shown below:

cv.cvtColor( src, code[, dst[, dstCn]] )

Parameter Description
src The input image.
code One of the color space conversion code, this must be one of the codes provided in ColorConversionCodes.
dst output image of the same size and depth as src.
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.

BGR to Grayscale Conversion

Let us see an example to convert a BGR image to a grayscale image. The following lines of code does this:

1
2
3
4
5
6
7
8
9
import cv2
import numpy as np
path = r'F:\img\lena.jpg'
img = cv2.imread(path,cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('BGR',img)
cv2.imshow('Gray',gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

Line 5: It reads a color image. Remember, OpenCV stores images in BGR format, i.e. the order of channels is like this: blue, green and red.
Line 6: This line actually converts the color image from RGB to grayscale format. The grayscale image has only one color, so not color information is retained.
Line 7, 8: Line 7 displays original image in BGR format while line 8 shows the results of grayscale image. Both original image and grayscale images are shown below:

No image
Figure 1. A sample RBG image on the left side. It is converted to grayscale shown on right side.

BGR to HSV Conversion

The following lines of code show color conversion from BGR to HSV colorspace.

1
2
3
4
5
6
7
8
9
import cv2
import numpy as np
path = r'F:\img\lena.jpg'
img = cv2.imread(path,cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('BGR',img)
cv2.imshow('Gray',gray)
cv2.waitKey(0)
cv2.destroyAllWindows()


The output obtained by running the above code is shown in Figure 2.

A generic square placeholder image with rounded corners in a figure.
Figure 2: Left is the input BGR image and right is output HSV.

BGR to CIE XYZ Conversion

The following lines of code show color conversion from BGR to CIE XYZ colorspace.

1
2
3
4
5
6
7
8
9
import cv2
import numpy as np
path = r'F:\img\lena.jpg'
img = cv2.imread(path,cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('BGR',img)
cv2.imshow('Gray',gray)
cv2.waitKey(0)
cv2.destroyAllWindows()


The output obtained by running the above code is shown in Figure 3.

.
Figure 3: Left is the input BGR image and right is output CIE XYZ.

BGR to YCC Conversion

The following lines of code show color conversion from BGR to YCC colorspace.

1
2
3
4
5
6
7
8
9
import cv2
import numpy as np
path = r'F:\img\lena.jpg'
img = cv2.imread(path,cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('BGR',img)
cv2.imshow('Gray',gray)
cv2.waitKey(0)
cv2.destroyAllWindows()


The output obtained by running the above code is shown in Figure 4.

.
Figure 4: Left is the input BGR image and right is output YCC.

BGR to CIE Luv Conversion

The following lines of code show color conversion from BGR to CIE Luv colorspace.

1
2
3
4
5
6
7
8
9
import cv2
import numpy as np
path = r'F:\img\lena.jpg'
img = cv2.imread(path,cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2Luv)
cv2.imshow('BGR',img)
cv2.imshow('Gray',gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

.
Figure 5: Left is the input BGR image and right is output CIE Luv .

For some conversions, we need to be careful about the range of values. We can normalize the pixel values between 0 and 1, then apply the conversion to make sure the values are correctly converted. For instance, the following code shows how can be rescale the pixels values between 0 and 1 then apply the conversion. The results are the same but conversion is correct.

1
2
3
4
5
6
scaled = img*(1./255);
normalized = cv2.cvtColor(img, cv2.COLOR_BGR2Luv);
cv2.imshow('BGR',scaled)
cv2.imshow('BGR2Luv',normalized)
cv2.waitKey(0)
cv2.destroyAllWindows()

OpenCV Tutorials