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

Opencv Notes

Uploaded by

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

Opencv Notes

Uploaded by

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

BIST Technologies Global Education

Private Limited Technology

OPENCV WITH PYTHON

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology

Index
1. Introduction To Opencv
1.1 OPENCV Overview
1.2 Applications of Opencv
1.3 OpenCV Functionality
1.4 Image-Processing
2. Working with Images
2.1 Reading an image in OpenCV using Python

2.2 Display an image in OpenCV using Python

2.3 Writing an image in OpenCV using Python

2.4 Color Spaces in OpenCV


2.5 Arithmetic operations on Images

2.6 Bitwise Operations on Binary Images

3. Image Processing
3.1 Image Resizing
3.2 Eroding an Image
3.3 Blurring an Image
3.4 Create Border around Images
3.5 Grayscaling of Images
3.6 Scaling, Rotating, Shifting and Edge Detection
3.7 Erosion and Dilation of images

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
3.8 Simple Thresholding
3.9 Adaptive Thresholding
3.10 Convert an image from one color space to another
3.11 Image Pyramid
4. Drawing Functions
4.1 Draw a line
4.2 Draw arrow segment
4.3 Draw an ellipse
4.4 Draw a circle
4.5 Draw a rectangle
4.6 Write a text string
5. Working with Videos
5.1 Play Video
5.2 Create Video
5.3 Extract Image From Video
5.4 Capture Video From Camera
5.5 Write Text on Video
5.6 Create Video Using Multiple Images

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology

1.Introduction To Opencv
1.1 OPENCV Overview
Computer vision is a process by which we can understand the images and
videos how they are stored and how we can manipulate and retrieve data
from them. Computer Vision is the base or mostly used for Artificial
Intelligence. Computer-Vision is playing a major role in self-driving cars,
robotics as well as in photo correction apps.
Computer Vision
Computer vision is a process by which we can understand the images
and videos how they are stored and how we can manipulate and retrieve
data from them. Computer Vision is the base or mostly used for Artificial
Intelligence. Computer-Vision is playing a major role in self-driving cars,
robotics as well as in photo correction apps.

OpenCV
OpenCV is the huge open-source library for the computer vision, machine
learning, and image processing and now it plays a major role in real-time
operation which is very important in today’s systems. By using it, one can
process images and videos to identify objects, faces, or even handwriting of a
human. When it integrated with various libraries, such as NumPy, python is
capable of processing the OpenCV array structure for analysis. To Identify
image pattern and its various features we use vector space and perform
mathematical operations on these features.

The first OpenCV version was 1.0. OpenCV is released under a BSD license

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
and hence it’s free for both academic and commercial use. It has C++, C,
Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and
Android. When OpenCV was designed the main focus was real-time
applications for computational efficiency. All things are written in optimized
C/C++ to take advantage of multi-core processing.
Look at the following images
1.2 Applications of OpenCV:
There are lots of applications which are solved using OpenCV, some of them
are listed below
● face recognition
● Automated inspection and surveillance
● number of people – count (foot traffic in a mall, etc)
● Vehicle counting on highways along with their speeds
● Interactive art installations
● Anomaly (defect) detection in the manufacturing process (the odd
defective products)
● Street view image stitching
● Video/image search and retrieval
● Robot and driver-less car navigation and control
● object recognition
● Medical image analysis
● Movies – 3D structure from motion
● TV Channels advertisement recognition

1.3 OpenCV Functionality


● Image/video I/O, processing, display (core, imgproc, highgui)
● Object/feature detection (objdetect, features2d, nonfree)

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
● Geometry-based monocular or stereo computer vision (calib3d,
stitching, videostab)
● Computational photography (photo, video, superres)
● Machine learning & clustering (ml, flann)
● CUDA acceleration

1.4 Image-Processing
Image processing is a method to perform some operations on an image,
in order to get an enhanced image and or to extract some useful
information from it.
If we talk about the basic definition of image processing then “Image
processing is the analysis and manipulation of a digitized image,
especially in order to improve its quality”.
Digital-Image :
An image may be defined as a two-dimensional function f(x, y), where x
and y are spatial(plane) coordinates, and the amplitude of fat any pair
of coordinates (x, y) is called the intensity or grey level of the image at
that point.
In another word An image is nothing more than a two-dimensional
matrix (3-D in case of coloured images) which is defined by the
mathematical function f(x, y) at any point is giving the pixel value at
that point of an image, the pixel value describes how bright that pixel
is, and what colour it should be.
Image processing is basically signal processing in which input is an
image and output is image or characteristics according to requirement
associated with that image.
Image processing basically includes the following three steps:

1. Importing the image

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
2. Analysing and manipulating the image
3. Output in which result can be altered image or report that is based
on image analysis

2. Working with Images

2.1 Reading an image in OpenCV using Python


To use the OpenCV library in python, we need to install these libraries as a
prerequisite:
1. Numpy Library : The computer processes images in the form of a
matrix for which NumPy is used and OpenCV uses it in the
background.
2. OpenCV python : OpenCV library previously it was cv but the
updated version is cv2. It is used to manipulate images and videos.

To install these libraries, we need to run these pip commands in cmd:


pip install opencv-python
pip install numpy
pip install matplotlib
The steps to read and display an image in OpenCV are:
1. Read an image using imread() function.
2. Create a GUI window and display image using imshow() function.
3. Use function waitkey(0) to hold the image window on the screen by the
specified number of seconds, o means till the user closes it, it will hold GUI
window on the screen.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
4. Delete image window from the memory after displaying using
destroyAllWindows() function.
Let’s start reading an image. using cv2.
To read the images cv2.imread() method is used. This method loads an
image from the specified file. If the image cannot be read (because of missing
file, improper permissions, unsupported or invalid format) then this method
returns an empty matrix.

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
cv2.IMREAD_COLOR

