Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
73 views

Basic Python OpenCV

OpenCV is an open-source computer vision and machine learning library created to facilitate the use of machine perception in applications. It provides over 2500 optimized algorithms for tasks such as image reading, saving, manipulation, and basic operations like resizing and rotating images. The document details functions like imread and imwrite for image handling, as well as methods for accessing pixel values, image properties, and performing transformations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Basic Python OpenCV

OpenCV is an open-source computer vision and machine learning library created to facilitate the use of machine perception in applications. It provides over 2500 optimized algorithms for tasks such as image reading, saving, manipulation, and basic operations like resizing and rotating images. The document details functions like imread and imwrite for image handling, as well as methods for accessing pixel values, image properties, and performing transformations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

1/14/25, 10:54 AM Basic OpenCV

What is OpenCV
OpenCV ( Open Source Computer Vision Library) is an open source software library for computer vision and machine
learning. OpenCV was created to provide a shared infrastructure for applications for computer vision and to speed up the
use of machine perception in consumer products. OpenCV, as a BSD-licensed software, makes it simple for companies to
use and change the code. There are some predefined packages and libraries that make our life simple and OpenCV is
one of them.
Gary Bradsky invented OpenCV in 1999 and soon the first release came in 2000. This library is based on optimised C /
C++ and supports Java and Python along with C++ through an interface. The library has more than 2500 optimised
algorithms, including an extensive collection of computer vision and machine learning algorithms, both classic and state-
of-the-art.Using OpenCV it becomes easy to do complex tasks such as identify and recognise 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.
Python is a user friendly language and easy to work with but this advantage comes with a cost of speed, as Python is
slower to languages such as C or C++.So we extend Python with C/C++, which allows us to write computationally
intensive code in C/C++ and create Python wrappers that can be used as Python modules. Doing this, the code is fast, as
it is written in original C/C++ code (since it is the actual C++ code working in the background) and also, it is easier to code
in Python than C/C++. OpenCV-Python is a Python wrapper for the original OpenCV C++ implementation.

Read & Save Images


Now for OpenCV to work on any image, it must be able to read it. Here we will see how to read a file and save it after we
are done with it. Let’s see how to do it.

Imread function in OpenCV


We use the imread function to read images,here is the syntax of this function

cv2.imread(path, flag)

path - string representing the path of the image to be read. The file should be in the working directory or we must
give the full path to the image
flag - integer value (1, 0 & -1) specify how our image should be read
cv2.IMREAD_COLOR: It specifies to convert the image to the 3 channel BGR colour image. Any transparency of
image will be neglected. It is the default flag. Alternatively, we can passinteger value 1 for this flag.
cv2.IMREAD_GRAYSCALE: It specifies to convert an image to thesingle channel grayscale image. Alternatively,
we can pass integer value 0 for this flag.
cv2.IMREAD_UNCHANGED: It specifies to load an image as such including alpha channel.Alternatively, we can
pass integer value -1 for this flag.

Usually the method imread() returns an image that is loaded from the specified file but in case the image cannot be read
because of unsupported file format, missing file, unsupported or invalid format, it just returns a matrix. Here is a example
in which we read an image from my storage.

Example

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 1/20
1/14/25, 10:54 AM Basic OpenCV
#importing the opencv module
import cv2
# using imread('path') and 1 denotes read as color image
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg',1)
#This is using for display the image
cv2.imshow('image',img)
cv2.waitKey(0) # This is necessary to be required so that the image doesn't close immediate
ly
#It will run continuously until the key press
cv2.destroyAllWindows()

Imwrite function in OpenCV


We can use OpenCV’s imwrite() function to save an image in a storage device and the file extension defines the image
format as shown in the example below.

cv2.imwrite(filename, image)

filename - string representing the image file name. The filename must include image format
image - It is the image object that is to be saved

Example
import cv2
# read image in grayscale
img_gray = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 0)
# save image
status = cv2.imwrite('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog_Gray.png',img_gray)
print(f"Image written sucess? : {status}")
Out:

Image written sucess? : True

If the file is successfully written then this function returns True and thus it is important to store the outcome of this function.
In the example above, we have done the same and used the ‘status’ variable to know if the file is written successfully.

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 2/20
1/14/25, 10:54 AM Basic OpenCV

Basic Operation On images


In this section, we are going to discuss some of the basic operations that we can do on the images once we have
successfully read them. The operations we are going to do here ae:
Access pixel values and modify them
Access image properties
Set a Region of Interest (ROI)
Split and merge image channels

