0% found this document useful (0 votes)
15 views8 pages

AI - Lab1 - Puviyarasu Ayyappan

This document outlines a lab series for AI Application (ENGI51071) focusing on Python programming and OpenCV for image manipulation and webcam control. It includes tasks for capturing images, manipulating color channels, and implementing video streaming, along with exercises for color detection in HSV color space. The lab emphasizes the use of Jupyter Notebooks for submissions and provides code examples for various tasks related to image processing and video capture.

Uploaded by

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

AI - Lab1 - Puviyarasu Ayyappan

This document outlines a lab series for AI Application (ENGI51071) focusing on Python programming and OpenCV for image manipulation and webcam control. It includes tasks for capturing images, manipulating color channels, and implementing video streaming, along with exercises for color detection in HSV color space. The lab emphasizes the use of Jupyter Notebooks for submissions and provides code examples for various tasks related to image processing and video capture.

Uploaded by

howyoudoin011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

This is part of the lab series for AI Application (ENGI51071).

In this lab, we learn how to use


basic Python programming and OpenCV to implement simple control flow, functions and
image manipulation. We will use your webcam to capture image frames and use looping
feature in Python to continuously update the captured image frames in the display window.

From this lab on, all labs will be distributed in the format of Jupyter Notebooks. You can copy
these commands from each cell into other IDEs of your preference (such as PyCharm or
Spyder). Please use the browser print option to save this page into .pdf format and then
submit. Submissions in other format will receive a zero automatically.

1. Python Basics

Online resources for Python programming is critical if you encounter problems. Some of the
recommended readings are listed below.

2. Import Needed packages For this lab, the following packages are needed

# Importing necessary libraries


import numpy as np
import cv2 as cv # For computer vision and image processing tasks
import matplotlib.pyplot as plt