Return Value: This method returns an image that is loaded from the
specified file.

Note:
1. The image should be in the working directory or a full path of
image should be given.
2. By default, OpenCV stores colored images in BGR(Blue Green and
Red) format.

All three types of flags are described below:


cv2.IMREAD_COLOR: It specifies to load a color image. Any transparency of
image will be neglected. It is the default flag. Alternatively, we can pass
www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
integer value 1 for this flag.
cv2.IMREAD_GRAYSCALE: It specifies to load an image in grayscale mode.
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.
Below codes are implementations to read images and display images on the
screen using OpenCV and matplotlib libraries functions.

2.2 Display an image in OpenCV using Python

Python OpenCV | cv2.imshow() method


OpenCV-Python is a library of Python bindings designed to solve
computer vision problems. cv2.imshow() method is used to display an
image in a window. The window automatically fits the image size.

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.
Return Value: It doesn’t returns anything.

2.3 Writing an image in OpenCV using Python

Python OpenCV | cv2.imwrite() method


OpenCV-Python is a library of Python bindings designed to solve computer
vision problems. cv2.imwrite() method is used to save an image to any
storage device. This will save the image according to the specified format in
current working directory.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
Syntax: cv2.imwrite(filename, image) Parameters: filename: A string
representing the file name. The filename must include image format like .jpg,
.png, etc. image: It is the image that is to be saved. Return Value: It returns
true if image is saved successfully.

2.4 Color Spaces in OpenCV | Python


Color spaces are a way to represent the color channels present in the
image that gives the image that particular hue. There are several
different color spaces and each has its own significance. Some of the
popular color spaces are RGB (Red, Green, Blue), CMYK (Cyan, Magenta,
Yellow, Black), HSV (Hue, Saturation, Value), etc. BGR color space:
OpenCV’s default color space is RGB. However, it actually stores color
in the BGR format. It is an additive color model where the different
intensities of Blue, Green and Red give different shades of color.

HSV color space: It stores color information in a cylindrical representation


of RGB color points. It attempts to depict the colors as perceived by the
human eye. Hue value varies from 0-179, Saturation value varies from

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
0-255 and Value value varies from 0-255. It is mostly used for color
segmentation purpose.

CMYK color space: Unlike, RGB it is a subtractive color space. The CMYK
model works by partially or entirely masking colors on a lighter, usually
white, background. The ink reduces the light that would otherwise be
reflected. Such a model is called subtractive because inks “subtract” the
colors red, green and blue from white light. White light minus red leaves
cyan, white light minus green leaves magenta, and white light minus blue
leaves yellow.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology

import cv2
from google.colab.patches import cv2_imshow
image=cv2.imread('')
B, G, R=cv2.split(image)
cv2.imshow("original", image)
cv2_imshow(B)
cv2_imshow(G)
cv2_imshow(R)
—-----------------------
2.4 Arithmetic operations on Images

Arithmetic Operations on Images using OpenCV | Set-1 (Addition and


Subtraction)

Arithmetic Operations like Addition, Subtraction, and Bitwise


Operations(AND, OR, NOT, XOR) can be applied to the input images. These
operations can be helpful in enhancing the properties of the input images.
The Image arithmetics are important for analyzing the input image
properties. The operated images can be further used as an enhanced input
image, and many more operations can be applied for clarifying,
thresholding, dilating etc of the image.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
Addition of Image:

We can add two images by using function cv2.add(). This directly adds up
image pixels in the two images.

Syntax: cv2.add(img1, img2)

But adding the pixels is not an ideal situation. So, we use cv2.addweighted().
Remember, both images should be of equal size and depth.

Syntax: cv2.addWeighted(img1, wt1, img2, wt2, gammaValue)


Parameters:
img1: First Input Image array(Single-channel, 8-bit or floating-point)
wt1: Weight of the first input image elements to be applied to the final image
img2: Second Input Image array(Single-channel, 8-bit or floating-point)
wt2: Weight of the second input image elements to be applied to the final
image
gammaValue: Measurement of light

Subtraction of Image:

Just like addition, we can subtract the pixel values in two images and merge
them with the help of cv2.subtract(). The images should be of equal size and
depth.

Syntax: cv2.subtract(src1, src2)

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
2.5 Bitwise Operations on Binary Images

Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise


Operations on Binary Images)

Prerequisite: Arithmetic Operations on Images | Set-1


Bitwise operations are used in image manipulation and used for extracting
essential parts in the image. In this article, Bitwise operations used are :

1. AND
2. OR
3. XOR
4. NOT

Also, Bitwise operations helps in image masking. Image creation can be


enabled with the help of these operations. These operations can be helpful
in enhancing the properties of the input images.
NOTE: The Bitwise operations should be applied on input images of same
dimensions
Input Image 1:

Bitwise AND operation on Image:

Bit-wise conjunction of input array elements.

Syntax: cv2.bitwise_and(source1, source2, destination, mask)


Parameters:

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
source1: First Input Image array(Single-channel, 8-bit or floating-point)
source2: Second Input Image array(Single-channel, 8-bit or floating-point)
dest: Output array (Similar to the dimensions and type of Input image array)
mask: Operation mask, Input / output 8-bit single-channel mask

Bitwise OR operation on Image:

Bit-wise disjunction of input array elements.

Syntax: cv2.bitwise_or(source1, source2, destination, mask)


Parameters:
source1: First Input Image array(Single-channel, 8-bit or floating-point)
source2: Second Input Image array(Single-channel, 8-bit or floating-point)
dest: Output array (Similar to the dimensions and type of Input image array)
mask: Operation mask, Input / output 8-bit single-channel mask

Bitwise XOR operation on Image


Bit-wise exclusive-OR operation on input array elements.

Syntax: cv2.bitwise_xor(source1, source2, destination, mask)


