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

Lab 01

This document outlines the objectives and procedures for Lab #1 in the Computer Vision course at Hazara University, focusing on introducing Python and the Spyder IDE. It includes instructions for opening Spyder, creating and executing files, and exercises involving reading, displaying, and saving images using OpenCV. Two example programs demonstrate how to read an image, display it, convert it to grayscale, and save the modified image.

Uploaded by

Noor Ul Wara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab 01

This document outlines the objectives and procedures for Lab #1 in the Computer Vision course at Hazara University, focusing on introducing Python and the Spyder IDE. It includes instructions for opening Spyder, creating and executing files, and exercises involving reading, displaying, and saving images using OpenCV. Two example programs demonstrate how to read an image, display it, convert it to grayscale, and save the modified image.

Uploaded by

Noor Ul Wara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

CV ‒ Lab # 1 ‒ Thursday, 26th September 2024

HAZARA UNIVERSITY
Department of CS & IT
Computer Vision
Lab # 1

Objectives
 To introduce Python and Spyder IDE

Using Spyder IDE


Open Spyder IDE by going to C:\Users\gul\Anaconda3\pythonw.exe. If you are
running Spyder for the first time on your machine, you will be required to go
through a first-time configuration process. Make sure that OpenCV is installed in
your Spyder IDE.

Create a new file


You can create a new file by following these steps:
 Go to the “File” menu and select “New File”
 Give your file a name. You can give your file any valid filename.

Executing file
 Go to the “Run” menu and select “Run” (or press F5) to run your file
 A dialog box will appear prompting you to save untitled1.py. Save it at
your desired location.
 If the run is successful, the program will execute and print the output in
the “IPython console” window.
CV ‒ Lab # 1 ‒ Thursday, 26th September 2024

Exercise 1 Reading and saving image files with Python,


OpenCV
 Read an image from file (using cv2.imread)
 Display an image in an OpenCV window (using cv2.imshow)
 Write an image to a file (using cv2.imwrite)

Program 1: Read and display image


"""
Created on Wed Mar 6 09:24:15 2024

@author: gul
"""

import cv2

# Load the image


img = cv2.imread("hu.png")

# Display the image


cv2.imshow("Image", img)

# Wait for the user to press a key


cv2.waitKey(0)

# Close all windows


cv2.destroyAllWindows()

Program Output
CV ‒ Lab # 1 ‒ Thursday, 26th September 2024

Program 2: Read the image, convert to grayscale, Save image


and
print

"""
Created on Wed Mar 6 09:24:15 2024

@author: gul
"""

import cv2

# Load the image and convert the image to grayscale


img = cv2.imread("hu.png", cv2.IMREAD_GRAYSCALE)

# Saving the image


cv2.imwrite("gray_img.png", img)

# Load the gray scale


img = cv2.imread("gray_img.png")

# Display the image


cv2.imshow("Image", img)

# Wait for the user to press a key


cv2.waitKey(0)

# Close all windows


cv2.destroyAllWindows()

Program output

You might also like