Detect the RGB color from a webcam using Python - OpenCV Last Updated : 15 Jul, 2025 Comments Improve Suggest changes 4 Likes Like Report Prerequisites: Python NumPy, Python OpenCVEvery image is represented by 3 colors that are Red, Green and Blue. Let us see how to find the most dominant color captured by the webcam using Python.Approach:Import the cv2 and NumPy modulesCapture the webcam video using the cv2.VideoCapture(0) method.Display the current frame using the cv2.imshow() method.Run a while loop and take the current frame using the read() method.Take the red, blue and green elements and store them in a list.Compute the average of each list.Whichever average has the greatest value, display that color. Python import cv2 import numpy as np # taking the input from webcam vid = cv2.VideoCapture(0) # running while loop just to make sure that # our program keeps running until we stop it while True: # capturing the current frame _, frame = vid.read() # displaying the current frame cv2.imshow("frame", frame) # setting values for base colors b = frame[:, :, 0] # Blue channel g = frame[:, :, 1] # Green channel r = frame[:, :, 2] # Red channel # computing the mean b_mean = np.mean(b) g_mean = np.mean(g) r_mean = np.mean(r) # displaying the most prominent color if b_mean > g_mean and b_mean > r_mean: print("Blue") elif g_mean > r_mean and g_mean > b_mean: print("Green") else: print("Red") # breaking the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # releasing the video capture object and closing all windows vid.release() cv2.destroyAllWindows() Output: Create Quiz Comment R romilvishol Follow 4 Improve R romilvishol Follow 4 Improve Article Tags : Python OpenCV Python-OpenCV Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like