0% found this document useful (0 votes)
11 views5 pages

Computer Vision Lab Manual: OpenCV Basics

The lab manual provides instructions for using Python and OpenCV for computer vision tasks, including reading, displaying, and saving images. It includes step-by-step guides for basic operations such as changing color spaces and creating new images from existing ones. Additionally, it outlines a lab assignment that involves manipulating and saving images in a specified format.

Uploaded by

ayethet.zaw
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Computer Vision Lab Manual: OpenCV Basics

The lab manual provides instructions for using Python and OpenCV for computer vision tasks, including reading, displaying, and saving images. It includes step-by-step guides for basic operations such as changing color spaces and creating new images from existing ones. Additionally, it outlines a lab assignment that involves manipulating and saving images in a specified format.

Uploaded by

ayethet.zaw
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab Manual

CT-605 Computer Vision and Robotics

Required Software

Python

Pycharm

To import OpenCV on PyCharm (Using Terminal):

1) Go to the terminal option at the bottom of the IDE window.


2) The pip (package manager) can also be used to download and install OpenCV. To
install OpenCV, just type the following command:
pip install opencv-python
3) Now simply import OpenCV in your python program in which you want to use image
processing functions.
To check if OpenCV is correctly installed, just run the following program in PyCharm to perform
a version check:
import cv2
print("GeeksForGeeks")
print("Your OpenCV version is: " + cv2.__version__)

Lab 1: READING IMAGE FILES


Create a new file named task_1.py inside the folder “task_1”.
First import cv2 library, and then you will use imread function to read an input image, imshow
function to display the image on a window, waitKey function to hold the program until there’s any
key press, and finally destroyAllWindows to kill all the active windows at the end of the program.
syntax:
[Link](path, flag) for more details on flag, refer: Flags used for image file reading and writing
[Link](window_name, image_name)
example code:
# Importing OpenCV Library
import cv2
# Relative or absolute path of the input image file
path = "./task_1/[Link]"
# reading image (by default the flag is 1 if not specidied)
image = [Link](path)
# Display image in a window
[Link]( "Output" ,image)
# Wait until any key press (press any key to close the window)
[Link]()
# kill all the windows [Link]()

Lab 2: CHANGING COLORSPACE


Now try using cvtColor to convert the image to Grayscale using COLOR_BGR2GRAY option in
the previous code and display both images using imshow
syntax:
[Link](source_image,color_conversion_code)
For more information on color_conversion_code, refer: Color Space Conversions
example:
# Importing OpenCV Library
import cv2
# Relative or absolute path of the input image file
path = "./task_1/[Link]"
# reading image (by default the flag is 1 if not specidied)
image = [Link](path)
image_gray = [Link](image,cv2.COLOR_BGR2GRAY)
# Display image in a window
[Link]( "Output",image)
[Link]("Output_gray" ,image_gray)
# Wait until any key press (press any key to close the window)
[Link]()
# kill all the windows [Link]()
Lab 3: SAVING/WRITING IMAGE FILE
Lets save the gray image using imwrite function.
syntax:
[Link](path + file_name, image_name)
example: task_1.py
# Importing OpenCV Library
import cv2
# Relative or absolute path of the input image file
path = "./task_1/[Link]"
# reading image (by default the flag is 1 if not specidied)
image = [Link](path)
# converting image to Grayscale (also OpenCV reads image in BGR format and hence BGR to
Gray)
image_gray = [Link](image,cv2.COLOR_BGR2GRAY)
# Display image in a window
[Link]( "Output" ,image)
[Link]( "Output_gray"
# Saving the image
[Link]( ,image_gray) './task_1/tom_gray.jpg' ,image_gray)
# Wait until any key press (press any key to close the window)
[Link]()
# kill all the windows
[Link]()

Lab Assignment I:
Read 2 different images.
Create a new image by arranging them side-by-side.
Convert that new image to Gray.
Finally, arrange the colored pair of images on top and the pair of gray images on the bottom and
save it as “A1_solution.jpg”.
Tip: Remember to match the shape of the Grayscale image to that of the colored image while
concatenating.
Expected output:

Matlab
Lab 1: READING IMAGE FILES
Syntax:
imread(‘ fileName . fileFormat ’)
clc;
clear all;
close all;
f=imread('[Link]');
subplot(1,2,1), imshow(f),title('Input Image');

Lab 2: CHANGING COLORSPACE


RGB = imread('[Link]');
imshow(RGB)
I = rgb2gray(RGB);
figure
imshow(I)

Lab 3: SAVING/WRITING IMAGE FILE


Syntax:
imwrite(img, filename)
Lab Assignment I:
Read 2 different images.
Create a new image by arranging them side-by-side.
Convert that new image to Gray.
Finally, arrange the colored pair of images on top and the pair of gray images on the bottom and
save it as “A1_solution.jpg”.
Use subplot( ) function for displaying

You might also like