Home Learn OpenCV Tutorial: A Guide to Learn OpenCV in Python

OpenCV Tutorial: A Guide to Learn OpenCV in Python

0
OpenCV Tutorial: A Guide to Learn OpenCV in Python

Welcome to the world of computer vision! On this OpenCV tutorial, we’ll embark on an exciting journey to learn and harness the facility of OpenCV (Open Source Computer Vision Library) using the Python programming language. Whether you’re an aspiring computer vision engineer, a seasoned developer seeking to expand your skill set, or just interested by the fascinating field of computer vision, this tutorial is the proper resource for you.

OpenCV is a widely acclaimed open-source library that gives an intensive suite of computer vision and image processing functions. It offers an unlimited array of tools and techniques that empower developers to construct cutting-edge applications in fields like robotics, augmented reality, facial recognition, object detection, and rather more. With its powerful capabilities and user-friendly interface, OpenCV has turn out to be a go-to selection for developers worldwide.

On this tutorial, we’ll start from the fundamentals and progressively delve into more advanced topics. We’ll cover the whole lot it is advisable to know to turn out to be proficient in OpenCV, no matter your prior experience with computer vision. Whether you’re a beginner taking your first steps or an experienced developer seeking to deepen your understanding, this guide will provide clear explanations, practical examples, and hands-on exercises to sharpen your skills.

Here’s a glimpse of what we’ll explore throughout this tutorial:

  1. Organising OpenCV: We’ll guide you thru the installation process, ensuring you’ve got OpenCV up and running in your machine.
  2. Image Manipulation: Learn the best way to read, display, and manipulate images using OpenCV, from basic operations akin to resizing and cropping to more advanced techniques like image mixing and morphological operations.
  3. Image Filtering and Enhancement: Discover various image filtering techniques, including Gaussian blur, edge detection, and sharpening. Dive into histogram equalization and other methods for image enhancement.
  4. Feature Detection and Description: Uncover the secrets of detecting and describing image features, including corners, blobs, and edges. Explore popular algorithms akin to SIFT and SURF.
  5. Object Detection and Tracking: Master the art of detecting and tracking objects inside images or video streams using OpenCV’s built-in algorithms and techniques like Haar cascades and optical flow.
  6. Deep Learning with OpenCV: Harness the facility of deep learning by integrating OpenCV with popular deep learning frameworks like TensorFlow and PyTorch. Learn the best way to perform tasks akin to image classification and object detection using pre-trained models.
  7. Real-Time Applications: Explore exciting real-world applications of OpenCV, including face recognition, augmented reality, and motion detection.

OpenCV is a Python library that means that you can perform image processing and computer vision tasks. It provides a wide selection of features, including object detection, face recognition, and tracking. On this OpenCV Tutorial in Python, we’ll be learning more concerning the library.

What’s OpenCV?

OpenCV is an open-source software library for computer vision and machine learning. The OpenCV full form is Open Source Computer Vision Library. It was created to supply a shared infrastructure for applications for computer vision and to hurry up the usage of machine perception in consumer products. OpenCV, as a BSD-licensed software, makes it easy for corporations to make use of and alter the code. There are some predefined packages and libraries that make our life easy and OpenCV is one in every of them.

Gary Bradsky invented OpenCV in 1999 and shortly the primary release got here in 2000. This library relies on optimised C / C++ and supports Java and Python together with C++ through an interface. The library has greater than 2500 optimised algorithms, including an intensive collection of computer vision and machine learning algorithms, each classic and state-of-the-art.Using OpenCV it becomes easy to do complex tasks akin to discover and recognise faces, discover objects, classify human actions in videos, track camera movements, track moving objects, extract 3D object models, generate 3D point clouds from stereo cameras, stitch images together to generate a complete scene with a high resolution image and lots of more.

Python is a user friendly language and straightforward to work with but this advantage comes with a price of speed, as Python is slower to languages akin to C or C++. So we extend Python with C/C++, which allows us to put in writing computationally intensive code in C/C++ and create Python wrappers that might be used as Python modules. Doing this, the code is fast, because it is written in original C/C++ code (because it is the actual C++ code working within the background) and likewise, it is simpler to code in Python than C/C++. OpenCV-Python is a Python wrapper for the unique OpenCV C++ implementation.

Let’s start!

What’s Computer Vision?

The term Computer Vision (CV) is used and heard fairly often in artificial intelligence (AI) and deep learning (DL) applications. The term essentially means giving a pc the power to see the world as we humans do.

Computer Vision is a field of study which enables computers to copy the human visual system. As already mentioned above, It’s a subset of artificial intelligence which collects information from digital images or videos and processes them to define the attributes. Your complete process involves image acquiring, screening, analysing, identifying and extracting information. This extensive processing helps computers to know any visual content and act on it accordingly. 

Computer vision projects translate digital visual content into explicit descriptions to assemble multi-dimensional data. This data is then became a computer-readable language to help the decision-making process. The major objective of this branch of artificial intelligence is to show machines to gather information from pixels. 

How does a pc read a picture?

How does a human mind apprehend a picture? While you see the image below, what do you really see and the way do you say what’s within the Image?

You  most likely look for various shapes and colors within the Image and that may show you how to determine that that is a picture of a dog. But does a pc also see it in the identical way? The reply is not any.

A digital image is a picture composed of picture elements, also referred to as pixels, each with finite, discrete quantities of numeric representation for its intensity or grey level. So the pc sees a picture as numerical values of those pixels and as a way to recognise a certain image, it has to recognise the patterns and regularities on this numerical data.

Here’s a hypothetical example of how pixels form a picture. The darker pixels are represented by a number closer to the zero and lighter pixels are represented by numbers approaching one. All other colors are represented by the numbers between 0 and 1. 

But normally, you will discover that for any color image, there are 3 primary channels – Red, green and blue and the worth of every channel varies from 0-255. In additional simpler terms we will say that a digital image is definitely formed by the mix of three basic color channels  Red, green, and blue whereas for a grayscale image now we have just one channel whose values also vary from 0-255.

OpenCV installation

There are numerous ways by which you possibly can install OpenCV in your computer. Listed below are some:

Install using Anaconda

Anaconda is a conditional free and open-source distribution of the Python and R programming languages for scientific computing, that goals to simplify package management and deployment. You’ll be able to download it from here and install it.

After successfully installing anaconda, just go to the anaconda prompt and use this command to put in OpenCV:

conda install -c conda-forge opencv  

After this command is successfully executed, OpenCV can be available in your computer.Now allow us to see another ways to put in OpenCV

For Windows

You need to use pip to put in OpenCV on windows. Pip is a de facto standard package-management system used to put in and manage software packages written in Python and it normally is available in installed once you install Python. For those who do not need Python installed, I might suggest download it from here. Use this command within the command prompt to put in OpenCV:

pip install opencv-python  

After installing it,do check whether it is installed successfully.For that just go to the command prompt and sort ‘python’ and hit enter.It is best to see some message like this:

If this will not be the message you see, I suggest reinstalling python into your system. Next type import cv2 and if there is no such thing as a error then it’s installed successfully.

For Mac

You need to use homebrew to put in OpenCV because it makes it very easy and you only should use this command for installing:

brew install opencv

Now that you’ve got installed the OpenCV onto your system, let’s see how it really works.

Read & Save Images

Now for OpenCV to work on any image, it must have the opportunity to read it. Here we’ll see the best way to read a file and reserve it after we’re done with it. Let’s see the best way to do it:

Imread function in OpenCV

We use the imread function to read images. Here is the syntax of this function:

The trail parameter takes a string representing the trail of the image to be read.The file must be within the working directory or we must give the complete path to the image.The opposite parameter is the flag which is used to specify how our image must be read. Listed below are possible values that it takes and their working:

cv2.IMREAD_COLOR: It specifies to convert the image to the three channel BGR 
color image. Any transparency of image can be neglected. It's the default
flag. Alternatively, we will passinteger value 1 for this flag.
cv2.IMREAD_GRAYSCALE: It specifies to convert a picture to thesingle channel 
grayscale image. Alternatively, we will pass integer value 0 for this flag.
cv2.IMREAD_UNCHANGED: It specifies to load a picture as such including alpha 
channel.Alternatively, we will pass integer value -1 for this flag.

Often the strategy imread() returns a picture that’s loaded from the desired file but in case the image can’t be read due to unsupported file format, missing file, unsupported or invalid format, it just returns a matrix. Here’s a example by which we read a picture from my storage.

#importing the opencv module  
import cv2  
# using imread('path') and 1 denotes read as  color image  
img = cv2.imread('dog.jpg',1)  
#That is using for display the image  
cv2.imshow('image',img)  
cv2.waitKey() # That is crucial to be required in order that the image doesn't close immediately.  
#It is going to run repeatedly until the important thing press.  
cv2.destroyAllWindows() 

Imwrite function in OpenCV

We are able to use OpenCV’s imwrite() function to avoid wasting a picture in a storage device and the file extension defines the image format as shown in the instance below. The syntax is the next:

cv2.imwrite(filename, image)  

Parameters:

filename: A string representing the file name. The filename must include image format.

image: It’s the image that’s to be saved.

Here is an example by which we use this function:

import cv2  
# read image  
img = cv2.imread(r'C:UsersMirzadog.jpeg', 1)  
# save image  
status = cv2.imwrite(r'C:UsersMirzadog.jpeg',img)  
print("Image written sucess? : ", status)  

If the file is successfully written then this function returns True and thus it is vital to store the consequence of this function.In the instance above,now we have done the identical and used the ‘status’ variable to know if the file is written successfully.

Basic Operation On images

On this section,we’re going to discuss among the basic operations that we will do on the photographs once now we have successfully read them.The operations we’re going to do here ae:

  • Access pixel values and modify them
  • Access image properties
  • Set a Region of Interest (ROI)
  • Split and merge image channels

Access pixel values and modify them

So there are mainly two ways to access a pixel value in an Image and modify them. First allow us to see how we will access a selected pixel value of a picture.

import numpy as np
import cv2 as cv
img = cv.imread(r'C:UsersMirzadog.jpeg')
px = img[100,100]
print( px )

Output:

[157 166 200]

Now as you possibly can see we got an inventory containing 3 values.As we all know OpenCV stores the colour image as BGR color image,so the primary value within the list is the worth of the blue channel of this particular pixel, and the remainder are values for green and red channels.

We may also access only one in every of the channels as shown below:

# accessing only blue pixel
blue = img[100,100,0]
print( blue )

Output:

157

To switch the values, we just must access the pixel after which overwrite it with a worth as shown below:

img[100,100] = [255,255,255]
print( img[100,100] )

Output:

[255 255 255]

This method to access and modify the pixel values is slow so you need to make use of NumPy library because it is  optimized for fast array calculations. For accessing individual pixel values, the Numpy array methods, array.item() and array.itemset() are considered higher as  they at all times return a scalar. Nevertheless, if you should access all of the B,G,R values, you will want to call array.item() individually for every value as shown below:

# accessing RED value
img.item(10,10,2)
>>59
# modifying RED value
img.itemset((10,10,2),100)
img.item(10,10,2)
>>100

Access Image properties

What can we mean by image properties here? Often it is vital to know the scale(total variety of pixels within the image), variety of rows, columns, and channels.We are able to access the later three by utilizing the form() method as shown below:

print( img.shape )
>>(342, 548, 3)
print( img.size )
>>562248

So here now we have three numbers within the returned tuple, these are variety of rows, variety of columns and variety of channels respectively. Incase a picture is grayscale, the tuple returned comprises only the variety of rows and columns.

Often a lot of errors in OpenCV-Python code are attributable to invalid datatype so img.dtype which returns the image datatype may be very necessary while debugging.

Here is an example:

print( img.dtype )
>>uint8

Image ROI(Region of interest)

Often you could come across some images where you’re only fascinated with a selected region. Say you should detect eyes in a picture, will you search your complete image, possibly not as that will not fetch accurate results. But we all know that eyes are an element of face, so it is healthier to detect a face first ,thus here the face is our ROI. You could wish to have a take a look at the article Face detection using Viola-Jones algorithm where we detect the faces after which find eyes in the world we found faces.

Splitting and Merging Image Channels

We may also split the channels from a picture after which work on each channel individually. Or sometimes you could must merge them back together, here is how we do it:

But this method is painfully slow, so we may also use the Numpy to do the identical, here is how:

b,g,r = cv.split(img)
img = cv.merge((b,g,r))
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]

Now suppose you should just set all of the values within the red channel to zero, here is the best way to try this:

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

OpenCV Resize Image

Often when working on images, we frequently must resize the photographs in keeping with certain requirements. Mostly you’ll do such operation in Machine learning and deep learning because it reduces the time of coaching of a neural network. Because the variety of pixels in a picture increases, the more is the variety of input nodes that in turn increases the complexity of the model. We use an inbuilt resize() method to resize a picture.

Syntax:

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

Parameters:

s – input image (required).

size – desired size for the output image after resizing (required)

fx – Scale factor along the horizontal axis.(optional)

fy – Scale factor along the vertical axis.

Interpolation(optional) – This flag uses following methods:

Interpolation(optional) – This flag uses following methods:
INTER_NEAREST – a nearest-neighbor interpolation
INTER_LINEAR – a bilinear interpolation (utilized by default) 
INTER_AREA – resampling using pixel area relation. It could be a preferred method for image decimation, because it gives moire’-free results. But when the image is zoomed, it is comparable to the INTER_NEAREST method.
INTER_CUBIC – a bicubic interpolation over 4×4 pixel neighborhood 
INTER_LANCZOS4 – a Lanczos interpolation over 8×8 pixel neighborhood

Here is an example of how we will use this method:

import cv2
import numpy as np

#importing the opencv module  
import cv2  
# using imread('path') and 1 denotes read as  color image  
img = cv2.imread('dog.jpg',1)  
print(img.shape)
img_resized=cv2.resize(img, (780, 540),  
               interpolation = cv2.INTER_NEAREST) 
cv2.imshow("Resized",img_resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

OpenCV Image Rotation

We might have to rotate a picture in among the cases and we will do it easily by utilizing OpenCV .We use cv2.rotate() method to rotate a 2D array in multiples of 90 degrees. Here is the syntax:

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

Parameters:
src: It’s the image to be rotated.
rotateCode: It’s an enum to specify the best way to rotate the array.Listed below are among the possible values :
cv2.cv2.ROTATE_90_CLOCKWISE
cv2.ROTATE_180
cv2.ROTATE_90_COUNTERCLOCKWISE

Here is an example using this function.

import cv2
import numpy as np

#importing the opencv module  
import cv2  
# using imread('path') and 1 denotes read as  color image  
img = cv2.imread('dog.jpg',1)  
print(img.shape)
image = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) 
cv2.imshow("Rotated",image)
cv2.waitKey()
cv2.destroyAllWindows()

Output:

rotated image

Now what if we would like to rotate the image by a certain angle.We are able to use one other method for that.First calculate the affine matrix that does the affine transformation (linear mapping of pixels) by utilizing the getRotationMatrix2D method,next we warp the input image with the affine matrix using warpAffine method.

Here is the syntax of those functions:

cv2.getRotationMatrix2D(center, angle, scale)
cv2.warpAffine(Img, M, (W, H))

center: center of the image (the purpose about which rotation has to occur)
angle: angle by which image must be rotated within the anti-clockwise direction.
scale: scales the image by the worth provided,1.0 means the form is preserved.
H:height of image
W: width of the image.
M: affine matrix returned by cv2.getRotationMatrix2D
Img: image to be rotated.

Here is an example by which we rotate a picture by various angles.

import cv2
import numpy as np

#importing the opencv module  
import cv2  
# using imread('path') and 1 denotes read as  color image  
img = cv2.imread('dog.jpg',1)  
# get image height, width
(h, w) = img.shape[:2]
# calculate the middle of the image
center = (w / 2, h / 2)
 
scale = 1.0
 
# Perform the counter clockwise rotation holding at the middle
# 45 degrees
M = cv2.getRotationMatrix2D(center, 45, scale)
print(M)
rotated45 = cv2.warpAffine(img, M, (h, w))
 
# 110 degrees
M = cv2.getRotationMatrix2D(center,110, scale)
rotated110 = cv2.warpAffine(img, M, (w, h))
 
# 150 degrees
M = cv2.getRotationMatrix2D(center, 150, scale)
rotated150 = cv2.warpAffine(img, M, (h, w))
 
 
cv2.imshow('Original Image',img)
cv2.waitKey(0) # waits until a key's pressed
cv2.destroyAllWindows() # destroys the window showing image
 
cv2.imshow('Image rotated by 45 degrees',rotated45)
cv2.waitKey(0) # waits until a key's pressed
cv2.destroyAllWindows() # destroys the window showing image
 
cv2.imshow('Image rotated by 110 degrees',rotated110)
cv2.waitKey(0) # waits until a key's pressed
cv2.destroyAllWindows() # destroys the window showing image
 
cv2.imshow('Image rotated by 150 degrees',rotated150)
cv2.waitKey(0) # waits until a key's pressed
cv2.destroyAllWindows() # destroys the window showing image

Output

OpenCV Drawing Functions

We may require to attract certain shapes on a picture akin to circle, rectangle, ellipse, polylines, convex, etc. and we will easily do that using OpenCV. It is usually used when we would like to focus on any object within the input image for instance in case of face detection, we would want to focus on the face with a rectangle. Here we’ll learn concerning the drawing functions akin to circle, rectangle, lines, polylines and likewise see the best way to write text on a picture.

Drawing circle:

We use the strategy to circle to attract a circle in a picture. Here is the syntax and parameters:

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

Parameters: 
image: It’s the input image on which a circle is to be drawn. 
center_coordinates: It’s the middle coordinates of the circle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value). 
radius: It’s the radius of the circle. 
color: It’s the colour of the border line of the circle to be drawn. We are able to pass a tuple For in BGR,  eg: (255, 0, 0) for blue color. 
thickness: It’s the thickness of the circle border line in px. Thickness of -1 px will fill the circle shape by the desired color.
Return Value: It returns a picture.

Listed below are the few of the examples:

import numpy as np  
import cv2  
img = cv2.imread(r'C:UsersMirzadog.jpeg', 1)  
cv2.circle(img,(80,80), 55, (255,0,0), -1)  
cv2.imshow('image',img)  
cv2.waitKey(0)  
cv2.destroyAllWindows() 

Drawing Rectangle

In an identical we will draw a rectangle. Here is the the syntax for this function:

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

Parameters:

image: It’s the input image on which rectangle is to be drawn.
start_point: It’s the starting coordinates(top left vertex) of the rectangle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
end_point: It’s the ending coordinates(bottom right) of the rectangle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
color: It’s the colour of the border line of the rectangle to be drawn. We are able to pass a tuple For in BGR,  eg: (255, 0, 0) for blue color. 
thickness: It’s the thickness of the rectangle border line in px. Thickness of -1 px will fill the rectangle shape by the desired color.

Return Value: It returns a picture.

Here is an example of this function:

import numpy as np  
import cv2  
img = cv2.imread(r'C:UsersMirzadog.jpeg', 1)  
cv2.rectangle(img,(15,25),(200,150),(0,255,255),15)  
cv2.imshow('image',img)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

Drawing Lines

Here is the syntax of the road method using which we will make lines on a picture.

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

Parameters:
image: It’s the input image on which line is to be drawn.
start_point: It’s the starting coordinates of the road. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
end_point: It’s the ending coordinates of the road. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
color: It’s the colour of the road to be drawn. We are able to pass a tuple For in BGR,  eg: (255, 0, 0) for blue color. 
thickness: It’s the thickness of the road in px.

Return Value: It returns a picture.

Here is an example:

import numpy as np  
import cv2  
img = cv2.imread(r'C:UsersMirzadog.jpeg', 1)  
#defining points for polylines  
pts = np.array([[100,50],[200,300],[700,200],[500,100]], np.int32)  
# pts = pts.reshape((-1,1,2))  
cv2.polylines(img, [pts], True, (0,255,255), 3)  
cv2.imshow('image',img)  
cv2.waitKey(0)  
cv2.destroyAllWindows() 

Drawing Polylines

We are able to draw the polylines using the polylines() method on the image. And these might be used to attract polygonal curves on the image. The syntax is given below:

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

Parameters:

img – It represents a picture.
arr -represents the coordinates of vertices into an array of shape nx1x2 where n is variety of vertices and it must be of type int32.
is_Closed – It’s a flag that indicates whether the drawn polylines are closed or not.
color – Color of polylines. We are able to pass a tuple For in BGR,  eg: (255, 0, 0) for blue color. 
thickness – It represents the Thickness of the polyline’s edges.

Here is an example:

import numpy as np  
import cv2  
img = cv2.imread(r'C:UsersMirzadog.jpeg', 1)  
#defining points for polylines  
pts = np.array([[100,50],[200,300],[700,200],[500,100]], np.int32)  
# pts = pts.reshape((-1,1,2))  
cv2.polylines(img, [pts], True, (0,255,255), 3)  
cv2.imshow('image',img)  
cv2.waitKey(0)  
cv2.destroyAllWindows() 

Write text on a picture

We are able to write text on the image by utilizing the putText() method. The syntax is given below.

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

Parameters:
img: It represents the input image on which now we have to put in writing text
text: The text which we would like to put in writing on the image.
org: It denotes the Bottom-left corner of the text string on the image. So it’s used to set the placement of text on the image
font: the font of text. Here is the list of supported fonts.
fontScale: The dimensions of the font by which you’ll be able to increase or decrease size
color: Represents the colour. We are able to pass a tuple For in BGR,  eg: (255, 0, 0) for blue color. 

Here is an example:

import numpy as np  
import cv2  
font = cv2.FONT_HERSHEY_SIMPLEX  
mg = cv2.imread(r'C:UsersMirzadog.jpeg', 1)  
cv2.putText(img,'Dog',(10,500), font, 1,(255,255,255),2)  
#Display the image  
cv2.imshow("image",img)  
cv2.waitKey(0) 

OpenCV Blob Detection

Blob stands for Binary Large Object where the term “Large” focuses on the article of a selected size, and that other “small” binary objects are frequently regarded as noise.

In simpler terms, a Blob is a bunch of connected pixels which we will find in a picture and all of those pixels have some common property. Within the image below, the colored connected regions are blobs, and the goal of blob detection is to discover and mark these regions( marked by red circle).

Using OpenCV’s SimpleBlobDetector method, we will  easily find blobs in our images.But how does this method work?Allow us to see this intimately:

  1. Thresholding :First the algorithm converts the source images to several binary images by applying thresholding with various thresholds.We define two threshold values,viz- minThreshold (inclusive) to maxThreshold (exclusive)  and begin from threshold value equal to minThreshold.Then it’s incremented by thresholdStep until we reach maxThreshold,so the primary threshold is minThreshold, the second is minThreshold + thresholdStep and so forth.
  2. Grouping : In each binary image, now we have a curve joining all the continual points (along the boundary), having the identical color or intensity.
  3. Merging  : The centers of the binary blobs within the binary images are computed, and  blobs positioned closer than minDistBetweenBlobs(minimum distance between two blobs) are merged.
  4. Center & Radius Calculation :  The centers and radii of the brand new merged blobs are computed and returned.

This class can perform several filtrations of returned blobs by setting filterBy* to True to activate corresponding filtration. Available filtrations are as following:

  • By color. We define a parameter blobColor to filter the blobs of colors we’re fascinated with. Set blobColor equal to zero to extract dark blobs and to extract light blobs,set it to 255. This filter compares the intensity of a binary image at the middle of a blob to blobColor and filters accordingly.
  • By area. By utilizing this filter the extracted blobs have an area between minArea (inclusive) and maxArea (exclusive).
  • By circularity. By utilizing this filter the extracted blobs have circularity between minCircularity (inclusive) and maxCircularity (exclusive).
  • By ratio of the minimum inertia to maximum inertia.By utilizing this filter the extracted blobs have this ratio between minInertiaRatio (inclusive) and maxInertiaRatio (exclusive).
  • By convexity.By utilizing this filter the extracted blobs have convexity (area / area of blob convex hull) between minConvexity (inclusive) and maxConvexity (exclusive).

By default, the values of  these parameters are tuned to extract dark circular blobs.

Here is an example of the best way to use easy SimpleBlobDetector()

import cv2  
import numpy as np;  
  
img = cv2.imread(r"pic1.jpeg", cv2.IMREAD_GRAYSCALE)  
# Arrange the detector with default parameters.  
detector = cv2.SimpleBlobDetector()  
  
# Detecting blobs.  
keypoints = detector.detect(img)  
# Draw detected blobs as red circles.  
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the scale of the circle corresponds to the scale of blob  
im_with_keypoints = cv2.drawKeypoints(img, keypoints, np.array([]), (0, 0, 255),  
                                      cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)  
# Show keypoints  
cv2.imshow("Keypoints", im_with_keypoints)  
cv2.waitKey(0)  

Now here is an example by which we use the filters mentioned above:

import cv2
import numpy as np;

# Read image
im = cv2.imread("blob.jpg")

# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Change thresholds
params.minThreshold = 10
params.maxThreshold = 200


# Filter by Area.
params.filterByArea = True
params.minArea = 1500

# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1

# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87

# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.01

# Create a detector with the parameters
detector = cv2.SimpleBlobDetector(params)


# Detect blobs.
keypoints = detector.detect(im)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the scale of the circle corresponds to the scale of blob

im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show blobs
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)

Canny Edge Detection

Edge detection is a picture processing technique used for locating the boundaries of objects inside images. Here we’ll use a well-liked edge detection algorithm Canny Edge Detection, developed by John F. Canny. In OpenCV, now we have Canny() method to implement this algorithm. Here is the syntax:

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

Parameters –

img: input image whose edges we would like to detect.
minVal: Minimum intensity gradient (required)
maxVal: Maximum intensity gradient (required)
L2gradient: is a flag with default value =False, indicating the default L1 norm is sufficient to calculate the image gradient magnitude, if its is about as True a more accurate L2 norm is used to calculate the image gradient magnitude but it surely is computationally costlier.
aperture: aperture size for the Sobel operator.

As we will see now we have two threshold values, minVal and maxVal. Any edges with intensity gradient greater than maxVal are sure to be edges.also those edges with intensity gradient lower than minVal are sure to be non-edges and are discarded. The perimeters which lie between these two thresholds are classified edges or non-edges based on their connectivity with the ‘sure edges’. In the event that they are connected to “sure-edge” pixels, they’re considered to be a part of edges. Otherwise, also they are discarded as non-edges.

Here is an example:

import cv2
img = cv2.imread('dog.jpg')
edges = cv2.Canny(img,200,300,True)
cv2.imshow("Edge Detected Image", edges)  
cv2.imshow("Original Image", img)  
cv2.waitKey(0)  # waits until a key's pressed  
cv2.destroyAllWindows()  # destroys the window showing image

Now we may also do that in real-time, here is how:

# import libraries of python OpenCV    
import cv2  
  
# import Numpy by alias name np  
import numpy as np  
  
# capture frames from a camera   
cap = cv2.VideoCapture(0)  
  
# loop runs if capturing has been initialized   
while (1):  
  
    # reads frames from a camera   
    ret, frame = cap.read()  
  
    # Display an original image   
    cv2.imshow('Original', frame)  
  
    # discovers edges within the input image image and   
    # marks them within the output map edges   
    edges = cv2.Canny(frame, 100, 200,True)  
  
    # Display edges in a frame   
    cv2.imshow('Edges', edges)  
  
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  
# Close the window   
cap.release()  
  
# De-allocate any associated memory usage   
cv2.destroyAllWindows()  

OpenCV Image Smoothing

Image smoothing is a picture processing technique used for removing the noise in a picture.Blurring(smoothing) removes low-intensity edges and can also be useful in hiding the small print; for instance, blurring is required in lots of cases, akin to hiding any confidential information in a picture.OpenCV provides mainly the next style of blurring techniques.

Listed below are a number of of the methods that we’re going to use for smoothing a picture:

  • OpenCV averaging
  • OpenCV median Blur
  • OpenCV Gaussian Blur
  • OpenCV Bilateral Filter

OpenCV averaging

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

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

Parameters:

src: It’s the image which is to be blurred.
ksize: A tuple representing the blurring kernel size.
anchor: It’s a variable of type integer representing anchor point and it’s default value Point is (-1, -1) which implies that the anchor is on the kernel center.
borderType: It represents the style of border for use for the output.

Here is an example:

import cv2
img = cv2.imread('dog.jpg')
cv2.imshow('Original Image',img)  
cv2.imshow('cv2.blur output', cv2.blur(img, (3,3)))  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

OpenCV median Blur 

In this system, the median of all of the pixels under the kernel window is computed and the central pixel is replaced with this median value. It has one advantage over the Gaussian and box filters, that being the filtered value for the central element is at all times replaced by some pixel value within the image which will not be the case in case of either Gaussian or box filters. OpenCV provides a function medianBlur() that might be used to simply implement this sort of smoothing. Here is the syntax:

cv2.medianBlur(src, dst, ksize)  

Parameters:

src- It represents the source (input image).
dst – It represents the destination (output image).
ksize – It represents the scale of the kernel.

Consider the next example:

import cv2
img = cv2.imread('dog.jpg')
cv2.imshow('Original Image',img)  
cv2.imshow('cv2.medianBlur output', cv2.medianBlur(img,5))  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

OpenCV Gaussian Blur

In this system, a Gaussian function(kernel) as an alternative of a box filter to blur the image. The width and height of the kernel must be specified and so they must be positive and odd. We also should specify the usual deviation within the directions X and Y and are represented by sigmaX and sigmaY respectively. If each sigmaX and sigmaY are given as zeros, they’re calculated from the kernel size and if we only specify sigmaX, sigmaY is about to the identical value. Gaussian blurring is extremely effective when removing Gaussian noise from a picture. In OpenCV now we have a function GaussianBlur() to implement this system easily. Here is the syntax:

GaussianBlur(src, dst, ksize, sigmaX,sigmaY)

Parameters:

src − Input image which is to be blurred
dst − output image of the identical size and sort as src.
ksize − A Size object representing the scale of the kernel.
sigmaX − A variable of the sort double representing the Gaussian kernel standard deviation in X direction.
sigmaY − A variable of the sort double representing the Gaussian kernel standard deviation in Y direction.

Here is an example:

import cv2
img = cv2.imread('dog.jpg')
cv2.imshow('Original Image',img)  
cv2.imshow('cv2.GaussianBlur output', cv2.GaussianBlur(img, (5, 5), cv2.BORDER_DEFAULT))     
cv2.waitKey(0)  
cv2.destroyAllWindows() 

OpenCV Bilateral Filter

This approach to noise removal is extremely effective but is slower in comparison with other filters. The Gaussian filter blurred the perimeters too and that will not be what we would like, but this filter makes sure that only those pixels with similar intensities to the central pixel are considered for blurring, thus preserving the perimeters since pixels at edges may have large intensity variation. In OpenCV now we have cv.bilateralFilter() method that may implement this filter. Here is the syntax:

cv2.bilateralFilter(src, dst, d, sigmaColor,sigmaSpace, borderType)  

Parameters:
src Source 8-bit or floating-point, 1-channel or 3-channel image.
dst Destination image of the identical size and sort as src .
d Diameter of every pixel neighborhood that’s used during filtering. Whether it is non-positive, it’s computed from sigmaSpace.
sigmaColor Filter sigma in the colour space. A bigger value of the parameter signifies that farther colours throughout the pixel neighborhood (see sigmaSpace) can be mixed together, leading to larger areas of semi-equal color.
sigmaSpace Filter sigma within the coordinate space. A bigger value of the parameter signifies that farther pixels will influence one another so long as their colours are close enough (see sigmaColor ). When d>0, it specifies the neighborhood size no matter sigmaSpace. Otherwise, d is proportional to sigmaSpace.
borderType border mode used to extrapolate pixels outside of the image, see the BorderTypes available here.

Here is an example:

import cv2
img = cv2.imread('dog.jpg')
cv2.imshow('Original Image',img)  
cv2.imshow('bilateral Image', cv2.bilateralFilter(img,9,75,75))  
cv2.waitKey(0)  
cv2.destroyAllWindows() 

OpenCV Image Threshold

Thresholding is a well-liked segmentation technique, used for separating an object regarded as a foreground from its background.In this system we assign pixel values in relation to the brink value provided.This system of thresholding is completed on grayscale images,so initially, the image must be converted in grayscale color space.Here we’ll discuss two different approaches taken when performing thresholding on a picture:

  • Easy Thresholding
  • Adaptive Thresholding

Easy Thresholding:

On this basic Thresholding technique, for each pixel, the identical threshold value is applied. If the pixel value is smaller than the brink, it is about to a certain value(normally zero) , otherwise, it is about to a different value(normally maximum value) .There are numerous variations of this system as shown below.

In OpenCV, we use cv2.threshold function to implement it. Here is the syntax:

cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique)

