Python OpenCV Specific Color Detection from Capture Video
In this article, you will learn to detect a specific colour (say blue) from a captured video (webcam or video file) using Python OpenCV.
OpenCV stands for Open Source Computer Vision Library. It is a free, open source library that is used for computer vision. It provides good support for Machine Learning, Face Recognition, and Deep Learning. This module is being used for a very wide range of applications, like- object identification, image processing, video processing, and so on. It provides lots of functions for video processing, like cv2.VideoCapture() to read a video either from a camera or from a video file, VideoWriter() to store the video in a file, and so on.
The color detection process is mostly in demand in computer vision. A color detection algorithm identifies pixels in an image that match a specified color or color range. The color of the detecting pixels can then be changed to distinguish them from the rest of the image. This process can be easily done using OpenCV. Here, we explain the color detection code step-by-step.
Import Required Modules
First, we will import the necessary modules-
import cv2 import numpy as np
Next, we can either capture the video from a camera or from a video file. OpenCV provides the cv2.VideoCapture() method for this. It accepts either a video file or a device index as an argument. If we want to connect one camera device, the device index will be '0', i.e., cv2.VideoCapture(0). In this article, we will use a webcam.
cap = cv2.VideoCapture(0)
We will use read() method to read each frame of the video.
_, frame = cap.read()
Convert Colorspace of Video
The cv2.cvtColor() method is used to convert an image from one color-space to another. It specifies the type of conversion. The following code converts from BGR to HSV color-space.
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
Define the color range
Next, we will assign lower and upper blue color rgb in two variables-
light_blue = np.array([110,50,50]) dark_blue = np.array([130,255,255])
The OpenCV provides the cv2.inRange() method to set the color range. It accepts three parameters, the input image in the first parameter and lower and upper color boundary of the threshold region. This function returns a binary mask, which we will pass into the bitwise AND operator.
output = cv2.bitwise_and(frame,frame, mask= mask)
The given line waits for the keyboard button (Q) press using cv2.waitKey() method to stop the video -
if cv2.waitKey(1) & 0xFF == ord('Q'): break
Release the objects and destroy the windows
Next, use the release() and the cv2.destroyAllWindows() method to release the objects and destroy all windows.
cap.release() output.release() cv2.destroyAllWindows()
Here is the complete code to detect color from a captured video. In this, we have captured a video from a webcam, detected the blue color from the specified color range, and stored it in an mp4 file.
import cv2 import numpy as np # Create an object to read camera video cap = cv2.VideoCapture(0) video_cod = cv2.VideoWriter_fourcc(*'XVID') video_output= cv2.VideoWriter('captured_video.avi', video_cod, 10, (640,480)) while(True): _, frame = cap.read() # Convert BGR to HSV hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # define blue color range light_blue = np.array([110,50,50]) dark_blue = np.array([130,255,255]) # Threshold the HSV image to get only blue colors mask = cv2.inRange(hsv, light_blue, dark_blue) # Bitwise-AND mask and original image output = cv2.bitwise_and(frame,frame, mask= mask) # Write the frame into the file 'captured_video.avi' video_output.write(output) # Display the frame, saved in the file cv2.imshow('output',output) # Press Q on keyboard to stop recording if cv2.waitKey(1) & 0xFF == ord('Q'): break # release video capture # and video write objects cap.release() video_output.release() # Closes all the frames cv2.destroyAllWindows() print("The video was successfully saved")
Related ArticlesOpenCV color detection
OpenCV bitwise and
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
OpenCV body tracking
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples