0% found this document useful (0 votes)
2 views4 pages

Color Detection with OpenCV in Python

The document contains Python scripts that utilize OpenCV for image processing, specifically focusing on color detection of blue and red in an image named 'spider.jfif'. Each script loads the image, converts it to HSV color space, creates masks for blue and red colors, and displays the original image alongside the processed results. The scripts demonstrate various methods for extracting and visualizing specific color regions in the image.

Uploaded by

johnyoulong
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Color Detection with OpenCV in Python

The document contains Python scripts that utilize OpenCV for image processing, specifically focusing on color detection of blue and red in an image named 'spider.jfif'. Each script loads the image, converts it to HSV color space, creates masks for blue and red colors, and displays the original image alongside the processed results. The scripts demonstrate various methods for extracting and visualizing specific color regions in the image.

Uploaded by

johnyoulong
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name : Tha davith

ID: 0005720

[Link]

import cv2 # Import OpenCV img = [Link]('[Link]') # Load image if

img is None: print("Error") # Check for load error else: # If loaded

successfully hsv = [Link](img, cv2.COLOR_BGR2HSV) # BGR to HSV

rgb = [Link](img, cv2.COLOR_BGR2RGB) # BGR to RGB gray =

[Link](img, cv2.COLOR_BGR2GRAY) # BGR to Grayscale

[Link]('Original', img) # Show original [Link]('RGB', rgb) #

Show RGB [Link]('Grayscale', gray) # Show Grayscale

[Link]('HSV', hsv) # Show HSV [Link](0) # Wait for key press

[Link]() # Close windows

[Link] import cv2 # Import OpenCV import numpy as np # Import

NumPy for array manipulation

img = [Link]('[Link]') # Load image if img is None:

print("Error") # Check for load error else: # If loaded successfully

hsv = [Link](img, cv2.COLOR_BGR2HSV) # Convert to HSV

lower_blue = [Link]() # Lower bound of blue in HSV upper_blue

= [Link]() # Upper bound of blue in HSV

mask = [Link](hsv, lower_blue, upper_blue) # Create mask of blue pixels


res = cv2.bitwise_and(img, img, mask=mask) # Extract blue parts using the mask

[Link]('Original', img) # Show original image [Link]('HSV', hsv) #

Show HSV image (for reference) [Link]('Mask', mask) # Show the mask

(white = blue, black = not blue) [Link]('Result', res) # Show the result (only

blue parts visible)

[Link](0) # Wait for key press

[Link]() # Close windows [Link]

import cv2 # Import OpenCV import numpy as np #

Import NumPy

img = [Link]('[Link]') # Load image if img is None: print("Error:

Could not load image.") # Check for errors else:

hsv = [Link](img, cv2.COLOR_BGR2HSV) # Convert to HSV

# Blue detection
lower_blue = [Link]([110, 50, 50]) # Lower blue range upper_blue =

[Link]([130, 255, 255]) # Upper blue range mask_blue = [Link](hsv,

lower_blue, upper_blue) # Blue mask res_blue = cv2.bitwise_and(img,

img, mask=mask_blue) # Extract blue

# Red detection lower_red = [Link]([0, 50, 50]) # Lower red range (first range)

upper_red = [Link]([10, 255, 255]) # Upper red range (first range) mask_red =
[Link](hsv, lower_red, upper_red) # Red mask (first range) res_red =

cv2.bitwise_and(img, img, mask=mask_red) # Extract red (first range)

[Link]('Original', img) # Show original [Link]('Blue',

res_blue) # Show blue result [Link]('Red', res_red) # Show red

result

[Link](0) # Wait for key press [Link]()

# Close windows

[Link]

import cv2 # Import OpenCV library import numpy

as np # Import NumPy for arrays

image = [Link]('[Link]') # Load the image if image is

None: # Check if image loaded correctly print("Error: Could

not load image.") # Print error if load fails else: # If image loaded

successfully hsv = [Link](image, cv2.COLOR_BGR2HSV) #

Convert image to HSV color space

lower_blue = [Link]([110, 50, 50]) # Lower bound for blue color upper_blue

= [Link]([130, 255, 255]) # Upper bound for blue color


lower_red1 = [Link]([0, 50, 50]) # Lower bound for first red range upper_red1

= [Link]([10, 255, 255]) # Upper bound for first red range lower_red2 =

[Link]([170, 50, 50]) # Lower bound for second red range (for wraparound)

upper_red2 = [Link]([180, 255, 255]) # Upper bound for second red range

mask_blue = [Link](hsv, lower_blue, upper_blue) # Create mask for blue pixels

mask_red1 = [Link](hsv, lower_red1, upper_red1) # Create mask for first red range

mask_red2 = [Link](hsv, lower_red2, upper_red2) # Create mask for second red range

mask_red = cv2.bitwise_or(mask_red1, mask_red2) # Combine red masks (for complete red

detection)

mask_combined = cv2.bitwise_or(mask_blue, mask_red) # Combine blue and red masks


result_combined = cv2.bitwise_and(image, image, mask=mask_combined) # Extract colored
regions

[Link]('Original Image', image) # Display the original image

[Link]('Blue Color Detection', cv2.bitwise_and(image, image, mask=mask_blue)) #


Display blue detection result

[Link]('Red Color Detection', cv2.bitwise_and(image, image, mask=mask_red)) #


Display red detection result [Link]('Red & Blue Combined', result_combined) #
Display combined red and blue result

[Link](0) # Wait for a key press [Link]()

# Close all windows

You might also like