Parameters:

-> source: Input Image array (have to be in Grayscale).
-> thresholdValue: Value of Threshold below and above which pixel values will change accordingly.
-> maxVal: Maximum value that might be assigned to a pixel.
-> thresholdingTechnique: The style of thresholding to be applied.Listed below are various kinds of thresholding we will use

cv2.THRESH_BINARY: If  the pixel intensity is larger than the brink, the pixel value is about to 255(white), else it is about to 0 (black).
cv2.THRESH_BINARY_INV: Inverted or Opposite case of cv2.THRESH_BINARY.If  the pixel intensity is larger than the brink, the pixel value is about to 0(black), else it is about to 255 (white).
cv.THRESH_TRUNC: If  the pixel intensity is larger than the brink,the pixel values are set to be the identical as the brink. All other values remain the identical.
cv.THRESH_TOZERO: Pixel intensity is about to 0, for all of the pixels intensity, lower than the brink value.All other pixel values remain same
cv.THRESH_TOZERO_INV: Inverted or Opposite case of cv2.THRESH_TOZERO.

Here is an example:

import cv2  
import numpy as np  
  
# path to input image is specified and   
# image is loaded with imread command  
image = cv2.imread('gl.png')  
  

# to convert the image in grayscale  
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
  
threshold=160
ret, thresh1 = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY) 
ret, thresh2 = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY_INV) 
ret, thresh3 = cv2.threshold(img, threshold, 255, cv2.THRESH_TRUNC) 
ret, thresh4 = cv2.threshold(img, threshold, 255, cv2.THRESH_TOZERO) 
ret, thresh5 = cv2.threshold(img, threshold, 255, cv2.THRESH_TOZERO_INV) 
  
# the window showing output images 
# with the corresponding thresholding  
# techniques applied to the input images 
cv2.imshow('Original',image)
cv2.imshow('Binary Threshold', thresh1) 
cv2.imshow('Binary Threshold Inverted', thresh2) 
cv2.imshow('Truncated Threshold', thresh3) 
cv2.imshow('Zero Threshold', thresh4) 
cv2.imshow('Zero Inverted', thresh5) 
    
# De-allocate any associated memory usage   
cv2.waitKey(0)
cv2.destroyAllWindows() 

Adaptive Thresholding:

In easy thresholding, the brink value was global which implies it was same for all of the pixels within the image. But this may occasionally not be the most effective approach for thresholding as different image sections can have different lightings. Thus we’d like Adaptive thresholding, which is the strategy where the brink value is calculated for smaller regions and due to this fact, there can be different threshold values for various regions. In OpenCV now we have adaptiveThreshold() function to implement the sort of thresholding. Here is the syntax of this function:

adaptiveThreshold(src, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C)

This method accepts the next parameters −

src − An object of the category Mat representing the source (input) image.
dst − An object of the category Mat representing the destination (output) image.
maxValue − A variable of double type representing the worth that’s to be given if pixel value is greater than the brink value.
adaptiveMethod − A variable of integer the sort representing the adaptive method for use. This can be either of the next two values:
cv.ADAPTIVE_THRESH_MEAN_C: The brink value is the mean of the neighbourhood area minus the constant C.
cv.ADAPTIVE_THRESH_GAUSSIAN_C: The brink value is a gaussian-weighted sum of the neighbourhood values minus the constant C.

thresholdType − A variable of integer type representing the style of threshold for use.
blockSize − A variable of the integer type representing size of the pixelneighborhood used to calculate the brink value.
C − A variable of double type representing the constant utilized in the each methods (subtracted from the mean or weighted mean).

Here is an example:

import cv2  
import numpy as np  
  
# path to input image is specified and   
# image is loaded with imread command  
image = cv2.imread('lamp.jpg')  
  

# to convert the image in grayscale  
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

ret, th1 = cv2.threshold(img,160, 255, cv2.THRESH_BINARY) 
  
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,
            cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
            cv2.THRESH_BINARY,11,2)

cv2.imshow('Original',image)
cv2.imshow('Binary Threshold', th1) 
cv2.imshow('Adaptive Threshold', th2) 
cv2.imshow('Gaussain Adaptive Threshold', th3) 
    
# De-allocate any associated memory usage   
cv2.waitKey(0)
cv2.destroyAllWindows() 
OpenCV Tutorial

OpenCV Contours

So what are contours? A Contour is a curve joining all the continual points having the identical color or intensity (along the boundary).So the counters are useful especially when we would like to seek out out a shape of some object or incase of object detection and recognition. Finding contours is like finding white object from black background, so remember, the article to be found must be white and background must be black. Thus, for higher accuracy, we should always use binary images and before finding contours, apply thresholding as we discussed within the last section.
In OpenCV, we use two functions, one to seek out contours and other to attract contours. We use findContours() function to seek out contours and drawCounter() to attract one. Here is the syntax of those functions.

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

This function returns three objects:

Img:The input image by which now we have to seek out contours
Contours: It comprises detected contours and  contour is stored as a vector of points
Hierarchy:Optional output vector, containing information concerning the image topology. It has as many elements because the variety of contours. For every i-th contour contours[i], the weather hierarchy[i][0] , hierarchy[i][1] , hierarchy[i][2] , and hierarchy[i][3] are set to 0-based indices in contours of the subsequent and former contours at the identical hierarchical level, the primary child contour and the parent contour, respectively. If for the contour i there are not any next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] can be negative.
Parameters of this function:

mode: Contour retrieval mode, see RetrievalModes
method:Contour approximation method, see ContourApproximationModes
offset : Optional offset by which each and every contour point is shifted. This is beneficial if the contours are extracted from the image ROI after which they must be analyzed in the entire image context.

Here is the syntax of drawCounter():

cv.drawContours(image, contours, contourIdx, color, thickness, lineType, hierarchy, maxLevel, offset)

Parameters

Image: Input image.
contours: All of the input contours. Each contour is stored as a degree vector.
contourIdx: Parameter indicating a contour to attract. Whether it is negative, all of the contours are drawn.
color: Color of the contours.
thickness: Thickness of lines the contours are drawn with. Whether it is negative (for instance, thickness=FILLED ), the contour interiors are drawn.
lineType: Line connectivity. See LineTypes
hierarchy: Optional details about hierarchy. It is barely needed if you should draw only among the contours (see maxLevel ).
maxLevel: Maximal level for drawn contours. Whether it is 0, only the desired contour is drawn. Whether it is 1, the function draws the contour(s) and all of the nested contours. Whether it is 2, the function draws the contours, all of the nested contours, all of the nested-to-nested contours, and so forth. This parameter is barely taken under consideration when there may be hierarchy available.
offset: Optional contour shift parameter. Shift all of the drawn contours by the desired offset=(dx,dy).

