Color filtering with OpenCV in python

This tutorial about Color Filtering in a given image. Sometimes we need to fetch the particular color or color range will be visible in the given image. This article will help you to build a python program which will produce an image which will show the particular color from the given image. OpenCV is a very popular python library for image processing and video processing. In this program, we have used the OpenCV library.

Filter color with OpenCV using python

color filtering in Python

Original Image -> Color filtered -> Background Grey( final image )


Requirements :

  • openCV library : cv2
  • NumPy library

Filtering the particular color range from a given image code is given below :

Here we used input image: ‘1.jpg’

color filtering in Python

colorful test image

Import libraries and image

import cv2 as cv
import numpy as np

img = cv.imread('1.jpg') # Importing Sample Test Image
cv.imshow('Image',img)  # Showing The Sample Test Image

Here we import the openCV and Numpy library.

Read an image in Python and open it in a Window

Then load the input image ‘1.jpg’ into img variable.

then show the img image.

Output :

color filtering in Python




print(type(img))    # Print the img variable data type
print(np.shape(img))  # Print the img variable dimension

Here we can find the data type of the img variable and what is the shape/Dimension of the array.

Output :

<class 'numpy.ndarray'>
(359, 640, 3)



lower_range = np.array([0,0,0])  # Set the Lower range value of color in BGR
upper_range = np.array([100,70,255])   # Set the Upper range value of color in BGR
mask = cv.inRange(img,lower_range,upper_range) # Create a mask with range
result = cv.bitwise_and(img,img,mask = mask)  # Performing bitwise and operation with mask in img variable

cv.imshow('Image1',result) # Image after bitwise operation

Here we set the lower_range and upper_range value of our requirement color in ->  BGR format [ Blue Green Red ].

Then we create a mask variable which holds a range.

Then we perform a bitwise And operation with the given image and applying mask variable as the mask parameter, then we stored the result in the result variable.

After doing the operations we displayed the result image.

Output :

color filtering in Python

output 2


bw = cv.cvtColor(img,cv.COLOR_BGR2GRAY)  # Converting the Orginal image to Gray
bw_bgr = cv.cvtColor(bw,cv.COLOR_GRAY2BGR) # Converting the Gray image to BGR format
result2 = cv.bitwise_or(bw_bgr,result) # Performing Bitwise OR operation with gray bgr image and previous result image

cv.imshow('Image2',result2)  # Showing The Final Result Image

Here we convert the given image into Gray ( Black & White ) format and stored into bw variable.

Then convert the bw image to BGR format to getting the 3-Channel color format and stored into bw_bgr variable.

Then we create a result2 variable and stored the bitwise OR operation with bw_bgr & result variable value.

Print the result2 image.

Finally, we got our result.


color filtering in Python

final output

Also, read,

Leave a Reply

Your email address will not be published. Required fields are marked *