Access pixel values and modify them


There are basically two ways to access a pixel value in an Image and modify them. First let's see how we can access a
particular pixel value of an image.

Example
import cv2
img = cv2.imread('img\OpenCV_Dog.jpg')
px = img[100,100]
print(px)
Out:

[204 214 232]

The result of pixel at position (100, 100) ( img[100, 100] ) shows 3 values [204 214 232]. As we know OpenCV stores the
color image as BGR color image, so the first value in the list is the value of the blue channel of this particular pixel, and
the rest are values for green and red channels.
We can also access only one of the channels as shown below.

Example
# accessing only blue pixel
blue = img[100, 100, 0]
green = img[100, 100, 1]
red = img[100, 100, 2]
print(f"blue - {blue}, green - {green}, red - {red}")
Out:

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 3/20
1/14/25, 10:54 AM Basic OpenCV

blue - 204, green - 214, red - 232

To modify the values, we just need to access the pixel and then overwrite it with a value.

Example
img[100, 100] = [255, 255, 255]
print(img[100,100])
This method to access and modify the pixel values is slow so you should make use of NumPy library as it is optimized for
fast array calculations. For accessing individual pixel values, the Numpy array methods, array.item() and array.itemset()
are considered better as they always return a scalar. However, if you want to access all the B,G,R values, you will need to
call array.item() separately for each value as shown below.

Example
# accessing RED value
img.item(100, 100,2)
Out:

232

# modifying RED value


img.itemset((100, 100, 2), 100)
img.item(100, 100, 2)
Out:

100

Access Image properties


What do we mean by image properties here? Often it is important to know the size (total number of pixels in the image),
number of rows, columns, and channels. We can access the later three by using the shape() method as shown below.

Example
# Colour Image
print(img.shape) # Result: (row, column, channels) e.g. (183, 275, 3) - ROW: 183, COLUMN:
275, CHANNELS: 3
print(img.size) # Result: Total number of pixels

# Grayscale Image
print(img_gray.shape) # Result: (row, column) e.g. (183, 275) - ROW: 183, COLUMN: 275
print(img.size)
Out:

(183, 275, 3)
150975
(183, 275)
150975

img.shape returns three numbers in tuple, these are number of rows, number of columns and number of channels
respectively. In case an image is grayscale, the tuple returned contains only the number of rows and columns.
Often a large number of errors in OpenCV-Python code are caused by invalid datatype so img.dtype which returns the
image datatype is very important while debugging.

Example
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 4/20
1/14/25, 10:54 AM Basic OpenCV
print(img.dtype)
print(img_gray.dtype)
Out:

uint8
uint8

Image ROI(Region of interest)


Often you may come across some images where you are only interested in a specific region. Say you want to detect eyes
in an image, will you search the entire image, possibly not as that may not fetch accurate results. But we know that eyes
are a part of face, so it is better to detect a face first, thus here the face is our ROI. You may want to have a look at the
article Face detection using Viola-Jones algorithm (https://www.mygreatlearning.com/blog/viola-jones-algorithm) where
we detect the faces and then find eyes in the area we found faces.

Splitting and Merging Image Channels


We can also split the channels from an image and then work on each channel separately.Or sometimes you may need to
merge them back together,here is how we do it:
But this method is painfully slow, so we can also use the Numpy to do the same.

Example
b,g,r = cv.split(img)
img = cv.merge((b,g,r))
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
Now suppose you want to just set all the values in the red channel to zero.

Example
#sets all values in red channel as zero
img[:,:,2] = 0

OpenCV Resize Image


Usually when working on images, we often need to resize the images according to certain requirements. Mostly you will
do such operation in Machine learning and deep learning as it reduces the time of training of a neural network. As the
number of pixels in an image increases, the more is the number of input nodes that in turn increases the complexity of the
model. We use an inbuilt resize() method to resize an image.

cv2.resize(s, size,fx,fy,interpolation)

s - input image (required)


size - desired size for the output image after resizing (required)
fx - scale factor along the horizontal axis.(optional)
fy - scale factor along the vertical axis
Interpolation(optional) - this flag uses following methods:
a. INTER_NEAREST – a nearest-neighbor interpolation
b. INTER_LINEAR – a bilinear interpolation (used by default)
c. INTER_AREA – resampling using pixel area relation. It may be a preferred method for image decimation, as it
gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method
d. INTER_CUBIC – a bicubic interpolation over 4×4 pixel neighborhood
e. INTER_LANCZOS4 – a Lanczos interpolation over 8×8 pixel neighborhood

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 5/20
1/14/25, 10:54 AM Basic OpenCV

Example
import cv2
# using imread('path') and 1 denotes read as color image
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
print(f"Image Original Size - {img.shape}")

img_resized = cv2.resize(img, (780, 540), interpolation = cv2.INTER_NEAREST)


print(f"Resized Image Size - {img_resized.shape}")
Out:

Image Original Size - (183, 275, 3)


Resized Image Size - (540, 780, 3)

OpenCV Image Rotation


We may need to rotate an image in some of the cases and we can do it easily by using OpenCV. We use cv2.rotate()
method to rotate a 2D array in multiples of 90 degrees.

cv2.rotate(src, rotateCode[, dst])

src - it is the image to be rotated


rotateCode - it is an enum to specify how to rotate the array. Here are some of the possible values:
cv2.cv2.ROTATE_90_CLOCKWISE
cv2.ROTATE_180
cv2.ROTATE_90_COUNTERCLOCKWISE

Example
import cv2
# using imread('path') and 1 denotes read as color image
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
print(img.shape)
img_rotate = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)