Parameters:
source1: First Input Image array(Single-channel, 8-bit or floating-point)
source2: Second Input Image array(Single-channel, 8-bit or floating-point)
dest: Output array (Similar to the dimensions and type of Input image array)
mask: Operation mask, Input / output 8-bit single-channel mask

Bitwise NOT operation on Image:

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
Inversion of input array elements.
Syntax: cv2.bitwise_not(source, destination, mask)
Parameters:
source: Input Image array(Single-channel, 8-bit or floating-point)
dest: Output array (Similar to the dimensions and type of Input image array)
mask: Operation mask, Input / output 8-bit single-channel mask

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
3. Image Processing
3.1 Image Resizing
Image Resizing using OpenCV | Python
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.
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.
Choice of Interpolation Method for Resizing:
● cv2.INTER_AREA: This is used when we need to shrink an image.
● cv2.INTER_CUBIC: This is slow but more efficient.
● cv2.INTER_LINEAR: This is primarily used when zooming is
required. This is the default interpolation technique in OpenCV.

Syntax: cv2.resize(source, dsize, dest, fx, fy, interpolation)


Parameters:

● source: Input Image array (Single-channel, 8-bit or floating-point)


● dsize: Size of the output array
● dest: Output array (Similar to the dimensions and type of Input
image array) [optional]
● fx: Scale factor along the horizontal axis [optional]

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
● fy: Scale factor along the vertical axis [optional]
● interpolation: One of the above interpolation methods [optional]

3.2 Eroding an Image


Python OpenCV | cv2.erode() method
OpenCV-Python is a library of Python bindings designed to solve computer
vision problems. cv2.erode() method is used to perform erosion on the
image. The basic idea of erosion is just like soil erosion only, it erodes away
the boundaries of foreground object (Always try to keep foreground in white).
It is normally performed on binary images. It needs two inputs, one is our
original image, second one is called structuring element or kernel which
decides the nature of operation. A pixel in the original image (either 1 or 0)
will be considered 1 only if all the pixels under the kernel is 1, otherwise it is
eroded (made to zero).
Syntax: cv2.erode(src, kernel[, dst[, anchor[, iterations[, borderType[,
borderValue]]]]]) Parameters: src: It is the image which is to be eroded .
kernel: A structuring element used for erosion. If element = Mat(), a 3 x 3
rectangular structuring element is used. Kernel can be created using
getStructuringElement. dst: It is the output image of the same size and type
as src. anchor: It is 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: It depicts what kind of border to be added. It is defined
by flags like cv2.BORDER_CONSTANT, cv2.BORDER_REFLECT, etc.
iterations: It is number of times erosion is applied. borderValue: It is border
value in case of a constant border. Return Value: It returns an image.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
3.3 Blurring an Image
Image Blurring refers to making the image less clear or distinct. It is done
with the help of various low pass filter kernels. Advantages of blurring:
● It helps in Noise removal. As noise is considered as high pass
signal so by the application of low pass filter kernel we restrict
noise.
● It helps in smoothing the image.
● Low intensity edges are removed.
● It helps in hiding the details when necessary. For e.g. in many
cases police deliberately want to hide the face of the victim, in such
cases blurring is required.

Important types of blurring:


● Gaussian Blurring:Gaussian blur is the result of blurring an image
by a Gaussian function. It is a widely used effect in graphics
software, typically to reduce image noise and reduce detail. It is
also used as a preprocessing stage before applying our machine
learning or deep learning models. E.g. of a Gaussian kernel(3×3)

● Median Blur: The Median Filter is a non-linear digital filtering


technique, often used to remove noise from an image or signal.
Median filtering is very widely used in digital image processing

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
because, under certain conditions, it preserves edges while
removing noise. It is one of the best algorithms to remove Salt and
pepper noise.
● Bilateral Blur: A bilateral filter is a non-linear, edge-preserving,
and noise-reducing smoothing filter for images. It replaces the
intensity of each pixel with a weighted average of intensity values
from nearby pixels. This weight can be based on a Gaussian
distribution. Thus, sharp edges are preserved while discarding the
weak ones.

3.4 Create Border around Images


Python OpenCV | cv2.copyMakeBorder() method
OpenCV-Python is a library of Python bindings designed to solve computer
vision problems. cv2.copyMakeBorder() method is used to create a border
around the image like a photo frame.

Syntax: cv2.copyMakeBorder(src, top, bottom, left, right, borderType, value)


Parameters:
src: It is the source image.
top: It is the border width in number of pixels in top direction.
bottom: It is the border width in number of pixels in bottom direction.
left: It is the border width in number of pixels in left direction.
right: It is the border width in number of pixels in right direction.
borderType: It depicts what kind of border to be added. It is defined by flags
like cv2.BORDER_CONSTANT, cv2.BORDER_REFLECT, etc dest: It is the
destination image
value: It is an optional parameter which depicts color of border if border type
is cv2.BORDER_CONSTANT.
Return Value: It returns an image.
www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
The borderType flags are described below:

cv2.BORDER_CONSTANT: It adds a constant colored border. The value


should be given as a keyword argument
cv2.BORDER_REFLECT: The border will be mirror reflection of the border
elements. Suppose, if image contains letters “abcdefg” then output will be
“gfedcba|abcdefg|gfedcba“.
cv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT: It does the same
works as cv2.BORDER_REFLECT but with slight change. Suppose, if image
contains letters “abcdefgh” then output will be
“gfedcb|abcdefgh|gfedcba“.
cv2.BORDER_REPLICATE: It replicates the last element. Suppose, if image
contains letters “abcdefgh” then output will be “aaaaa|abcdefgh|hhhhh“.

3.5 Grayscaling of Images


Python | Grayscaling of Images using OpenCV
Grayscaling is the process of converting an image from other color spaces
e.g. RGB, CMYK, HSV, etc. to shades of gray. It varies between complete
black and complete white.

Importance of grayscaling

● Dimension reduction: For example, In RGB images there are


three color channels and three dimensions while grayscale images
are single-dimensional.
● Reduces model complexity: Consider training neural articles on
RGB images of 10x10x3 pixels. The input layer will have 300 input
nodes. On the other hand, the same neural network will need only
100 input nodes for grayscale images.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
● For other algorithms to work: Many algorithms are customized to
work only on grayscale images e.g. Canny edge detection function
pre-implemented in the OpenCV library works on Grayscale images
only.

Let’s learn the different image processing methods to convert a colored


image into a grayscale image.

Method 1: Using the cv2.cvtColor() function

Import the OpenCV and read the original image using imread() than convert
to grayscale using cv2.cvtcolor() function. destroyAllWindows() function
allows users to destroy or close all windows at any time after exiting the
script.

Method 2: Using the cv2.imread() function with flag=zero

Import the OpenCV and read the original image using imread() than convert
to grayscale using cv2.cvtcolor() function.

3.6 Scaling, Rotating, Shifting and Edge Detection


Image Processing in Python
Image processing in Python is a rapidly growing field with a wide range of
applications. It is used in a variety of industries, including Computer vision,
medical imaging, security, etc.
In this article, we’ll look at how to use OpenCV in Python to process the
images.
What is Image Processing?

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
Image processing is the field of study and application that deals with
modifying and analyzing digital images using computer algorithms. The goal
of image processing is to enhance the visual quality of images, extract useful
information, and make images suitable for further analysis or interpretation.
Image Processing Using OpenCV
OpenCV (Open Source Computer Vision) is a powerful and widely-used
library for image processing and computer vision tasks. It provides a
comprehensive set of functions and tools that facilitate the development of
applications dealing with images and videos.
While taking photographs is as simple as pressing a button, processing and
improving those images sometimes takes more than a few lines of code.
That’s where image processing libraries like OpenCV come into play.
OpenCV is a popular open-source package that covers a wide range of image
processing and computer vision capabilities and methods. It supports
multiple programming languages including Python, C++, and Java. OpenCV
is highly tuned for real-time applications and has a wide range of
capabilities.
Image Processing with Python
We will make the following operations most commonly uses for data
augmentation task which training the model in computer Vision.
1. Image Resizing
2. Image Rotation
3. Image Translation
4. Image Shearing
5. Image Normalization
6. Edge detection in an Image
7. Image Blurring
8. Morphological Image Processing

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
Image Resizing

Scaling operations increase or reduce the size of an image.


● The cv2.resize() function is used to resize an python image in
OpenCV. It takes the following arguments:

cv2.resize(src, dsize,interpolation)
Here,
src :The image to be resized.
dsize :The desired width and height of the resized image.
interpolation:The interpolation method to be used.

● When the python image is resized, the interpolation method


defines how the new pixels are computed. There are several
interpolation techniques, each of which has its own quality vs.
speed trade-offs.
● It is important to note that resizing an image can reduce its
quality. This is because the new pixels are calculated by
interpolating between the existing pixels, and this can introduce
some blurring.

Image Rotation

Images can be rotated to any degree clockwise or otherwise. We just need to


define rotation matrix listing rotation point, degree of rotation and the
scaling factor.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
● The cv2.getRotationMatrix2D() function is used to create a
rotation matrix for an image. It takes the following arguments:
○ The center of rotation for the image.
○ The angle of rotation in degrees.
○ The scale factor.
● The cv2.warpAffine() function is used to apply a transformation
matrix to an image. It takes the following arguments:
○ The python image to be transformed.
○ The transformation matrix.
○ The output image size.
● The rotation angle can be positive or negative. A positive angle
rotates the image clockwise, while a negative angle rotates the
image counterclockwise.
● The scale factor can be used to scale the image up or down. A
scale factor of 1 will keep the image the same size, while a scale
factor of 2 will double the size of the python image.

Image Translation

Translating an image means shifting it within a given frame of reference that


can be along the x-axis and y-axis.
● To translate an image using OpenCV, we need to create a
transformation matrix. This matrix is a 2×3 matrix that specifies
the amount of translation in each direction.
● The cv2.warpAffine() function is used to apply a transformation
matrix to an image. It takes the following arguments:
○ The image to be transformed.
○ The transformation matrix.
○ The output image size.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
● The translation parameters are specified in the transformation
matrix as the tx and ty elements. The tx element specifies the
amount of translation in the x-axis, while the ty element specifies
the amount of translation in the y-axis.

Image shearing is a geometric transformation that skews an image along one


or both axes i.e x or y axis.
● To shear an image using OpenCV, we need to create a
transformation matrix. This matrix is a 2×3 matrix that specifies
the amount of shearing in each direction.
● The cv2.warpAffine() function is used to apply a transformation
matrix to an image. It takes the following arguments:
○ The image to be transformed.
○ The transformation matrix.
○ The output image size.
● The shearing parameters are specified in the transformation
matrix as the shearX shearY elements. The shearX element
specifies the amount of shearing in the x-axis, while the shearY
element specifies the amount of shearing in the y-axis.

Image Normalization

Image normalization is a process of scaling the pixel values in an image to a


specific range.This is often done to improve the performance of image
processing algorithms, as many algorithms work better when the pixel
values are within a certain range.
● In OpenCV, the cv2.normalize() function is used to normalize an
image. This function takes the following arguments:

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
○ The input image.
○ The output image.
○ The minimum and maximum values of the normalized
image.
○ The normalization type.
○ The dtype of the output image.
● The normalization type specifies how the pixel values are scaled.
There are several different normalization types available, each with
its own trade-offs between accuracy and speed.
● Image normalization is a common preprocessing step in many
image processing tasks. It can help to improve the performance of
algorithms such as image classification, object detection, and
image segmentation.

3.7 Erosion and Dilation of images

Edge detection of Image

The process of image edge detection involves detecting sharp edges in the
image. This edge detection is essential in the context of image recognition or
object localization/detection. There are several algorithms for detecting
edges due to its wide applicability.
In image processing and computer vision applications, Canny Edge
Detection is a well-liked edge detection approach. In order to detect edges,
the Canny edge detector first smoothes the image to reduce noise, then
computes its gradient, and then applies a threshold to the gradient. The
multi-stage Canny edge detection method includes the following steps:
● Gaussian smoothing: The image is smoothed using a Gaussian
filter to remove noise.
www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
● Gradient calculation: The gradient of the image is calculated using
the Sobel operator.
● Non-maximum suppression: Non-maximum suppression is applied
to the gradient image to remove spurious edges.
● Hysteresis thresholding: Hysteresis thresholding is applied to the
gradient image to identify strong and weak edges.

The Canny edge detector is a powerful edge detection algorithm that can
produce high-quality edge images. However, it can also be computationally
expensive.

Image Blurring

Image blurring is the technique of reducing the detail of an image by


averaging the pixel values in the neighborhood. This can be done to reduce
noise, soften edges, or make it harder to identify a picture. In many image
processing tasks, image blurring is a common preprocessing step. It is
useful in the optimization of algorithms such as image classification, object
identification, and image segmentation. In OpenCV, a variety of different
blurring methods are available, each with a particular trade-off between
blurring strength and speed.
Some of the most common blurring techniques include:
● Gaussian blurring: This is a popular blurring technique that uses
a Gaussian kernel to smooth out the image.
● Median blurring: This blurring technique uses the median of the
pixel values in a neighborhood to smooth out the image.
● Bilateral blurring: This blurring technique preserves edges while
blurring the image.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
Morphological Image Processing

Morphological image processing is a set of python image processing


techniques based on the geometry of objects in an image. These procedures
are commonly used to eliminate noise, separate objects, and detect edges in
images.
Two of the most common morphological operations are:
● Dilation: This operation expands the boundaries of objects in an
image.
● Erosion: This operation shrinks the boundaries of objects in an
image.

Morphological procedures are often used in conjunction with other image


processing methods like segmentation and edge detection.

Erosion and Dilation of images using OpenCV in python

Morphological operations are a set of operations that process images


based on shapes. They apply a structuring element to an input image and
generate an output image.
The most basic morphological operations are two: Erosion and Dilation
Basics of Erosion:
● Erodes away the boundaries of the foreground object
● Used to diminish the features of an image.

