# Motion estimation opencv python

Optical flow is the pattern of apparent motion of image objects between two consecutive frames caused by the movemement of object or camera. It is 2D vector field where each vector is a displacement vector showing the movement of points from first frame to second.

It shows a ball moving in 5 consecutive frames. The arrow shows its displacement vector. Optical flow has many applications in areas like :. Consider a pixel in first frame Check a new dimension, time, is added here. Earlier we were working with images only, so no need of time.

It moves by distance in next frame taken after time. So since those pixels are the same and intensity does not change, we can say. Then take taylor series approximation of right-hand side, remove common terms and divide by to get the following equation:. Above equation is called Optical Flow equation. In it, we can find andthey are image gradients.

Similarly is the gradient along time. But is unknown. We cannot solve this one equation with two unknown variables. So several methods are provided to solve this problem and one of them is Lucas-Kanade. We have seen an assumption before, that all the neighbouring pixels will have similar motion.

Lucas-Kanade method takes a 3x3 patch around the point. So all the 9 points have the same motion. We can find for these 9 points. So now our problem becomes solving 9 equations with two unknown variables which is over-determined. A better solution is obtained with least square fit method.

Below is the final solution which is two equation-two unknown problem and solve to get the solution. Check similarity of inverse matrix with Harris corner detector. It denotes that corners are better points to be tracked. So from user point of view, idea is simple, we give some points to track, we receive the optical flow vectors of those points.

But again there are some problems. Until now, we were dealing with small motions. So it fails when there is large motion. So again we go for pyramids. When we go up in the pyramid, small motions are removed and large motions becomes small motions.But I muttered them to myself in an exasperated sigh of disgust as I closed the door to my refrigerator.

My brain was fried, practically leaking out my ears like half cooked scrambled eggs. But I had a feeling he was the culprit. He is my only ex- friend who drinks IPAs. But I take my beer seriously. This is the first post in a two part series on building a motion detection and tracking system for home surveillance. The remainder of this article will detail how to build a basic motion detection and tracking system for home surveillance using computer vision techniques. Background subtraction is critical in many computer vision applications.

We use it to count the number of cars passing through a toll booth. We use it to count the number of people walking in and out of a store. Some are very simple. And others are very complicated.

## Basic motion detection and tracking with Python and OpenCV

The two primary methods are forms of Gaussian Mixture Model-based foreground and background segmentation:. And in newer versions of OpenCV we have Bayesian probability based foreground and background segmentation, implemented from Godbehere et al. We can find this implementation in the cv2. So why is this so important? Therefore, if we can model the background, we monitor it for substantial changes. Now obviously in the real-world this assumption can easily fail. Due to shadowing, reflections, lighting conditions, and any other possible change in the environment, our background can look quite different in various frames of a video.

And if the background appears to be different, it can throw our algorithms off. The methods I mentioned above, while very powerful, are also computationally expensive. Alright, are you ready to help me develop a home surveillance system to catch that beer stealing jackass? Lines import our necessary packages. If you do not already have imutils installed on your system, you can install it via pip: pip install imutils. It simply defines a path to a pre-recorded video file that we can detect motion in.

Obviously we are making a pretty big assumption here. A call to vs. If there is indeed activity in the room, we can update this string. Now we can start processing our frame and preparing it for motion analysis Lines This helps smooth out high frequency noise that could throw our motion detection algorithm off. As I mentioned above, we need to model the background of our image somehow.

The above frame satisfies the assumption that the first frame of the video is simply the static background — no motion is taking place. Computing the difference between two frames is a simple subtraction, where we take the absolute value of their corresponding pixel intensity differences Line 52 :.

This implies that larger frame deltas indicate that motion is taking place in the image. If the delta is less than 25we discard the pixel and set it to black i.

An example of our thresholded delta image can be seen below:. Again, note that the background of the image is black, whereas the foreground and where the motion is taking place is white. The remainder of this example simply wraps everything up. It looks down on the kitchen and living room, detecting motion as people move and walk around.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I have a set of images, and would like to recursively predict where a bunch of pixels will be in the next image. I am using Python, OpenCV, and believe Kalman filtering may be the way forward, but am struggling on the implementation.

For simplicity, the code below opens and image and extracts just one colour channel, in this case the red one. So far, I am using optical flow to determine the motion between images in X and Y for each pixel. The group of pixels I will look at and predict is not specified, but is not relevant for the example. It would just be a Numpy array of x,y values. I am not sure if I can explain this here; but I will have a shot.

Kalman filter is nothing but a prediction-measurement correction based loop. Make an assumption like constant velocity model.

T is your sampling time generally taken as the frame rate if used with cameras. You need to know the time difference of your images here. Later, you are going to correct this assumption with the next measurement load image3 here and obtain v1' from flow of image2 and image3. Also take x1' from image3.

If you want to use the exact filter, and use kalman gain and coveriance calculations, I'd say you need to check out the algorithmpage 4. Take R small if your images are accurate enough it is the sensor noise. If you check the opencv docthe algorithm might already be there for you to use. If you are not going to use a camera and opencv methods; I would suggest you to use MATLAB, just because it is easier to manipulate matrices there.

Learn more. Asked 6 years, 9 months ago. Active 6 years ago.The function cv::accumulate can be used, for example, to collect statistics of a scene background viewed by a still camera and for the further foreground-background segmentation.

### Introduction to Motion Estimation with Optical Flow

The function adds the input image src or its selected region, raised to a power of 2, to the accumulator dst :. The function calculates the weighted sum of the input image src and the accumulator dst so that dst becomes a running average of a frame sequence:.

That is, alpha regulates the update speed how fast the accumulator "forgets" about earlier images. The function supports multi-channel images. Each channel is processed independently. The operation takes advantage of the Fourier shift theorem for detecting the translational shift in the frequency domain. It can be used for fast image registration as well as motion estimation.

Calculates the cross-power spectrum of two supplied source arrays. See also accumulateSquareaccumulateProductaccumulateWeighted. Parameters src1 First input image, 1- or 3-channel, 8-bit or bit floating point. See also accumulateaccumulateSquareaccumulateWeighted. Parameters src Input image as 1- or 3-channel, 8-bit or bit floating point.

See also accumulateaccumulateSquareaccumulateProduct. This window is cached until the array size changes to speed up processing time. It is normalized to a maximum of 1 meaning there is a single peak and will be smaller when there are multiple peaks. Returns detected phase shift sub-pixel between the two arrays. Adds an image to the accumulator image. Adds the per-element product of two input images to the accumulator image.

Adds the square of a source image to the accumulator image. Updates a running average. This function computes a Hanning window coefficients in two dimensions. The function is used to detect translational shifts that occur between two images. Accumulator image with the same number of channels as input images, bit or bit floating-point.

Accumulator image with the same number of channels as input image, bit or bit floating-point. Point2d cv::phaseCorrelate.This is going to be a small section. During the last session on camera calibration, you have found the camera matrix, distortion coefficients etc. Given a pattern image, we can utilize the above information to calculate its pose, or how the object is situated in space, like how it is rotated, how it is displaced etc.

So, if we know how the object lies in the space, we can draw some 2D diagrams in it to simulate the 3D effect. X axis in blue color, Y axis in green color and Z axis in red color.

So in-effect, Z axis should feel like it is perpendicular to our chessboard plane. Then as in previous case, we create termination criteria, object points 3D points of corners in chessboard and axis points. Axis points are points in 3D space for drawing the axis. We draw axis of length 3 units will be in terms of chess square size since we calibrated based on that size.

So our X axis is drawn from 0,0,0 to 3,0,0so for Y axis. For Z axis, it is drawn from 0,0,0 to 0,0, Negative denotes it is drawn towards the camera. Now, as usual, we load each image. Search for 7x6 grid.

If found, we refine it with subcorner pixels. Then to calculate the rotation and translation, we use the function, cv2. Once we those transformation matrices, we use them to project our axis points to the image plane. In simple words, we find the points on image plane corresponding to each of 3,0,00,3,00,0,3 in 3D space. Once we get them, we draw lines from the first corner to each of these points using our draw function.

If you are interested in graphics, augmented reality etc, you can use OpenGL to render more complicated figures. OpenCV-Python Tutorials latest.The video stabilization module contains a set of functions and classes for global motion estimation between point clouds or between images. In the last case features are extracted and matched internally.

