Change language

Python OpenCV cv2.cvtColor () method

| |

OpenCV is an open source computer vision and machine learning library. It includes more than 2,500 algorithms, which include both classical and modern algorithms for computer vision and machine learning. This library has interfaces in various languages, among which are Python (we use it in this article), Java, C++ and Matlab.

The cv2.cvtColor () method is used to convert an image from one color space to another. There are over 150 color space conversion methods available in OpenCV. Below we will use some of the color space conversion codes.

Syntax: cv2.cvtColor(src, code[, dst[, dstCn]])

src: It is the image whose color space is to be changed.
code: It is the color space conversion code.
dst: It is the output image of the same size and depth as src image. It is an optional parameter.
dstCn: It is the number of channels in the destination image. If the parameter is 0 then the number of the channels is derived automatically from src and code. It is an optional parameter.

Return Value: It returns an image.

The default color format in OpenCV is often referred to as RGB, but it’s actually BGR (bytes are inverted). Therefore, the first byte in a standard color image (24-bit) will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red. The fourth, fifth and sixth bytes would then be the second pixel (blue, green, red) and so on.

We’ll discuss the importance of lighting conditions in any computer vision and image processing pipeline.

We’ll then review the three goals you should seek to obtain when working with lighting conditions:

  • High contrast
  • Generalizable
  • Stable

Example #1

def __get_annotation__(self, mask, image=None):

        _, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        segmentation = []
        for contour in contours:
            # Valid polygons have >= 6 coordinates (3 points)
            if contour.size >= 6:
        RLEs = cocomask.frPyObjects(segmentation, mask.shape[0], mask.shape[1])
        RLE = cocomask.merge(RLEs)
        # RLE = cocomask.encode(np.asfortranarray(mask))
        area = cocomask.area(RLE)
        [x, y, w, h] = cv2.boundingRect(mask)

        if image is not None:
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            cv2.drawContours(image, contours, -1, (0,255,0), 1)
            cv2.rectangle(image,(x,y),(x+w,y+h), (255,0,0), 2)
            cv2.imshow("", image)

        return segmentation, [x, y, w, h], area 

Example #2

def main():
	imagePath = "test3.jpg"
	cascPath = "cascades/haarcascade_pedestrian.xml"

	pplCascade = cv2.CascadeClassifier(cascPath)
	image = cv2.imread(imagePath)
	gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	gray = normalize_grayimage(gray)
	pedestrians = pplCascade.detectMultiScale(
		flags =

	print "Found {0} ppl!".format(len(pedestrians))

	#Draw a rectangle around the detected objects
	for (x, y, w, h) in pedestrians:
		cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

	cv2.imwrite("saida.jpg", image)
	cv2.imshow("Ppl found", image)
	return 0 

Example #3

def _update_mean_shift_bookkeeping(self, frame, box_grouped):
        """Preprocess all valid bounding boxes for mean-shift tracking

            This method preprocesses all relevant bounding boxes (those that
            have been detected by both mean-shift tracking and saliency) for
            the next mean-shift step.

            :param frame: current RGB input frame
            :param box_grouped: list of bounding boxes
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        self.object_roi = []
        self.object_box = []
        for box in box_grouped:
            (x, y, w, h) = box
            hsv_roi = hsv[y:y + h, x:x + w]
            mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)),
                               np.array((180., 255., 255.)))
            roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
            cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)


Example #4

def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):
    x = (np.random.uniform(-1, 1, 3) * np.array([hgain, sgain, vgain]) + 1).astype(np.float32)  # random gains
    img_hsv = (cv2.cvtColor(img, cv2.COLOR_BGR2HSV) * x.reshape((1, 1, 3))).clip(None, 255).astype(np.uint8)
    cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img)  # no return needed

# def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):  # original version
#     # SV augmentation by 50%
#     img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)  # hue, sat, val
#     S = img_hsv[:, :, 1].astype(np.float32)  # saturation
#     V = img_hsv[:, :, 2].astype(np.float32)  # value
#     a = random.uniform(-1, 1) * sgain + 1
#     b = random.uniform(-1, 1) * vgain + 1
#     S *= a
#     V *= b
#     img_hsv[:, :, 1] = S if a < 1 else S.clip(None, 255)
#     img_hsv[:, :, 2] = V if b < 1 else V.clip(None, 255)
#     cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img)  # no return needed 

Example #5

def main():
	imagePath = "img.jpg"
	img = cv2.imread(imagePath)
	gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	cv2.imwrite("before.jpg", gray)

	gray = cv2.equalizeHist(gray)
	return 0 


When we talk about a color image, we usually mean an image that contains pixels of at least 3 different colors, with different wavelengths. Such as red, blue, green. Why we use exactly 3 different colors and how we can describe any color with their help is perfectly described in this article.

So let’s load this image into OpenCV:

import cv2
import numpy as np
color_image = cv2.imread(’opencv_color.jpg’)

If we currently check the dimension of the array through the shape attribute, we will see the following:

(343, 702, 3)

The first numbers mean the resolution of the image in pixels: width 702, height 343, and the third number is more interesting - at the moment it shows that we are dealing with an image in which each pixel is characterized by three numbers - B G R, which mean its blue, green and red Components. Let’s look at each component separately:

cv2.imshow(’Color all’,color_image)
cv2.imshow(’Color blue’,color_image[:,:,0])
cv2.imshow(’Color green’,color_image[:,:,1])
cv2.imshow(’Color red’,color_image[:,:,2])


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method