“It seems probable that once the machine thinking method had started, it would not take long to outstrip our feeble powers… They would be able to converse with each other to sharpen their wits. At some stage, therefore, we should have to expect the machines to take control.” - Alan Turing
In the last blog, we featured OpenCV along with the functions it provides and the areas of application. OpenCV is a Computer Vision library, written in two languages, i.e., C++ & Python to deal with both images and videos.
In addition, it provides real-time support using the camera or web cameras in many applications as can be seen in Yolo, Face Recognition, and many more.
Some of the common functions available by OpenCV are,
Read, write and display the image,
Different color formats for image i.e. BGR, RGB, grayscale, HSV, etc,
Data Augmentation i.e. rotation, shrinking, resize, magnification, etc,
Image Thresholding,
Edge Detection, and
Contour Detection will help in detecting different shapes from the image.
Common Areas of application are,
Image Processing,
Face Recognition,
Object Detection,
Gesture Recognition,
Can create a high-resolution image from different images,
Track moving objects,
Track 3D models,
Track camera movements,
Track human motions, and
Motion Detection.
No doubt there are other languages available for computer vision but none of them compete with OpenCV, as its an open-source and also there is no restriction on its use as you can use it without a license while some language requires a license to use like Matlab. To use Matlab on a large scale you have to purchase a license that makes it less economical.
It also fits best in terms of execution as OpenCV is much faster as its speed can reach up to 80 in some cases. So in this way, anyone who wants to explore the field of computer vision or image processing can easily use Open CV whereas Matlab is more feasible for the researchers who can afford its license.
The core concept is to detect motion in frame i.e. if any change will occur in the frame using OpenCV. It can be done for either recorded video or live streaming i.e web-camera or any other camera mounted.
In this article, we will detect motion from the web camera using OpenCV and a bounding box will be rendered to the motion detected as if any new object will introduce in the frame then a bounding box will appear surrounding the object.
In videos, multiple frames are stack together to form video. For motion detection, we calculate the difference between two continuous frames and if it’s higher than the set threshold, it means motion detection has been observed there.
Import the required libraries to be used i.e. cv2 (OpenCV), NumPy. As the video is comprised of images, and OpenCV will be used to preprocess images i.e. starting the web camera, to read video as a stack of frames, deal with the color formatting of the frame, find the gaussian blur of the image, calculation of the difference between frames, image thresholding, dilation, contour detection, and bounding boxes over the motion detected.
Code Snippet: 1
To perform motion detection, we will compare every frame with the first frame that will be termed as a static frame. So every time each frame will be compared with the static frame and based on the difference motion in the frames will be detected.
Initially, the static frame will be None and will initialize with the first frame once the web camera opened.
Code snippet: 2
To capture a live stream from the web camera using OpenCV the first step is to create a VideoCapture object by passing 0 as an argument to signify the camera to use. Set the window size to a particular dimension.
Next, we will run while loop, to capture every frame from the web camera and run until the camera will be closed using waitkey.
Code Snippet: 3
Now read the frames using object initialized above and further convert it into the grayscale format.
Code Snippet: 4
Now the next step is to remove the noise from each frame basically remove high frequencies from the images using GaussianBlur function by CV2. Pass the height and width of the low pass filter kernel as an argument along with the standard deviation in both directions.
Also, pass the grayscale image as input to the function. After blurring the image, motion in the consecutive frames will be detected easily.
Code Snippet: 5
Now its time to update the static frame with the first frame from the live streaming as no changes in the initial level. This frame will be used as a reference to calculate the absolute difference between each frame.
Code snippet: 6
Calculate the absolute difference between the static frame and each frame from the live streaming. Hence the difference is greater than 30, apply thresholding to the grayscale so as to where ever the motion will be detected that the region will be white and later dilate that thresholded image.
Code Snippet: 7
Now the region of motion has been converted to the white using thresholding, which can be treated as contours, so use find contours to detect coordinates of the moving objects.
Apart from the motion detection, once we will get the coordinates of the moving objects to plot the rectangle (bounding box) on them and put the text “Motion Detected”.
Code Snippet: 8, Source
Now the motion has been detected in the video, so the next step is for live streaming with results of motion detection.
Put the code to release the camera and to destroy all the windows.
Code Snippet: 9
OpenCV a computer vision library supports many functions and has many applications including facial recognition, object detection, tracking human motions, tracking objects, camera movements, motion detection, etc. In comparison to the other tools, easily accessible to everyone as its open-source and its speed is another factor.
Many companies are using OpenCV due to its real-time application as it can be integrated with different languages and some of the hardware devices i.e. raspberry pi and some others.
Out of its vast applications, this blog has been framed to cover one of that i.e. Motion Detection based on background subtraction from the live streaming, where each new frame will be keep subtracted from the very first frame after image smoothening and if any region has a value less than some threshold, that signifies the motion detection in the frames. Later using contours, plot the bounding boxes on the region where motion has been detected and put the text over there.
5 Factors Influencing Consumer Behavior
READ MOREElasticity of Demand and its Types
READ MOREAn Overview of Descriptive Analysis
READ MOREWhat is PESTLE Analysis? Everything you need to know about it
READ MOREWhat is Managerial Economics? Definition, Types, Nature, Principles, and Scope
READ MORE5 Factors Affecting the Price Elasticity of Demand (PED)
READ MORE6 Major Branches of Artificial Intelligence (AI)
READ MOREScope of Managerial Economics
READ MOREDijkstra’s Algorithm: The Shortest Path Algorithm
READ MOREDifferent Types of Research Methods
READ MORE
Latest Comments
Ripul Agrawal
Dec 09, 2020For any queries, please reach out to me at ripulagrawal98@gmail.com
himanshuy69486
Oct 21, 2021how to get this code
long33291
Feb 01, 2022Waow Awesome information here ,this is unbelievable, it is my first time to be carried away by such a perfect profile article, I will immediately contact you directly and also to take the advantage to share some latest information about how to <a href="https://bexuniversal.com/buy-fake-bank-statement-buy-real-bank-statement/">buy-fake-bank-statement-buy-real-bank-statement</a> ,I will also take the advantage to ask for your permission to join our <a href="http://t.me/bexuniversal/">TELEGRAM GROUP</a>
long33291
Feb 01, 2022Wonderful piece you got here , I feel quite happy after I read your Article because I found out that we still have intelligent informative researchers who can still take time out of their busy schedule to put all this together I will instantly grab your rss feed to stay informed of any updates you make and as well take the advantage to share some latest information about.<a href="https://travelquicks.com/services/">panic and axienty treatment</a> also weight lose medications <a href="https://travelquicks.com/2022/01/29/buy-duromine-online-paypal/">buy duromine online paypal</a> <a href="https://travelquicks.com/2022/01/29/buy-cheap-anavar-online/">Buy cheap Anavar Online</a> <a href="https:https://travelquicks.com/sample-page-buy-xanax-3mg-online/">Buy Xanax 3mg online</a> <a href="https://travelquicks.com/projects-buy-diazepam-valium-online/">Buy Diazepam Valium online</a>
akunnadozie
Feb 10, 2022I used to be a very poor man who has always not found luck when it comes to playing the lottery. I have been playing the lottery since I was 21 years and now I am 52 meaning I have been playing the lottery for 31 years. The biggest amount I have ever won in my life was 400 bucks. But one day my story turns to history after I find the man named CHIEF UZO who is the best when it comes to winning the lottery. This man is a very strong spiritual wizard who gives out the numbers that can never fail. After all my years of laboring and struggling to win the lottery, I finally won ( $270,000) which was my first big win and I have been winning even bigger ever since then. All my cars, houses, lands, and all my wealth have been money from the lotto. I am one with the majority. Chief Uzo of Igbondu is his name, Email: uzoigbonduspiritualhome@gmail.com this is the only way to win the lottery big and the best way