For the sake of convenience the motion estimation functions are wrapped into classes. Both the functions and the classes are available. Classes Enumerations Functions. Global Motion Estimation Video Stabilization. Detailed Description The video stabilization module contains a set of functions and classes for global motion estimation between point clouds or between images. Parameters size Subset size.

Note Works in-place and changes input point arrays. Parameters points0 Source set of 2D points 32F. Returns 3x3 2D transformation matrix 32F. See cv::videostab::MotionModel. See videostab::RansacParams. Parameters from Source frame index. Base class for global 2D motion estimation methods which take frames as input.

Describes a global 2D motion estimation method which uses keypoints detection and optical flow for matching. Base class for all global motion estimation methods. Describes a global 2D motion estimation method which minimizes L1 error. Describes motion model between two point clouds. Estimates best global motion between two 2D point clouds in the least-squares sense. Computes motion between two frames assuming that all the intermediate motions are known.

Mat cv::videostab::ensureInclusionConstraint. Mat cv::videostab::estimateGlobalMotionLeastSquares. Mat cv::videostab::estimateGlobalMotionRansac. Motion model. Mat cv::videostab::getMotion.Recent breakthroughs in computer vision research have allowed machines to perceive its surrounding world through techniques such as object detection for detecting instances of objects belonging to a certain class and semantic segmentation for pixel-wise classification.

In other words, they re-evaluate each frame independently, as if they are completely unrelated images, for each run. However, what if we do need the relationships between consecutive frames, for example, we want to track the motion of vehicles across frames to estimate its current velocity and predict its position in the next frame? Or, alternatively, what if we require information on human pose relationships between consecutive frames to recognize human actions such as archery, baseball, and basketball?

### Video Stabilization Using Point Feature Matching in OpenCV

In this tutorial, we will learn what Optical Flow is, how to implement its two main variants sparse and denseand also get a big picture of more recent approaches involving deep learning and promising future directions. What is Optical Flow?

Let us begin with a high-level understanding of optical flow. Optical flow is the motion of objects between consecutive frames of sequence, caused by the relative movement between the object and camera. The problem of optical flow may be expressed as:.

We will implement some methods such as the Lucas-Kanade method to address this issue. Sparse optical flow gives the flow vectors of some "interesting features" say few pixels depicting the edges or corners of an object within the frame while Dense optical flowwhich gives the flow vectors of the entire frame all pixels - up to one flow vector per pixel.

Sparse optical flow selects a sparse feature set of pixels e. The extracted features are passed in the optical flow function from frame to frame to ensure that the same points are being tracked.

**Laser Tracking System -using OpenCV 3.1 and Raspberry Pi 3**

There are various implementations of sparse optical flow, including the Lucas—Kanade method, the Horn—Schunck method, the Buxton—Buxton method, and more. We will be using the Lucas-Kanade method with OpenCV, an open source library of computer vision algorithms, for implementation. Next, open sparse-starter. We will be writing all of the code in this Python file.

For the implementation of sparse optical flow, we only track the motion of a feature set of pixels. Features in images are points of interest which present rich image content information.

For example, such features may be points in the image that are invariant to translation, scale, rotation, and intensity changes such as corners. The Shi-Tomasi Corner Detector is very similar to the popular Harris Corner Detector which can be implemented by the following three procedures:. If you would like to know more on a step-by-step mathematical explanation of the Harris Corner Detector, feel free to go through these slides. In the Harris Corner Detector, the scoring function is given by:.

There may be scenarios where you want to only track a specific object of interest say tracking a certain person or one category of objects like all 2 wheeler-vehicles in traffic. You can easily modify the code to track the pixels of the object s you want by changing the prev variable. You can also combine Object Detection with this method to only estimate the flow of pixels within the detected bounding boxes.

Lucas and Kanade proposed an effective technique to estimate the motion of interesting features by comparing two consecutive frames in their paper An Iterative Image Registration Technique with an Application to Stereo Vision.

The Lucas-Kanade method works under the following assumptions:. First, under these assumptions, we can take a small 3x3 window neighborhood around the features detected by Shi-Tomasi and assume that all nine points have the same motion.

This is just the Optical Flow Equation that we described earlier for each of the n pixels. Take note that previously see " What is optical flow? Second, to address the over-determined issue, we apply least squares fitting to obtain the following two-equation-two-unknown problem:.