ML Lab 3
ML Lab 3
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
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.
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
Machine Learning
plt.show()
Machine Learning
### TASK 1 OUTPUT SCREENSHOT ENDS HERE ###
Machine Learning
walle_img = cv2.imread("lab3_walle.jpeg", 1)
# 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()
Machine Learning
Machine Learning
### TASK 2 OUTPUT SCREENSHOT ENDS HERE ###
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
Machine Learning
### TASK 3 OUTPUT SCREENSHOT ENDS HERE ###
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)
Machine Learning
### TASK 4 OUTPUT SCREENSHOT ENDS HERE ###
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.
if count % 2 == 0:
color = (0, 0, 255) # Red
else:
color = (0, 0, 0) # Black
Machine Learning
print("Failed to load the image. Please check the file path and format.")
### TASK 5 CODE ENDS HERE ###
Machine Learning
# task 6
# Load the provided image
image = cv2.imread("lab3_robots.jpeg", 1)
Machine Learning
### TASK 6 OUTPUT SCREENSHOT ENDS HERE ###
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)
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.")
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.
Machine Learning
# Perform the rotation
rotated_img = cv2.warpAffine(image, M, (w, h))
Machine Learning
### TASK 8 OUTPUT SCREENSHOT ENDS HERE ###
Machine Learning
# Stretch the image by changing the scale
new_width = 800
new_height = 600
image = image.resize((new_width, new_height))
# 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)
Machine Learning
### TASK 9 CODE 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