0% found this document useful (0 votes)
5 views

Lab 1_ Introduction to OpenCV for Computer Vision

This document outlines a lab introduction to OpenCV for computer vision, detailing objectives such as installing OpenCV, loading and saving images, converting color spaces, and performing basic image transformations. It includes a step-by-step guide for installation, image manipulation, and transformation techniques. By the end of the lab, participants will have foundational skills in using OpenCV for image processing tasks.

Uploaded by

sodayac916
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 1_ Introduction to OpenCV for Computer Vision

This document outlines a lab introduction to OpenCV for computer vision, detailing objectives such as installing OpenCV, loading and saving images, converting color spaces, and performing basic image transformations. It includes a step-by-step guide for installation, image manipulation, and transformation techniques. By the end of the lab, participants will have foundational skills in using OpenCV for image processing tasks.

Uploaded by

sodayac916
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

This document is the property of Al Nafi.

Any unauthorized redistribution or reproduction, including in printed form, is strictly


prohibited. This document can only be read electronically.

Lab 1: Introduction to OpenCV for Computer Vision


Objectives:
1. Install OpenCV: Set up the OpenCV library on your preferred operating system to start
working with computer vision tasks.
2. Load, Display, and Save Images: Learn how to load images from your filesystem,
display them in a window, and save them back to disk using OpenCV.

td
3. Convert Colour Spaces: Explore how to convert images between different color
spaces, such as BGR to grayscale or HSV.

tl
4. Perform Basic Image Transformations: Apply basic transformations to images,
including resizing, rotating, and flipping.

Pv
Prerequisites:

ng
● Basic Python Knowledge: Familiarity with Python programming.
● Environment Setup: A Python environment with the ability to install libraries.

Tasks :
ni
ar
Step-by-Step Lab Guide:
Le

Step 1: Install OpenCV


IE

1. Install OpenCV using pip:


○ OpenCV can be installed easily using pip.
AF

pip install opencv-python


N

2. Verify Installation:
○ You can verify that OpenCV is installed by importing it in a Python script or in an
AL

interactive session.

import cv2
print(cv2.__version__)

Step 2: Load, Display, and Save Images


1. Load an Image:
○ Use the cv2.imread() function to load an image from your filesystem.

import cv2

# Load an image from file


image = cv2.imread('path_to_image.jpg')

td
# Check if the image was loaded correctly
if image is None:

tl
print("Error loading image")
else:

Pv
print("Image loaded successfully")

2. Display the Image:

ng
○ Display the image in a window using cv2.imshow().

# Display the image


cv2.imshow('Loaded Image', image)
ni
ar
# Wait for a key press and close the window
Le

cv2.waitKey(0)
cv2.destroyAllWindows()
IE

3. Save the Image:


○ Save the image to a new file using cv2.imwrite().
AF

# Save the image to a new file


cv2.imwrite('saved_image.jpg', image)
N
AL

Step 3: Convert Colour Spaces

1. Convert BGR to Grayscale:


○ Convert the loaded image from BGR to Grayscale using cv2.cvtColor().

# Convert the image to grayscale


gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Display the grayscale image
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. Convert BGR to HSV:


○ Convert the image from BGR to HSV color space.

td
# Convert the image to HSV

tl
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

Pv
# Display the HSV image
cv2.imshow('HSV Image', hsv_image)
cv2.waitKey(0)

ng
cv2.destroyAllWindows()

ni
Step 4: Perform Basic Image Transformations
ar
1. Resize an Image:
○ Resize the image using cv2.resize().
Le

# Resize the image to 50% of its original size


resized_image = cv2.resize(image, (0, 0), fx=0.5, fy=0.5)
IE

# Display the resized image


cv2.imshow('Resized Image', resized_image)
AF

cv2.waitKey(0)
cv2.destroyAllWindows()
N

2. Rotate an Image:
AL

○ Rotate the image by a specified angle using cv2.getRotationMatrix2D()


and cv2.warpAffine().

# Rotate the image by 45 degrees


(h, w) = image.shape[:2]
center = (w // 2, h // 2)

# Define the rotation matrix


rotation_matrix = cv2.getRotationMatrix2D(center, 45, 1.0)

# Perform the rotation


rotated_image = cv2.warpAffine(image, rotation_matrix, (w, h))

# Display the rotated image


cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0)

td
cv2.destroyAllWindows()

tl
3. Flip an Image:

Pv
○ Flip the image horizontally, vertically, or both using cv2.flip().

# Flip the image horizontally

ng
flipped_image = cv2.flip(image, 1)

# Display the flipped image


ni
cv2.imshow('Flipped Image', flipped_image)
cv2.waitKey(0)
ar
cv2.destroyAllWindows()
Le

Conclusion:
IE

By the end of this lab, you should be able to:

● Install and configure OpenCV in your Python environment.


AF

● Load, display, and save images using OpenCV functions.


● Convert images between different color spaces.
● Apply basic image transformations like resizing, rotating, and flipping.
N

This lab provides a foundational understanding of working with images using OpenCV, an
essential skill in computer vision tasks.
AL

You might also like