Opencv python color detection

Real time object color detection using OpenCV

In this article, we will discuss how to detect a monochromatic colour object using python and OpenCV. Monochromatic color means light of a single wavelength. We will use the video, captured using a webcam as input and try to detect objects of a single color, especially Blue. But you can detect any color if you set the range properly, we’ll discuss later.

To achieve that it’s highly recommended to convert the colors from RGB format to HSV. HSV stands for hue, saturation, and value. Hue signifies the color itself, technically it denotes the angle in the HSV color model.

Let’s understand the HSV color model first. Imagine a cylinder, it has a height, a radius, and a curved surface area. Take the initial position of the radius as a reference and move the radius anticlockwise to an angle θ, i.e. θ is the angle between the current position of the radius with its initial position. If θ is 0 degrees then it denotes the color red, if θ is 120 degrees then it denotes green, and so on. The length of the radius is the saturation of the color and the height signifies the value or V in HSV.

The main idea is to convert the input RGB image (BGR in the case of OpenCV because that’s how images are formatted in this module) to HSV format, which will make it easier for us to mask the specific color out of the frame. That is, whatever color in the provided HSV range will be given a value of 255 and others will be simply 0, and as a result, every object with color in the specified range will change to white leaving the rest of the image i.e. background black.

To show the color we need to bitwise and the current frame with the mask. For this, there is an inbuilt function called bitwise_and()

syntax: result = cv2.bitwise_and(pic1, pic2, mask)

where pic1 and pic 2 are the input images, and the other is a mask. A mask can be thought of as a cut-out shape applied to an image so that only the cut-out part is visible.

Implementation:

First, create an OpenCV video capture object after importing the required module using the following two codes:

import cv2 import numpy as np

Then start an infinite while loop, to read every frame read by the webcam. Convert every frame from BGR format to HSV format using the cv2.cvtColor() function, it takes the frame as the first input and the type of color conversion as the second input.

syntax: cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

After that specify the lower and upper limit of the color blue ( or any color you prefer). By creating two NumPy arrays with the desired lower and upper limit as [H, S, V]. These two NumPy arrays will be used as arguments in the thresholding function i.e. the cv2.inRange() function. Which takes three arguments, image source, lower limit, and the upper limit.

syntax: cv2.inRange(source, lower_limit, upper_limit)

cv2.inRange() function sets all the values that lie within the range to 255 and the rest to 0. The output of this function will be our mask. Finally passing this mask in the bitwise_and function mentioned earlier will produce the desired result. The output is given below.

Читайте также:  No module named xlrd python

Источник