Here is an example using these functions:

import cv2 
import numpy as np 
  
# Let's load a straightforward image with 3 black squares 
image = cv2.imread('contor.png',1) 
# Find Canny edges 
edged = cv2.Canny(image, 30, 200) 
cv2.waitKey(0) 
  
# Finding Contours 
# Use a duplicate of the image e.g. edged.copy() 
# since findContours alters the image 
contours, hierarchy = cv2.findContours(edged,  
    cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 
cv2.imshow('Original', image) 
cv2.imshow('Canny Edges After Contouring', edged)  
  
cv2.drawContours(image, contours, -1, (0, 255, 0), 3) 
  
cv2.imshow('Contours', image) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 
OpenCV Tutorial

OpenCV Mouse Event

OpenCV also provides the functionality to manage and manage several types of mouse events and offers us the pliability to administer them. As we all know there might be several types of mouse events akin to double_click, left button click, right button click, etc. For managing these events, we’d like to design callback functions for every of those mouse click events while the window or frame is opened by OpenCV.The callback function gives us flexibility to implement what style of functionality you wish with a selected mouse click event.

Mouse as a Paint Brush

Using OpenCV, now we have an option to make use of the mouse as a paint brush or a drawing tool. At any time when any mouse event occurs on the window screen, it gives us the coordinates (x,y) for that exact mouse event. Now that now we have the coordinates of the purpose we’re fascinated with, we will draw anything we desire, be it a circle or a rectangle or a straightforward line. First allow us to see the available mouse events, to get the list of all these events, run the code below:

import cv2  
mouse_events = [j for j in dir(cv2) if 'EVENT' in j]  
print(mouse_events)  

Output:

[‘EVENT_FLAG_ALTKEY’, ‘EVENT_FLAG_CTRLKEY’, ‘EVENT_FLAG_LBUTTON’, ‘EVENT_FLAG_MBUTTON’, ‘EVENT_FLAG_RBUTTON’, ‘EVENT_FLAG_SHIFTKEY’, ‘EVENT_LBUTTONDBLCLK’, ‘EVENT_LBUTTONDOWN’, ‘EVENT_LBUTTONUP’, ‘EVENT_MBUTTONDBLCLK’, ‘EVENT_MBUTTONDOWN’, ‘EVENT_MBUTTONUP’, ‘EVENT_MOUSEHWHEEL’, ‘EVENT_MOUSEMOVE’, ‘EVENT_MOUSEWHEEL’, ‘EVENT_RBUTTONDBLCLK’, ‘EVENT_RBUTTONDOWN’, ‘EVENT_RBUTTONUP’]

Draw Circle

To attract anything on the window screen, we first must create a mouse callback function by utilizing the cv2.setMouseCallback() function. It has a selected format that continues to be the identical in all places. Our mouse callback function is facilitated by drawing a circle using double-click. Here is the code:

import cv2  
import numpy as np  
# Creating mouse callback function  
def draw_circle(event,x,y,flags,param):  
    if(event == cv2.EVENT_LBUTTONDBLCLK):  
            cv2.circle(img,(x,y),50,(123,125, 200),-1)  
# Making a black image, a window and bind the function to window  
img = np.zeros((512,512,3), np.uint8)  
cv2.namedWindow('image')  
cv2.setMouseCallback('image',draw_circle)  
while(1):  
    cv2.imshow('image',img)  
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  
cv2.destroyAllWindows()  
OpenCV Tutorial

OpenCV Template Matching

Template Matching is a technique used for locating the placement of a template image in a bigger image. In OpenCV, we use a function cv.matchTemplate() for template matching. It simply slides the template image over the larger input image (as in 2D convolution) and compares the template image with the patch of input image under the template image. It returns a grayscale image, where each pixel denotes how much does the neighbourhood of that pixel match with the template. There are several comparison methods that might be implemented in OpenCV. 

If input image is of size (WxH) and template image is of size (wxh), output image may have a size of (W-w+1, H-h+1).Upon getting results, the most effective matches might be found as global minimums (when TM_SQDIFF was used) or maximums (when TM_CCORR or TM_CCOEFF was used) using the minMaxLoc function. Take it because the top-left corner of the rectangle and take (w,h) as width and height of the rectangle. That rectangle is your region of template.

Here is the syntax of  cv.matchTemplate():

cv.matchTemplate(image, templ, method,mask)

Parameters:

image: Image where the search is running. It have to be 8-bit or 32-bit floating-point.
templ: Searched template. It have to be not greater than the source image and have the identical data type.
result Map of comparison results. It have to be single-channel 32-bit floating-point. If image is W×H and templ is w×h , then result’s (W−w+1)×(H−h+1) .
method: Parameter specifying the comparison method, see TemplateMatchModes
mask: Optional

Here is an example by which we take this image because the template image:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('headphone1.jpeg',0)
img2 = img.copy()
template = cv.imread('logo1.jpeg',0)
w, h = template.shape[::-1]
# All of the 6 methods for comparison in an inventory
# Apply template Matching
res = cv.matchTemplate(img,template,eval('cv.TM_CCOEFF'))
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
# If the strategy is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv.rectangle(img,top_left, bottom_right, 255, 2)
plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img,cmap = 'gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.show()
OpenCV Tutorial

Template Matching with Multiple Objects

Within the above example, we looked for template images that occurred just once within the image. Suppose a selected object occurs multiple times in a selected image. On this scenario, we’ll use the thresholding as cv2.minMaxLoc() just gives the placement of 1 template image and it won’t give all locations of the template images. Consider the next example.

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img2=cv.imread('headohone2.jpeg',1)
img_gray = cv.imread('headohone2.jpeg',0)
template = cv.imread('logo1.jpeg',0)
w, h = template.shape[::-1]
res = cv.matchTemplate(img_gray,template,eval('cv.TM_CCOEFF_NORMED'))
print(res)
threshold = 0.52
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv.rectangle(img2, pt, (pt[0] + w, pt[1] + h), (255,0,0), 1)
plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img2,cmap = 'gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.show()
OpenCV Tutorial

OpenCV Video Capture

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

Capture Video from Camera

Often, now we have to capture a live stream with a camera. Using OpenCV’s quite simple interface, we will easily do it. Here is an easy task to start. On this task we’ll capture a video from the camera ( in-built webcam of my laptop) and display it as a grayscale video.

In OpenCV we’d like to create a VideoCapture object to capture a video. We pass either the device index or the name of a video file as its arguments. Device index is just the number to specify the camera in case now we have multiple webcams available. Normally one has only a single camera connected (as in my case), so simply pass 0.After this we begin to capture each frame using a loop and process it accordingly. At the tip, we just break from the loop and release the capture.

import numpy as np
import cv2

capture = cv2.VideoCapture(0)

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

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

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

# When the whole lot done, release the capture
capture.release()
cv2.destroyAllWindows()

capture.read() returns a bool (True/False) and the frame which webcam is currently reading. If the frame is read appropriately, it should be True. So you possibly can check the tip of the video by checking this return value.

Playing Video from file

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

import numpy as np
import cv2

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

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

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

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

cap.release()
cv2.destroyAllWindows()

Saving a Video

Saving a picture after processing it using OpenCV is kind of easy and we saw the best way to do it using cv2.imwrite() function. But for a video, after processing it frame-by-frame a bit more work is required to reserve it.

Here to avoid wasting a video we create a VideoWriter object along with VideoCapture Object. The syntax of VideoWriter is given below:

cv2.VideoWriter(filename,fourcc,fps,frameSize,isColor)

Parameters:

filename: The output file name (eg: bday.avi).
fourcc: specify the FourCC code. FourCC is a 4-byte code used to specify the video codec. The list of accessible codes might be present in fourcc.org. It’s platform dependent. Following codecs works effective for me.

In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG leads to high size video. X264 gives very small size video)
In Windows: DIVX (More to be tested and added)
In OSX : (I don’t have access to OSX. Can some one fill this?)
FourCC code is passed as cv2.VideoWriter_fourcc(‘M’,’J’,’P’,’G’) or cv2.VideoWriter_fourcc(*’MJPG) for MJPG.