3. Image Manipulations Your webcam is one of the most accessible imaging sensor, for this
lab, we are going to explore its capacity and learn to use OpenCV to manipulate and control
it. To begin, use the following OpenCV functions to interface with an image file. In your lab1
package, there is an image included (image from https://createplaytravel.com/) but you can
use your own if you prefer.

Note: You need to place the required image in your work folder in order for the following
lines to run properly. The following segment completes a list of tasks. Use OpenCV to read
in this image. Create a blank display window named 'myWindow' Display the image in
window 'myWindow'. If needed, Resize the image to be visible in window 'myWindow'.
Close all windows when you press any key on your keyboard. Save the image into a different
file name.

# Load the image and display it in a custom resizable window


img = cv.imread('Lab1-red_rose_highres.jpg')
cv.namedWindow('myWindow', cv.WINDOW_NORMAL)
cv.imshow('myWindow', img)

# Resize the image to the specified dimensions and display it


downwidth = 700
downheight = 700
downpoint = (downwidth, downheight)
resized = cv.resize(img, downpoint, interpolation=cv.INTER_LINEAR)
cv.imshow('Resized Down by defining height and width', resized)

# Save the original image to a new file


cv.waitKey(0)
cv.destroyAllWindows()
cv.imwrite('new_image.jpg', img)

Task 1. (1') Now use if statement to add control flow to this process so that only when you
press 'ESC' the window closes. The condition is: key_stroke = cv.waitKey(0) if key_stroke &
0xFF == 27: ... Please record and run your code in the cell below.

# Wait for a key press and close the window if 'ESC' (Escape) key is
pressed
key_stroke = cv.waitKey(0)

# Check if 'ESC' key is pressed, If it is, close the windows.


if key_stroke & 0xFF == 27:
cv.destroyAllWindows()

By default, cv.imread will read the image as in BGR format, meaning three channels
represent color Blue, Red and Green respectively. In Python, these three channels of image
matrix named 'img' are referred to as img[:,:,0], img[:,:,1], img[:,:,2]. Often, OpenCV uses a
built-in function cv2.split() to split the three channels. Below are codes for two ways of
implementation.

# Loop through each color channel (0=Blue, 1=Green, 2=Red) to


display and resize the image
for i in [0, 1, 2]:
cv.imshow('myWindow', img[:, :, i]) # Display the original
channel (may not be visible if too large)

# Resize the current channel to make it viewable on the screen


downwidth = 700
downheight = 700
downpoint = (downwidth, downheight)
resized = cv.resize(img[:, :, i], downpoint,
interpolation=cv.INTER_LINEAR)
cv.imshow('Resized Down by defining height and width', resized)
# Display the resized channel

cv.waitKey(0)

# Close all windows after the loop


cv.destroyAllWindows()

# Split the image into its Blue, Green, and Red channels
b, g, r = cv.split(img)

# Loop through each color channel to display and resize it


for i in [b, g, r]:
cv.imshow('myWindow', i) # Display the original channel

# Resize the current channel to make it viewable on the screen


downwidth = 700
downheight = 700
downpoint = (downwidth, downheight)
resized = cv.resize(i, downpoint, interpolation=cv.INTER_LINEAR)
cv.imshow('Resized Down by defining height and width', resized)
# Display the resized channel

cv.waitKey(0)

# Close all windows after the loop


cv.destroyAllWindows()

OpenCV can be used to visualize the individual channels of a color image.

Task 2 (2'). Create three windows to display three channels individually. Label the correct
color channel as your window name. Take screenshots of all three windows simultaneously
and record your code in the cell below.

# Load the image from file


img = cv.imread('Lab1-red_rose_highres.jpg')

# Separate the image into Blue, Green, and Red color channels
blue_channel, green_channel, red_channel = cv.split(img)

# Create resizable windows for each channel display


cv.namedWindow('Blue Channel', cv.WINDOW_NORMAL)
cv.namedWindow('Green Channel', cv.WINDOW_NORMAL)
cv.namedWindow('Red Channel', cv.WINDOW_NORMAL)

# Resize each channel for better viewing on screen


downwidth = 700
downheight = 700
downpoint = (downwidth, downheight)

blue_resized = cv.resize(blue_channel, downpoint,


interpolation=cv.INTER_LINEAR)
green_resized = cv.resize(green_channel, downpoint,
interpolation=cv.INTER_LINEAR)
red_resized = cv.resize(red_channel, downpoint,
interpolation=cv.INTER_LINEAR)

# Display each resized channel in their respective windows


cv.imshow('Blue Channel', blue_resized)
cv.imshow('Green Channel', green_resized)
cv.imshow('Red Channel', red_resized)

# Wait for a key press before closing


cv.waitKey(0)

# Save the resized channel images to files


cv.imwrite('blue_channel.jpg', blue_resized)
cv.imwrite('green_channel.jpg', green_resized)
cv.imwrite('red_channel.jpg', red_resized)

# Close all open windows


cv.destroyAllWindows()
Task 3 (2') Convert your image from RGB color space to HSV color space by using the
following command and display the three channels in individual windows as well: img_hsv =
cv.cvtColor(img_RGB, cv.COLOR_BGR2HSV) Please label the corresponding windows with
the proper channel name "Hue", ""Saturation", "Value". Record your code in the cell below.

# Load the image from file


img = cv.imread('Lab1-red_rose_highres.jpg')

# Convert the image from BGR to HSV color space


img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)

# Split the HSV image into individual Hue, Saturation, and Value
channels
hue, saturation, value = cv.split(img_hsv)

# Display the Hue, Saturation, and Value channels in separate


windows
cv.namedWindow('Hue', cv.WINDOW_NORMAL)
cv.imshow('Hue', hue)

cv.namedWindow('Saturation', cv.WINDOW_NORMAL)
cv.imshow('Saturation', saturation)

cv.namedWindow('Value', cv.WINDOW_NORMAL)
cv.imshow('Value', value)

# Resize and display the original image for comparison


downwidth = 700
downheight = 700
downpoint = (downwidth, downheight)
resized = cv.resize(img, downpoint, interpolation=cv.INTER_LINEAR)
cv.imshow('Resized Image', resized)

# Wait for a key press and then close all open windows
cv.waitKey(0)
cv.destroyAllWindows()

# Loop through each color channel (0=Blue, 1=Green, 2=Red)


for i in [0, 1, 2]:
# Display each channel in a subplot with a grayscale color map
plt.subplot(1, 3, i+1)
plt.imshow(img[:,:,i], cmap='gray', vmin=0, vmax=255)
plt.axis('off')

cv.destroyAllWindows()

4. Camera Control Your webcam is one of the most accessible imaging sensors. For this lab,
we are going to explore its capacity and learn to use OpenCV to manipulate and control it.
OpenCV provides a detailed tutorial on on how to capture from Camera and display it. The
link can be found here.

To summarize, you need to: Create a camera object and name it accordingly (in this lab,
please name it cap for consistency) Create a capture success flag ret and set its default value
to be True Use cap.read() to read in the captured frame and the capture success flag Display
the captured frame in a Window Close the display window if any key is pressed
The cell below shows the code sequence to achieve that.

# Capture video from the default camera


cap = cv.VideoCapture(0)
ret = True
ret, frame = cap.read()

# Display the captured frame in a resizable window


cv.namedWindow('test_frame', cv.WINDOW_NORMAL)
cv.imshow('test_frame', frame)

# Wait for a key press and release the video capture, then close the
window
cv.waitKey(0)
cap.release()
cv.destroyAllWindows()

Task 4 (2') Now use the control flow statement to change the code above to implement
video streaming. Hint: in this case, you may want to use loop statement such as while to
continuously refresh the display window with the latest captured frame. Please record and
test your code in the cell below.

# Initialize video capture from the default camera


cap = cv.VideoCapture(0) # Use 0 for the default camera

# Check if the video capture was successfully opened


if not cap.isOpened():
print("Error: Could not open video capture")
exit()

# Create windows to display Hue, Saturation, and Value channels


cv.namedWindow('Hue', cv.WINDOW_NORMAL)
cv.namedWindow('Saturation', cv.WINDOW_NORMAL)
cv.namedWindow('Value', cv.WINDOW_NORMAL)

# Continuously capture frames from the camera


while True:
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame")
break

# Convert the frame to HSV color space and split the channels
frame_hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
hue, saturation, value = cv.split(frame_hsv)

# Display each HSV channel in separate windows


cv.imshow('Hue', hue)
cv.imshow('Saturation', saturation)
cv.imshow('Value', value)

# Exit loop on pressing 'q'


if cv.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture and close all windows
cap.release()
cv.destroyAllWindows()

Task 5 (1') Now with all the knowledge we have, please write a code to display all three Blue,
Green, and Red channel of the camera video stream and check if it is working properly in the
cell below.

# Initialize video capture


cap = cv.VideoCapture(0) # Use 0 for the default camera

if not cap.isOpened():
print("Error: Could not open video capture")
exit()

# Create named windows for the channels


cv.namedWindow('Blue', cv.WINDOW_NORMAL)
cv.namedWindow('Green', cv.WINDOW_NORMAL)
cv.namedWindow('Red', cv.WINDOW_NORMAL)

while True:
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame")
break

# Split the BGR channels


blue, green, red = cv.split(frame)

# Display each channel in separate windows


cv.imshow('Blue', blue)
cv.imshow('Green', green)
cv.imshow('Red', red)

# Break the loop on pressing 'q'


if cv.waitKey(1) & 0xFF == ord('q'):
break

# Release the video capture and close all windows


cap.release()
cv.destroyAllWindows()

5. Practice Color detection exercise using HSV color space Why is HSV preferred in some
computer vision applications? It is because that human visual perception understands Hue,
Saturation and Value better. Below we are going to create a color detection application that
detects specified colors in a video stream. First, let's start with a blue color detector. In order
to achieve this we need to first convert the captured frames into HSV color space, and then
report back whenever pixels with values in a certain range (Blue in this case) are detected.
import numpy as np
import cv2 as cv

# Initialize video capture from the default camera


cap = cv.VideoCapture(0)
ret = True

# Create windows to display original image, mask, and blue channel


cv.namedWindow('Original', cv.WINDOW_NORMAL)
cv.namedWindow('Mask', cv.WINDOW_NORMAL)
cv.namedWindow('Blue', cv.WINDOW_NORMAL)

# Continuously capture frames from the camera


while(ret):
ret, frame = cap.read()

# Convert the captured frame from BGR to HSV color space


hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)

# Define blue color range and create a mask for blue detection
lower_blue = np.array([100, 50, 50])
upper_blue = np.array([140, 255, 255])
mask = cv.inRange(hsv, lower_blue, upper_blue)

# Isolate the blue areas using the mask and display the results
blue = cv.bitwise_and(frame, frame, mask=mask)
cv.imshow('Original', frame)
cv.imshow('Mask', mask)
cv.imshow('Blue', blue)

# Exit on 'ESC' key press


k = cv.waitKey(10) & 0xFF
if k == 27:
break
else:
print('Video capture fault, exit...')

# Release resources and close all windows


cap.release()
cv.destroyAllWindows()

Now change this code to make a yellow color detector by changing the color range:

Hint: Yellow color range is commonly defined as between (20, 50, 50) to (30,255,255). Task
6 (2') Please record your code in the cell below and comment on why RGB is not a good color
space for this application. Also, upload your complete notebook in .pdf format.

import cv2 as cv
import numpy as np

# Start capturing video


cap = cv.VideoCapture(0)
ret = True
# Create display windows
cv.namedWindow('Original', cv.WINDOW_NORMAL)
cv.namedWindow('Mask', cv.WINDOW_NORMAL)
cv.namedWindow('Yellow', cv.WINDOW_NORMAL)

while ret:
# Read a frame from the video capture
ret, frame = cap.read()

if not ret:
print('Video capture fault, exit...')
break

# Convert the frame from BGR to HSV


hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)

# Define range of yellow color in HSV


lower_yellow = np.array([20, 50, 50])
upper_yellow = np.array([30, 255, 255])

# Create a mask for yellow color


mask = cv.inRange(hsv, lower_yellow, upper_yellow)

# Bitwise-AND mask and the original frame to isolate yellow


regions
yellow = cv.bitwise_and(frame, frame, mask=mask)

# Display the original frame, mask, and yellow-detected output


cv.imshow('Original', frame)
cv.imshow('Mask', mask)
cv.imshow('Yellow', yellow)

# Break loop on pressing 'ESC' key


k = cv.waitKey(10) & 0xFF
if k == 27:
break

# Release the video capture and close windows


cap.release()
cv.destroyAllWindows()

You might also like