Working of erosion:

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
1. A kernel(a matrix of odd size(3,5,7) is convolved with the image.
2. A pixel in the original image (either 1 or 0) will be considered 1 only
if all the pixels under the kernel are 1, otherwise, it is eroded
(made to zero).
3. Thus all the pixels near the boundary will be discarded depending
upon the size of the kernel.
4. So the thickness or size of the foreground object decreases or
simply the white region decreases in the image.

Basics of dilation:
● Increases the object area
● Used to accentuate features

Working of dilation:
1. A kernel(a matrix of odd size(3,5,7) is convolved with the image
2. A pixel element in the original image is ‘1’ if at least one pixel under
the kernel is ‘1’.
3. It increases the white region in the image or the size of the
foreground object increases

Uses of Erosion and Dilation:


1. Erosion:

○ It is useful for removing small white noises.


○ Used to detach two connected objects etc.
2. Dilation:
www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
○ In cases like noise removal, erosion is followed by dilation.
Because, erosion removes white noises, but it also
shrinks our object. So we dilate it. Since noise is gone,
they won’t come back, but our object area increases.
○ It is also useful in joining broken parts of an object.

Python | Thresholding techniques using OpenCV | Set-1 (Simple


Thresholding)
Thresholding is a technique in OpenCV, which is the assignment of pixel
values in relation to the threshold value provided. In thresholding, each
pixel value is compared with the threshold value. If the pixel value is smaller
than the threshold, it is set to 0, otherwise, it is set to a maximum value
(generally 255). Thresholding is a very popular segmentation technique,
used for separating an object considered as a foreground from its
background. A threshold is a value which has two regions on its either side
i.e. below the threshold or above the threshold.
In Computer Vision, this technique of thresholding is done on grayscale
images. So initially, the image has to be converted in grayscale color space.
If f (x, y) < T
then f (x, y) = 0
else
f (x, y) = 255

where
f (x, y) = Coordinate Pixel Value
T = Threshold Value.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology

In OpenCV with Python, the function cv2.threshold is used for


thresholding.

Syntax: cv2.threshold(source, thresholdValue, maxVal,


thresholdingTechnique)
Parameters:
-> source: Input Image array (must be in Grayscale).
-> thresholdValue: Value of Threshold below and above which pixel values
will change accordingly.
-> maxVal: Maximum value that can be assigned to a pixel.
-> thresholdingTechnique: The type of thresholding to be applied.

3.8 Simple Thresholding

Simple Thresholding

The basic Thresholding technique is Binary Thresholding. For every pixel,


the same threshold value is applied. If the pixel value is smaller than the
threshold, it is set to 0, otherwise, it is set to a maximum value.
The different Simple Thresholding Techniques are:

● cv2.THRESH_BINARY: If pixel intensity is greater than the set


threshold, value set to 255, else set to 0 (black).
● cv2.THRESH_BINARY_INV: Inverted or Opposite case of
cv2.THRESH_BINARY.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
● cv.THRESH_TRUNC: If pixel intensity value is greater than
threshold, it is truncated to the threshold. The pixel values are set
to be the same as the threshold. All other values remain the same.
● cv.THRESH_TOZERO: Pixel intensity is set to 0, for all the pixels
intensity, less than the threshold value.
● cv.THRESH_TOZERO_INV: Inverted or Opposite case of
cv2.THRESH_TOZERO.

3.9 Adaptive Thresholding


Python | Thresholding techniques using OpenCV | Set-2 (Adaptive
Thresholding)

Prerequisite: Simple Thresholding using OpenCV In the previous post,


Simple Thresholding was explained with different types of thresholding
techniques. Another Thresholding technique is Adaptive Thresholding. In
Simple Thresholding, a global value of threshold was used which remained
constant throughout. So, a constant threshold value won’t help in the case

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
of variable lighting conditions in different areas. Adaptive thresholding is the
method where the threshold value is calculated for smaller regions. This
leads to different threshold values for different regions with respect to the
change in lighting. We use cv2.adaptiveThreshold for this.
Syntax: cv2.adaptiveThreshold(source, maxVal, adaptiveMethod,
thresholdType, blocksize, constant) Parameters: -> source: Input Image
array(Single-channel, 8-bit or floating-point) -> maxVal: Maximum value that
can be assigned to a pixel. -> adaptiveMethod: Adaptive method decides
how threshold value is calculated. cv2.ADAPTIVE_THRESH_MEAN_C:
Threshold Value = (Mean of the neighbourhood area values – constant value).
In other words, it is the mean of the blockSize×blockSize neighborhood of a
point minus constant. cv2.ADAPTIVE_THRESH_GAUSSIAN_C: Threshold
Value = (Gaussian-weighted sum of the neighbourhood values – constant
value). In other words, it is a weighted sum of the blockSize×blockSize
neighborhood of a point minus constant. -> thresholdType: The type of
thresholding to be applied. -> blockSize: Size of a pixel neighborhood that is
used to calculate a threshold value. -> constant: A constant value that is
subtracted from the mean or weighted sum of the neighbourhood pixels.

3.10 Convert an image from one color space to another


Python OpenCV | cv2.cvtColor() method

OpenCV-Python is a library of Python bindings designed to solve computer


vision problems. 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 some of color space conversion
codes below.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
Syntax: cv2.cvtColor(src, code[, dst[, dstCn]]) Parameters: src: It is the image
whose color space is to be changed. code: It is the color space conversion
code. dst: It is the output image of the same size and depth as src image. It is
an optional parameter. dstCn: It is the number of channels in the destination
image. If the parameter is 0 then the number of the channels is derived
automatically from src and code. It is an optional parameter. Return Value: It
returns an image.

Image Pyramid using OpenCV | Python

Image Pyramids are one of the most beautiful concept of image


processing.Normally, we work with images with default resolution but many
times we need to change the resolution (lower it) or resize the original image
in that case image pyramids comes handy. The pyrUp() function increases the
size to double of its original size and pyrDown() function decreases the size to
half. If we keep the original image as a base image and go on applying
pyrDown function on it and keep the images in a vertical stack, it will look like
a pyramid. The same is true for upscaling the original image by pyrUp function.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology

Once we scale down and if we rescale it to the original size, we lose some
information and the resolution of the new image is much lower than the
original one. Below is an example of Image Pyramiding –

Advantages of Image pyramids:

● Lowering of resolution
● Getting various sizes of image
● Image Blending
● Edge detection

Don't miss your chance to ride the wave of the data revolution! Every industry
is scaling new heights by tapping into the power of data. Sharpen your skills
and become a part of the hottest trend in the 21st century.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
4. Drawing Functions
4.1 Draw a line
Python OpenCV | cv2.line() method
OpenCV-Python is a library of Python bindings designed to solve computer
vision problems.cv2.line() method is used to draw a line on any image.
Syntax:
cv2.line(image, start_point, end_point, color, thickness)
Parameters: image: It is the image on which line is to be drawn.
● start_point: It is the starting coordinates of the line. 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 the line. The coordinates
are represented as tuples of two values i.e. (X coordinate value, Y
coordinate value).
● color: It is the color of the line to be drawn. For RGB, we pass a
tuple. eg: (255, 0, 0) for blue color.
● thickness: It is the thickness of the line in px.

Return Value: It returns an image.

4.2 Draw arrow segment


Python OpenCV | cv2.arrowedLine() method

OpenCV-Python is a library of Python bindings designed to solve computer


vision problems.
www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
cv2.arrowedLine() method is used to draw arrow segment pointing from the
start point to the end point.

Syntax: cv2.arrowedLine(image, start_point, end_point, color, thickness,


line_type, shift, tipLength)
Parameters:
image: It is the image on which line is to be drawn.
start_point: It is the starting coordinates of line. 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 line. The coordinates are
represented as tuples of two values i.e. (X coordinate value, Y coordinate
value).
color: It is the color of line 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.
line_type: It denotes the type of the line for drawing.
shift: It denotes number of fractional bits in the point coordinates.
tipLength: It denotes the length of the arrow tip in relation to the arrow
length.
Return Value: It returns an image.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
4.3 Draw an ellipse
Python OpenCV | cv2.ellipse() method

OpenCV-Python is a library of Python bindings designed to solve computer


vision problems. cv2.ellipse() method is used to draw a ellipse on any image.
Syntax: cv2.ellipse(image, centerCoordinates, axesLength, angle, startAngle,
endAngle, color [, thickness[, lineType[, shift]]]) Parameters: image: It is the
image on which ellipse is to be drawn. centerCoordinates: It is the center
coordinates of ellipse. The coordinates are represented as tuples of two values
i.e. (X coordinate value, Y coordinate value). axesLength: It contains tuple of
two variables containing major and minor axis of ellipse (major axis length,
minor axis length). angle: Ellipse rotation angle in degrees. startAngle:
Starting angle of the elliptic arc in degrees. endAngle: Ending angle of the
elliptic arc in degrees. color: It is the color of border line of shape to be drawn.
For BGR, we pass a tuple. eg: (255, 0, 0) for blue color. thickness: It is the
thickness of the shape border line in px. Thickness of -1 px will fill the shape
by the specified color. lineType: This is an optional parameter.It gives the
type of the ellipse boundary. shift: This is an optional parameter. It denotes
the number of fractional bits in the coordinates of the center and values of
axes. Return Value: It returns an image.

4.4 Draw a circle


Python OpenCV | cv2.circle() method
OpenCV-Python is a library of Python bindings designed to solve computer
vision problems. cv2.circle() method is used to draw a circle on any image.
The syntax of cv2.circle() method is:

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology

Syntax:
cv2.circle(image, center_coordinates, radius, color, thickness)
Parameters:
● image: It is the image on which the circle is to be drawn.
● center_coordinates: It is the center coordinates of the circle. The
coordinates are represented as tuples of two values i.e. (X
coordinate value, Y coordinate value).
● radius: It is the radius of the circle.
● color: It is the color of the borderline of a circle to be drawn. For
BGR, we pass a tuple. eg: (255, 0, 0) for blue color.
● thickness: It is the thickness of the circle border line in px.
Thickness of -1 px will fill the circle shape by the specified color.

Return Value: It returns an image.


The steps to create a circle on an image are:
1. Read the image using imread() function.
2. Pass this image to the cv2.circle() method along with other
parameters such as center_coordinates, radius, color and
thickness.
3. Display the image using cv2.imshow() method.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
4.5 Draw a rectangle
Python OpenCV | cv2.rectangle() method
OpenCV-Python is a library of Python bindings designed to solve computer
vision problems. cv2.rectangle() method is used to draw a rectangle on any
image.
Syntax: cv2.rectangle(image, start_point, end_point, color, thickness)
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). 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.

4.6 Draw a text string

Python OpenCV | cv2.putText() method

OpenCV-Python is a library of Python bindings designed to solve


computer vision problems. cv2.putText() method is used to draw a text
string on any 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

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
coordinates are represented as tuples of two values i.e. (X coordinate
value, Y coordinate value). 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. lineType: This is an optional parameter.It gives the type
of the line to be used. bottomLeftOrigin: This is an optional parameter.
When it is true, the image data origin is at the bottom-left corner.
Otherwise, it is at the top-left corner. Return Value: It returns an image.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology

5. WORKING WITH VIDEOS

5.1 Play Video


Python | Play a video using OpenCV
OpenCV (Open Source Computer Vision) is a computer vision library that
contains various functions to perform operations on Images or videos.
OpenCV library can be used to perform multiple operations on videos.

Let’s see how to play a video using the OpenCV Python. To capture a video,
we need to create a VideoCapture object. VideoCapture have the device index
or the name of a video file. Device index is just the number to specify which
camera. If we pass 0 then it is for first camera, 1 for second camera so on. We
capture the video frame by frame.

Syntax

● cv2.VideoCapture(0): Means first camera or webcam.


● cv2.VideoCapture(1): Means second camera or webcam.
● cv2.VideoCapture(“file name.mp4”): Means video file

Step 1: Import the required modules,

Note : Video file should have in same directory where program is executed.

Explanation:

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
Here we are first importing the required modules first and using the
VideoCapture() method to read the input video file and raise a error if the file
didn’t open and imshow() to present the frame that was fetched.

5.2 Create Video


Python | Create video using multiple images
using OpenCV
As we know OpenCV is a widely used library for image processing. It provides
a wide sense of image processing. Let’s see how to create video using
multiple images using OpenCV.

Install the following libraries: PIL cv2

Also, check the path before running the code otherwise you will be full with
errors. How it works ? Using PIL library we are opening images and resizing
them to their mean_height and mean_width because the video which will be
created using cv2 library required the input images of same height and width.
Resized images are included in an array and frame of video is set with the
mean_height and mean_width. Then by looping, we are appending each
image to that frame. Below is the implementation :

5.3 Extract images from video in Python


OpenCV comes with many powerful video editing functions. In current
scenario, techniques such as image scanning, face recognition can be
accomplished using OpenCV. Image Analysis is a very common field in the

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
area of Computer Vision. It is the extraction of meaningful information from
videos or images. OpenCv library can be used to perform multiple operations
on videos. Modules Needed:
import cv2

import os

Function Used :

VideoCapture(File_path) : Read the video(.mp4 format)


read() : Read data depending upon the type of object that
calls imwrite(filename, img[, params]) : Saves an image to
a specified file.

5.4 Capture Video From Camera


Python OpenCV: Capture Video from Camera
Python provides various libraries for image and video processing. One of them
is OpenCV. 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.

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
● Use cv2.imshow() method to show the frames in the video.
● Breaks the loop when the user clicks a specific key.

Python – Process images of a video using


OpenCV
Processing a video means, performing operations on the video frame by
frame. Frames are nothing but just the particular instance of the video in a
single point of time. We may have multiple frames even in a single second.
Frames can be treated as similar to an image.
So, whatever operations we can perform on images can be performed on
frames as well. Let us see some of the operations with examples.

Adaptive Threshold –

By using this technique we can apply thresholding on small regions of the


frame. So the collective value will be different for the whole frame.

Smoothing –

Smoothing a video means removing the sharpness of the video and providing
a blurriness to the video. There are various methods for smoothing such as
cv2.Gaussianblur(), cv2.medianBlur(), cv2.bilateralFilter(). For our purpose,
we are going to use cv2.Gaussianblur().

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
Edge Detection –

Edge detection is a useful technique to detect the edges of surfaces and


objects in the video. Edge detection involves the following steps:

● Noise reduction
● Gradient calculation
● Non-maximum suppression
● Double threshold
● Edge tracking by hysteresis

Bitwise Operations –

Bitwise operations are useful to mask different frames of a video together.


Bitwise operations are just like we have studied in the classroom such as
AND, OR, NOT, XOR.

Python – Writing to video with OpenCV

Approach
● Import the required libraries into the working space.
● Read the video on which you have to write.

Syntax:

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
cap = cv2.VideoCapture("path")

● Create a output file using cv2.VideoWriter_fourcc() method

Syntax:

output =
cv2.VideoWriter(“path”,cv2.VideoWriter_fourcc(*’MPEG’),30,(1080,1920))

● Then edit the frames of the video by adding shapes to it (for the
example given here, the same can be applied to any other
technique.).

Syntax:

cv2.rectangle(frame, (100,100), (500,500), (0,255,0), 3)

● Then write the video.

Syntax:

output.write(frame)

5.5 Write Text on Video


Python OpenCv: Write text on video
OpenCV is the huge open-source library for computer vision, machine
learning, and image processing and now it plays a major role in real-time
operation which is very important in today’s systems. By using it, one can

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
process images and videos to identify objects, faces, or even the handwriting
of a human. cv2.putText() method inserts a text on the video frame at the
desired position specified by the user. We can style the type of font and also
it’s color and thickness.

Syntax : cv2.putText(frame, Text, org, font, color, thickness) Parameters:


frame: current running frame of the video. Text: The text string to be inserted.
org: bottom-left corner of the text string font: the type of font to be used.
color: the colour of the font. thickness: the thickness of the font

5.6 Create Video Using Multiple Images


Python | Create video using multiple images
using OpenCV

As we know OpenCV is a widely used library for image processing. It provides


a wide sense of image processing. Let’s see how to create video using
multiple images using OpenCV.

Install the following libraries: PIL cv2

Also, check the path before running the code otherwise you will be full with
errors. How it works ? Using PIL library we are opening images and resizing
them to their mean_height and mean_width because the video which will be
created using cv2 library required the input images of same height and width.
Resized images are included in an array and frame of video is set with the
mean_height and mean_width. Then by looping, we are appending each
image to that frame. Below is the implementation :

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
PROGRAMS

# Python code to read image


import cv2
# To read image from disk, we use
# cv2.imread function, in below method,
img=cv2.imread("geeksforgeeks.png", cv2.IMREAD_COLOR)
# Creating GUI window to display an image on screen
# first Parameter is windows title (should be in string
format)
# Second Parameter is image array
cv2.imshow("image", img)

