Department of Electrical Engineering
Faculty Member: LE Munadi Sial Date: 27-Sep-2023
Semester: 7th Group: 4
CS471 Machine Learning
Lab 3: Introduction to OpenCV and PIL
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
Maria Noor Wagho 336228
Ahmad Arslan 356766
Muhammad Hamza 331502
Maryam Asif 336531
Shameen Mazhar 347358
Muhammad Adnan Mirza 350471
Machine Learning
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 overlaps extensively with the field of Machine Learning particularly deep
learning. A number of image-based applications such as face detection, image
classification, object tracking and pose estimation rely on machine learning
techniques. In such cases, images will be the dataset and so, image processing
becomes a prerequisite for applying machine learning to images. It is important to
familiarize with the basics of image processing which is the subject of this lab. You
will also learn PIL which is another imaging library native to python.
Objectives
• Load, save and display image data using Python
• Access and modify pixels as well as ROIs in images
• Place lines, rectangles, circles and text in images
• Resize and rotate images at various scales/angles
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. PIL (Python Imaging Library) is another imaging
library native to python. The extension of PIL for Python 3 is called “Pillow”.
Unlike OpenCV which uses a BGR format for image data, PIL uses the RGB
format.
A brief summary of the relevant keywords and functions in python is provided
below:
Machine Learning
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 true
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
Lab Task 1 – Load and Display Images
__________________________________________
Write a python script in which you will load 3 different 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 at the same time.
### TASK 1 CODE STARTS HERE ###
import cv2
import matplotlib.pyplot as plt
# Read and display the first image
img1 = cv2.imread("lab3_robot_green_bg.jpeg", 1)
plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
plt.show()
# Read and display the second image
img2 = cv2.imread("lab3_robots.jpeg", 1)
plt.imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))
Machine Learning
plt.show()
# Read and display the third image
img3 = cv2.imread("lab3_walle.jpeg", 1)
plt.imshow(cv2.cvtColor(img3, cv2.COLOR_BGR2RGB))
plt.show()
### TASK 1 CODE ENDS HERE ###
### TASK 1 OUTPUT SCREENSHOT STARTS HERE ###
Machine Learning
### TASK 1 OUTPUT SCREENSHOT ENDS HERE ###
Lab Task 2 – Cropping Images
_____________________________________________________
Write code to load the walle.jpg file. Using the slice operation, crop out the four
quadrants of the image and display them in separate windows. The code must be generic
enough to take the image size into account. For submission, provide the code and a single
screenshot showing all 4 windows.
### TASK 2 CODE STARTS HERE ###
#task 2
# Read the image
Machine Learning
walle_img = cv2.imread("lab3_walle.jpeg", 1)
# Get the dimensions of the image
h, w, c = walle_img.shape
# Split the image into four quadrants and display each quadrant using matplotlib
quad1 = walle_img[0:int(h/2), 0:int(w/2)]
plt.imshow(cv2.cvtColor(quad1, cv2.COLOR_BGR2RGB))
plt.show()
quad2 = walle_img[0:int(h/2), int(w/2):]
plt.imshow(cv2.cvtColor(quad2, cv2.COLOR_BGR2RGB))
plt.show()
quad3 = walle_img[int(h/2):, 0:int(w/2)]
plt.imshow(cv2.cvtColor(quad3, cv2.COLOR_BGR2RGB))
plt.show()
quad4 = walle_img[int(h/2):, int(w/2):]
plt.imshow(cv2.cvtColor(quad4, cv2.COLOR_BGR2RGB))
plt.show()
### TASK 2 CODE ENDS HERE ###
### TASK 2 OUTPUT SCREENSHOT STARTS HERE ###
Machine Learning
Machine Learning
### TASK 2 OUTPUT SCREENSHOT ENDS HERE ###
Lab Task 3 – Modifying Pixel Data
___________________________________________
Write code to load the walle.jpg file and place alternating green and white horizontal
lines in the image. Do NOT use the line function (cv2.line). You need to this by changing
the pixel colors. Each line must be 1-pixel thick. The lines are also spaced apart by 1-
pixel wide gap. Thus, the 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
screenshot for the submission.
### TASK 3 CODE STARTS HERE ###
# task 3
# Read the image
walle_img = cv2.imread("lab3_walle.jpeg", 1)
Machine Learning
# Check if the image is loaded successfully
if walle_img is not None:
# Get the dimensions of the image
h, w, c = walle_img.shape
# Modify the image as per your requirements
for i in range(0, h, 3):
walle_img[i, :, :] = [0, 255, 0]
for i in range(2, h, 3):
walle_img[i, :, :] = [255, 255, 255]
# Display the modified image using matplotlib
plt.imshow(cv2.cvtColor(walle_img, cv2.COLOR_BGR2RGB))
plt.show()
else:
print("Failed to load the image. Please check the file path and format.")
### TASK 3 CODE ENDS HERE ###
### TASK 3 OUTPUT SCREENSHOT STARTS HERE ###
Machine Learning
### TASK 3 OUTPUT SCREENSHOT ENDS HERE ###
Lab Task 4 – Placing Shapes and Text
___________________________________________
Load any one of the provided images and place a line, rectangle, circle and text using the
inbuilt functions in OpenCV. Each of the placed object must have a different color. The
text must contain the name of at least one member of your group. Provide the code and
screenshot of the image.
### TASK 4 CODE STARTS HERE ###
#Task 4
# Load the provided image
sample_img = cv2.imread("lab3_robots.jpeg", 1)
Machine Learning
# Check if the image is loaded successfully
if sample_img is not None:
# Place a blue line
sample_img = cv2.line(sample_img, (200, 200), (250, 300), (255, 0, 0), 5)
# Place a cyan rectangle
sample_img = cv2.rectangle(sample_img, (300, 50), (450, 200), (0, 255, 255), 5)
# Place a magenta circle
sample_img = cv2.circle(sample_img, (100, 100), 30, (255, 0, 255), 5)
# Add text with a white color
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(sample_img, 'Ahmed Arsalan', (10, 300), font, 2, (255, 255, 255), 5,
cv2.LINE_AA)
# Display the modified image using matplotlib
plt.imshow(cv2.cvtColor(sample_img, cv2.COLOR_BGR2RGB))
plt.axis('off') # Turn off the axis
plt.show()
else:
print("Failed to load the image. Please check the file path and format.")
### TASK 4 CODE ENDS HERE ###
### TASK 4 OUTPUT SCREENSHOT STARTS HERE ###
Machine Learning
### TASK 4 OUTPUT SCREENSHOT ENDS HERE ###
Lab Task 5 – Placing Shapes II
______________________________________________________
In this task, you will place solid circles of alternating colors similar to those shown in the
figure.
Machine Learning
To make the circle solid, the thickness argument should be set to -1 in the cv2.circle
function. The above shown pattern must be placed on one of the provided images. It is up
to you to choose the radius of the circles as well as their colors. At least, 2 colors must be
used. Provide the code and screenshot of the final result.
### TASK 5 CODE STARTS HERE ###
# Task 5
# Load the provided image
image = cv2.imread("lab3_robots.jpeg", 1)
# Check if the image is loaded successfully
if image is not None:
h, w, c = image.shape
count = 0
for i in range(50, h, 100):
count += 1
for j in range(50, w, 100):
count += 1
color = (0, 0, 0) # Default color (black)
if count % 2 == 0:
color = (0, 0, 255) # Red
else:
color = (0, 0, 0) # Black
cv2.circle(image, (j, i), 30, color, -1)
# Display the modified image using matplotlib
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off') # Turn off the axis
plt.show()
else:
Machine Learning
print("Failed to load the image. Please check the file path and format.")
### TASK 5 CODE ENDS HERE ###
### TASK 5 OUTPUT SCREENSHOT STARTS HERE ###
### TASK 5 OUTPUT SCREENSHOT ENDS HERE ###
Lab Task 6 – Bounding Boxes
_______________________________________________
For detection of faces, people, objects etc. in images, the result of the detection is
depicted as a rectangular box around the detection. Load the robots.jpg image and use the
cv2.rectangle function to place bounding boxes around the robots. Each bounding box
must be of a different color. Provide the code and screenshot of the final result.
### TASK 6 CODE STARTS HERE ###
Machine Learning
# task 6
# Load the provided image
image = cv2.imread("lab3_robots.jpeg", 1)
# Check if the image is loaded successfully
if sample_img is not None:
# Draw rectangles with different colors and positions
image = cv2.rectangle(sample_img, (400, 35), (480, 100), (0, 255, 255), 3)
image = cv2.rectangle(sample_img, (500, 45), (570, 110), (255, 0, 255), 3)
image = cv2.rectangle(sample_img, (190, 45), (260, 110), (255, 255, 0), 3)
image = cv2.rectangle(sample_img, (100, 25), (150, 60), (100, 255, 100), 3)
# Display the modified image using matplotlib
plt.imshow(cv2.cvtColor(sample_img, cv2.COLOR_BGR2RGB))
plt.axis('off') # Turn off the axis
plt.show()
else:
print("Failed to load the image. Please check the file path and format.")
### TASK 6 CODE ENDS HERE ###
### TASK 6 OUTPUT SCREENSHOT STARTS HERE ###
Machine Learning
### TASK 6 OUTPUT SCREENSHOT ENDS HERE ###
Lab Task 7 – Resizing Images
______________________________________________________
Load any one of the provided images and use the resize function to make copies of the
image at different sizes. Display at least 3 different sizes in separate windows and take
the screenshot. Provide the code and screenshot for the submission.
### TASK 7 CODE STARTS HERE ###
# Task 7
# Load the image
image = cv2.imread("lab3_walle.jpeg", 1)
Machine Learning
# Check if the image is loaded successfully
if image is not None:
# Resize the image with different scaling factors
imgResize1 = cv2.resize(image, None, fx=0.5, fy=0.5,
interpolation=cv2.INTER_CUBIC)
imgResize2 = cv2.resize(image, None, fx=1.2, fy=1.2,
interpolation=cv2.INTER_CUBIC)
imgResize3 = cv2.resize(image, None, fx=0.2, fy=0.5,
interpolation=cv2.INTER_CUBIC)
# Display the original and resized images using matplotlib
plt.subplot(2, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title("Original Image")
plt.axis('off')
plt.subplot(2, 2, 2)
plt.imshow(cv2.cvtColor(imgResize1, cv2.COLOR_BGR2RGB))
plt.title("Resized 0.5x")
plt.axis('off')
plt.subplot(2, 2, 3)
plt.imshow(cv2.cvtColor(imgResize2, cv2.COLOR_BGR2RGB))
plt.title("Resized 1.2x")
plt.axis('off')
plt.subplot(2, 2, 4)
plt.imshow(cv2.cvtColor(imgResize3, cv2.COLOR_BGR2RGB))
plt.title("Resized 0.2x")
plt.axis('off')
Machine Learning
plt.show()
else:
print("Failed to load the image. Please check the file path and format.")
### TASK 7 CODE ENDS HERE ###
### TASK 7 OUTPUT SCREENSHOT STARTS HERE ###
### TASK 7 OUTPUT SCREENSHOT ENDS HERE ###
Lab Task 8 – Rotating Images
________________________________________________
Load any one of the provided images and use the rotate function to rotate the image at
angles of 30, 60 and 90 degrees. For each rotated image, you need to manually adjust the
Machine Learning
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 ###
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load the provided image
image = cv2.imread("lab3_walle.jpeg", 1)
# Check if the image is loaded successfully
if image is not None:
# Get image dimensions
h, w, _ = image.shape
# Define rotation angles
angles = [30, 60, 90]
# Create a figure to display the rotated images side by side
fig, axes = plt.subplots(1, len(angles) + 1, figsize=(15, 5))
# Display the original image
axes[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
axes[0].set_title("Original Image")
axes[0].axis('off')
# Rotate and display the image for each angle
for i, angle in enumerate(angles, start=1):
# Calculate the rotation matrix
M = cv2.getRotationMatrix2D((w / 2, h / 2), angle, 1)
Machine Learning
# Perform the rotation
rotated_img = cv2.warpAffine(image, M, (w, h))
# Display the rotated image
axes[i].imshow(cv2.cvtColor(rotated_img, cv2.COLOR_BGR2RGB))
axes[i].set_title(f"Rotated {angle} degrees")
axes[i].axis('off')
# Adjust layout for better visualization
plt.tight_layout()
# Show the figure with all rotated images
plt.show()
else:
print("Failed to load the image. Please check the file path and format.")
### TASK 8 CODE ENDS HERE ###
### TASK 8 OUTPUT SCREENSHOT STARTS HERE ###
Machine Learning
### TASK 8 OUTPUT SCREENSHOT ENDS HERE ###
Lab Task 9 – PIL ______________________________________________________
Import the Python Imaging Library and use it to load any one of the provided image files.
Stretch the image by changing the scale of the image and place different shapes and text
in the image. Provide the code and screenshot of the final output. You will need to learn
to use PIL on your own for this task.
### TASK 9 CODE STARTS HERE ###
#task 9
from PIL import Image, ImageDraw, ImageFont
# Load the provided image using Pillow
image = Image.open("lab3_walle.jpeg")
Machine Learning
# Stretch the image by changing the scale
new_width = 800
new_height = 600
image = image.resize((new_width, new_height))
# Create a drawing context on the image
draw = ImageDraw.Draw(image)
# Define colors in RGB format (Red, Green, Blue)
line_color = (255, 0, 0) # Red
rectangle_color = (0, 255, 0) # Green
circle_color = (0, 0, 255) # Blue
text_color = (255, 255, 255) # White
# Draw a line
draw.line([(100, 100), (300, 200)], fill=line_color, width=5)
# Draw a rectangle
draw.rectangle([(400, 50), (600, 150)], outline=rectangle_color, width=5)
# Draw a circle
draw.ellipse([(200, 300), (350, 450)], outline=circle_color, width=5)
# Add text
text = "Hello, OpenAI"
font = ImageFont.load_default() # Use the default font
draw.text((50, 500), text, fill=text_color, font=font)
# Save or display the modified image
image.save("output_image.jpg")
image.show()
Machine Learning
### TASK 9 CODE ENDS HERE ###
### TASK 9 OUTPUT SCREENSHOT STARTS HERE ###
### TASK 9 OUTPUT SCREENSHOT ENDS HERE ###
Conclusion
In this lab, we learnt the use of openCV library which is an integral part of computer
vision as well as machine learning applications. PIL library is another library similar to
this which was used in different tasks.
Machine Learning