[go: up one dir, main page]

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

Homework Exercise

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)
9 views4 pages

Homework Exercise

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
You are on page 1/ 4

Name : Tha davith

ID: 0005720

Test20.py

import cv2 # Import OpenCV img = cv2.imread('spider.jfif') # Load image if

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

successfully hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # BGR to HSV

rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # BGR to RGB gray =

cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # BGR to Grayscale

cv2.imshow('Original', img) # Show original cv2.imshow('RGB', rgb) #

Show RGB cv2.imshow('Grayscale', gray) # Show Grayscale

cv2.imshow('HSV', hsv) # Show HSV cv2.waitKey(0) # Wait for key press

cv2.destroyAllWindows() # Close windows

Test21.py import cv2 # Import OpenCV import numpy as np # Import

NumPy for array manipulation

img = cv2.imread('spider.jfif') # Load image if img is None:

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

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Convert to HSV

lower_blue = np.array() # Lower bound of blue in HSV upper_blue

= np.array() # Upper bound of blue in HSV

mask = cv2.inRange(hsv, lower_blue, upper_blue) # Create mask of blue pixels


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

cv2.imshow('Original', img) # Show original image cv2.imshow('HSV', hsv) #

Show HSV image (for reference) cv2.imshow('Mask', mask) # Show the mask

(white = blue, black = not blue) cv2.imshow('Result', res) # Show the result (only

blue parts visible)

cv2.waitKey(0) # Wait for key press

cv2.destroyAllWindows() # Close windows Test22.py

import cv2 # Import OpenCV import numpy as np #

Import NumPy

img = cv2.imread('spider.jfif') # Load image if img is None: print("Error:

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

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Convert to HSV

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

np.array([130, 255, 255]) # Upper blue range mask_blue = cv2.inRange(hsv,

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

img, mask=mask_blue) # Extract blue

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

upper_red = np.array([10, 255, 255]) # Upper red range (first range) mask_red =
cv2.inRange(hsv, lower_red, upper_red) # Red mask (first range) res_red =

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

cv2.imshow('Original', img) # Show original cv2.imshow('Blue',

res_blue) # Show blue result cv2.imshow('Red', res_red) # Show red

result

cv2.waitKey(0) # Wait for key press cv2.destroyAllWindows()

# Close windows

Test23.py

import cv2 # Import OpenCV library import numpy

as np # Import NumPy for arrays

image = cv2.imread('spider.jfif') # 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 = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) #

Convert image to HSV color space

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

= np.array([130, 255, 255]) # Upper bound for blue color


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

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

np.array([170, 50, 50]) # Lower bound for second red range (for wraparound)

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

mask_blue = cv2.inRange(hsv, lower_blue, upper_blue) # Create mask for blue pixels

mask_red1 = cv2.inRange(hsv, lower_red1, upper_red1) # Create mask for first red range

mask_red2 = cv2.inRange(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

cv2.imshow('Original Image', image) # Display the original image

cv2.imshow('Blue Color Detection', cv2.bitwise_and(image, image, mask=mask_blue)) #


Display blue detection result

cv2.imshow('Red Color Detection', cv2.bitwise_and(image, image, mask=mask_red)) #


Display red detection result cv2.imshow('Red & Blue Combined', result_combined) #
Display combined red and blue result

cv2.waitKey(0) # Wait for a key press cv2.destroyAllWindows()

# Close all windows

You might also like