cv2.imshow("Image Rotated", img_rotate)


cv2.waitKey()
cv2.destroyAllWindows()
Out:

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 6/20
1/14/25, 10:54 AM Basic OpenCV

Now what if we want to rotate the image by a certain angle. We can use another method for that. First calculate the affine
matrix that does the affine transformation (linear mapping of pixels) by using the getRotationMatrix2D method, next we
warp the input image with the affine matrix using warpAffine method.

cv2.getRotationMatrix2D(center, angle, scale)


cv2.warpAffine(Img, M, (W, H))

center - center of the image (the point about which rotation has to happen)
angle - angle by which image has to be rotated in the anti-clockwise direction
scale - scales the image by the value provided, 1.0 means the shape is preserved
H - height of image
W - width of the image
M - affine matrix returned by cv2.getRotationMatrix2D
Img - image to be rotated

Example

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 7/20
1/14/25, 10:54 AM Basic OpenCV
import cv2
import numpy as np

#importing the opencv module


import cv2
# using imread('path') and 1 denotes read as color image
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
# get image height(img.shape[0]), width(img.shape[1])
(h, w) = img.shape[:2]

image = np.zeros(img.shape, np.uint8)

img_resize = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)

# calculate the center of the image


center = (w / 2, h / 2)

scale = 1.0

# Perform the counter clockwise rotation holding at the center


# 45 degrees
M = cv2.getRotationMatrix2D(center, 45, scale)
print(M)
rotated45 = cv2.warpAffine(img_resize, M, (h, w))

# 110 degrees
M = cv2.getRotationMatrix2D(center,110, scale)
rotated110 = cv2.warpAffine(img_resize, M, (w, h))

# 150 degrees
M = cv2.getRotationMatrix2D(center, 150, scale)
rotated150 = cv2.warpAffine(img_resize, M, (h, w))

cv2.imshow('Image rotated by 45 degrees',rotated45)


cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image

cv2.imshow('Image rotated by 110 degrees',rotated110)


cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image

cv2.imshow('Image rotated by 150 degrees',rotated150)


cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image
Out:

OpenCV Drawing Functions


https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 8/20
1/14/25, 10:54 AM Basic OpenCV
We may require to draw certain shapes on an image such as circle, rectangle, ellipse, polylines, convex, etc. and we can
easily do this using OpenCV. It is often used when we want to highlight any object in the input image for example in case
of face detection, we might want to highlight the face with a rectangle. Here we will learn about the drawing functions such
as circle, rectangle, lines, polylines and also see how to write text on an image.

Drawing circle:
We use the method to circle to draw a circle in an image.

cv2.circle(image, center_coordinates, radius, color, thickness)

image - image on which a circle is to be drawn


center_coordinates - center coordinates of the circle. The coordinates are represented as tuples of two values i.e.
(X coordinate value, Y coordinate value)
radius - radius of the circle
color - color of the border line of the circle to be drawn. We can pass a tuple in for in BGR, e.g. (255, 0, 0) for
blue color
thickness - thickness of the circle border line in px. Thickness of -1 px will fill the circle shape by the specified color
Return Value - returns an image

Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
cv2.circle(img,(80,80), 55, (255,0,0), -1)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:

Drawing Rectangle
In a similar we can draw a rectangle.

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 9/20
1/14/25, 10:54 AM Basic OpenCV