Opencv python color detection

  • Image Resizing using OpenCV | Python
  • Python OpenCV | cv2.erode() method
  • Python | Image blurring using OpenCV
  • Python OpenCV | cv2.copyMakeBorder() method
  • Python | Grayscaling of Images using OpenCV
  • Image Processing in Python (Scaling, Rotating, Shifting and Edge Detection)
  • Erosion and Dilation of images using OpenCV in python
  • OpenCV Python Program to analyze an image using Histogram
  • Histograms Equalization in OpenCV
  • Python | Thresholding techniques using OpenCV | Set-1 (Simple Thresholding)
  • Python | Thresholding techniques using OpenCV | Set-2 (Adaptive Thresholding)
  • Python | Thresholding techniques using OpenCV | Set-3 (Otsu Thresholding)
  • OpenCV: Segmentation using Thresholding
  • Python OpenCV | cv2.cvtColor() method
  • Filter Color with OpenCV
  • Python | Denoising of colored images using opencv
  • Python | Visualizing image in different color spaces
  • Find Co-ordinates of Contours using OpenCV | Python
  • Python | Bilateral Filtering
  • Image Inpainting using OpenCV
  • Python | Intensity Transformation Operations on Images
  • Python | Image Registration using OpenCV
  • Python | Background subtraction using OpenCV
  • Background Subtraction in an Image using Concept of Running Average
  • Python | Foreground Extraction in an Image using Grabcut Algorithm
  • Python | Morphological Operations in Image Processing (Opening) | Set-1
  • Python | Morphological Operations in Image Processing (Closing) | Set-2
  • Python | Morphological Operations in Image Processing (Gradient) | Set-3
  • Image segmentation using Morphological operations in Python
  • Image Translation using OpenCV | Python
  • Image Pyramid using OpenCV | Python
  • Python | Program to extract frames using OpenCV
  • Displaying the coordinates of the points clicked on the image using Python-OpenCV
  • White and black dot detection using OpenCV | Python
  • Python | OpenCV BGR color palette with trackbars
  • Draw a rectangular shape and extract objects using Python’s OpenCV
  • Invisible Cloak using OpenCV | Python Project
  • ML | Unsupervised Face Clustering Pipeline
  • Saving Operated Video from a webcam using OpenCV
  • Face Detection using Python and OpenCV with webcam
  • Opening multiple color windows to capture using OpenCV in Python
  • Python | Play a video in reverse mode using OpenCV
  • Template matching using OpenCV in Python
  • Cartooning an Image using OpenCV – Python
  • Vehicle detection using OpenCV Python
  • Count number of Faces using Python – OpenCV
  • Live Webcam Drawing using OpenCV
  • Detect and Recognize Car License Plate from a video in real time
  • Build GUI Application Pencil Sketch from Photo in Python
  • Python OpenCV – Drowsiness Detection
  • Face Alignment with OpenCV and Python
  • Age Detection using Deep Learning in OpenCV
  • Right and Left Hand Detection Using Python
  • OpenCV Python: How to detect if a window is closed?
  • Save frames of live video with timestamps – Python OpenCV
  • Detecting low contrast images with OpenCV, scikit-image, and Python
  • Animate image using OpenCV in Python
  • Drawing a cross on an image with OpenCV
  • Blur and anonymize faces with OpenCV and Python
  • Face detection using Cascade Classifier using OpenCV-Python
  • Real time object color detection using OpenCV
  • Python – Writing to video with OpenCV
  • Add image to a live camera feed using OpenCV-Python
  • Face and Hand Landmarks Detection using Python – Mediapipe, OpenCV
  • Emotion Based Music Player – Python Project
  • Realtime Distance Estimation Using OpenCV – Python
  • Webcam QR code scanner using OpenCV
  • Color Identification in Images using Python – OpenCV
  • Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
  • Opencv Python program for Face Detection
  • Image Resizing using OpenCV | Python
  • Python OpenCV | cv2.erode() method
  • Python | Image blurring using OpenCV
  • Python OpenCV | cv2.copyMakeBorder() method
  • Python | Grayscaling of Images using OpenCV
  • Image Processing in Python (Scaling, Rotating, Shifting and Edge Detection)
  • Erosion and Dilation of images using OpenCV in python
  • OpenCV Python Program to analyze an image using Histogram
  • Histograms Equalization in OpenCV
  • Python | Thresholding techniques using OpenCV | Set-1 (Simple Thresholding)
  • Python | Thresholding techniques using OpenCV | Set-2 (Adaptive Thresholding)
  • Python | Thresholding techniques using OpenCV | Set-3 (Otsu Thresholding)
  • OpenCV: Segmentation using Thresholding
  • Python OpenCV | cv2.cvtColor() method
  • Filter Color with OpenCV
  • Python | Denoising of colored images using opencv
  • Python | Visualizing image in different color spaces
  • Find Co-ordinates of Contours using OpenCV | Python
  • Python | Bilateral Filtering
  • Image Inpainting using OpenCV
  • Python | Intensity Transformation Operations on Images
  • Python | Image Registration using OpenCV
  • Python | Background subtraction using OpenCV
  • Background Subtraction in an Image using Concept of Running Average
  • Python | Foreground Extraction in an Image using Grabcut Algorithm
  • Python | Morphological Operations in Image Processing (Opening) | Set-1
  • Python | Morphological Operations in Image Processing (Closing) | Set-2
  • Python | Morphological Operations in Image Processing (Gradient) | Set-3
  • Image segmentation using Morphological operations in Python
  • Image Translation using OpenCV | Python
  • Image Pyramid using OpenCV | Python
  • Python | Program to extract frames using OpenCV
  • Displaying the coordinates of the points clicked on the image using Python-OpenCV
  • White and black dot detection using OpenCV | Python
  • Python | OpenCV BGR color palette with trackbars
  • Draw a rectangular shape and extract objects using Python’s OpenCV
  • Invisible Cloak using OpenCV | Python Project
  • ML | Unsupervised Face Clustering Pipeline
  • Saving Operated Video from a webcam using OpenCV
  • Face Detection using Python and OpenCV with webcam
  • Opening multiple color windows to capture using OpenCV in Python
  • Python | Play a video in reverse mode using OpenCV
  • Template matching using OpenCV in Python
  • Cartooning an Image using OpenCV – Python
  • Vehicle detection using OpenCV Python
  • Count number of Faces using Python – OpenCV
  • Live Webcam Drawing using OpenCV
  • Detect and Recognize Car License Plate from a video in real time
  • Build GUI Application Pencil Sketch from Photo in Python
  • Python OpenCV – Drowsiness Detection
  • Face Alignment with OpenCV and Python
  • Age Detection using Deep Learning in OpenCV
  • Right and Left Hand Detection Using Python
  • OpenCV Python: How to detect if a window is closed?
  • Save frames of live video with timestamps – Python OpenCV
  • Detecting low contrast images with OpenCV, scikit-image, and Python
  • Animate image using OpenCV in Python
  • Drawing a cross on an image with OpenCV
  • Blur and anonymize faces with OpenCV and Python
  • Face detection using Cascade Classifier using OpenCV-Python
  • Real time object color detection using OpenCV
  • Python – Writing to video with OpenCV
  • Add image to a live camera feed using OpenCV-Python
  • Face and Hand Landmarks Detection using Python – Mediapipe, OpenCV
  • Emotion Based Music Player – Python Project
  • Realtime Distance Estimation Using OpenCV – Python
  • Webcam QR code scanner using OpenCV
  • Color Identification in Images using Python – OpenCV
  • Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
  • Opencv Python program for Face Detection
Читайте также:  Java integer valueof exception

Источник

Оцените статью