How to detect specific colors from an image using OpenCV?

This recipe helps you detect specific colors from an image using OpenCV.

Recipe Objective: How to detect specific colors from an image using OpenCV?

This recipe will discuss color detection from image in Python OpenCV.

Deep Learning Project for Text Detection in Images Using Python

Steps For Python OpenCV Detect Color

The following steps will show you how to detect red color with OpenCV Python.

Step 1: Import Library and read the image

The image that we are using here is

OpenCV_Logo.jpg

 

import cv2
import numpy as np
image = cv2.imread('OpenCV_Logo.jpg')

Step 2: Define boundaries for the colors

In this example, let us detect the red color from the above image. We need to define a lower limit and an upper limit of BGR values of red colors for that purpose.

lower_red = np.array([0, 0, 200], dtype = "uint8")
upper_red= np.array([0, 0, 255], dtype = "uint8")

As we can see from the above lines of code, any color whose R > 200 and R < 255 is considered red.

Step 3: Python OpenCV- Find Color in Image Using cv2.inRange Function

The cv2.inRange() is the most commonly used function to detect colors in an image. This function expects three parameters as given below

  • src: Image from which the colors are to be detected

  • lowerb: Lower boundary of the color

  • upperb: Upper boundary of the color

The cv2.inRange() function returns a white binary image where the colors are detected and 0 otherwise.

mask = cv2.inRange(image, lower_red, upper_red)

Step 4: Perform Bitwise AND with the input image.

We perform bitwise and of the input image and mask the output with the image present in the mask variable to highlight the area where the color is detected in the input image. This can be achieved using the cv2.bitwise_and() function

detected_output = cv2.bitwise_and(image, image, mask = mask)
cv2.imshow("red color detection", detected_output)
cv2.waitKey(0)

Output:

red_detect.jpg




Download Materials

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy