0% found this document useful (0 votes)
21 views9 pages

CV Lab 03 - Introduction to OpenCV (Updated 2024)

Uploaded by

Hashaam Zafar
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)
21 views9 pages

CV Lab 03 - Introduction to OpenCV (Updated 2024)

Uploaded by

Hashaam Zafar
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/ 9

Department of Electrical Engineering

Faculty Member: <> Date: <>


Semester: <> Group: <>

CS477 Computer Vision


Lab 3: Introduction to OpenCV

PLO4 - PLO4 - PLO5 - PLO8 - PLO9 -


CLO4 CLO4 CLO5 CLO6 CLO7
Name Reg. No Viva /Quiz / Analysis Modern Ethics Individual
Lab of data in Tool and Team
Performance Lab Usage Work
Report

5 Marks 5 Marks 5 Marks 5 Marks 5 Marks

CS477 Computer Vision


Introduction

This laboratory exercise will introduce OpenCV which is a popular and widely
used library for image processing and computer vision applications. The field of
computer vision is very important for many applications. A large number of
algorithms employ aspects of vision such as in feature extraction, image stitching,
stereovision, optical flow, structure from motion, segmentation, appearance
matching etc. Before starting computer vision, it is important to familiarize with
the basics of OpenCV which is the subject of this lab.

Objectives

• Load, save and display image data using Python


• Crop images via slicing
• Access and modify pixels in images
• Place lines, rectangles, circles and text in images
• Resize image at various scales
• Rotate image at various angles
• Use NumPy arrays within the OpenCV framework

Lab Conduct

• Respect faculty and peers through speech and actions


• The lab faculty will be available to assist the students. In case some aspect
of the lab experiment is not understood, the students are advised to seek
help from the faculty.
• In the tasks, there are commented lines such as #YOUR CODE STARTS
HERE# where you have to provide the code. You must put the code
between the #START and #END parts of these commented lines. Do NOT
remove the commented lines.

CS477 Computer Vision


Theory
OpenCV is a library that focuses on image processing and computer vision. An
image is an array of colored square called pixels. Each pixel has a certain
location in the array and color values in BGR format. By referring to the array
indices, the individual pixels or a range of pixels can be accessed and modified.
OpenCV provides many functions for resizing, rotating, and placing objects in
images. Rotation involves computing a 2-D rotation matrix which is applied for
the transformation of the image.

A brief summary of the relevant keywords and functions in python is provided


below. (For more details, check the slides for this lab)

print() output text on console


input() get input from user on console
range() create a sequence of numbers
len() gives the number of characters in a string
if contains code that executes depending on a logical condition
else connects with if and elif, executes when conditions are not met
elif equivalent to else if
while loops code as long as a condition is met
for loops code through a sequence of items in an iterable object
break exit loop immediately
continue jump to the next iteration of the loop
def used to define a function

CS477 Computer Vision


In this lab, you will need to download at least 3 image files which you must use in
the tasks.

Lab Task 1 – Load and Display Images [1]


Write a python script in which you will load the images from disk. Then, display the
images in different windows at the same time. You will need to provide the code and
a single screenshot which shows all 3 windows together.

### TASK 1 CODE STARTS HERE ###

### TASK 1 CODE ENDS HERE ###

### TASK 1 SCREENSHOT STARTS HERE ###

### TASK 1 SCREENSHOT ENDS HERE ###

Lab Task 2 – Cropping [1]


Write code to load the image files. Using the slice operation, crop out the four
quadrants of the images and display them in separate windows. The code must be
generic to take the image size into account. For the submission, provide the code
and screenshots showing all 4 windows for every image.

### TASK 2 CODE STARTS HERE ###

### TASK 2 CODE ENDS HERE ###

### TASK 2 SCREENSHOT STARTS HERE ###

### TASK 2 SCREENSHOT ENDS HERE ###

CS477 Computer Vision


Lab Task 3 – Placing Shapes and Text [2]
Load any one of the downloaded images and place a line, rectangle, circle and text
using the inbuilt functions in OpenCV. Each of the placed objects must have a
different color. The text must contain the names of the group members. Provide the
code and screenshot of the image.

### TASK 3 CODE STARTS HERE ###

### TASK 3 CODE ENDS HERE ###

### TASK 3 SCREENSHOT STARTS HERE ###

### TASK 3 SCREENSHOT ENDS HERE ###

Lab Task 4 – Modifying Pixel Colors [1]


Write code to load the image files and place alternating green and white horizontal
lines in the images. Do NOT use the inbuilt line function (cv2.line). You need to
perform this by changing the individual pixel colors. Each line is to be 1-pixel thick.
The lines are also spaced apart by 1-pixel wide gap. Thus, each image will have one
green line, then one line of image pixels, then one white line, then another line of
image pixels and so on. Provide the code and all relevant screenshots for the
submission.

### TASK 4 CODE STARTS HERE ###

### TASK 4 CODE ENDS HERE ###

### TASK 4 SCREENSHOT STARTS HERE ###

### TASK 4 SCREENSHOT ENDS HERE ###

CS477 Computer Vision


Lab Task 5 – Pattern Placement [2]
In this task, you will place solid circles of alternating colors similar to those shown
in the figure. Load the images for this task.

To make the circle solid, the thickness argument is set to -1 in the cv2.circle function.
The above shown pattern must be placed on the images. It is up to you to choose the
radius of the circles as well as their colors. At least, 2 different colors must be used.
Provide the code and all relevant screenshots of the final result.

### TASK 5 CODE STARTS HERE ###

### TASK 5 CODE ENDS HERE ###

### TASK 5 SCREENSHOT STARTS HERE ###

### TASK 5 SCREENSHOT ENDS HERE ###

CS477 Computer Vision


Lab Task 6 – Bounding Boxes [1]
For detection of faces, people, objects etc. in images, the result of the detection can
be depicted as a rectangular box around the detection. Load an image containing a
subject (humans, animals, cars, objects etc.) and use the cv2.rectangle function to
place bounding boxes around the subjects. Each bounding box must be of a different
color. Provide the code and screenshot of the final result.

### TASK 6 CODE STARTS HERE ###

### TASK 6 CODE ENDS HERE ###

### TASK 6 OUTPUT SCREENSHOT STARTS HERE ###

### TASK 6 OUTPUT SCREENSHOT ENDS HERE ###

Lab Task 7 – Resizing Images [1]


Load the images and use the resize function to make copies of the image at different
sizes. For each image, display at least 3 different sizes in separate windows and take
the screenshot. Provide the code and all relevant screenshots for the submission.

### TASK 7 CODE STARTS HERE ###

### TASK 7 CODE ENDS HERE ###

### TASK 7 SCREENSHOT STARTS HERE ###

### TASK 7 SCREENSHOT ENDS HERE ###

Lab Task 8 – Rotating Images [1]


CS477 Computer Vision
Load any one of the provided images and use the rotate function to rotate the image
at angles of 30, 45, 60, 90 and 120 degrees. For each rotated image, you need to
manually adjust the scale factor (in get2DRotationMatrix function) so that the entire
image is shown in the window. The rotated image’s border/corner must touch the
window’s border. Show all 3 windows in the screenshot. Provide the code and
screenshot for the submission.

### TASK 8 CODE STARTS HERE ###

### TASK 8 CODE ENDS HERE ###

### TASK 8 SCREENSHOT STARTS HERE ###

### TASK 8 SCREENSHOT ENDS HERE ###

Lab Task 9 – NumPy Arrays [1]


Load one of the images. Write a program that creates a NumPy array (np.zeros())
that has the same dimensions as the size of the loaded image. Ensure that the data
type in the NumPy array is compatible with OpenCV (8-bit unsigned integer):

imageNew = np.zeros((height, width, channels),dtype='uint8')

Using loops, go through every pixel of the loaded image and place them in the array
is such a way that it is flipped horizontally. Finally, use the imshow function on the
array to display the reflected image.

### TASK 9 CODE STARTS HERE ###

### TASK 9 CODE ENDS HERE ###

### TASK 9 SCREENSHOT STARTS HERE ###

CS477 Computer Vision


### TASK 9 SCREENSHOT ENDS HERE ###

CS477 Computer Vision

You might also like