cv2.rectangle(image, start_point, end_point, color, thickness)

image - image on which rectangle is to be drawn


start_point - starting coordinates (top left vertex) of the rectangle. The coordinates are represented as tuples of two
values i.e. (X coordinate value, Y coordinate value)
end_point - ending coordinates (bottom right) of the rectangle. The coordinates are represented as tuples of two
values i.e. (X coordinate value, Y coordinate value)
color - color of the border line of the rectangle to be drawn. We can pass a tuple for in BGR, e.g. (255, 0, 0) for
blue color
thickness - thickness of the rectangle border line in px. Thickness of -1 px will fill the rectangle shape by the
specified color
Return Value - returns an image

Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
cv2.rectangle(img,(15,25),(200,150),(0,255,255),15)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:

Drawing Lines
Here is the syntax of the line method using which we can make lines on an image.

cv2.line(image, start_point, end_point, color, thickness)

image - image on which line is to be drawn


start_point - starting coordinates of the line. The coordinates are represented as tuples of two values i.e. (X
coordinate value, Y coordinate value)
end_point - ending coordinates of the line. The coordinates are represented as tuples of two values i.e. (X
coordinate value, Y coordinate value)
color - color of the line to be drawn. We can pass a tuple for in BGR, e.g. (255, 0, 0) for blue color
thickness - thickness of the line in px
Return Value - returns an image

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 10/20
1/14/25, 10:54 AM Basic OpenCV

Example
import numpy as np
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)

# Start coordinate, here (0, 0). Represents the top left corner of image
start_point = (0, 0)

# End coordinate, here (250, 250). Represents the bottom right corner of image
end_point = (250, 250)

# Green color in BGR


color = (0, 255, 0)

# Line thickness of 9 px
thickness = 4

# Using cv2.line() method. Draw a diagonal green line with thickness of 9 px


cv2.line(img, start_point, end_point, color, thickness)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:

Drawing Polylines
We can draw the polylines using the polylines() method on the image. And these can be used to draw polygonal curves on
the image.

cv2.polyLine(image, arr, is_closed, color, thickness)

image - represents an image


arr - represents the coordinates of vertices into an array of shape nx1x2 where n is number of vertices and it
should be of type int32
is_Closed - flag that indicates whether the drawn polylines are closed or not
color - color of polylines. We can pass a tuple for in BGR, e.g. (255, 0, 0) for blue color
thickness - represents the thickness of the polyline's edges

Example
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 11/20
1/14/25, 10:54 AM Basic OpenCV
import numpy as np
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
#defining points for polylines
pts = np.array([[100,50],[200,300],[700,200],[500,100]], np.int32)
cv2.polylines(img, [pts], True, (0,255,255), 3)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:

Write text on an image


We can write text on the image by using the putText() method.

cv2.putText(img, text, org, font,fontScale color)

img - image on which we have to write text


text - text to write on the image
org - denotes the Bottom-Left corner of the text string on the image. It is used to set the location of text on the
image
font - font of text
fontScale - scale of the font by which you can increase or decrease size
color - Represents the color. We can pass a tuple for in BGR, e.g. (255, 0, 0) for blue color

Example
import cv2
font = cv2.FONT_HERSHEY_SIMPLEX
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
cv2.putText(img,'Dog',(10, 35), font, 1,(255,255,255),2)
# Display the image
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 12/20
1/14/25, 10:54 AM Basic OpenCV

Canny Edge Detection


Edge detection is an image processing technique used for finding the boundaries of objects within images.Here we will
use a popular edge detection algorithm Canny Edge Detection, developed by John F. Canny.In OpenCV, we have
Canny() method to implement this algorithm.

edges = cv2.Canny(img, minVal, maxVal, apertureSize, L2gradient)

img - image which edges we want to detect


