Opencv Notes
Opencv Notes
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
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
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:
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
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.
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.
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.
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
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.
But adding the pixels is not an ideal situation. So, we use cv2.addweighted().
Remember, both images should be of equal size and depth.
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.
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
1. AND
2. OR
3. XOR
4. NOT
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
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.
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]
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.
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.
Importance of grayscaling
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.
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.
Import the OpenCV and read the original image using imread() than convert
to grayscale using cv2.cvtcolor() 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
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
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.
Image Rotation
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
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 Normalization
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.
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
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
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
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
Simple Thresholding
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.
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.
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.
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 –
● 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.
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
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.
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.
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
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
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.
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
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 :
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.
Adaptive Threshold –
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 –
● Noise reduction
● Gradient calculation
● Non-maximum suppression
● Double threshold
● Edge tracking by hysteresis
Bitwise Operations –
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")
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:
Syntax:
output.write(frame)
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.
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
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')
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
# importing cv2
importncv2
# path
path=r'geeksforgeeks.png'
# path
path=r'C:\Users\Rajnish\Desktop\geeksforgeeks.png'
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'
# importing cv2
import cv2
# path
path=r'C:\Users\Rajnish\Desktop\geeksforgeeks.png'
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)
# 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'
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)
# Filename
filename='savedImage.jpg'
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