# To hold the window on screen, we use cv2.waitKey method


# Once it detected the close input, it will release the
control
# To the next line
# First Parameter is for holding screen for specified
milliseconds
# It should be positive integer. If 0 pass an parameter, then
it will
# hold the screen until user close it.
cv2.waitKey(0)
# It is for removing/deleting created GUI window from screen
# and memory
cv2.destroyAllWindows()

#import cv2, numpy and matplotlib libraries

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
import cv2
import numpy as np
import matplotlib.pyplot as plt
img=cv2.imread("geeks.png")
#Displaying image using plt.imshow() method
plt.imshow(img)
#hold the window
plt.waitforbuttonpress()
plt.close('all')

#import cv2, numpy and matplotlib libraries


import cv2
import
numpy as np
import matplotlib.pyplot as plt
img=cv2.imread("geeks.png")

# Converting BGR color to RGB color format


RGB_img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

#Displaying image using plt.imshow() method


plt.imshow(RGB_img)

# hold the window


plt.waitforbuttonpress()
plt.close('all')

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology

# Python program to explain cv2.imread() method

# importing cv2
importncv2

# path
path=r'geeksforgeeks.png'

# Using cv2.imread() method


# Using 0 to read image in grayscale mode
img=cv2.imread(path, cv2.IMREAD_GRAYSCALE)