minVal - minimum intensity gradient (required)
maxVal - maximum intensity gradient (required)
L2gradient - flag with default value False , indicating the default L1 norm is enough to calculate the image
gradient magnitude. If its is set as True a more accurate L2 norm is used to calculate the image gradient
magnitude but it is computationally more expensive
aperture - aperture size for the Sobel filter (https://en.wikipedia.org/wiki/Sobel_operator)

As we can see we have two threshold values, minVal and maxVal. Any edges with intensity gradient more than maxVal
are sure to be edges, also those edges with intensity gradient less than minVal are sure to be non-edges and are
discarded. The edges which lie between these two thresholds are classified edges or non-edges based on their
connectivity with the ‘sure edges’. If they are connected to “sure-edge” pixels, they are considered to be part of edges.
Otherwise, they are also discarded as non-edges.

Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg')
edges = cv2.Canny(img,200,300,True)
cv2.imshow("Edge Detected Image", edges)
cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image
Out:

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 13/20
1/14/25, 10:54 AM Basic OpenCV

Now we can also do this in real-time.

Example
import cv2

# import Numpy by alias name np


import numpy as np

# capture frames from a camera


cap = cv2.VideoCapture(0)

# loop runs if capturing has been initialized


while (1):
# reads frames from a camera
ret, frame = cap.read()

# Display an original image


cv2.imshow('Original', frame)

# discovers edges in the input image image and


# marks them in the output map edges
edges = cv2.Canny(frame, 100, 200,True)

# Display edges in a frame


cv2.imshow('Edges', edges)

if cv2.waitKey(1) & 0xFF == ord('q'):


break

# Close the window


cap.release()

# De-allocate any associated memory usage


cv2.destroyAllWindows()

OpenCV Image Smoothing


Image smoothing is an image processing technique used for removing the noise in an image.Blurring(smoothing) removes
low-intensity edges and is also beneficial in hiding the details; for example, blurring is required in many cases, such as
hiding any confidential information in an image.OpenCV provides mainly the following type of blurring techniques.

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 14/20
1/14/25, 10:54 AM Basic OpenCV
Here are a few of the methods that we are going to use for smoothing an image:
OpenCV averaging
OpenCV median Blur
OpenCV Gaussian Blur
OpenCV Bilateral Filter

OpenCV averaging
In this technique, we normalize the image with a box filter. It calculates the average of all the pixels which are under the
kernel area(box filter) and replaces the value of the pixel at the center of the box filter with the calculated average.
OpenCV provides the cv2.blur() to perform this operation.The syntax of cv2.blur() function is as follows.

cv2.blur(img, ksize, anchor, borderType)

img - image which is to be blurred


ksize - a tuple representing the blurring kernel size. Large value will make the image more blur
anchor - a variable of type integer representing anchor point and it’s default value Point is (-1, -1) which means
that the anchor is at the kernel center
borderType - represents the type of border to be used for the output

Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg')
img_blur = cv2.blur(img, (10,10))
cv2.imshow('cv2.blur output', img_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:

OpenCV median Blur


In this technique, the median of all the pixels under the kernel window is computed and the central pixel is replaced with
this median value. It has one advantage over the Gaussian and box filters,that being the filtered value for the central
element is always replaced by some pixel value in the image which is not the case in case of either Gaussian or box
filters. OpenCV provides a function medianBlur() that can be used to easily implement this kind of smoothing.

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 15/20
1/14/25, 10:54 AM Basic OpenCV

cv2.medianBlur(src, ksize)

src- represents the source (input image)


ksize - represents the size of the kernel. Must be odd number

Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg')
img_median_blur = cv2.medianBlur(img, 11)
cv2.imshow('cv2.medianBlur output', img_median_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:

OpenCV Gaussian Blur


In this technique, a Gaussian function(kernel) instead of a box filter to blur the image.The width and height of the kernel
needs to be specified and they should be positive and odd. We also have to specify the standard deviation in the
directions X and Y and are represented by sigmaX and sigmaY respectively. If both sigmaX and sigmaY are given as
zeros, they are calculated from the kernel size and if we only specify sigmaX, sigmaY is set to the same value. Gaussian
blurring is highly effective when removing Gaussian noise from an image . In OpenCV we have a function
GaussianBlur() to implement this technique easily.

GaussianBlur(src, ksize, sigmaX,sigmaY)

src − image which is to be blurred


ksize − size of the kernel. Must be odd number
sigmaX − variable of the type double representing the Gaussian kernel standard deviation in X direction
sigmaY − variable of the type double representing the Gaussian kernel standard deviation in Y direction

Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg')
img_gaussian_blur = cv2.GaussianBlur(img, (7, 7), cv2.BORDER_DEFAULT)
cv2.imshow('cv2.GaussianBlur output', img_gaussian_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 16/20
1/14/25, 10:54 AM Basic OpenCV
Out:

OpenCV Contours
What are contours? A Contour is a curve joining all the continuous points having the same color or intensity (along the
boundary). Counters are useful especially when we want to find out a shape of some object or incase of object detection
and recognition. Finding contours is like finding white object from black background, so remember, the object to be found
should be white and background should be black. Thus for better accuracy, we should use binary images and before
finding contours, apply thresholding as we discussed in the last section.
In OpenCV,we use two functions,one to find contours and other to draw contours.We use findContours() function to find
contours and drawCounter() to draw one.

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 17/20
1/14/25, 10:54 AM Basic OpenCV

image, contours, hierarchy=cv.findContours(img, mode, method,contours, hierarchy,offset)

mode - Contour retrieval mode, see RetrievalModes


method - Contour approximation method, see ContourApproximationModes
offset - Optional offset by which every contour point is shifted. This is useful if the contours are extracted from the
image ROI and then they should be analyzed in the whole image context

This function returns three objects:


1. Img - input image in which we have to find contours
2. Contours - contains detected contours and contour is stored as a vector of points
3. Hierarchy - Optional output vector, containing information about the image topology. It has as many elements as
the number of contours. For each i-th contour contours[i], the elements hierarchy[i][0] , hierarchy[i][1] , hierarchy[i]
[2] , and hierarchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same
hierarchical level, the first child contour and the parent contour, respectively. If for the contour i there are no next,
previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.
cv.drawContours(img, contours, contourIdx, color, thickness, lineType, hierarchy, maxLevel, offset)

img - Input image


contours - all the input contours. Each contour is stored as a point vector
contourIdx - parameter indicating a contour to draw. If it is negative, all the contours are drawn
color - color of the contours
thickness - thickness of lines the contours are drawn with. If it is negative (for example, thickness=FILLED ), the
contour interiors are drawn
lineType - line connectivity. See LineTypes
hierarchy - optional information about hierarchy. It is only needed if you want to draw only some of the contours
(see maxLevel)
maxLevel - maximal level for drawn contours. If it is 0, only the specified contour is drawn. If it is 1, the function
draws the contour(s) and all the nested contours. If it is 2, the function draws the contours, all the nested contours,
all the nested-to-nested contours, and so on. This parameter is only taken into account when there is hierarchy
available
offset - optional contour shift parameter. Shift all the drawn contours by the specified offset=(dx,dy)

Example
import cv2

# Let's load a simple image with 3 black squares


image = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
# Find Canny edges
edged = cv2.Canny(image, 30, 200)
cv2.waitKey(0)

# Finding Contours
# Use a copy of the image e.g. edged.copy()
# since findContours alters the image
contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.imshow('Original', image)
cv2.imshow('Canny Edges After Contouring', edged)

cv2.drawContours(image, contours, -1, (0, 255, 0), 3)

cv2.imshow('Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 18/20
1/14/25, 10:54 AM Basic OpenCV

OpenCV Video Capture


OpenCV can also be used for video processing. With OpenCV, we can capture a video from the camera and it also lets us
create a video capture object which is helpful to capture videos through webcam and then you may perform desired
operations on that video. Besides this you can also play and perform operation on a video file and save them.

Capture Video from Camera


Often, we have to capture a live stream with a camera. Using OpenCV’s very simple interface,we can easily do it. Here is
a simple task to get started. In this task we will capture a video from the camera (in-built webcam of my laptop) and
display it as a grayscale video.
In OpenCV we need to create a VideoCapture object to capture a video. We pass either the device index or the name of
a video file as its arguments. Device index is just the number to specify the camera in case we have multiple webcams
available. Normally one has only a single camera connected (as in my case), so simply pass 0. After this we start to
capture each frame using a loop and process it accordingly. At the end, we just break from the loop and release the
capture.

Example
import cv2

capture = cv2.VideoCapture(0)

while(True):
# Capture frame-by-frame
ret, frame = cap.read()

# Our operations on the frame come here


gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame


cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# When everything done, release the capture


capture.release()
cv2.destroyAllWindows()
capture.read() returns a bool (True/False) and the frame which webcam is currently reading. If the frame is read
correctly, it will be True. So you can check the end of the video by checking this return value.

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 19/20
1/14/25, 10:54 AM Basic OpenCV

Playing Video from file


Playing a video using OpenCV is very similar to capturing live feed from a webcam as we saw in the last section. We just
have to change the camera index with the video file name. But sometimes the video file may be corrupt or couldn’t be
read properly, so we use isOpened() method of VideoCapture object to make sure that the video is read successfully.
Also while displaying the frame, we should use appropriate time for cv2.waitKey() , as for too less, video will be very fast
and for too high values, video will be slow.

Example
import cv2

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
ret, frame = cap.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 20/20

You might also like