AI - Lab1 - Puviyarasu Ayyappan
AI - Lab1 - Puviyarasu Ayyappan
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
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.
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)
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.
cv.waitKey(0)
# Split the image into its Blue, Green, and Red channels
b, g, r = cv.split(img)
cv.waitKey(0)
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.
# Separate the image into Blue, Green, and Red color channels
blue_channel, green_channel, red_channel = cv.split(img)
# Split the HSV image into individual Hue, Saturation, and Value
channels
hue, saturation, value = cv.split(img_hsv)
cv.namedWindow('Saturation', cv.WINDOW_NORMAL)
cv.imshow('Saturation', saturation)
cv.namedWindow('Value', cv.WINDOW_NORMAL)
cv.imshow('Value', value)
# Wait for a key press and then close all open windows
cv.waitKey(0)
cv.destroyAllWindows()
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.
# 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.
# 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)
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.
if not cap.isOpened():
print("Error: Could not open video capture")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame")
break
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
# 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)
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
while ret:
# Read a frame from the video capture
ret, frame = cap.read()
if not ret:
print('Video capture fault, exit...')
break