# Displaying the image


cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Python program to explain cv2.imshow() method


# importing cv2
importbcv2

# path
path=r'C:\Users\Rajnish\Desktop\geeksforgeeks.png'

# Reading an image in default mode


image=cv2.imread(path)

# Window name in which image is displayed

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
window_name='image'

# Using cv2.imshow() method


# Displaying the image
cv2.imshow(window_name, image)

# waits for user to press any key


# (this is necessary to avoid Python kernel form crashing)
cv2.waitKey(0)

# closing all open windows


cv2.destroyAllWindows()

# Python program to explain cv2.imshow() method

# importing cv2
import cv2

# path
path=r'C:\Users\Rajnish\Desktop\geeksforgeeks.png'

# Reading an image in grayscale mode


image=cv2.imread(path,0)

# Window name in which image is displayed


window_name='image'

# Using cv2.imshow() method


# Displaying the image
cv2.imshow(window_name, image)

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
# waits for user to press any key
# (this is necessary to avoid Python kernel form crashing)
cv2.waitKey(0)

# closing all open windows


cv2.destroyAllWindows()

# Python program to explain cv2.imwrite() method

# importing cv2
import cv2

# importing os module
import os

# Image path
image_path=r'C:\Users\Rajnish\Desktop\GeeksforGeeks\geeks.png'

# Image directory
directory=r'C:\Users\Rajnish\Desktop\GeeksforGeeks'

# Using cv2.imread() method


# to read the image
img=cv2.imread(image_path)

# Change the current directory

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415
BIST Technologies Global Education
Private Limited Technology
# to specified directory
os.chdir(directory)

# List files and directories


# in 'C:/Users/Rajnish/Desktop/GeeksforGeeks'
print("Before saving image:")
print(os.listdir(directory))

# Filename
filename='savedImage.jpg'

# Using cv2.imwrite() method


# Saving the image
cv2.imwrite(filename, img)

# List files and directories


# in 'C:/Users / Rajnish / Desktop / GeeksforGeeks'
print("After saving image:")
print(os.listdir(directory))

print('Successfully saved')

www.bisttechnologies.com www.gedutech.net
mail: [email protected] mail: [email protected]
contact: +91 8919651415 contact: +91 7676886524
+91 7676886524 +91 8919651415

You might also like