vendor: OpenCV 5.0.0 snapshot at 40738fb16ceddb5fb3fea747585f7ce6abb0605b

This commit is contained in:
Gitea Mirror Bot
2026-08-22 00:10:33 +08:00
commit f7f077da11
6933 changed files with 2335208 additions and 0 deletions
@@ -0,0 +1,4 @@
Images stitching (stitching module) {#tutorial_table_of_content_stitching}
===================================
Content has been moved to this page: @ref tutorial_table_of_content_other
@@ -0,0 +1,4 @@
Video analysis (video module) {#tutorial_table_of_content_video}
=============================
Content has been moved to this page: @ref tutorial_table_of_content_other
@@ -0,0 +1,176 @@
How to Use Background Subtraction Methods {#tutorial_background_subtraction}
=========================================
@tableofcontents
@prev_tutorial{tutorial_stitcher}
@next_tutorial{tutorial_meanshift}
| | |
| -: | :- |
| Original author | Domenico Daniele Bloisi |
| Compatibility | OpenCV >= 3.0 |
- Background subtraction (BS) is a common and widely used technique for generating a foreground
mask (namely, a binary image containing the pixels belonging to moving objects in the scene) by
using static cameras.
- As the name suggests, BS calculates the foreground mask performing a subtraction between the
current frame and a background model, containing the static part of the scene or, more in
general, everything that can be considered as background given the characteristics of the
observed scene.
![](images/Background_Subtraction_Tutorial_Scheme.png)
- Background modeling consists of two main steps:
-# Background Initialization;
-# Background Update.
In the first step, an initial model of the background is computed, while in the second step that
model is updated in order to adapt to possible changes in the scene.
- In this tutorial we will learn how to perform BS by using OpenCV.
Goals
-----
In this tutorial you will learn how to:
-# Read data from videos or image sequences by using @ref cv::VideoCapture ;
-# Create and update the background model by using @ref cv::BackgroundSubtractor class;
-# Get and show the foreground mask by using @ref cv::imshow ;
### Code
In the following you can find the source code. We will let the user choose to process either a video
file or a sequence of images.
We will use @ref cv::BackgroundSubtractorMOG2 in this sample, to generate the foreground mask.
The results as well as the input data are shown on the screen.
@add_toggle_cpp
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/cpp/tutorial_code/video/bg_sub.cpp)
- **Code at glance:**
@include samples/cpp/tutorial_code/video/bg_sub.cpp
@end_toggle
@add_toggle_java
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java)
- **Code at glance:**
@include samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java
@end_toggle
@add_toggle_python
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/python/tutorial_code/video/background_subtraction/bg_sub.py)
- **Code at glance:**
@include samples/python/tutorial_code/video/background_subtraction/bg_sub.py
@end_toggle
Explanation
-----------
We discuss the main parts of the code above:
- A @ref cv::BackgroundSubtractor object will be used to generate the foreground mask. In this
example, default parameters are used, but it is also possible to declare specific parameters in
the create function.
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/video/bg_sub.cpp create
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java create
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py create
@end_toggle
- A @ref cv::VideoCapture object is used to read the input video or input images sequence.
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/video/bg_sub.cpp capture
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java capture
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py capture
@end_toggle
- Every frame is used both for calculating the foreground mask and for updating the background. If
you want to change the learning rate used for updating the background model, it is possible to
set a specific learning rate by passing a parameter to the `apply` method.
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/video/bg_sub.cpp apply
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java apply
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py apply
@end_toggle
- The current frame number can be extracted from the @ref cv::VideoCapture object and stamped in
the top left corner of the current frame. A white rectangle is used to highlight the black
colored frame number.
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/video/bg_sub.cpp display_frame_number
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java display_frame_number
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py display_frame_number
@end_toggle
- We are ready to show the current input frame and the results.
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/video/bg_sub.cpp show
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java show
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py show
@end_toggle
Results
-------
- With the `vtest.avi` video, for the following frame:
![](images/Background_Subtraction_Tutorial_frame.jpg)
The output of the program will look as the following for MOG2 method (gray areas are detected shadows):
![](images/Background_Subtraction_Tutorial_result_MOG2.jpg)
The output of the program will look as the following for the KNN method (gray areas are detected shadows):
![](images/Background_Subtraction_Tutorial_result_KNN.jpg)
References
----------
- [Background Models Challenge (BMC) website](https://web.archive.org/web/20140418093037/http://bmc.univ-bpclermont.fr/)
- A Benchmark Dataset for Foreground/Background Extraction @cite vacavant2013benchmark
Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 687 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 535 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 490 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

@@ -0,0 +1,217 @@
Introduction to Principal Component Analysis (PCA) {#tutorial_introduction_to_pca}
=======================================
@tableofcontents
@prev_tutorial{tutorial_optical_flow}
| | |
| -: | :- |
| Original author | Theodore Tsesmelis |
| Compatibility | OpenCV >= 3.0 |
Goal
----
In this tutorial you will learn how to:
- Use the OpenCV class @ref cv::PCA to calculate the orientation of an object.
What is PCA?
--------------
Principal Component Analysis (PCA) is a statistical procedure that extracts the most important features of a dataset.
![](images/pca_line.png)
Consider that you have a set of 2D points as it is shown in the figure above. Each dimension corresponds to a feature you are interested in. Here some could argue that the points are set in a random order. However, if you have a better look you will see that there is a linear pattern (indicated by the blue line) which is hard to dismiss. A key point of PCA is the Dimensionality Reduction. Dimensionality Reduction is the process of reducing the number of the dimensions of the given dataset. For example, in the above case it is possible to approximate the set of points to a single line and therefore, reduce the dimensionality of the given points from 2D to 1D.
Moreover, you could also see that the points vary the most along the blue line, more than they vary along the Feature 1 or Feature 2 axes. This means that if you know the position of a point along the blue line you have more information about the point than if you only knew where it was on Feature 1 axis or Feature 2 axis.
Hence, PCA allows us to find the direction along which our data varies the most. In fact, the result of running PCA on the set of points in the diagram consist of 2 vectors called _eigenvectors_ which are the _principal components_ of the data set.
![](images/pca_eigen.png)
The size of each eigenvector is encoded in the corresponding eigenvalue and indicates how much the data vary along the principal component. The beginning of the eigenvectors is the center of all points in the data set. Applying PCA to N-dimensional data set yields N N-dimensional eigenvectors, N eigenvalues and 1 N-dimensional center point. Enough theory, lets see how we can put these ideas into code.
How are the eigenvectors and eigenvalues computed?
--------------------------------------------------
The goal is to transform a given data set __X__ of dimension _p_ to an alternative data set __Y__ of smaller dimension _L_. Equivalently, we are seeking to find the matrix __Y__, where __Y__ is the _KarhunenLoève transform_ (KLT) of matrix __X__:
\f[ \mathbf{Y} = \mathbb{K} \mathbb{L} \mathbb{T} \{\mathbf{X}\} \f]
__Organize the data set__
Suppose you have data comprising a set of observations of _p_ variables, and you want to reduce the data so that each observation can be described with only _L_ variables, _L_ < _p_. Suppose further, that the data are arranged as a set of _n_ data vectors \f$ x_1...x_n \f$ with each \f$ x_i \f$ representing a single grouped observation of the _p_ variables.
- Write \f$ x_1...x_n \f$ as row vectors, each of which has _p_ columns.
- Place the row vectors into a single matrix __X__ of dimensions \f$ n\times p \f$.
__Calculate the empirical mean__
- Find the empirical mean along each dimension \f$ j = 1, ..., p \f$.
- Place the calculated mean values into an empirical mean vector __u__ of dimensions \f$ p\times 1 \f$.
\f[ \mathbf{u[j]} = \frac{1}{n}\sum_{i=1}^{n}\mathbf{X[i,j]} \f]
__Calculate the deviations from the mean__
Mean subtraction is an integral part of the solution towards finding a principal component basis that minimizes the mean square error of approximating the data. Hence, we proceed by centering the data as follows:
- Subtract the empirical mean vector __u__ from each row of the data matrix __X__.
- Store mean-subtracted data in the \f$ n\times p \f$ matrix __B__.
\f[ \mathbf{B} = \mathbf{X} - \mathbf{h}\mathbf{u^{T}} \f]
where __h__ is an \f$ n\times 1 \f$ column vector of all 1s:
\f[ h[i] = 1, i = 1, ..., n \f]
__Find the covariance matrix__
- Find the \f$ p\times p \f$ empirical covariance matrix __C__ from the outer product of matrix __B__ with itself:
\f[ \mathbf{C} = \frac{1}{n-1} \mathbf{B^{*}} \cdot \mathbf{B} \f]
where * is the conjugate transpose operator. Note that if B consists entirely of real numbers, which is the case in many applications, the "conjugate transpose" is the same as the regular transpose.
__Find the eigenvectors and eigenvalues of the covariance matrix__
- Compute the matrix __V__ of eigenvectors which diagonalizes the covariance matrix __C__:
\f[ \mathbf{V^{-1}} \mathbf{C} \mathbf{V} = \mathbf{D} \f]
where __D__ is the diagonal matrix of eigenvalues of __C__.
- Matrix __D__ will take the form of an \f$ p \times p \f$ diagonal matrix:
\f[ D[k,l] = \left\{\begin{matrix} \lambda_k, k = l \\ 0, k \neq l \end{matrix}\right. \f]
here, \f$ \lambda_j \f$ is the _j_-th eigenvalue of the covariance matrix __C__
- Matrix __V__, also of dimension _p_ x _p_, contains _p_ column vectors, each of length _p_, which represent the _p_ eigenvectors of the covariance matrix __C__.
- The eigenvalues and eigenvectors are ordered and paired. The _j_ th eigenvalue corresponds to the _j_ th eigenvector.
@note sources [[1]](https://robospace.wordpress.com/2013/10/09/object-orientation-principal-component-analysis-opencv/), [[2]](http://en.wikipedia.org/wiki/Principal_component_analysis) and special thanks to Svetlin Penkov for the original tutorial.
Source Code
-----------
@add_toggle_cpp
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp)
- **Code at glance:**
@include samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp
@end_toggle
@add_toggle_java
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/java/tutorial_code/ml/introduction_to_pca/IntroductionToPCADemo.java)
- **Code at glance:**
@include samples/java/tutorial_code/ml/introduction_to_pca/IntroductionToPCADemo.java
@end_toggle
@add_toggle_python
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/python/tutorial_code/ml/introduction_to_pca/introduction_to_pca.py)
- **Code at glance:**
@include samples/python/tutorial_code/ml/introduction_to_pca/introduction_to_pca.py
@end_toggle
@note Another example using PCA for dimensionality reduction while maintaining an amount of variance can be found at [opencv_source_code/samples/cpp/pca.cpp](https://github.com/opencv/opencv/tree/5.x/samples/cpp/pca.cpp)
Explanation
-----------
- __Read image and convert it to binary__
Here we apply the necessary pre-processing procedures in order to be able to detect the objects of interest.
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp pre-process
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ml/introduction_to_pca/IntroductionToPCADemo.java pre-process
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/ml/introduction_to_pca/introduction_to_pca.py pre-process
@end_toggle
- __Extract objects of interest__
Then find and filter contours by size and obtain the orientation of the remaining ones.
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp contours
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ml/introduction_to_pca/IntroductionToPCADemo.java contours
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/ml/introduction_to_pca/introduction_to_pca.py contours
@end_toggle
- __Extract orientation__
Orientation is extracted by the call of getOrientation() function, which performs all the PCA procedure.
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp pca
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ml/introduction_to_pca/IntroductionToPCADemo.java pca
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/ml/introduction_to_pca/introduction_to_pca.py pca
@end_toggle
First the data need to be arranged in a matrix with size n x 2, where n is the number of data points we have. Then we can perform that PCA analysis. The calculated mean (i.e. center of mass) is stored in the _cntr_ variable and the eigenvectors and eigenvalues are stored in the corresponding std::vectors.
- __Visualize result__
The final result is visualized through the drawAxis() function, where the principal components are drawn in lines, and each eigenvector is multiplied by its eigenvalue and translated to the mean position.
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp visualization
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ml/introduction_to_pca/IntroductionToPCADemo.java visualization
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/ml/introduction_to_pca/introduction_to_pca.py visualization
@end_toggle
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp visualization1
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/ml/introduction_to_pca/IntroductionToPCADemo.java visualization1
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/ml/introduction_to_pca/introduction_to_pca.py visualization1
@end_toggle
Results
-------
The code opens an image, finds the orientation of the detected objects of interest and then visualizes the result by drawing the contours of the detected objects of interest, the center point, and the x-axis, y-axis regarding the extracted orientation.
![](images/pca_test1.jpg)
![](images/pca_output.png)
+139
View File
@@ -0,0 +1,139 @@
Meanshift and Camshift {#tutorial_meanshift}
======================
@tableofcontents
@prev_tutorial{tutorial_background_subtraction}
@next_tutorial{tutorial_optical_flow}
Goal
----
In this chapter,
- We will learn about the Meanshift and Camshift algorithms to track objects in videos.
Meanshift
---------
The intuition behind the meanshift is simple. Consider you have a set of points. (It can be a pixel
distribution like histogram backprojection). You are given a small window (may be a circle) and you
have to move that window to the area of maximum pixel density (or maximum number of points). It is
illustrated in the simple image given below:
![image](images/meanshift_basics.jpg)
The initial window is shown in blue circle with the name "C1". Its original center is marked in blue
rectangle, named "C1_o". But if you find the centroid of the points inside that window, you will
get the point "C1_r" (marked in small blue circle) which is the real centroid of the window. Surely
they don't match. So move your window such that the circle of the new window matches with the previous
centroid. Again find the new centroid. Most probably, it won't match. So move it again, and continue
the iterations such that the center of window and its centroid falls on the same location (or within a
small desired error). So finally what you obtain is a window with maximum pixel distribution. It is
marked with a green circle, named "C2". As you can see in the image, it has maximum number of points. The
whole process is demonstrated on a static image below:
![image](images/meanshift_face.gif)
So we normally pass the histogram backprojected image and initial target location. When the object
moves, obviously the movement is reflected in the histogram backprojected image. As a result, the meanshift
algorithm moves our window to the new location with maximum density.
### Meanshift in OpenCV
To use meanshift in OpenCV, first we need to setup the target, find its histogram so that we can
backproject the target on each frame for calculation of meanshift. We also need to provide an initial
location of window. For histogram, only Hue is considered here. Also, to avoid false values due to
low light, low light values are discarded using **cv.inRange()** function.
@add_toggle_cpp
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/cpp/tutorial_code/video/meanshift/meanshift.cpp)
- **Code at glance:**
@include samples/cpp/tutorial_code/video/meanshift/meanshift.cpp
@end_toggle
@add_toggle_python
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/python/tutorial_code/video/meanshift/meanshift.py)
- **Code at glance:**
@include samples/python/tutorial_code/video/meanshift/meanshift.py
@end_toggle
@add_toggle_java
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/java/tutorial_code/video/meanshift/MeanshiftDemo.java)
- **Code at glance:**
@include samples/java/tutorial_code/video/meanshift/MeanshiftDemo.java
@end_toggle
Three frames in a video I used is given below:
![image](images/meanshift_result.jpg)
Camshift
--------
Did you closely watch the last result? There is a problem. Our window always has the same size whether
the car is very far or very close to the camera. That is not good. We need to adapt the window
size with size and rotation of the target. Once again, the solution came from "OpenCV Labs" and it
is called CAMshift (Continuously Adaptive Meanshift) published by Gary Bradsky in his paper
"Computer Vision Face Tracking for Use in a Perceptual User Interface" in 1998 @cite Bradski98 .
It applies meanshift first. Once meanshift converges, it updates the size of the window as,
\f$s = 2 \times \sqrt{\frac{M_{00}}{256}}\f$. It also calculates the orientation of the best fitting ellipse
to it. Again it applies the meanshift with new scaled search window and previous window location.
The process continues until the required accuracy is met.
![image](images/camshift_face.gif)
### Camshift in OpenCV
It is similar to meanshift, but returns a rotated rectangle (that is our result) and box
parameters (used to be passed as search window in next iteration). See the code below:
@add_toggle_cpp
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/cpp/tutorial_code/video/meanshift/camshift.cpp)
- **Code at glance:**
@include samples/cpp/tutorial_code/video/meanshift/camshift.cpp
@end_toggle
@add_toggle_python
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/python/tutorial_code/video/meanshift/camshift.py)
- **Code at glance:**
@include samples/python/tutorial_code/video/meanshift/camshift.py
@end_toggle
@add_toggle_java
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/java/tutorial_code/video/meanshift/CamshiftDemo.java)
- **Code at glance:**
@include samples/java/tutorial_code/video/meanshift/CamshiftDemo.java
@end_toggle
Three frames of the result is shown below:
![image](images/camshift_result.jpg)
Additional Resources
--------------------
-# French Wikipedia page on [Camshift](http://fr.wikipedia.org/wiki/Camshift). (The two animations
are taken from there)
2. Bradski, G.R., "Real time face and object tracking as a component of a perceptual user
interface," Applications of Computer Vision, 1998. WACV '98. Proceedings., Fourth IEEE Workshop
on , vol., no., pp.214,219, 19-21 Oct 1998
Exercises
---------
-# OpenCV comes with a Python [sample](https://github.com/opencv/opencv/blob/5.x/samples/python/snippets/camshift.py) for an interactive demo of camshift. Use it, hack it, understand
it.
+179
View File
@@ -0,0 +1,179 @@
Optical Flow {#tutorial_optical_flow}
============
@tableofcontents
@prev_tutorial{tutorial_meanshift}
@next_tutorial{tutorial_introduction_to_pca}
Goal
----
In this chapter,
- We will understand the concepts of optical flow and its estimation using Lucas-Kanade
method.
- We will use functions like **cv.calcOpticalFlowPyrLK()** to track feature points in a
video.
- We will create a dense optical flow field using the **cv.calcOpticalFlowFarneback()** method.
Optical Flow
------------
Optical flow is the pattern of apparent motion of image objects between two consecutive frames
caused by the movement 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. Consider the image
below (Image Courtesy: [Wikipedia article on Optical Flow](http://en.wikipedia.org/wiki/Optical_flow)).
![image](images/optical_flow_basic1.jpg)
It shows a ball moving in 5 consecutive frames. The arrow shows its displacement vector. Optical
flow has many applications in areas like :
- Structure from Motion
- Video Compression
- Video Stabilization ...
Optical flow works on several assumptions:
-# The pixel intensities of an object do not change between consecutive frames.
2. Neighbouring pixels have similar motion.
Consider a pixel \f$I(x,y,t)\f$ 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 \f$(dx,dy)\f$ in next frame
taken after \f$dt\f$ time. So since those pixels are the same and intensity does not change, we can say,
\f[I(x,y,t) = I(x+dx, y+dy, t+dt)\f]
Then take taylor series approximation of right-hand side, remove common terms and divide by \f$dt\f$ to
get the following equation:
\f[f_x u + f_y v + f_t = 0 \;\f]
where:
\f[f_x = \frac{\partial f}{\partial x} \; ; \; f_y = \frac{\partial f}{\partial y}\f]\f[u = \frac{dx}{dt} \; ; \; v = \frac{dy}{dt}\f]
Above equation is called Optical Flow equation. In it, we can find \f$f_x\f$ and \f$f_y\f$, they are image
gradients. Similarly \f$f_t\f$ is the gradient along time. But \f$(u,v)\f$ 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.
### Lucas-Kanade method
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 \f$(f_x, f_y, f_t)\f$ 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.
\f[\begin{bmatrix} u \\ v \end{bmatrix} =
\begin{bmatrix}
\sum_{i}{f_{x_i}}^2 & \sum_{i}{f_{x_i} f_{y_i} } \\
\sum_{i}{f_{x_i} f_{y_i}} & \sum_{i}{f_{y_i}}^2
\end{bmatrix}^{-1}
\begin{bmatrix}
- \sum_{i}{f_{x_i} f_{t_i}} \\
- \sum_{i}{f_{y_i} f_{t_i}}
\end{bmatrix}\f]
( Check similarity of inverse matrix with Harris corner detector. It denotes that corners are better
points to be tracked.)
So from the user point of view, the 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 a large motion. To deal with this we use pyramids. When we go up in
the pyramid, small motions are removed and large motions become small motions. So by applying
Lucas-Kanade there, we get optical flow along with the scale.
Lucas-Kanade Optical Flow in OpenCV
-----------------------------------
OpenCV provides all these in a single function, **cv.calcOpticalFlowPyrLK()**. Here, we create a
simple application which tracks some points in a video. To decide the points, we use
**cv.goodFeaturesToTrack()**. We take the first frame, detect some Shi-Tomasi corner points in it,
then we iteratively track those points using Lucas-Kanade optical flow. For the function
**cv.calcOpticalFlowPyrLK()** we pass the previous frame, previous points and next frame. It
returns next points along with some status numbers which has a value of 1 if next point is found,
else zero. We iteratively pass these next points as previous points in next step. See the code
below:
@add_toggle_cpp
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/cpp/tutorial_code/video/optical_flow/optical_flow.cpp)
- **Code at glance:**
@include samples/cpp/tutorial_code/video/optical_flow/optical_flow.cpp
@end_toggle
@add_toggle_python
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/python/tutorial_code/video/optical_flow/optical_flow.py)
- **Code at glance:**
@include samples/python/tutorial_code/video/optical_flow/optical_flow.py
@end_toggle
@add_toggle_java
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/java/tutorial_code/video/optical_flow/OpticalFlowDemo.java)
- **Code at glance:**
@include samples/java/tutorial_code/video/optical_flow/OpticalFlowDemo.java
@end_toggle
(This code doesn't check how correct are the next keypoints. So even if any feature point disappears
in image, there is a chance that optical flow finds the next point which may look close to it. So
actually for a robust tracking, corner points should be detected in particular intervals. OpenCV
samples comes up with such a sample which finds the feature points at every 5 frames. It also run a
backward-check of the optical flow points got to select only good ones. Check
samples/python/lk_track.py).
See the results we got:
![image](images/opticalflow_lk.jpg)
Dense Optical Flow in OpenCV
----------------------------
Lucas-Kanade method computes optical flow for a sparse feature set (in our example, corners detected
using Shi-Tomasi algorithm). OpenCV provides another algorithm to find the dense optical flow. It
computes the optical flow for all the points in the frame. It is based on Gunnar Farneback's
algorithm which is explained in "Two-Frame Motion Estimation Based on Polynomial Expansion" by
Gunnar Farneback in 2003.
Below sample shows how to find the dense optical flow using above algorithm. We get a 2-channel
array with optical flow vectors, \f$(u,v)\f$. We find their magnitude and direction. We color code the
result for better visualization. Direction corresponds to Hue value of the image. Magnitude
corresponds to Value plane. See the code below:
@add_toggle_cpp
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/cpp/tutorial_code/video/optical_flow/optical_flow_dense.cpp)
- **Code at glance:**
@include samples/cpp/tutorial_code/video/optical_flow/optical_flow_dense.cpp
@end_toggle
@add_toggle_python
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/python/tutorial_code/video/optical_flow/optical_flow_dense.py)
- **Code at glance:**
@include samples/python/tutorial_code/video/optical_flow/optical_flow_dense.py
@end_toggle
@add_toggle_java
- **Downloadable code**: Click
[here](https://github.com/opencv/opencv/tree/5.x/samples/java/tutorial_code/video/optical_flow/OpticalFlowDenseDemo.java)
- **Code at glance:**
@include samples/java/tutorial_code/video/optical_flow/OpticalFlowDenseDemo.java
@end_toggle
See the result below:
![image](images/opticalfb.jpg)
+186
View File
@@ -0,0 +1,186 @@
High level stitching API (Stitcher class) {#tutorial_stitcher}
=========================================
@tableofcontents
@prev_tutorial{tutorial_hdr_imaging}
@next_tutorial{tutorial_background_subtraction}
| | |
| -: | :- |
| Original author | Jiri Horner |
| Compatibility | OpenCV >= 3.2 |
Goal
----
In this tutorial you will learn how to:
- use the high-level stitching API for stitching provided by
- @ref cv::Stitcher
- learn how to use preconfigured Stitcher configurations to stitch images
using different camera models.
Code
----
@add_toggle_cpp
This tutorial's code is shown in the lines below. You can download it from [here](https://github.com/opencv/opencv/tree/5.x/samples/cpp/stitching.cpp).
Note: The C++ version includes additional options such as image division (--d3) and more detailed error handling, which are not present in the Python example.
@include samples/cpp/snippets/stitching.cpp
@end_toggle
@add_toggle_python
This tutorial's code is shown in the lines below. You can download it from [here](https://github.com/opencv/opencv/blob/5.x/samples/python/stitching.py).
Note: The C++ version includes additional options such as image division (--d3) and more detailed error handling, which are not present in the Python example.
@include samples/python/snippets/stitching.py
@end_toggle
Explanation
-----------
The most important code part is:
@add_toggle_cpp
@snippet cpp/snippets/stitching.cpp stitching
@end_toggle
@add_toggle_python
@snippet python/snippets/stitching.py stitching
@end_toggle
A new instance of stitcher is created and the @ref cv::Stitcher::stitch will
do all the hard work.
@ref cv::Stitcher::create can create stitcher in one of the predefined
configurations (argument `mode`). See @ref cv::Stitcher::Mode for details. These
configurations will setup multiple stitcher properties to operate in one of
predefined scenarios. After you create stitcher in one of predefined
configurations you can adjust stitching by setting any of the stitcher
properties.
If you have cuda device @ref cv::Stitcher can be configured to offload certain
operations to GPU. If you prefer this configuration set `try_use_gpu` to true.
OpenCL acceleration will be used transparently based on global OpenCV settings
regardless of this flag.
Stitching might fail for several reasons, you should always check if
everything went good and resulting pano is stored in `pano`. See
@ref cv::Stitcher::Status documentation for possible error codes.
Camera models
-------------
There are currently 2 camera models implemented in stitching pipeline.
- _Homography model_ expecting perspective transformations between images
implemented in @ref cv::detail::BestOf2NearestMatcher cv::detail::HomographyBasedEstimator
cv::detail::BundleAdjusterReproj cv::detail::BundleAdjusterRay
- _Affine model_ expecting affine transformation with 6 DOF or 4 DOF implemented in
@ref cv::detail::AffineBestOf2NearestMatcher cv::detail::AffineBasedEstimator
cv::detail::BundleAdjusterAffine cv::detail::BundleAdjusterAffinePartial cv::AffineWarper
Homography model is useful for creating photo panoramas captured by camera,
while affine-based model can be used to stitch scans and object captured by
specialized devices.
@note
Certain detailed settings of @ref cv::Stitcher might not make sense. Especially
you should not mix classes implementing affine model and classes implementing
Homography model, as they work with different transformations.
Try it out
----------
If you enabled building samples you can found binary under
`build/bin/cpp-example-stitching`. This example is a console application, run it without
arguments to see help. `opencv_extra` provides some sample data for testing all available
configurations.
to try panorama mode run:
```
./cpp-example-stitching --mode panorama <path to opencv_extra>/testdata/stitching/boat*
```
![](images/boat.jpg)
to try scans mode run (dataset from home-grade scanner):
```
./cpp-example-stitching --mode scans <path to opencv_extra>/testdata/stitching/newspaper*
```
![](images/newspaper.jpg)
or (dataset from professional book scanner):
```
./cpp-example-stitching --mode scans <path to opencv_extra>/testdata/stitching/budapest*
```
![](images/budapest.jpg)
@note
Examples above expects POSIX platform, on windows you have to provide all files names explicitly
(e.g. `boat1.jpg` `boat2.jpg`...) as windows command line does not support `*` expansion.
Stitching detailed (python opencv >4.0.1)
--------
If you want to study internals of the stitching pipeline or you want to experiment with detailed
configuration you can use stitching_detailed source code available in C++ or python
<H4>stitching_detailed</H4>
@add_toggle_cpp
[stitching_detailed.cpp](https://raw.githubusercontent.com/opencv/opencv/5.x/samples/cpp/stitching_detailed.cpp)
@end_toggle
@add_toggle_python
[stitching_detailed.py](https://raw.githubusercontent.com/opencv/opencv/5.x/samples/python/stitching_detailed.py)
@end_toggle
stitching_detailed program uses command line to get stitching parameter. Many parameters exists. Above examples shows some command line parameters possible :
boat5.jpg boat2.jpg boat3.jpg boat4.jpg boat1.jpg boat6.jpg --work_megapix 0.6 --features orb --matcher homography --estimator homography --match_conf 0.3 --conf_thresh 0.3 --ba ray --ba_refine_mask xxxxx --save_graph test.txt --wave_correct no --warp fisheye --blend multiband --expos_comp no --seam gc_colorgrad
![](images/fisheye.jpg)
Pairwise images are matched using an homography --matcher homography and estimator used for transformation estimation too --estimator homography
Confidence for feature matching step is 0.3 : --match_conf 0.3. You can decrease this value if you have some difficulties to match images
Threshold for two images are from the same panorama confidence is 0. : --conf_thresh 0.3 You can decrease this value if you have some difficulties to match images
Bundle adjustment cost function is ray --ba ray
Refinement mask for bundle adjustment is xxxxx ( --ba_refine_mask xxxxx) where 'x' means refine respective parameter and '_' means don't. Refine one, and has the following format: fx,skew,ppx,aspect,ppy
Save matches graph represented in DOT language to test.txt ( --save_graph test.txt) : Labels description: Nm is number of matches, Ni is number of inliers, C is confidence
![](images/gvedit.jpg)
Perform wave effect correction is no (--wave_correct no)
Warp surface type is fisheye (--warp fisheye)
Blending method is multiband (--blend multiband)
Exposure compensation method is not used (--expos_comp no)
Seam estimation estimator is Minimum graph cut-based seam (--seam gc_colorgrad)
you can use those arguments on command line too :
boat5.jpg boat2.jpg boat3.jpg boat4.jpg boat1.jpg boat6.jpg --work_megapix 0.6 --features orb --matcher homography --estimator homography --match_conf 0.3 --conf_thresh 0.3 --ba ray --ba_refine_mask xxxxx --wave_correct horiz --warp compressedPlaneA2B1 --blend multiband --expos_comp channels_blocks --seam gc_colorgrad
You will get :
![](images/compressedPlaneA2B1.jpg)
For images captured using a scanner or a drone ( affine motion) you can use those arguments on command line :
newspaper1.jpg newspaper2.jpg --work_megapix 0.6 --features surf --matcher affine --estimator affine --match_conf 0.3 --conf_thresh 0.3 --ba affine --ba_refine_mask xxxxx --wave_correct no --warp affine
![](images/affinepano.jpg)
You can find all images in https://github.com/opencv/opencv_extra/tree/5.x/testdata/stitching
@@ -0,0 +1,8 @@
Other tutorials (stitching, video) {#tutorial_table_of_content_other}
========================================================
- stitching. @subpage tutorial_stitcher
- video. @subpage tutorial_background_subtraction
- video. @subpage tutorial_meanshift
- video. @subpage tutorial_optical_flow
- ml. @subpage tutorial_introduction_to_pca