fps: variety of frames per second (fps) 
frameSize: size of frame. 
isColor: It’s a flag value. Whether it is True, encoders expect a color frame, otherwise it really works with grayscale frames.

Here’s a code that captures frames from a Camera, flip every one in a vertical direction and reserve it.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

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

# Release the whole lot if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

Face Detection Using OpenCV

Using OpenCV, complex tasks akin to face detection becomes easy to implement and since pre-trained models which might be able to detecting faces, noses, and eyes are included within the OpenCV package, we don’t must train any classifier. Here is an article on Face detection using Viola-Jones algorithm that explains how we will detect faces using OpenCV.You may also undergo the concept of cascading classifier in this text that can also be utilized in our next section i.e. automotive detection using OpenCV

https://youtu.be/6myMFJl8CbA

Automobile detection with OpenCV

You could have seen within the article Face detection using Face Recognition Using Python and OpenCV Viola-Jones algorithm that we used pre-trained models to detect faces and eyes. Similarly, we even have a pre-trained model that may detect cars. All now we have to do is to make use of this model to erect cars after which mark the cars with rectangles.

# OpenCV Python program to detect cars in video frame 
# import libraries of python OpenCV  
import cv2 
  
# capture frames from a video 
cap = cv2.VideoCapture('video.avi') 
  
# Trained XML classifiers describes some features of some object we would like to detect 
car_cascade = cv2.CascadeClassifier('cars.xml') 
  
# loop runs if capturing has been initialized. 
while True: 
    # reads frames from a video 
    ret, frames = cap.read() 
      
    # convert to gray scale of every frames 
    gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY) 
      
  
    # Detects cars of various sizes within the input image 
    cars = car_cascade.detectMultiScale(gray, 1.1, 1) 
      
    # To attract a rectangle in each cars 
    for (x,y,w,h) in cars: 
        cv2.rectangle(frames,(x,y),(x+w,y+h),(0,0,255),2) 
  
   # Display frames in a window  
   cv2.imshow('video2', frames) 
      
    # Wait for Esc key to stop 
    if cv2.waitKey(33) == 27: 
        break
  
# De-allocate any associated memory usage 
cv2.destroyAllWindows() 

Face Recognition Using OpenCV

Face recognition, because the names suggest includes detecting faces after which label them with the name of the person. It is a little more complex than the last two examples. Here now we have to make use of some libraries outside of OpenCV. I might highly recommend going through the article Face Recognition Using Python and OpenCV to know face recognition works and the best way to implement it using OpenCV.

FAQs of OpenCV in Python

Q: What’s the usage of OpenCV in Python? 

A: OpenCV-Python uses Numpy, which is taken into account an effectively optimized library for numerical operations with a MATLAB-style syntax. Each of the OpenCV array structures is turned into in addition to from Numpy arrays. This also helps in incorporating other libraries that use Numpy, which include SciPy and Matplotlib.

Q: How do I install OpenCV 3.0 and Python 3.4+ on Ubuntu?

A: To put in OpenCV 3.0 and Python 3.4+ on Ubuntu, it is advisable to follow the steps mentioned below:

  • Start with installing prerequisites
  • Now Setup Python (Part 1) 
  • Setup Python (Part 2) 
  • Now it is advisable to construct and install OpenCV 3.0 with Python 3.4+ bindings
  • Sym-link OpenCV 3.0
  • The last step includes testing out the OpenCV 3.0 and Python 3.4+ install.

Q: How do I start learning OpenCV? 

A: To begin learning OpenCV, you possibly can consult with the tutorials offered by Great Learning. You won’t only learn the fundamentals and likewise get idea of the general OpenCV.

Q: What’s the complete type of OpenCV? 

A: The complete form for OpenCV is Open Source Computer Vision Library.

Q: Why is OpenCV used? 

A: OpenCV is an unlimited open-source library that’s used for machine learning, computer vision, and image processing. At present, it plays a key role in real-time. Using OpenCV helps in processing images in addition to videos to categorise faces, objects, and even handwriting of humans.

Q: Is OpenCV easy? 

A: Earlier OpenCV was not one in every of the best things to learn. Nevertheless, as of late it has been simplified. You’ll be able to undergo the easy-to-learn tutorials to know OpenCV.

Q: Is OpenCV higher in Python or C++? 

A: It totally will depend on the stage of a project. For those who are prototyping, Python is more useful. Whether it is for the aim of production, C++ is healthier. Nevertheless, you furthermore mght must know that Python is slower than C++.

Q: Is OpenCV value learning? 

A: Learning OpenCV is actually value it, and you need to start learning OpenCV with Python. This programming language is simpler to learn and faster to prototype the Computer Vision algorithms.

Q: What should I learn before OpenCV?

A: It’s a library; due to this fact you first must know the best way to use a library. The subsequent thing is learning the basics of image processing. You furthermore mght must have in-depth knowledge of classes and inheritance in C++.
Q: How do I do know if Opencv is installed?

A: First fan the flames of your Python and follow the commands mentioned below:

  • Importing cv2 # import the opencv library, and
  • cv2. __version__ #. It will assist in printing the version of your opencv3.

This brings us to the tip of this text on OpenCV Tutorial where we learned about OpenCV. We hope that you just found this useful and at the moment are higher equipped in OpenCV.

LEAVE A REPLY

Please enter your comment!
Please enter your name here