0% found this document useful (0 votes)
40 views48 pages

Open CV

OpenCV allows users to perform complex computer vision tasks such as object detection, face recognition, and more. It provides functions for reading, displaying, and modifying images and videos. Key functions include cv2.imread() to read an image, cv2.imshow() to display an image, cv2.resize() to resize an image, and cv2.VideoCapture() to capture video from a camera. OpenCV makes it possible to process visual media and extract useful information.

Uploaded by

ali bajwa
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)
40 views48 pages

Open CV

OpenCV allows users to perform complex computer vision tasks such as object detection, face recognition, and more. It provides functions for reading, displaying, and modifying images and videos. Key functions include cv2.imread() to read an image, cv2.imshow() to display an image, cv2.resize() to resize an image, and cv2.VideoCapture() to capture video from a camera. OpenCV makes it possible to process visual media and extract useful information.

Uploaded by

ali bajwa
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
You are on page 1/ 48

Benefits of Open CV

• Using OpenCV it becomes easy to do complex tasks such


as identify and recognize faces, identify objects, classify
human actions in videos, track camera movements, track
moving objects, extract 3D object models, generate 3D
point clouds from stereo cameras, stitch images together
to generate an entire scene with a high resolution image
and many more.
• Let us start with the Open CV Functions used in our
Model.
Read an Image
Use the function cv2.imread() to read an image. First
argument is the image name. The image should be in
the working directory or a full path of image should be
given. Second argument is a flag which specifies the
way image should be read.
• cv2.IMREAD COLOR (1): Loads a color image.
• cv2.IMREAD GRAYSCALE (0): Loads image in grayscale
mode
Read an Image
• Syntax: cv2.imread(path, flag)
▪ Parameters:
Path: A string representing the path of the image to
be read.
Flag: It specifies the way in which image should be
read. It’s default value is RGB.
• Return Value: This method returns an image that is
loaded from the specified file.
Read an Image
• import cv2
• img = cv2.imread('C:\\Users\\hamid\\Downloads\\messi.jpg’,1)
• cv2.imshow('result',img)
Read an Image
• import cv2
• img = cv2.imread('C:\\Users\\hamid\\Downloads\\messi.jpg’,0)
• cv2.imshow('result',img)
Read an Image # 2
• import cv2
• path = r'C:\Users\hamid\Downloads\messi.jpg'
• img = cv2.imread(path,1)
• cv2.imshow('result',img)
Read an Image # 2
• import cv2
• path = r'C:\Users\hamid\Downloads\messi.jpg'
• img = cv2.imread(path,0)
• cv2.imshow('result',img)
Display an Image
Use the function cv2.imshow() to display an image in a
window. The window automatically fits to the image
size. First argument is a window name which is a string.
Second argument is our image.
• Syntax: cv2.imshow(window_name, image)
▪ Parameters:
• window_name: A string representing the name of
the window in which image to be displayed.
• image: It is the image that is to be displayed.
Display an Image
• import cv2
• img = cv2.imread('C:\\Users\\hamid\\Downloads\\messi.jpg’,1)
• cv2.imshow('result',img)
Shape of an Image
• In Image Processing applications, it is often necessary
to know the size of an image that is loaded or
transformed through various stages.

import cv2
img = cv2.imread("C:\\Users\\hamid\\Downloads\\messi.jpg")
img.shape

Output: (678, 678, 3)


Resize an Image
• Image resizing refers to the scaling of images. Scaling
comes in handy in many image processing as well as
machine learning applications.
• It helps in reducing the number of pixels from an
image and that has several advantages e.g. It can
reduce the time of training of a neural network as the
more the number of pixels in an image more is the
number of input nodes that in turn increases the
complexity of the model.
Resize an Image
It also helps in zooming in on images. Many times we
need to resize the image i.e. either shrink it or scale it up
to meet the size requirements. OpenCV provides us
several interpolation methods for resizing an image.
• Syntax: cv2.resize(source, size)
▪ Parameters:
• source: Input Image array (Single-channel, 8-bit or
floating-point)
• size: Size of the output array
Resize an Image

NOTE!
One thing to keep in mind while using the cv2.resize()
function is that the tuple passed for determining the size
of the new image ((1050, 1610) in this case) follows the
order (width, height) unlike as expected (height, width).
Resize an Image

• import cv2
• img =
cv2.imread("C:\\Users\\hamid\\
Downloads\\messi.jpg")
• small = cv2.resize(img,(150,150))
• small.shape → (150, 150, 3)
Converting Color of an Image

cv2.cvtColor() method is used to convert an image


from one color space to another. There are more than
150 color-space conversion methods available in
OpenCV. We will use 2 of color space conversion codes
in this presentation.

Check the 150 Conversions!


Converting Color of an Image
• Syntax: cv2.cvtColor(src, code)
▪ Parameters:
• src: It is the image whose color space is to be changed.
• code: It is the color space conversion code.
Converting Color of an Image

import cv2
path = cv2.imread("C:\\Users\\hamid\\Downloads\\messi.jpg")
image = cv2.cvtColor(path, cv2.COLOR_BGR2GRAY )
cv2.imshow('result', image)
Converting Color of an Image
Converting Color of an Image

import cv2
path = cv2.imread("C:\\Users\\hamid\\Downloads\\messi.jpg")
image = cv2.cvtColor(path, cv2.COLOR_BGR2RGB )
cv2.imshow('result', image)
Converting Color of an Image
Converting Color of an Image

import cv2
path = cv2.imread("C:\\Users\\hamid\\Downloads\\messi.jpg")
image = cv2.cvtColor(path, cv2.COLOR_BGR2HSV )
cv2.imshow('result', image)
Converting Color of an Image
waitKey Function

• waitKey() function of Python OpenCV allows users to


display a window for given milliseconds or until any
key is pressed. It takes time in milliseconds as a
parameter and waits for the given time to destroy
the window, if 0 is passed in the argument it waits
till any key is pressed.
waitKey Function # 1
Display image with a time limit
• import cv2
• img =
cv2.imread("C:\\Users\\hamid\\Downloads\\messi.jpg")
• cv2.imshow('result', img)
• cv2.waitKey(3000)
waitKey Function # 2
Display image until a key is pressed
• import cv2
• img =
cv2.imread("C:\\Users\\hamid\\Downloads\\messi.jpg")
• cv2.imshow('result', img)
• cv2.waitKey(0)
waitKey Function # 3
Display image until a key is pressed
• import cv2
• img = cv2.imread (“C:\\Users\\hamid\\Downloads\\messi.jpg”)
• while True:
• cv2.imshow('result',img)
• if cv2.waitKey(2) == 27:
• break
• cv2.destroyAllWindows()
destroyAllWindows Function
• destroyAllWindows() function allows users to destroy
or close all windows at any time after exiting the
script. If you have multiple windows open at the same
time and you want to close then you would use this
function. It doesn’t take any parameters and doesn’t
return anything. It is similar to destroyWindow()
function but this function only destroys a specific
window unlike destroyAllWindows().
Closing Window using
destroyWindow() Function
• In the Python script given below, we have created two windows
named ‘first’ and ‘second’ respectively that displayed an image
of “messi.jpg” using the cv2.imshow() function that is supposed
to display window ‘first’ first on the screen but before calling
the waitKey() function to delay the closing of windows, we will
destroy only the window named ‘first’ with
destroyWindow(‘first’) function by passing the window name
‘second’ as its argument. We will see that the window ‘second’
is only displayed on the screen which will close only when the
user closes it.
Closing Window using
destroyWindow() Function
• import cv2
• img = cv2.imread("C:\\Users\\hamid\\Downloads\\messi.jpg")
• cv2.imshow(‘first', img)
• cv2.imshow(‘second', img)
• cv2.destroyWindow(‘first')
• cv2.waitKey(0)
Closing Windows using
destroyAllWindows() Function
• In this case, instead of calling destroyWindow() to
delete or close a particular window, we will use
destroyAllWindows() to destroy all windows on the
screen here we have called this function before
waitKey(0), so the images will not at all displayed on
the screen. DestroyAllWindows() is just a good coding
practice.
Closing Windows using
destroyAllWindows() Function
• import cv2
• img = cv2.imread("C:\\Users\\hamid\\Downloads\\messi.jpg")
• cv2.imshow(‘first', img)
• cv2.imshow(‘second', img)
• cv2.destroyAllWindows()
• cv2.waitKey(0)
Capturing Video
• OpenCV is a vast library that helps in providing various
functions for image and video operations. With
OpenCV, we can capture a video from the camera. It
lets you create a video capture object which is helpful
to capture videos through webcam and then you may
perform desired operations on that video.
Steps to Capture a Video
•Use cv2.VideoCapture() to get a video capture
object for the camera.
•Set up an infinite while loop and use
the read() method to read the frames using the
above created object.
•Use cv2.imshow() method to show the frames in
the video.
•Breaks the loop when the user clicks a specific key.
Steps to Capture a Video # 1
• import cv2
• vid = cv2.VideoCapture(0)
• while True:
• ret, frame = vid.read()
• cv2.imshow(‘result', frame)
• if cv2.waitKey(0) == 27:
• break
• vid.release()
• cv2.destroyAllWindows()
Steps to Capture a Video # 2
• import cv2
• vid = cv2.VideoCapture(0)
• while True:
• ret, frame = vid.read()
• cv2.imshow('result', frame)
• if cv2.waitKey(0) == ord('h'):
• break
• vid.release()
• cv2.destroyAllWindows()
Steps to Capture a Video # 3
• import cv2
• vid =
cv2.VideoCapture('C:\\Users\\hamid\\Downloads\\Football.mp4’)
• while True:
• ret, frame = vid.read()
• cv2.imshow('result', frame)
• if cv2.waitKey(0) == ord('h'):
• break
• vid.release()
• cv2.destroyAllWindows()
Displaying Text on an Image
cv2.putText() method is used to draw a text string
on any image. This will be used for textual
representation whether a person is wearing a
mask or not.

Check All Available Fonts


Displaying Text on an Image
• Syntax: cv2.putText(image, text, org, font, fontScale,
color[, thickness[, lineType[, bottomLeftOrigin]]])
▪ Parameters:
• image: It is the image on which text is to be drawn.
• text: Text string to be drawn.
• org: It is the coordinates of the bottom-left corner of
the text string in the image. The coordinates are
represented as tuples of two values i.e. (X coordinate
value, Y coordinate value).
Displaying Text on an Image
font: It denotes the font type. Some of font types
are FONT_HERSHEY_SIMPLEX,
FONT_HERSHEY_PLAIN, , etc.
fontScale: Font scale factor that is multiplied by the
font-specific base size.
color: It is the color of text string to be drawn.
For BGR, we pass a tuple. eg: (255, 0, 0) for blue
color.
thickness: It is the thickness of the line in px.
Image for Reference
Displaying Text on an Image
• import cv2
• image =
cv2.imread("C:\\Users\\hamid\\Downloads\\mes
si.jpg")
• font = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
• org = (50, 200)
• fontScale = 1
• color = (225, 225, 225)
• thickness = 2
Displaying Text on an Image
• image = cv2.putText(image, 'Hamid Ali', org, font,
fontScale, color, thickness)
• while True:
• cv2.imshow('result',image)
• if cv2.waitKey(0):
• break
• cv2.destroyAllWindows()
Displaying Text on an Image
Displaying Text on an Image
Displaying Shape on an Image
cv2.rectangle() method is used to draw a rectangle
on any image. This will be used for the visual
assistance of the position of the face.

Syntax: cv2.rectangle(image, start_point,


end_point, color, thickness)
Displaying Shape on an Image
▪ Parameters:
• image: It is the image on which rectangle is to be
drawn.
• start_point: It is the starting coordinates of rectangle.
The coordinates are represented as tuples of two
values i.e. (X coordinate value, Y coordinate value).
• end_point: It is the ending coordinates of rectangle.
The coordinates are represented as tuples of two
values i.e. (X coordinate value, Y coordinate value).
Displaying Shape on an Image
• color: It is the color of border line of rectangle to be
drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for
blue color.
thickness: It is the thickness of the rectangle border
line in px. Thickness of -1 px will fill the rectangle
shape by the specified color.
• Return Value: It returns an image.
Displaying Shape on an Image
• import cv2
• image =
cv2.imread("C:\\Users\\hamid\\Downloads\\messi.jpg")
• start_point = (5, 5)
• end_point = (220, 220)
• color = (255, 202, 150)
• thickness = 2
• image = cv2.rectangle(image, start_point, end_point,
color, thickness)

You might also like