vendor: OpenCV 5.0.0 snapshot at 755e50675d97db9b7d449d8bd6b09888646f6c6e

This commit is contained in:
Gitea Mirror Bot
2026-08-22 00:11:13 +08:00
commit 12022378a3
3872 changed files with 2513409 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
# https://editorconfig.org/
root = true
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4
[{CMakeLists.*,*.cmake}]
indent_style = space
indent_size = 2
[Makefile]
indent_style = tab
[*.{bat,cmd,cmd.*}]
end_of_line = crlf
indent_style = space
indent_size = 2
[*.{ps1,ps1.*}]
end_of_line = crlf
indent_style = space
indent_size = 4
[*.{md,markdown}]
indent_size = 2
+7
View File
@@ -0,0 +1,7 @@
*.c text
*.cpp text
*.h text
*.hpp text
*.txt text
*.mk text
*.java text
+69
View File
@@ -0,0 +1,69 @@
<!--
If you have a question rather than reporting a bug please go to https://forum.opencv.org where you get much faster responses.
If you need further assistance please read [How To Contribute](https://github.com/opencv/opencv/wiki/How_to_contribute).
This is a template helping you to create an issue which can be processed as quickly as possible. This is the bug reporting section for the OpenCV library.
-->
##### System information (version)
<!-- Example
- OpenCV => 4.2
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 2017
-->
- OpenCV => :grey_question:
- Operating System / Platform => :grey_question:
- Compiler => :grey_question:
##### Detailed description
<!-- your description -->
##### Steps to reproduce
<!-- to add code example fence it with triple backticks and optional file extension
```.cpp
// C++ code example
```
or attach as .txt or .zip file
-->
##### Issue submission checklist
- [ ] I report the issue, it's not a question
<!--
OpenCV team works with forum.opencv.org, Stack Overflow and other communities
to discuss problems. Tickets with questions without a real issue statement will be
closed.
-->
- [ ] I checked the problem with documentation, FAQ, open issues,
forum.opencv.org, Stack Overflow, etc and have not found any solution
<!--
Places to check:
* OpenCV documentation: https://docs.opencv.org
* FAQ page: https://github.com/opencv/opencv/wiki/FAQ
* OpenCV forum: https://forum.opencv.org
* OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
* Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
-->
- [ ] I updated to the latest OpenCV version and the issue is still there
<!--
master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
OpenCV team supports only the latest release for each branch.
The ticket is closed if the problem is not reproduced with the modern version.
-->
- [ ] There is reproducer code and related data files: videos, images, onnx, etc
<!--
The best reproducer -- test case for OpenCV that we can add to the library.
Recommendations for media files and binary files:
* Try to reproduce the issue with images and videos in opencv_extra repository
to reduce attachment size
* Use PNG for images, if you report some CV related bug, but not image reader
issue
* Attach the image as an archive to the ticket, if you report some reader issue.
Image hosting services compress images and it breaks the repro code.
* Provide ONNX file for some public model or ONNX file with random weights,
if you report ONNX parsing or handling issue. Architecture details diagram
from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
-->
+11
View File
@@ -0,0 +1,11 @@
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
+33
View File
@@ -0,0 +1,33 @@
name: PR:5.x
on:
pull_request:
branches:
- 5.x
jobs:
Linux:
uses: opencv/ci-gha-workflow/.github/workflows/OCV-PR-Linux.yaml@geometry_module
with:
workflow_branch: geometry_module
Ubuntu2404-ARM64:
uses: opencv/ci-gha-workflow/.github/workflows/OCV-Contrib-PR-5.x-ARM64.yaml@geometry_module
Ubuntu2404-ARM64-FastCV:
uses: opencv/ci-gha-workflow/.github/workflows/OCV-Contrib-PR-5.x-ARM64-FastCV.yaml@geometry_module
Ubuntu2004-x64-CUDA:
uses: opencv/ci-gha-workflow/.github/workflows/OCV-Contrib-PR-5.x-U20-Cuda.yaml@geometry_module
Windows10-x64:
uses: opencv/ci-gha-workflow/.github/workflows/OCV-Contrib-PR-5.x-W10.yaml@geometry_module
macOS-ARM64:
uses: opencv/ci-gha-workflow/.github/workflows/OCV-Contrib-PR-5.x-macOS-ARM64.yaml@geometry_module
macOS-X64:
uses: opencv/ci-gha-workflow/.github/workflows/OCV-Contrib-PR-5.x-macOS-x86_64.yaml@geometry_module
Linux-RISC-V-Clang:
uses: opencv/ci-gha-workflow/.github/workflows/OCV-Contrib-PR-5.x-RISCV.yaml@geometry_module
+12
View File
@@ -0,0 +1,12 @@
*.autosave
*.pyc
*.user
*~
.*.swp
.DS_Store
.sw[a-z]
Thumbs.db
tags
tegra/
*.i
.download*
+19
View File
@@ -0,0 +1,19 @@
language: cpp
compiler:
- gcc
- clang
before_script:
- cd ../
- git clone --branch master --depth=1 https://github.com/opencv/opencv.git
- mkdir build-opencv
- cd build-opencv
- cmake
-DBUILD_EXAMPLES=ON
-DBUILD_PERF_TESTS=OFF
-DBUILD_TESTS=OFF
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules
../opencv
script:
- make -j5
notifications:
email: false
+3
View File
@@ -0,0 +1,3 @@
## Contributing guidelines
All guidelines for contributing to the OpenCV repository can be found at [`How to contribute guideline`](https://github.com/opencv/opencv/wiki/How_to_contribute).
+202
View File
@@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+60
View File
@@ -0,0 +1,60 @@
## Repository for OpenCV's extra modules
This repository is intended for the development of so-called "extra" modules,
contributed functionality. New modules quite often do not have stable API,
and they are not well-tested. Thus, they shouldn't be released as a part of the
official OpenCV distribution, since the library maintains binary compatibility,
and tries to provide decent performance and stability.
So, all the new modules should be developed separately, and published in the
`opencv_contrib` repository at first. Later, when the module matures and gains
popularity, it is moved to the central OpenCV repository, and the development team
provides production-quality support for this module.
### How to build OpenCV with extra modules
You can build OpenCV, so it will include the modules from this repository. Contrib modules are under constant development and it is recommended to use them alongside the master branch or latest releases of OpenCV.
Here is the CMake command for you:
```
$ cd <opencv_build_directory>
$ cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules <opencv_source_directory>
$ make -j5
```
As the result, OpenCV will be built in the `<opencv_build_directory>` with all
modules from `opencv_contrib` repository. If you don't want all of the modules,
use CMake's `BUILD_opencv_*` options. Like in this example:
```
$ cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -DBUILD_opencv_legacy=OFF <opencv_source_directory>
```
If you also want to build the samples from the "samples" folder of each module, also include the "-DBUILD_EXAMPLES=ON" option.
If you prefer using the GUI version of CMake (cmake-gui), then, you can add `opencv_contrib` modules within `opencv` core by doing the following:
1. Start cmake-gui.
2. Select the opencv source code folder and the folder where binaries will be built (the 2 upper forms of the interface).
3. Press the `configure` button. You will see all the opencv build parameters in the central interface.
4. Browse the parameters and look for the form called `OPENCV_EXTRA_MODULES_PATH` (use the search form to focus rapidly on it).
5. Complete this `OPENCV_EXTRA_MODULES_PATH` by the proper pathname to the `<opencv_contrib>/modules` value using its browse button.
6. Press the `configure` button followed by the `generate` button (the first time, you will be asked which makefile style to use).
7. Build the `opencv` core with the method you chose (make and make install if you chose Unix makefile at step 6).
8. To run, linker flags to contrib modules will need to be added to use them in your code/IDE. For example to use the aruco module, "-lopencv_aruco" flag will be added.
### Update the repository documentation
In order to keep a clean overview containing all contributed modules, the following files need to be created/adapted:
1. Update the README.md file under the modules folder. Here, you add your model with a single-line description.
2. Add a README.md inside your own module folder. This README explains which functionality (separate functions) is available, links to the corresponding samples, and explains in somewhat more detail what the module is expected to do. If any extra requirements are needed to build the module without problems, add them here also.
Binary file not shown.

After

Width:  |  Height:  |  Size: 147 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 101 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 143 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 208 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 317 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 241 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 213 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 422 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 KiB

@@ -0,0 +1,73 @@
function modelConvert(model, outname)
%% script for converting Piotr's matlab model into YAML format
outfile = fopen(outname, 'w');
fprintf(outfile, '%%YAML:1.0\n\n');
fprintf(outfile, ['options:\n'...
' numberOfTrees: 8\n'...
' numberOfTreesToEvaluate: 4\n'...
' selfsimilarityGridSize: 5\n'...
' stride: 2\n'...
' shrinkNumber: 2\n'...
' patchSize: 32\n'...
' patchInnerSize: 16\n'...
' numberOfGradientOrientations: 4\n'...
' gradientSmoothingRadius: 0\n'...
' regFeatureSmoothingRadius: 2\n'...
' ssFeatureSmoothingRadius: 8\n'...
' gradientNormalizationRadius: 4\n\n']);
fprintf(outfile, 'childs:\n');
printToYML(outfile, model.child', 0);
fprintf(outfile, 'featureIds:\n');
printToYML(outfile, model.fids', 0);
fprintf(outfile, 'thresholds:\n');
printToYML(outfile, model.thrs', 0);
N = 1000;
fprintf(outfile, 'edgeBoundaries:\n');
printToYML(outfile, model.eBnds, N);
fprintf(outfile, 'edgeBins:\n');
printToYML(outfile, model.eBins, N);
fclose(outfile);
gzip(outname);
end
function printToYML(outfile, A, N)
%% append matrix A to outfile as
%% - [a11, a12, a13, a14, ..., a1n]
%% - [a21, a22, a23, a24, ..., a2n]
%% ...
%%
%% if size(A, 2) == 1, A is printed by N elemnent per row
if (length(size(A)) ~= 2)
error('printToYML: second-argument matrix should have two dimensions');
end
if (size(A,2) ~= 1)
for i=1:size(A,1)
fprintf(outfile, ' - [');
fprintf(outfile, '%d,', A(i, 1:end-1));
fprintf(outfile, '%d]\n', A(i, end));
end
else
len = length(A);
for i=1:ceil(len/N)
first = (i-1)*N + 1;
last = min(i*N, len) - 1;
fprintf(outfile, ' - [');
fprintf(outfile, '%d,', A(first:last));
fprintf(outfile, '%d]\n', A(last + 1));
end
end
fprintf(outfile, '\n');
end
+105
View File
@@ -0,0 +1,105 @@
An overview of the opencv_contrib modules
-----------------------------------------
This list gives an overview of all modules available inside the contrib repository.
To turn off building one of these module repositories, set the names in bold below to <reponame>
```
$ cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -D BUILD_opencv_<reponame>=OFF <opencv_source_directory>
```
- **alphamat**: Computer Vision based Alpha Matting -- Given an input image and a trimap, generate an alpha matte.
- **aruco**: ArUco and ChArUco Markers -- Augmented reality ArUco marker and "ChARUco" markers where ArUco markers embedded inside the white areas of the checker board.
- **bgsegm**: Background segmentation algorithm combining statistical background image estimation and per-pixel Bayesian segmentation.
- **bioinspired**: Biological Vision -- Biologically inspired vision model: minimize noise and luminance variance, transient event segmentation, high dynamic range tone mapping methods.
- **ccalib**: Custom Calibration -- Patterns for 3D reconstruction, omnidirectional camera calibration, random pattern calibration and multi-camera calibration.
- **cnn_3dobj**: Deep Object Recognition and Pose -- Uses Caffe Deep Neural Net library to build, train and test a CNN model of visual object recognition and pose.
- **cvv**: Computer Vision Debugger -- Simple code that you can add to your program that pops up a GUI allowing you to interactively and visually debug computer vision programs.
- **datasets**: Datasets Reader -- Code for reading existing computer vision databases and samples of using the readers to train, test and run using that dataset's data.
- **dnn_objdetect**: Object Detection using CNNs -- Implements compact CNN Model for object detection. Trained using Caffe but uses opencv_dnn module.
- **dnn_superres**: Superresolution using CNNs -- Contains four trained convolutional neural networks to upscale images.
- **dnns_easily_fooled**: Subvert DNNs -- This code can use the activations in a network to fool the networks into recognizing something else.
- **dpm**: Deformable Part Model -- Felzenszwalb's Cascade with deformable parts object recognition code.
- **face**: Face Recognition -- Face recognition techniques: Eigen, Fisher and Local Binary Pattern Histograms LBPH methods.
- **freetype**: Drawing text using freetype and harfbuzz.
- **fuzzy**: Fuzzy Logic in Vision -- Fuzzy logic image transform and inverse; Fuzzy image processing.
- **hdf**: Hierarchical Data Storage -- This module contains I/O routines for Hierarchical Data Format: https://en.m.wikipedia.org/wiki/Hierarchical_Data_Format meant to store large amounts of data.
- **hfs**: Hierarchical Feature Selection for Efficient Image Segmentation -- This module contains an efficient algorithm to segment an image.
- **img_hash**: This module contains algorithms to extract hash of an image allowing to efficiently estimate similarity between images.
- **intensity_transform**: The module brings implementations of intensity transformation algorithms to adjust image contrast.
- **julia**: Julia language wrappers with samples and tests.
- **line_descriptor**: Line Segment Extract and Match -- Methods of extracting, describing and matching line segments using binary descriptors.
- **matlab**: Matlab Interface -- OpenCV Matlab Mex wrapper code generator for certain opencv core modules.
- **mcc**: Macbeth Color Chart detector -- Find and return color patch location in MacBeth color calibration charts.
- **optflow**: Optical Flow -- Algorithms for running and evaluating deepflow, simpleflow, sparsetodenseflow and motion templates (silhouette flow).
- **ovis**: OGRE 3D Visualiser -- allows you to render 3D data using the OGRE 3D engine.
- **phase_unwrapping**: Quality-guided phase unwrapping.
- **plot**: Plotting -- The plot module allows you to easily plot data in 1D or 2D.
- **quality**: Image Quality Analysis (IQA) API.
- **rapid**: Silhouette based 3D object tracking.
- **reg**: Image Registration -- Pixels based image registration for precise alignment. Follows the paper "Image Alignment and Stitching: A Tutorial", by Richard Szeliski.
- **rgbd**: RGB-Depth Processing module -- Linemod 3D object recognition; Fast surface normals and 3D plane finding. 3D visual odometry. 3d reconstruction using KinectFusion.
- **saliency**: Saliency API -- Where humans would look in a scene. Has routines for static, motion and "objectness" saliency.
- **signal**: Signal processing algorithms
- **sfm**: Structure from Motion -- This module contains algorithms to perform 3d reconstruction from 2d images. The core of the module is a light version of Libmv.
- **shape**: Shape Distance and Matching
- **stereo**: Stereo Correspondence -- Stereo matching done with different descriptors: Census / CS-Census / MCT / BRIEF / MV and dense stereo correspondence using Quasi Dense Stereo method.
- **structured_light**: Structured Light Use -- How to generate and project gray code patterns and use them to find dense depth in a scene.
- **superres**: Super Resolution
- **surface_matching**: Point Pair Features -- Implements 3d object detection and localization using multimodal point pair features.
- **text**: Scene Text Detection and Recognition -- This module contains algorithms to perform text detection, words segmentation and text recognition in a visual scene.
- **tracking**: Vision Based Object Tracking -- Use and/or evaluate different visual object tracking techniques.
- **videostab**: Video Stabilization
- **viz**: 3D Visualizer
- **wechat_qrcode**: WeChat QR code detector for detecting and parsing QR code.
- **xfeatures2d**: Features2D extra -- Extra 2D Features Framework containing experimental and non-free 2D feature detector/descriptor algorithms. SURF, BRIEF, Censure, Freak, LUCID, Daisy, Self-similar.
- **ximgproc**: Extended Image Processing -- Structured Forests / Domain Transform Filter / Guided Filter / Adaptive Manifold Filter / Joint Bilateral Filter / Superpixels / Ridge Detection Filter.
- **xobjdetect**: Boosted 2D Object Detection -- Uses a Waldboost cascade and local binary patterns computed as integral features for 2D object detection.
- **xphoto**: Extra Computational Photography -- Additional photo processing algorithms: Color balance / Denoising / Inpainting.
+10
View File
@@ -0,0 +1,10 @@
if(NOT HAVE_EIGEN)
message(STATUS "Module opencv_alphamat disabled because the following dependencies are not found: Eigen")
ocv_module_disable(alphamat)
endif()
ocv_define_module(alphamat
opencv_core
opencv_imgproc
WRAP python
)
+28
View File
@@ -0,0 +1,28 @@
# Computer Vision based Alpha Matting
![Trimap Alphamat Example from Askoy et al 2017](https://github.com/opencv/opencv_contrib/assets/810997/5cafae51-18e1-4e75-9035-6b40c9a064cb)
This project was part of the Google Summer of Code 2019.
#### Student: Muskaan Kularia
#### Mentor: Sunita Nayak
***
Alphamatting is the problem of extracting the foreground from an image. Given the input of an image and its corresponding trimap, we try to extract the foreground from the background.
This project is implementation of "[Designing Effective Inter-Pixel Information Flow for Natural Image Matting](https://www.researchgate.net/publication/318489370_Designing_Effective_Inter-Pixel_Information_Flow_for_Natural_Image_Matting)" by Yağız Aksoy, Tunç Ozan Aydın and Marc Pollefeys[1]. It required implementation of parts of other papers [2,3,4].
## References
[1] Yagiz Aksoy, Tunc Ozan Aydin, Marc Pollefeys, "[Designing Effective Inter-Pixel Information Flow for Natural Image Matting](https://www.researchgate.net/publication/318489370_Designing_Effective_Inter-Pixel_Information_Flow_for_Natural_Image_Matting)", CVPR, 2017.
[2] Roweis, Sam T., and Lawrence K. Saul. "[Nonlinear dimensionality reduction by locally linear embedding](https://science.sciencemag.org/content/290/5500/2323)" Science 290.5500 (2000): 2323-2326.
[3] Anat Levin, Dani Lischinski, Yair Weiss, "[A Closed Form Solution to Natural Image Matting](https://www.researchgate.net/publication/5764820_A_Closed-Form_Solution_to_Natural_Image_Matting)", IEEE TPAMI, 2008.
[4] Qifeng Chen, Dingzeyu Li, Chi-Keung Tang, "[KNN Matting](http://dingzeyu.li/files/knn-matting-tpami.pdf)", IEEE TPAMI, 2013.
[5] Yagiz Aksoy, "[Affinity Based Matting Toolbox](https://github.com/yaksoy/AffinityBasedMattingToolbox)".
+26
View File
@@ -0,0 +1,26 @@
@inproceedings{aksoy2017designing,
title={Designing effective inter-pixel information flow for natural image matting},
author={Aksoy, Yagiz and Ozan Aydin, Tunc and Pollefeys, Marc},
booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
pages={29--37},
year={2017}
}
@article{roweis2000nonlinear,
title={Nonlinear dimensionality reduction by locally linear embedding},
author={Roweis, Sam T and Saul, Lawrence K},
journal={science},
volume={290},
number={5500},
pages={2323--2326},
year={2000},
publisher={American Association for the Advancement of Science}
}
@inproceedings{shahrian2013improving,
title={Improving image matting using comprehensive sampling sets},
author={Shahrian, Ehsan and Rajan, Deepu and Price, Brian and Cohen, Scott},
booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
pages={636--643},
year={2013}
}
@@ -0,0 +1,43 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
/** Information Flow algorithm implementaton for alphamatting */
#ifndef _OPENCV_ALPHAMAT_HPP_
#define _OPENCV_ALPHAMAT_HPP_
#include <opencv2/core.hpp>
/**
* @defgroup alphamat Alpha Matting
* Alpha matting is used to extract a foreground object with soft boundaries from a background image.
*
* This module is dedicated to computing alpha matte of objects in images from a given input image and a greyscale trimap image that contains information about the foreground, background and unknown pixels. The unknown pixels are assumed to be a combination of foreground and background pixels. The algorithm uses a combination of multiple carefully defined pixels affinities to estimate the opacity of the foreground pixels in the unkown region.
*
* The implementation is based on @cite aksoy2017designing.
*
* This module was developed by Muskaan Kularia and Sunita Nayak as a project
* for Google Summer of Code 2019 (GSoC 19).
*
*/
namespace cv { namespace alphamat {
//! @addtogroup alphamat
//! @{
/**
* @brief Compute alpha matte of an object in an image
* @param image Input RGB image
* @param tmap Input greyscale trimap image
* @param result Output alpha matte image
*
* The function infoFlow performs alpha matting on a RGB image using a greyscale trimap image, and outputs a greyscale alpha matte image. The output alpha matte can be used to softly extract the foreground object from a background image. Examples can be found in the samples directory.
*
*/
CV_EXPORTS_W void infoFlow(InputArray image, InputArray tmap, OutputArray result);
//! @}
}} // namespace
#endif
@@ -0,0 +1,87 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
// Include relevant headers
#include <iostream>
#include "opencv2/highgui.hpp"
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/alphamat.hpp>
// Set namespaces
using namespace std;
using namespace cv;
using namespace cv::alphamat;
// Set the usage parameter names
const char* keys =
"{img || input image name}"
"{tri || input trimap image name}"
"{out || output image name}"
"{help h || print help message}"
;
int main(int argc, char* argv[])
{
CommandLineParser parser(argc, argv, keys);
parser.about("This sample demonstrates Information Flow Alpha Matting");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
// Read the paths to the input image, input trimap and the location of the output image.
string img_path = parser.get<std::string>("img");
string trimap_path = parser.get<std::string>("tri");
string result_path = parser.get<std::string>("out");
// Make sure the user inputs paths to the input image and trimap
if (!parser.check()
|| img_path.empty() || trimap_path.empty())
{
parser.printMessage();
parser.printErrors();
return 1;
}
Mat image, tmap;
// Read the input image
image = imread(img_path, IMREAD_COLOR);
if (image.empty())
{
printf("Cannot read image file: '%s'\n", img_path.c_str());
return 1;
}
// Read the trimap
tmap = imread(trimap_path, IMREAD_GRAYSCALE);
if (tmap.empty())
{
printf("Cannot read trimap file: '%s'\n", trimap_path.c_str());
return 1;
}
Mat result;
// Perform information flow alpha matting
infoFlow(image, tmap, result);
if (result_path.empty())
{
// Show the alpha matte if a result filepath is not provided.
namedWindow("result alpha matte", WINDOW_NORMAL);
imshow("result alpha matte", result);
waitKey(0);
}
else
{
// Save the alphamatte
imwrite(result_path, result);
printf("Result saved: '%s'\n", result_path.c_str());
}
return 0;
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 268 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

@@ -0,0 +1,134 @@
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2011-16 Jose Luis Blanco (joseluisblancoc@gmail.com).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#pragma once
#include "nanoflann.hpp"
#include <vector>
// ===== This example shows how to use nanoflann with these types of containers:
// using my_vector_of_vectors_t = std::vector<std::vector<double> > ;
//
// The next one requires #include <Eigen/Dense>
// using my_vector_of_vectors_t = std::vector<Eigen::VectorXd> ;
// =============================================================================
/** A simple vector-of-vectors adaptor for nanoflann, without duplicating the
* storage. The i'th vector represents a point in the state space.
*
* \tparam DIM If set to >0, it specifies a compile-time fixed dimensionality
* for the points in the data set, allowing more compiler optimizations.
* \tparam num_t The type of the point coordinates (typ. double or float).
* \tparam Distance The distance metric to use: nanoflann::metric_L1,
* nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc.
* \tparam IndexType The type for indices in the KD-tree index
* (typically, size_t of int)
*/
template <
class VectorOfVectorsType, typename num_t = double, int DIM = -1,
class Distance = nanoflann::metric_L2, typename IndexType = size_t>
struct KDTreeVectorOfVectorsAdaptor
{
using self_t = KDTreeVectorOfVectorsAdaptor<
VectorOfVectorsType, num_t, DIM, Distance, IndexType>;
using metric_t =
typename Distance::template traits<num_t, self_t>::distance_t;
using index_t =
nanoflann::KDTreeSingleIndexAdaptor<metric_t, self_t, DIM, IndexType>;
/** The kd-tree index for the user to call its methods as usual with any
* other FLANN index */
index_t* index = nullptr;
/// Constructor: takes a const ref to the vector of vectors object with the
/// data points
KDTreeVectorOfVectorsAdaptor(
const size_t /* dimensionality */, const VectorOfVectorsType& mat,
const int leaf_max_size = 10, const unsigned int n_thread_build = 1)
: m_data(mat)
{
assert(mat.size() != 0 && mat[0].size() != 0);
const size_t dims = mat[0].size();
if (DIM > 0 && static_cast<int>(dims) != DIM)
throw std::runtime_error(
"Data set dimensionality does not match the 'DIM' template "
"argument");
index = new index_t(
static_cast<int>(dims), *this /* adaptor */,
nanoflann::KDTreeSingleIndexAdaptorParams(
leaf_max_size, nanoflann::KDTreeSingleIndexAdaptorFlags::None,
n_thread_build));
}
~KDTreeVectorOfVectorsAdaptor() { delete index; }
const VectorOfVectorsType& m_data;
/** Query for the \a num_closest closest points to a given point
* (entered as query_point[0:dim-1]).
* Note that this is a short-cut method for index->findNeighbors().
* The user can also call index->... methods as desired.
*/
inline void query(
const num_t* query_point, const size_t num_closest,
IndexType* out_indices, num_t* out_distances_sq) const
{
nanoflann::KNNResultSet<num_t, IndexType> resultSet(num_closest);
resultSet.init(out_indices, out_distances_sq);
index->findNeighbors(resultSet, query_point);
}
/** @name Interface expected by KDTreeSingleIndexAdaptor
* @{ */
const self_t& derived() const { return *this; }
self_t& derived() { return *this; }
// Must return the number of data points
inline size_t kdtree_get_point_count() const { return m_data.size(); }
// Returns the dim'th component of the idx'th point in the class:
inline num_t kdtree_get_pt(const size_t idx, const size_t dim) const
{
return m_data[idx][dim];
}
// Optional bounding-box computation: return false to default to a standard
// bbox computation loop.
// Return true if the BBOX was already computed by the class and returned
// in "bb" so it can be avoided to redo it again. Look at bb.size() to
// find out the expected dimensionality (e.g. 2 or 3 for point clouds)
template <class BBOX>
bool kdtree_get_bbox(BBOX& /*bb*/) const
{
return false;
}
/** @} */
}; // end of KDTreeVectorOfVectorsAdaptor
File diff suppressed because it is too large Load Diff
+171
View File
@@ -0,0 +1,171 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "precomp.hpp"
#include "intraU.hpp"
#include "cm.hpp"
namespace cv { namespace alphamat {
static
void generateFVectorCM(my_vector_of_vectors_t& samples, Mat& img)
{
int nRows = img.rows;
int nCols = img.cols;
samples.resize(nRows * nCols);
int i, j;
for (i = 0; i < nRows; ++i)
{
for (j = 0; j < nCols; ++j)
{
samples[i * nCols + j].resize(ALPHAMAT_DIM);
samples[i * nCols + j][0] = img.at<cv::Vec3b>(i, j)[0] / 255.0;
samples[i * nCols + j][1] = img.at<cv::Vec3b>(i, j)[1] / 255.0;
samples[i * nCols + j][2] = img.at<cv::Vec3b>(i, j)[2] / 255.0;
samples[i * nCols + j][3] = double(i) / nRows;
samples[i * nCols + j][4] = double(j) / nCols;
}
}
}
static
void kdtree_CM(Mat& img, my_vector_of_vectors_t& indm, my_vector_of_vectors_t& samples, std::unordered_set<int>& unk)
{
// Generate feature vectors for intra U:
generateFVectorCM(samples, img);
// Query point: same as samples from which KD tree is generated
// construct a kd-tree index:
// Dimensionality set at run-time (default: L2)
// ------------------------------------------------------------
typedef KDTreeVectorOfVectorsAdaptor<my_vector_of_vectors_t, double> my_kd_tree_t;
my_kd_tree_t mat_index(ALPHAMAT_DIM /*dim*/, samples, 10 /* max leaf */);
mat_index.index->buildIndex();
// do a knn search with cm = 20
const size_t num_results = 20 + 1;
int N = unk.size();
std::vector<size_t> ret_indexes(num_results);
std::vector<double> out_dists_sqr(num_results);
nanoflann::KNNResultSet<double> resultSet(num_results);
indm.resize(N);
int i = 0;
for (std::unordered_set<int>::iterator it = unk.begin(); it != unk.end(); it++)
{
resultSet.init(&ret_indexes[0], &out_dists_sqr[0]);
mat_index.index->findNeighbors(resultSet, &samples[*it][0], nanoflann::SearchParameters(10));
indm[i].resize(num_results - 1);
for (std::size_t j = 1; j < num_results; j++)
{
indm[i][j - 1] = ret_indexes[j];
}
i++;
}
}
static
void lle(my_vector_of_vectors_t& indm, my_vector_of_vectors_t& samples, float eps, std::unordered_set<int>& unk,
SparseMatrix<double>& Wcm, SparseMatrix<double>& Dcm, Mat& img)
{
CV_LOG_INFO(NULL, "ALPHAMAT: In cm's lle function");
int k = indm[0].size(); //number of neighbours that we are considering
int n = indm.size(); //number of unknown pixels
typedef Triplet<double> T;
std::vector<T> triplets, td;
my_vector_of_vectors_t wcm;
wcm.resize(n);
Mat C(20, 20, DataType<float>::type), rhs(20, 1, DataType<float>::type), Z(3, 20, DataType<float>::type), weights(20, 1, DataType<float>::type), pt(3, 1, DataType<float>::type);
Mat ptDotN(20, 1, DataType<float>::type), imd(20, 1, DataType<float>::type);
Mat Cones(20, 1, DataType<float>::type), Cinv(20, 1, DataType<float>::type);
float alpha, beta, lagrangeMult;
Cones.setTo(cv::Scalar::all(1));
C.setTo(cv::Scalar::all(0));
rhs = 1;
int i, ind = 0;
for (std::unordered_set<int>::iterator it = unk.begin(); it != unk.end(); it++)
{
// filling values in Z
i = *it;
int index_nbr;
for (int j = 0; j < k; j++)
{
index_nbr = indm[ind][j];
for (int p = 0; p < ALPHAMAT_DIM - 2; p++)
{
Z.at<float>(p, j) = samples[index_nbr][p];
}
}
pt.at<float>(0, 0) = samples[i][0];
pt.at<float>(1, 0) = samples[i][1];
pt.at<float>(2, 0) = samples[i][2];
C = Z.t() * Z;
for (int p = 0; p < k; p++)
{
C.at<float>(p, p) += eps;
}
ptDotN = Z.t() * pt;
solve(C, ptDotN, imd);
alpha = 1 - cv::sum(imd)[0];
solve(C, Cones, Cinv);
beta = cv::sum(Cinv)[0]; //% sum of elements of inv(corr)
lagrangeMult = alpha / beta;
solve(C, ptDotN + lagrangeMult * Cones, weights);
float sum = cv::sum(weights)[0];
weights = weights / sum;
int cMaj_i = findColMajorInd(i, img.rows, img.cols);
for (int j = 0; j < k; j++)
{
int cMaj_ind_j = findColMajorInd(indm[ind][j], img.rows, img.cols);
triplets.push_back(T(cMaj_i, cMaj_ind_j, weights.at<float>(j, 0)));
td.push_back(T(cMaj_i, cMaj_i, weights.at<float>(j, 0)));
}
ind++;
}
Wcm.setFromTriplets(triplets.begin(), triplets.end());
Dcm.setFromTriplets(td.begin(), td.end());
}
void cm(Mat& image, Mat& tmap, SparseMatrix<double>& Wcm, SparseMatrix<double>& Dcm)
{
my_vector_of_vectors_t samples, indm, Euu;
int i, j;
std::unordered_set<int> unk;
for (i = 0; i < tmap.rows; i++)
{
for (j = 0; j < tmap.cols; j++)
{
uchar pix = tmap.at<uchar>(i, j);
if (pix == 128)
unk.insert(i * tmap.cols + j);
}
}
kdtree_CM(image, indm, samples, unk);
float eps = 0.00001;
lle(indm, samples, eps, unk, Wcm, Dcm, image);
CV_LOG_INFO(NULL, "ALPHAMAT: cm DONE");
}
}} // namespace cv::alphamat
+17
View File
@@ -0,0 +1,17 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_ALPHAMAT_CM_H__
#define __OPENCV_ALPHAMAT_CM_H__
namespace cv { namespace alphamat {
using namespace Eigen;
using namespace nanoflann;
void cm(Mat& image, Mat& tmap, SparseMatrix<double>& Wcm, SparseMatrix<double>& Dcm);
}}
#endif
+130
View File
@@ -0,0 +1,130 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "precomp.hpp"
#include <Eigen/Sparse>
using namespace Eigen;
namespace cv { namespace alphamat {
static
void solve(SparseMatrix<double> Wcm, SparseMatrix<double> Wuu, SparseMatrix<double> Wl, SparseMatrix<double> Dcm,
SparseMatrix<double> Duu, SparseMatrix<double> Dl, SparseMatrix<double> T,
Mat& wf, Mat& alpha)
{
float suu = 0.01, sl = 0.1, lamd = 100;
SparseMatrix<double> Lifm = ((Dcm - Wcm).transpose()) * (Dcm - Wcm) + sl * (Dl - Wl) + suu * (Duu - Wuu);
SparseMatrix<double> A;
int n = wf.rows;
VectorXd b(n), x(n);
Eigen::VectorXd wf_;
cv2eigen(wf, wf_);
A = Lifm + lamd * T;
b = (lamd * T) * (wf_);
ConjugateGradient<SparseMatrix<double>, Lower | Upper> cg;
cg.setMaxIterations(500);
cg.compute(A);
x = cg.solve(b);
CV_LOG_INFO(NULL, "ALPHAMAT: #iterations: " << cg.iterations());
CV_LOG_INFO(NULL, "ALPHAMAT: estimated error: " << cg.error());
int nRows = alpha.rows;
int nCols = alpha.cols;
float pix_alpha;
for (int j = 0; j < nCols; ++j)
{
for (int i = 0; i < nRows; ++i)
{
pix_alpha = x(i + j * nRows);
if (pix_alpha < 0)
pix_alpha = 0;
if (pix_alpha > 1)
pix_alpha = 1;
alpha.at<uchar>(i, j) = uchar(pix_alpha * 255);
}
}
}
void infoFlow(InputArray image_ia, InputArray tmap_ia, OutputArray result)
{
Mat image = image_ia.getMat();
Mat tmap = tmap_ia.getMat();
int64 begin = cv::getTickCount();
int nRows = image.rows;
int nCols = image.cols;
int N = nRows * nCols;
SparseMatrix<double> T(N, N);
typedef Triplet<double> Tr;
std::vector<Tr> triplets;
//Pre-process trimap
for (int i = 0; i < nRows; ++i)
{
for (int j = 0; j < nCols; ++j)
{
uchar& pix = tmap.at<uchar>(i, j);
if (pix <= 0.2f * 255)
pix = 0;
else if (pix >= 0.8f * 255)
pix = 255;
else
pix = 128;
}
}
Mat wf = Mat::zeros(nRows * nCols, 1, CV_8U);
// Column Major Interpretation for working with SparseMatrix
for (int i = 0; i < nRows; ++i)
{
for (int j = 0; j < nCols; ++j)
{
uchar pix = tmap.at<uchar>(i, j);
// collection of known pixels samples
triplets.push_back(Tr(i + j * nRows, i + j * nRows, (pix != 128) ? 1 : 0));
// foreground pixel
wf.at<uchar>(i + j * nRows, 0) = (pix > 200) ? 1 : 0;
}
}
SparseMatrix<double> Wl(N, N), Dl(N, N);
local_info(image, tmap, Wl, Dl);
SparseMatrix<double> Wcm(N, N), Dcm(N, N);
cm(image, tmap, Wcm, Dcm);
Mat new_tmap = tmap.clone();
SparseMatrix<double> Wuu(N, N), Duu(N, N);
Mat image_t = image.t();
Mat tmap_t = tmap.t();
UU(image, tmap, Wuu, Duu);
double elapsed_secs = ((double)(getTickCount() - begin)) / getTickFrequency();
T.setFromTriplets(triplets.begin(), triplets.end());
Mat alpha = Mat::zeros(nRows, nCols, CV_8UC1);
solve(Wcm, Wuu, Wl, Dcm, Duu, Dl, T, wf, alpha);
alpha.copyTo(result);
elapsed_secs = ((double)(getTickCount() - begin)) / getTickFrequency();
CV_LOG_INFO(NULL, "ALPHAMAT: total time: " << elapsed_secs);
}
}} // namespace cv::alphamat
+152
View File
@@ -0,0 +1,152 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "precomp.hpp"
#include "intraU.hpp"
namespace cv { namespace alphamat {
int findColMajorInd(int rowMajorInd, int nRows, int nCols)
{
int iInd = rowMajorInd / nCols;
int jInd = rowMajorInd % nCols;
return (jInd * nRows + iInd);
}
static
void generateFVectorIntraU(my_vector_of_vectors_t& samples, Mat& img, Mat& tmap, std::vector<int>& orig_ind)
{
int nRows = img.rows;
int nCols = img.cols;
int unk_count = 0;
int i, j;
for (i = 0; i < nRows; ++i)
{
for (j = 0; j < nCols; ++j)
{
uchar pix = tmap.at<uchar>(i, j);
if (pix == 128)
unk_count++;
}
}
samples.resize(unk_count);
orig_ind.resize(unk_count);
int c1 = 0;
for (i = 0; i < nRows; ++i)
{
for (j = 0; j < nCols; ++j)
{
uchar pix = tmap.at<uchar>(i, j);
if (pix == 128) // collection of unknown pixels samples
{
samples[c1].resize(ALPHAMAT_DIM);
samples[c1][0] = img.at<cv::Vec3b>(i, j)[0] / 255.0;
samples[c1][1] = img.at<cv::Vec3b>(i, j)[1] / 255.0;
samples[c1][2] = img.at<cv::Vec3b>(i, j)[2] / 255.0;
samples[c1][3] = (double(i + 1) / nRows) / 20;
samples[c1][4] = (double(j + 1) / nCols) / 20;
orig_ind[c1] = i * nCols + j;
c1++;
}
}
}
CV_LOG_INFO(NULL, "ALPHAMAT: Total number of unknown pixels : " << c1);
}
static
void kdtree_intraU(Mat& img, Mat& tmap, my_vector_of_vectors_t& indm, my_vector_of_vectors_t& samples, std::vector<int>& orig_ind)
{
// Generate feature vectors for intra U:
generateFVectorIntraU(samples, img, tmap, orig_ind);
typedef KDTreeVectorOfVectorsAdaptor<my_vector_of_vectors_t, double> my_kd_tree_t;
my_kd_tree_t mat_index(ALPHAMAT_DIM /*dim*/, samples, 10 /* max leaf */);
mat_index.index->buildIndex();
// do a knn search with ku = 5
const size_t num_results = 5 + 1;
int N = samples.size(); // no. of unknown samples
std::vector<size_t> ret_indexes(num_results);
std::vector<double> out_dists_sqr(num_results);
nanoflann::KNNResultSet<double> resultSet(num_results);
indm.resize(N);
for (int i = 0; i < N; i++)
{
resultSet.init(&ret_indexes[0], &out_dists_sqr[0]);
mat_index.index->findNeighbors(resultSet, &samples[i][0], nanoflann::SearchParameters(10));
indm[i].resize(num_results - 1);
for (std::size_t j = 1; j < num_results; j++)
{
indm[i][j - 1] = ret_indexes[j];
}
}
}
static
double l1norm(std::vector<double>& x, std::vector<double>& y)
{
double sum = 0;
for (int i = 0; i < ALPHAMAT_DIM; i++)
sum += abs(x[i] - y[i]);
return sum / ALPHAMAT_DIM;
}
static
void intraU(Mat& img, my_vector_of_vectors_t& indm, my_vector_of_vectors_t& samples,
std::vector<int>& orig_ind, SparseMatrix<double>& Wuu, SparseMatrix<double>& Duu)
{
// input: indm, samples
int n = indm.size(); // num of unknown samples
CV_LOG_INFO(NULL, "ALPHAMAT: num of unknown samples, n : " << n);
int i, j, nbr_ind;
for (i = 0; i < n; i++)
{
samples[i][3] *= 1 / 100;
samples[i][4] *= 1 / 100;
}
my_vector_of_vectors_t weights;
typedef Triplet<double> T;
std::vector<T> triplets, td;
double weight;
for (i = 0; i < n; i++)
{
int num_nbr = indm[i].size();
int cMaj_i = findColMajorInd(orig_ind[i], img.rows, img.cols);
for (j = 0; j < num_nbr; j++)
{
nbr_ind = indm[i][j];
int cMaj_nbr_j = findColMajorInd(orig_ind[nbr_ind], img.rows, img.cols);
weight = max(1 - l1norm(samples[i], samples[j]), 0.0);
triplets.push_back(T(cMaj_i, cMaj_nbr_j, weight / 2));
td.push_back(T(cMaj_i, cMaj_i, weight / 2));
triplets.push_back(T(cMaj_nbr_j, cMaj_i, weight / 2));
td.push_back(T(cMaj_nbr_j, cMaj_nbr_j, weight / 2));
}
}
Wuu.setFromTriplets(triplets.begin(), triplets.end());
Duu.setFromTriplets(td.begin(), td.end());
}
void UU(Mat& image, Mat& tmap, SparseMatrix<double>& Wuu, SparseMatrix<double>& Duu)
{
my_vector_of_vectors_t samples, indm;
std::vector<int> orig_ind;
kdtree_intraU(image, tmap, indm, samples, orig_ind);
intraU(image, indm, samples, orig_ind, Wuu, Duu);
CV_LOG_INFO(NULL, "ALPHAMAT: Intra U Done");
}
}} // namespace cv::alphamat
+23
View File
@@ -0,0 +1,23 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_ALPHAMAT_INTRAU_H__
#define __OPENCV_ALPHAMAT_INTRAU_H__
namespace cv { namespace alphamat {
const int ALPHAMAT_DIM = 5; // dimension of feature vectors
using namespace Eigen;
using namespace nanoflann;
typedef std::vector<std::vector<double>> my_vector_of_vectors_t;
int findColMajorInd(int rowMajorInd, int nRows, int nCols);
void UU(Mat& image, Mat& tmap, SparseMatrix<double>& Wuu, SparseMatrix<double>& Duu);
}} // namespace
#endif
+153
View File
@@ -0,0 +1,153 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
// #ifndef local_info
// #define local_info
#include "precomp.hpp"
#include "local_info.hpp"
namespace cv { namespace alphamat {
void local_info(Mat& img, Mat& tmap, SparseMatrix<double>& Wl, SparseMatrix<double>& Dl)
{
float eps = 0.000001;
int win_size = 1;
int nRows = img.rows;
int nCols = img.cols;
int N = img.rows * img.cols;
Mat unk_img = Mat::zeros(cv::Size(nCols, nRows), CV_32FC1);
for (int i = 0; i < nRows; ++i)
{
for (int j = 0; j < nCols; ++j)
{
uchar pix = tmap.at<uchar>(i, j);
if (pix == 128) // collection of unknown pixels samples
{
unk_img.at<float>(i, j) = 255;
}
}
}
Mat element = getStructuringElement(MORPH_RECT, Size(2 * win_size + 1, 2 * win_size + 1));
/// Apply the dilation operation
Mat dilation_dst = unk_img.clone();
//dilate(unk_img, dilation_dst, element);
int num_win = (win_size * 2 + 1) * (win_size * 2 + 1); // number of pixels in window
typedef Triplet<double> T;
std::vector<T> triplets, td, tl;
int neighInd[9];
int i, j;
for (j = win_size; j < nCols - win_size; j++)
{
for (i = win_size; i < nRows - win_size; i++)
{
uchar pix = tmap.at<uchar>(i, j);
//std::cout << i+j*nRows << " --> " << pix << std::endl;
if (pix != 128)
continue;
// extract the window out of image
Mat win = img.rowRange(i - win_size, i + win_size + 1);
win = win.colRange(j - win_size, j + win_size + 1);
Mat win_ravel = Mat::zeros(9, 3, CV_64F); // doubt ??
double sum1 = 0;
double sum2 = 0;
double sum3 = 0;
int c = 0;
for (int q = -1; q <= 1; q++)
{
for (int p = -1; p <= 1; p++)
{
neighInd[c] = (j + q) * nRows + (i + p); // column major
c++;
}
}
c = 0;
//parsing column major way in the window
for (int q = 0; q < win_size * 2 + 1; q++)
{
for (int p = 0; p < win_size * 2 + 1; p++)
{
win_ravel.at<double>(c, 0) = win.at<cv::Vec3b>(p, q)[0] / 255.0;
win_ravel.at<double>(c, 1) = win.at<cv::Vec3b>(p, q)[1] / 255.0;
win_ravel.at<double>(c, 2) = win.at<cv::Vec3b>(p, q)[2] / 255.0;
sum1 += win.at<cv::Vec3b>(p, q)[0] / 255.0;
sum2 += win.at<cv::Vec3b>(p, q)[1] / 255.0;
sum3 += win.at<cv::Vec3b>(p, q)[2] / 255.0;
c++;
}
}
win = win_ravel;
Mat win_mean = Mat::zeros(1, 3, CV_64F);
win_mean.at<double>(0, 0) = sum1 / num_win;
win_mean.at<double>(0, 1) = sum2 / num_win;
win_mean.at<double>(0, 2) = sum3 / num_win;
// calculate the covariance matrix
Mat covariance = (win.t() * win / num_win) - (win_mean.t() * win_mean);
Mat I = Mat::eye(img.channels(), img.channels(), CV_64F);
Mat I1 = (covariance + (eps / num_win) * I);
Mat I1_inv = I1.inv();
Mat X = win - repeat(win_mean, num_win, 1);
Mat vals = (1 + X * I1_inv * X.t()) / num_win;
for (int q = 0; q < num_win; q++)
{
for (int p = 0; p < num_win; p++)
{
triplets.push_back(T(neighInd[p], neighInd[q], vals.at<double>(p, q)));
}
}
}
}
std::vector<T> tsp;
SparseMatrix<double> W(N, N), Wsp(N, N);
W.setFromTriplets(triplets.begin(), triplets.end());
SparseMatrix<double> Wt = W.transpose();
SparseMatrix<double> Ws = Wt + W;
W = Ws;
for (int k = 0; k < W.outerSize(); ++k)
{
double sumCol = 0;
for (SparseMatrix<double>::InnerIterator it(W, k); it; ++it)
{
sumCol += it.value();
}
if (sumCol < 0.05)
sumCol = 1;
tsp.push_back(T(k, k, 1 / sumCol));
}
Wsp.setFromTriplets(tsp.begin(), tsp.end());
Wl = Wsp * W; // For normalization
//Wl = W; // No normalization
SparseMatrix<double> Wlt = Wl.transpose();
for (int k = 0; k < Wlt.outerSize(); ++k)
{
double sumarr = 0;
for (SparseMatrix<double>::InnerIterator it(Wlt, k); it; ++it)
sumarr += it.value();
td.push_back(T(k, k, sumarr));
}
Dl.setFromTriplets(td.begin(), td.end());
CV_LOG_INFO(NULL, "ALPHAMAT: local_info DONE");
}
}} // namespace cv::alphamat
// #endif
+17
View File
@@ -0,0 +1,17 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_ALPHAMAT_LOCAL_INFO_H__
#define __OPENCV_ALPHAMAT_LOCAL_INFO_H__
namespace cv { namespace alphamat {
using namespace Eigen;
void local_info(Mat& img, Mat& tmap, SparseMatrix<double>& Wl, SparseMatrix<double>& Dl);
}} // namespace
#endif
+31
View File
@@ -0,0 +1,31 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_PRECOMP_H__
#define __OPENCV_PRECOMP_H__
#include <vector>
#include <unordered_set>
#include <set>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core/utils/logger.hpp>
#include <opencv2/alphamat.hpp>
#include "3rdparty/nanoflann.hpp"
#include "3rdparty/KDTreeVectorOfVectorsAdaptor.h"
#ifdef HAVE_EIGEN
#include <Eigen/Eigen>
#include <opencv2/core/eigen.hpp>
#include <Eigen/IterativeLinearSolvers>
#endif
#include "intraU.hpp"
#include "cm.hpp"
#include "local_info.hpp"
#endif
@@ -0,0 +1,63 @@
Information Flow Alpha Matting {#tutorial_alphamat}
============================
This project was part of Google Summer of Code 2019.
*Student:* Muskaan Kularia
*Mentor:* Sunita Nayak
Alphamatting is the problem of extracting the foreground with soft boundaries from a background image. The extracted foreground can be used for further operations like changing the background in an image.
Given an input image and its corresponding trimap, we try to extract the foreground from the background. Following is an example:
Input Image: ![](alphamat/samples/input_images/plant.jpg)
Input image should be preferably a RGB image.
Input Trimap: ![](alphamat/samples/trimaps/plant.png)
The trimap image is a greyscale image that contains information about the foreground(white pixels), background(black pixels) and unknown(grey) pixels.
Output alpha Matte: ![](alphamat/samples/output_mattes/plant_result.png)
The computed alpha matte is saved as a greyscale image where the pixel values indicate the opacity of the extracted foreground object. These opacity values can be used to blend the foreground object into a diffferent backgound, as shown below:
![](plant_new_backgrounds.jpg)
Following are some more results.
![](matting_results.jpg)
The first column is input RGB image, the second column is input trimap, third column is the extracted alpha matte and the last two columns show the foreground object blended on new backgrounds.
This project is implementation of @cite aksoy2017designing . It also required implementation of parts of other papers [2,3,4].
# Building
This module uses the Eigen package.
Build the sample code of the alphamat module using the following two cmake commands run inside the build folder:
```
cmake -DOPENCV_EXTRA_MODULES_PATH=<path to opencv_contrib modules> -DBUILD_EXAMPLES=ON ..
cmake --build . --config Release --target example_alphamat_information_flow_matting
```
Please refer to OpenCV building tutorials for further details, if needed.
# Testing
The built target can be tested as follows:
```
<path to your opencv build directory>/bin/example_alphamat_information_flow_matting -img=<path to input image file> -tri=<path to the corresponding trimap> -out=<path to save output matte file>
```
# Source Code of the sample
@includelineno alphamat/samples/information_flow_matting.cpp
# References
[1] Yagiz Aksoy, Tunc Ozan Aydin, Marc Pollefeys, [Designing Effective Inter-Pixel Information Flow for Natural Image Matting](https://www.researchgate.net/publication/318489370_Designing_Effective_Inter-Pixel_Information_Flow_for_Natural_Image_Matting), CVPR, 2017.
[2] Roweis, Sam T., and Lawrence K. Saul. [Nonlinear dimensionality reduction by locally linear embedding](https://science.sciencemag.org/content/290/5500/2323), Science 290.5500 (2000): 2323-2326.
[3] Anat Levin, Dani Lischinski, Yair Weiss, [A Closed Form Solution to Natural Image Matting](https://www.researchgate.net/publication/5764820_A_Closed-Form_Solution_to_Natural_Image_Matting), IEEE TPAMI, 2008.
[4] Qifeng Chen, Dingzeyu Li, Chi-Keung Tang, [KNN Matting](http://dingzeyu.li/files/knn-matting-tpami.pdf), IEEE TPAMI, 2013.
[5] Yagiz Aksoy, [Affinity Based Matting Toolbox](https://github.com/yaksoy/AffinityBasedMattingToolbox).
Binary file not shown.

After

Width:  |  Height:  |  Size: 264 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

+2
View File
@@ -0,0 +1,2 @@
set(the_description "Background Segmentation Algorithms")
ocv_define_module(bgsegm opencv_core opencv_imgproc opencv_video opencv_geometry WRAP python java objc)
+10
View File
@@ -0,0 +1,10 @@
Improved Background-Foreground Segmentation Methods
===================================================
This algorithm combines statistical background image estimation and per-pixel Bayesian segmentation. It[1] was introduced by Andrew B. Godbehere, Akihiro Matsukawa, Ken Goldberg in 2012. As per the paper, the system ran a successful interactive audio art installation called "Are We There Yet?" from March 31 - July 31 2011 at the Contemporary Jewish Museum in San Francisco, California.
It uses first few (120 by default) frames for background modelling. It employs probabilistic foreground segmentation algorithm that identifies possible foreground objects using Bayesian inference. The estimates are adaptive; newer observations are more heavily weighted than old observations to accommodate variable illumination. Several morphological filtering operations like closing and opening are done to remove unwanted noise. You will get a black window during first few frames.
References
----------
[1]: A.B. Godbehere, A. Matsukawa, K. Goldberg. Visual tracking of human visitors under variable-lighting conditions for a responsive audio art installation. American Control Conference. (2012), pp. 43054312
+27
View File
@@ -0,0 +1,27 @@
@incollection{KB2001,
title={An improved adaptive background mixture model for real-time tracking with shadow detection},
author={KaewTraKulPong, Pakorn and Bowden, Richard},
booktitle={Video-Based Surveillance Systems},
pages={135--144},
year={2002},
publisher={Springer}
}
@inproceedings{Gold2012,
title={Visual tracking of human visitors under variable-lighting conditions for a responsive audio art installation},
author={Godbehere, Andrew B and Matsukawa, Akihiro and Goldberg, Ken},
booktitle={American Control Conference (ACC), 2012},
pages={4305--4312},
year={2012},
organization={IEEE}
}
@inproceedings{LGuo2016,
author={L. Guo and D. Xu and Z. Qiang},
booktitle={2016 IEEE Conference on Computer Vision and Pattern Recognition Workshops (CVPRW)},
title={Background Subtraction Using Local SVD Binary Pattern},
year={2016},
pages={1159-1167},
doi={10.1109/CVPRW.2016.148},
month={June}
}
+453
View File
@@ -0,0 +1,453 @@
/*
By downloading, copying, installing or using the software you agree to this
license. If you do not agree to this license, do not download, install,
copy or use the software.
License Agreement
For Open Source Computer Vision Library
(3-clause BSD License)
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
Third party copyrights are property of their respective owners.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the names of the copyright holders nor the names of the contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall copyright holders or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
*/
#ifndef __OPENCV_BGSEGM_HPP__
#define __OPENCV_BGSEGM_HPP__
#include "opencv2/video.hpp"
#ifdef __cplusplus
/** @defgroup bgsegm Improved Background-Foreground Segmentation Methods
*/
namespace cv
{
namespace bgsegm
{
//! @addtogroup bgsegm
//! @{
/** @brief Gaussian Mixture-based Background/Foreground Segmentation Algorithm.
The class implements the algorithm described in @cite KB2001 .
*/
class CV_EXPORTS_W BackgroundSubtractorMOG : public BackgroundSubtractor
{
public:
// BackgroundSubtractor interface
/** @brief Computes a foreground mask.
@param image Next video frame of type CV_8UC(n),CV_8SC(n),CV_16UC(n),CV_16SC(n),CV_32SC(n),CV_32FC(n),CV_64FC(n), where n is 1,2,3,4.
@param fgmask The output foreground mask as an 8-bit binary image.
@param learningRate The value between 0 and 1 that indicates how fast the background model is
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
rate. 0 means that the background model is not updated at all, 1 means that the background model
is completely reinitialized from the last frame.
*/
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
/** @brief Computes a foreground mask and skips known foreground in evaluation.
@param image Next video frame of type CV_8UC(n),CV_8SC(n),CV_16UC(n),CV_16SC(n),CV_32SC(n),CV_32FC(n),CV_64FC(n), where n is 1,2,3,4.
@param fgmask The output foreground mask as an 8-bit binary image.
@param knownForegroundMask The mask for inputting already known foreground, allows model to ignore learning known pixels.
@param learningRate The value between 0 and 1 that indicates how fast the background model is
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
rate. 0 means that the background model is not updated at all, 1 means that the background model
is completely reinitialized from the last frame.
*/
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
CV_WRAP virtual int getHistory() const = 0;
CV_WRAP virtual void setHistory(int nframes) = 0;
CV_WRAP virtual int getNMixtures() const = 0;
CV_WRAP virtual void setNMixtures(int nmix) = 0;
CV_WRAP virtual double getBackgroundRatio() const = 0;
CV_WRAP virtual void setBackgroundRatio(double backgroundRatio) = 0;
CV_WRAP virtual double getNoiseSigma() const = 0;
CV_WRAP virtual void setNoiseSigma(double noiseSigma) = 0;
};
/** @brief Creates mixture-of-gaussian background subtractor
@param history Length of the history.
@param nmixtures Number of Gaussian mixtures.
@param backgroundRatio Background ratio.
@param noiseSigma Noise strength (standard deviation of the brightness or each color channel). 0
means some automatic value.
*/
CV_EXPORTS_W Ptr<BackgroundSubtractorMOG>
createBackgroundSubtractorMOG(int history=200, int nmixtures=5,
double backgroundRatio=0.7, double noiseSigma=0);
/** @brief Background Subtractor module based on the algorithm given in @cite Gold2012 .
Takes a series of images and returns a sequence of mask (8UC1)
images of the same size, where 255 indicates Foreground and 0 represents Background.
This class implements an algorithm described in "Visual Tracking of Human Visitors under
Variable-Lighting Conditions for a Responsive Audio Art Installation," A. Godbehere,
A. Matsukawa, K. Goldberg, American Control Conference, Montreal, June 2012.
*/
class CV_EXPORTS_W BackgroundSubtractorGMG : public BackgroundSubtractor
{
public:
// BackgroundSubtractor interface
/** @brief Computes a foreground mask.
@param image Next video frame of type CV_8UC(n),CV_8SC(n),CV_16UC(n),CV_16SC(n),CV_32SC(n),CV_32FC(n),CV_64FC(n), where n is 1,2,3,4.
@param fgmask The output foreground mask as an 8-bit binary image.
@param learningRate The value between 0 and 1 that indicates how fast the background model is
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
rate. 0 means that the background model is not updated at all, 1 means that the background model
is completely reinitialized from the last frame.
*/
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
/** @brief Computes a foreground mask with known foreground mask input.
@param image Next video frame.
@param fgmask The output foreground mask as an 8-bit binary image.
@param knownForegroundMask The mask for inputting already known foreground.
@param learningRate The value between 0 and 1 that indicates how fast the background model is
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
rate. 0 means that the background model is not updated at all, 1 means that the background model
is completely reinitialized from the last frame.
@note This method has a default virtual implementation that throws a "not implemented" error.
Foreground masking may not be supported by all background subtractors.
*/
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
/** @brief Returns total number of distinct colors to maintain in histogram.
*/
CV_WRAP virtual int getMaxFeatures() const = 0;
/** @brief Sets total number of distinct colors to maintain in histogram.
*/
CV_WRAP virtual void setMaxFeatures(int maxFeatures) = 0;
/** @brief Returns the learning rate of the algorithm.
It lies between 0.0 and 1.0. It determines how quickly features are "forgotten" from
histograms.
*/
CV_WRAP virtual double getDefaultLearningRate() const = 0;
/** @brief Sets the learning rate of the algorithm.
*/
CV_WRAP virtual void setDefaultLearningRate(double lr) = 0;
/** @brief Returns the number of frames used to initialize background model.
*/
CV_WRAP virtual int getNumFrames() const = 0;
/** @brief Sets the number of frames used to initialize background model.
*/
CV_WRAP virtual void setNumFrames(int nframes) = 0;
/** @brief Returns the parameter used for quantization of color-space.
It is the number of discrete levels in each channel to be used in histograms.
*/
CV_WRAP virtual int getQuantizationLevels() const = 0;
/** @brief Sets the parameter used for quantization of color-space
*/
CV_WRAP virtual void setQuantizationLevels(int nlevels) = 0;
/** @brief Returns the prior probability that each individual pixel is a background pixel.
*/
CV_WRAP virtual double getBackgroundPrior() const = 0;
/** @brief Sets the prior probability that each individual pixel is a background pixel.
*/
CV_WRAP virtual void setBackgroundPrior(double bgprior) = 0;
/** @brief Returns the kernel radius used for morphological operations
*/
CV_WRAP virtual int getSmoothingRadius() const = 0;
/** @brief Sets the kernel radius used for morphological operations
*/
CV_WRAP virtual void setSmoothingRadius(int radius) = 0;
/** @brief Returns the value of decision threshold.
Decision value is the value above which pixel is determined to be FG.
*/
CV_WRAP virtual double getDecisionThreshold() const = 0;
/** @brief Sets the value of decision threshold.
*/
CV_WRAP virtual void setDecisionThreshold(double thresh) = 0;
/** @brief Returns the status of background model update
*/
CV_WRAP virtual bool getUpdateBackgroundModel() const = 0;
/** @brief Sets the status of background model update
*/
CV_WRAP virtual void setUpdateBackgroundModel(bool update) = 0;
/** @brief Returns the minimum value taken on by pixels in image sequence. Usually 0.
*/
CV_WRAP virtual double getMinVal() const = 0;
/** @brief Sets the minimum value taken on by pixels in image sequence.
*/
CV_WRAP virtual void setMinVal(double val) = 0;
/** @brief Returns the maximum value taken on by pixels in image sequence. e.g. 1.0 or 255.
*/
CV_WRAP virtual double getMaxVal() const = 0;
/** @brief Sets the maximum value taken on by pixels in image sequence.
*/
CV_WRAP virtual void setMaxVal(double val) = 0;
};
/** @brief Creates a GMG Background Subtractor
@param initializationFrames number of frames used to initialize the background models.
@param decisionThreshold Threshold value, above which it is marked foreground, else background.
*/
CV_EXPORTS_W Ptr<BackgroundSubtractorGMG> createBackgroundSubtractorGMG(int initializationFrames=120,
double decisionThreshold=0.8);
/** @brief Background subtraction based on counting.
About as fast as MOG2 on a high end system.
More than twice faster than MOG2 on cheap hardware (benchmarked on Raspberry Pi3).
%Algorithm by Sagi Zeevi ( https://github.com/sagi-z/BackgroundSubtractorCNT )
*/
class CV_EXPORTS_W BackgroundSubtractorCNT : public BackgroundSubtractor
{
public:
// BackgroundSubtractor interface
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
/** @brief Computes a foreground mask with known foreground mask input.
@param image Next video frame.
@param knownForegroundMask The mask for inputting already known foreground.
@param fgmask The output foreground mask as an 8-bit binary image.
@param learningRate The value between 0 and 1 that indicates how fast the background model is
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
rate. 0 means that the background model is not updated at all, 1 means that the background model
is completely reinitialized from the last frame.
@note This method has a default virtual implementation that throws a "not impemented" error.
Foreground masking may not be supported by all background subtractors.
*/
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
/** @brief Returns number of frames with same pixel color to consider stable.
*/
CV_WRAP virtual int getMinPixelStability() const = 0;
/** @brief Sets the number of frames with same pixel color to consider stable.
*/
CV_WRAP virtual void setMinPixelStability(int value) = 0;
/** @brief Returns maximum allowed credit for a pixel in history.
*/
CV_WRAP virtual int getMaxPixelStability() const = 0;
/** @brief Sets the maximum allowed credit for a pixel in history.
*/
CV_WRAP virtual void setMaxPixelStability(int value) = 0;
/** @brief Returns if we're giving a pixel credit for being stable for a long time.
*/
CV_WRAP virtual bool getUseHistory() const = 0;
/** @brief Sets if we're giving a pixel credit for being stable for a long time.
*/
CV_WRAP virtual void setUseHistory(bool value) = 0;
/** @brief Returns if we're parallelizing the algorithm.
*/
CV_WRAP virtual bool getIsParallel() const = 0;
/** @brief Sets if we're parallelizing the algorithm.
*/
CV_WRAP virtual void setIsParallel(bool value) = 0;
};
/** @brief Creates a CNT Background Subtractor
@param minPixelStability number of frames with same pixel color to consider stable
@param useHistory determines if we're giving a pixel credit for being stable for a long time
@param maxPixelStability maximum allowed credit for a pixel in history
@param isParallel determines if we're parallelizing the algorithm
*/
CV_EXPORTS_W Ptr<BackgroundSubtractorCNT>
createBackgroundSubtractorCNT(int minPixelStability = 15,
bool useHistory = true,
int maxPixelStability = 15*60,
bool isParallel = true);
enum LSBPCameraMotionCompensation {
LSBP_CAMERA_MOTION_COMPENSATION_NONE = 0,
LSBP_CAMERA_MOTION_COMPENSATION_LK
};
/** @brief Implementation of the different yet better algorithm which is called GSOC, as it was implemented during GSOC and was not originated from any paper.
This algorithm demonstrates better performance on CDNET 2014 dataset compared to other algorithms in OpenCV.
*/
class CV_EXPORTS_W BackgroundSubtractorGSOC : public BackgroundSubtractor
{
public:
// BackgroundSubtractor interface
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
};
/** @brief Background Subtraction using Local SVD Binary Pattern. More details about the algorithm can be found at @cite LGuo2016
*/
class CV_EXPORTS_W BackgroundSubtractorLSBP : public BackgroundSubtractor
{
public:
// BackgroundSubtractor interface
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
};
/** @brief This is for calculation of the LSBP descriptors.
*/
class CV_EXPORTS_W BackgroundSubtractorLSBPDesc
{
public:
static void calcLocalSVDValues(OutputArray localSVDValues, const Mat& frame);
static void computeFromLocalSVDValues(OutputArray desc, const Mat& localSVDValues, const Point2i* LSBPSamplePoints);
static void compute(OutputArray desc, const Mat& frame, const Point2i* LSBPSamplePoints);
};
/** @brief Creates an instance of BackgroundSubtractorGSOC algorithm.
Implementation of the different yet better algorithm which is called GSOC, as it was implemented during GSOC and was not originated from any paper.
@param mc Whether to use camera motion compensation.
@param nSamples Number of samples to maintain at each point of the frame.
@param replaceRate Probability of replacing the old sample - how fast the model will update itself.
@param propagationRate Probability of propagating to neighbors.
@param hitsThreshold How many positives the sample must get before it will be considered as a possible replacement.
@param alpha Scale coefficient for threshold.
@param beta Bias coefficient for threshold.
@param blinkingSupressionDecay Blinking supression decay factor.
@param blinkingSupressionMultiplier Blinking supression multiplier.
@param noiseRemovalThresholdFacBG Strength of the noise removal for background points.
@param noiseRemovalThresholdFacFG Strength of the noise removal for foreground points.
*/
CV_EXPORTS_W Ptr<BackgroundSubtractorGSOC> createBackgroundSubtractorGSOC(int mc = LSBP_CAMERA_MOTION_COMPENSATION_NONE, int nSamples = 20, float replaceRate = 0.003f, float propagationRate = 0.01f, int hitsThreshold = 32, float alpha = 0.01f, float beta = 0.0022f, float blinkingSupressionDecay = 0.1f, float blinkingSupressionMultiplier = 0.1f, float noiseRemovalThresholdFacBG = 0.0004f, float noiseRemovalThresholdFacFG = 0.0008f);
/** @brief Creates an instance of BackgroundSubtractorLSBP algorithm.
Background Subtraction using Local SVD Binary Pattern. More details about the algorithm can be found at @cite LGuo2016
@param mc Whether to use camera motion compensation.
@param nSamples Number of samples to maintain at each point of the frame.
@param LSBPRadius LSBP descriptor radius.
@param Tlower Lower bound for T-values. See @cite LGuo2016 for details.
@param Tupper Upper bound for T-values. See @cite LGuo2016 for details.
@param Tinc Increase step for T-values. See @cite LGuo2016 for details.
@param Tdec Decrease step for T-values. See @cite LGuo2016 for details.
@param Rscale Scale coefficient for threshold values.
@param Rincdec Increase/Decrease step for threshold values.
@param noiseRemovalThresholdFacBG Strength of the noise removal for background points.
@param noiseRemovalThresholdFacFG Strength of the noise removal for foreground points.
@param LSBPthreshold Threshold for LSBP binary string.
@param minCount Minimal number of matches for sample to be considered as foreground.
*/
CV_EXPORTS_W Ptr<BackgroundSubtractorLSBP> createBackgroundSubtractorLSBP(int mc = LSBP_CAMERA_MOTION_COMPENSATION_NONE, int nSamples = 20, int LSBPRadius = 16, float Tlower = 2.0f, float Tupper = 32.0f, float Tinc = 1.0f, float Tdec = 0.05f, float Rscale = 10.0f, float Rincdec = 0.005f, float noiseRemovalThresholdFacBG = 0.0004f, float noiseRemovalThresholdFacFG = 0.0008f, int LSBPthreshold = 8, int minCount = 2);
/** @brief Synthetic frame sequence generator for testing background subtraction algorithms.
It will generate the moving object on top of the background.
It will apply some distortion to the background to make the test more complex.
*/
class CV_EXPORTS_W SyntheticSequenceGenerator : public Algorithm
{
private:
const double amplitude;
const double wavelength;
const double wavespeed;
const double objspeed;
unsigned timeStep;
Point2d pos;
Point2d dir;
Mat background;
Mat object;
RNG rng;
public:
/** @brief Creates an instance of SyntheticSequenceGenerator.
@param background Background image for object.
@param object Object image which will move slowly over the background.
@param amplitude Amplitude of wave distortion applied to background.
@param wavelength Length of waves in distortion applied to background.
@param wavespeed How fast waves will move.
@param objspeed How fast object will fly over background.
*/
CV_WRAP SyntheticSequenceGenerator(InputArray background, InputArray object, double amplitude, double wavelength, double wavespeed, double objspeed);
/** @brief Obtain the next frame in the sequence.
@param frame Output frame.
@param gtMask Output ground-truth (reference) segmentation mask object/background.
*/
CV_WRAP void getNextFrame(OutputArray frame, OutputArray gtMask);
};
/** @brief Creates an instance of SyntheticSequenceGenerator.
@param background Background image for object.
@param object Object image which will move slowly over the background.
@param amplitude Amplitude of wave distortion applied to background.
@param wavelength Length of waves in distortion applied to background.
@param wavespeed How fast waves will move.
@param objspeed How fast object will fly over background.
*/
CV_EXPORTS_W Ptr<SyntheticSequenceGenerator> createSyntheticSequenceGenerator(InputArray background, InputArray object, double amplitude = 2.0, double wavelength = 20.0, double wavespeed = 0.2, double objspeed = 6.0);
//! @}
}
}
#endif
#endif
+11
View File
@@ -0,0 +1,11 @@
{
"AdditionalImports" : {
"*" : [ "\"bgsegm.hpp\"" ]
},
"func_arg_fix" : {
"Bgsegm" : {
"createBackgroundSubtractorGSOC" : { "mc" : {"ctype" : "LSBPCameraMotionCompensation"} },
"createBackgroundSubtractorLSBP" : { "mc" : {"ctype" : "LSBPCameraMotionCompensation"} }
}
}
}
+124
View File
@@ -0,0 +1,124 @@
#include "opencv2/bgsegm.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/core/utility.hpp>
#include <iostream>
using namespace cv;
using namespace cv::bgsegm;
const String about =
"\nA program demonstrating the use and capabilities of different background subtraction algorithms\n"
"Using OpenCV version " + String(CV_VERSION) +
"\n\nPress 'c' to change the algorithm"
"\nPress 'm' to toggle showing only foreground mask or ghost effect"
"\nPress 'n' to change number of threads"
"\nPress SPACE to toggle wait delay of imshow"
"\nPress 'q' or ESC to exit\n";
const String algos[7] = { "GMG", "CNT", "KNN", "MOG", "MOG2", "GSOC", "LSBP" };
static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName)
{
Ptr<BackgroundSubtractor> algo;
if(algoName == String("GMG"))
algo = createBackgroundSubtractorGMG(20, 0.7);
else if(algoName == String("CNT"))
algo = createBackgroundSubtractorCNT();
else if(algoName == String("KNN"))
algo = createBackgroundSubtractorKNN();
else if(algoName == String("MOG"))
algo = createBackgroundSubtractorMOG();
else if(algoName == String("MOG2"))
algo = createBackgroundSubtractorMOG2();
else if(algoName == String("GSOC"))
algo = createBackgroundSubtractorGSOC();
else if(algoName == String("LSBP"))
algo = createBackgroundSubtractorLSBP();
return algo;
}
int main(int argc, char** argv)
{
CommandLineParser parser(argc, argv, "{@video | vtest.avi | path to a video file}");
parser.about(about);
parser.printMessage();
String videoPath = samples::findFile(parser.get<String>(0),false);
Ptr<BackgroundSubtractor> bgfs = createBGSubtractorByName(algos[0]);
VideoCapture cap;
cap.open(videoPath);
if (!cap.isOpened())
{
std::cerr << "Cannot read video. Try moving video file to sample directory." << std::endl;
return -1;
}
Mat frame, fgmask, segm;
int delay = 30;
int algo_index = 0;
int nthreads = getNumberOfCPUs();
bool show_fgmask = false;
for (;;)
{
cap >> frame;
if (frame.empty())
{
cap.set(CAP_PROP_POS_FRAMES, 0);
cap >> frame;
}
bgfs->apply(frame, fgmask);
if (show_fgmask)
segm = fgmask;
else
{
frame.convertTo(segm, CV_8U, 0.5);
add(frame, Scalar(100, 100, 0), segm, fgmask);
}
putText(segm, algos[algo_index], Point(10, 30), FONT_HERSHEY_PLAIN, 2.0, Scalar(255, 0, 255), 2, LINE_AA);
putText(segm, format("%d threads", nthreads), Point(10, 60), FONT_HERSHEY_PLAIN, 2.0, Scalar(255, 0, 255), 2, LINE_AA);
imshow("FG Segmentation", segm);
int c = waitKey(delay);
if (c == ' ')
delay = delay == 30 ? 1 : 30;
if (c == 'c' || c == 'C')
{
algo_index++;
if ( algo_index > 6 )
algo_index = 0;
bgfs = createBGSubtractorByName(algos[algo_index]);
}
if (c == 'n' || c == 'N')
{
nthreads++;
if ( nthreads > 8 )
nthreads = 1;
setNumThreads(nthreads);
}
if (c == 'm' || c == 'M')
show_fgmask = !show_fgmask;
if (c == 'q' || c == 'Q' || c == 27)
break;
}
return 0;
}
+134
View File
@@ -0,0 +1,134 @@
import argparse
import cv2 as cv
import glob
import numpy as np
import os
import time
# This tool is intended for evaluation of different background subtraction algorithms presented in OpenCV.
# Several presets with different settings are available. You can see them below.
# This tool measures quality metrics as well as speed.
ALGORITHMS_TO_EVALUATE = [
(cv.bgsegm.createBackgroundSubtractorMOG, 'MOG', {}),
(cv.bgsegm.createBackgroundSubtractorGMG, 'GMG', {}),
(cv.bgsegm.createBackgroundSubtractorCNT, 'CNT', {}),
(cv.bgsegm.createBackgroundSubtractorLSBP, 'LSBP-vanilla', {'nSamples': 20, 'LSBPRadius': 4, 'Tlower': 2.0, 'Tupper': 200.0, 'Tinc': 1.0, 'Tdec': 0.05, 'Rscale': 5.0, 'Rincdec': 0.05, 'LSBPthreshold': 8}),
(cv.bgsegm.createBackgroundSubtractorLSBP, 'LSBP-speed', {'nSamples': 10, 'LSBPRadius': 16, 'Tlower': 2.0, 'Tupper': 32.0, 'Tinc': 1.0, 'Tdec': 0.05, 'Rscale': 10.0, 'Rincdec': 0.005, 'LSBPthreshold': 8}),
(cv.bgsegm.createBackgroundSubtractorLSBP, 'LSBP-quality', {'nSamples': 20, 'LSBPRadius': 16, 'Tlower': 2.0, 'Tupper': 32.0, 'Tinc': 1.0, 'Tdec': 0.05, 'Rscale': 10.0, 'Rincdec': 0.005, 'LSBPthreshold': 8}),
(cv.bgsegm.createBackgroundSubtractorLSBP, 'LSBP-camera-motion-compensation', {'mc': 1}),
(cv.bgsegm.createBackgroundSubtractorGSOC, 'GSOC', {}),
(cv.bgsegm.createBackgroundSubtractorGSOC, 'GSOC-camera-motion-compensation', {'mc': 1})
]
def contains_relevant_files(root):
return os.path.isdir(os.path.join(root, 'groundtruth')) and os.path.isdir(os.path.join(root, 'input'))
def find_relevant_dirs(root):
relevant_dirs = []
for d in sorted(os.listdir(root)):
d = os.path.join(root, d)
if os.path.isdir(d):
if contains_relevant_files(d):
relevant_dirs += [d]
else:
relevant_dirs += find_relevant_dirs(d)
return relevant_dirs
def load_sequence(root):
gt_dir, frames_dir = os.path.join(root, 'groundtruth'), os.path.join(root, 'input')
gt = sorted(glob.glob(os.path.join(gt_dir, '*.png')))
f = sorted(glob.glob(os.path.join(frames_dir, '*.jpg')))
assert(len(gt) == len(f))
return gt, f
def evaluate_algorithm(gt, frames, algo, algo_arguments):
bgs = algo(**algo_arguments)
mask = []
t_start = time.time()
for i in range(len(gt)):
frame = np.uint8(cv.imread(frames[i], cv.IMREAD_COLOR))
mask.append(bgs.apply(frame))
average_duration = (time.time() - t_start) / len(gt)
average_precision, average_recall, average_f1, average_accuracy = [], [], [], []
for i in range(len(gt)):
gt_mask = np.uint8(cv.imread(gt[i], cv.IMREAD_GRAYSCALE))
roi = ((gt_mask == 255) | (gt_mask == 0))
if roi.sum() > 0:
gt_answer, answer = gt_mask[roi], mask[i][roi]
tp = ((answer == 255) & (gt_answer == 255)).sum()
tn = ((answer == 0) & (gt_answer == 0)).sum()
fp = ((answer == 255) & (gt_answer == 0)).sum()
fn = ((answer == 0) & (gt_answer == 255)).sum()
if tp + fp > 0:
average_precision.append(float(tp) / (tp + fp))
if tp + fn > 0:
average_recall.append(float(tp) / (tp + fn))
if tp + fn + fp > 0:
average_f1.append(2.0 * tp / (2.0 * tp + fn + fp))
average_accuracy.append(float(tp + tn) / (tp + tn + fp + fn))
return average_duration, np.mean(average_precision), np.mean(average_recall), np.mean(average_f1), np.mean(average_accuracy)
def evaluate_on_sequence(seq, summary):
gt, frames = load_sequence(seq)
category, video_name = os.path.basename(os.path.dirname(seq)), os.path.basename(seq)
print('=== %s:%s ===' % (category, video_name))
for algo, algo_name, algo_arguments in ALGORITHMS_TO_EVALUATE:
print('Algorithm name: %s' % algo_name)
sec_per_step, precision, recall, f1, accuracy = evaluate_algorithm(gt, frames, algo, algo_arguments)
print('Average accuracy: %.3f' % accuracy)
print('Average precision: %.3f' % precision)
print('Average recall: %.3f' % recall)
print('Average F1: %.3f' % f1)
print('Average sec. per step: %.4f' % sec_per_step)
print('')
if category not in summary:
summary[category] = {}
if algo_name not in summary[category]:
summary[category][algo_name] = []
summary[category][algo_name].append((precision, recall, f1, accuracy))
def main():
parser = argparse.ArgumentParser(description='Evaluate all background subtractors using Change Detection 2014 dataset')
parser.add_argument('--dataset_path', help='Path to the directory with dataset. It may contain multiple inner directories. It will be scanned recursively.', required=True)
parser.add_argument('--algorithm', help='Test particular algorithm instead of all.')
args = parser.parse_args()
dataset_dirs = find_relevant_dirs(args.dataset_path)
assert len(dataset_dirs) > 0, ("Passed directory must contain at least one sequence from the Change Detection dataset. There is no relevant directories in %s. Check that this directory is correct." % (args.dataset_path))
if args.algorithm is not None:
global ALGORITHMS_TO_EVALUATE
ALGORITHMS_TO_EVALUATE = filter(lambda a: a[1].lower() == args.algorithm.lower(), ALGORITHMS_TO_EVALUATE)
summary = {}
for seq in dataset_dirs:
evaluate_on_sequence(seq, summary)
for category in summary:
for algo_name in summary[category]:
summary[category][algo_name] = np.mean(summary[category][algo_name], axis=0)
for category in summary:
print('=== SUMMARY for %s (Precision, Recall, F1, Accuracy) ===' % category)
for algo_name in summary[category]:
print('%05s: %.3f %.3f %.3f %.3f' % ((algo_name,) + tuple(summary[category][algo_name])))
if __name__ == '__main__':
main()
+41
View File
@@ -0,0 +1,41 @@
import numpy as np
import cv2 as cv
import argparse
import os
def main():
argparser = argparse.ArgumentParser(description='Vizualization of the LSBP/GSOC background subtraction algorithm.')
argparser.add_argument('-g', '--gt', help='Directory with ground-truth frames', required=True)
argparser.add_argument('-f', '--frames', help='Directory with input frames', required=True)
argparser.add_argument('-l', '--lsbp', help='Display LSBP instead of GSOC', default=False)
args = argparser.parse_args()
gt = map(lambda x: os.path.join(args.gt, x), os.listdir(args.gt))
gt.sort()
f = map(lambda x: os.path.join(args.frames, x), os.listdir(args.frames))
f.sort()
gt = np.uint8(map(lambda x: cv.imread(x, cv.IMREAD_GRAYSCALE), gt))
f = np.uint8(map(lambda x: cv.imread(x, cv.IMREAD_COLOR), f))
if not args.lsbp:
bgs = cv.bgsegm.createBackgroundSubtractorGSOC()
else:
bgs = cv.bgsegm.createBackgroundSubtractorLSBP()
for i in xrange(f.shape[0]):
cv.imshow('Frame', f[i])
cv.imshow('Ground-truth', gt[i])
mask = bgs.apply(f[i])
bg = bgs.getBackgroundImage()
cv.imshow('BG', bg)
cv.imshow('Output mask', mask)
k = cv.waitKey(0)
if k == 27:
break
if __name__ == '__main__':
main()
@@ -0,0 +1,26 @@
import cv2 as cv
import argparse
def main():
argparser = argparse.ArgumentParser(description='Vizualization of the SyntheticSequenceGenerator.')
argparser.add_argument('-b', '--background', help='Background image.', required=True)
argparser.add_argument('-o', '--obj', help='Object image. It must be strictly smaller than background.', required=True)
args = argparser.parse_args()
bg = cv.imread(args.background)
obj = cv.imread(args.obj)
generator = cv.bgsegm.createSyntheticSequenceGenerator(bg, obj)
while True:
frame, mask = generator.getNextFrame()
cv.imshow('Generated frame', frame)
cv.imshow('Generated mask', mask)
k = cv.waitKey(int(1000.0 / 30))
if k == 27:
break
if __name__ == '__main__':
main()
+485
View File
@@ -0,0 +1,485 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#include <float.h>
#include "opencv2/core/utils/logger.hpp"
// to make sure we can use these short names
#undef K
#undef L
#undef T
// This is based on the "An Improved Adaptive Background Mixture Model for
// Real-time Tracking with Shadow Detection" by P. KaewTraKulPong and R. Bowden
// http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
//
// The windowing method is used, but not the shadow detection. I make some of my
// own modifications which make more sense. There are some errors in some of their
// equations.
//
namespace cv
{
namespace bgsegm
{
static const int defaultNMixtures = 5;
static const int defaultHistory = 200;
static const double defaultBackgroundRatio = 0.7;
static const double defaultVarThreshold = 2.5*2.5;
static const double defaultNoiseSigma = 30*0.5;
static const double defaultInitialWeight = 0.05;
class BackgroundSubtractorMOGImpl CV_FINAL : public BackgroundSubtractorMOG
{
public:
//! the default constructor
BackgroundSubtractorMOGImpl()
{
frameSize = Size(0,0);
frameType = 0;
nframes = 0;
nmixtures = defaultNMixtures;
history = defaultHistory;
varThreshold = defaultVarThreshold;
backgroundRatio = defaultBackgroundRatio;
noiseSigma = defaultNoiseSigma;
name_ = "BackgroundSubtractor.MOG";
}
// the full constructor that takes the length of the history,
// the number of gaussian mixtures, the background ratio parameter and the noise strength
BackgroundSubtractorMOGImpl(int _history, int _nmixtures, double _backgroundRatio, double _noiseSigma=0)
{
frameSize = Size(0,0);
frameType = 0;
nframes = 0;
nmixtures = std::min(_nmixtures > 0 ? _nmixtures : defaultNMixtures, 8);
history = _history > 0 ? _history : defaultHistory;
varThreshold = defaultVarThreshold;
backgroundRatio = std::min(_backgroundRatio > 0 ? _backgroundRatio : 0.95, 1.);
noiseSigma = _noiseSigma <= 0 ? defaultNoiseSigma : _noiseSigma;
}
//! the update operator
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=0) CV_OVERRIDE;
virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
//! re-initiaization method
virtual void initialize(Size _frameSize, int _frameType)
{
frameSize = _frameSize;
frameType = _frameType;
nframes = 0;
int nchannels = CV_MAT_CN(frameType);
CV_Assert( CV_MAT_DEPTH(frameType) == CV_8U );
// for each gaussian mixture of each pixel bg model we store ...
// the mixture sort key (w/sum_of_variances), the mixture weight (w),
// the mean (nchannels values) and
// the diagonal covariance matrix (another nchannels values)
bgmodel.create( 1, frameSize.height*frameSize.width*nmixtures*(2 + 2*nchannels), CV_32F );
bgmodel = Scalar::all(0);
}
virtual void getBackgroundImage(OutputArray) const CV_OVERRIDE
{
CV_Error( Error::StsNotImplemented, "" );
}
virtual int getHistory() const CV_OVERRIDE { return history; }
virtual void setHistory(int _nframes) CV_OVERRIDE { history = _nframes; }
virtual int getNMixtures() const CV_OVERRIDE { return nmixtures; }
virtual void setNMixtures(int nmix) CV_OVERRIDE { nmixtures = nmix; }
virtual double getBackgroundRatio() const CV_OVERRIDE { return backgroundRatio; }
virtual void setBackgroundRatio(double _backgroundRatio) CV_OVERRIDE { backgroundRatio = _backgroundRatio; }
virtual double getNoiseSigma() const CV_OVERRIDE { return noiseSigma; }
virtual void setNoiseSigma(double _noiseSigma) CV_OVERRIDE { noiseSigma = _noiseSigma; }
virtual void write(FileStorage& fs) const CV_OVERRIDE
{
fs << "name" << name_
<< "history" << history
<< "nmixtures" << nmixtures
<< "backgroundRatio" << backgroundRatio
<< "noiseSigma" << noiseSigma;
}
virtual void read(const FileNode& fn) CV_OVERRIDE
{
CV_Assert( (String)fn["name"] == name_ );
history = (int)fn["history"];
nmixtures = (int)fn["nmixtures"];
backgroundRatio = (double)fn["backgroundRatio"];
noiseSigma = (double)fn["noiseSigma"];
}
protected:
Size frameSize;
int frameType;
Mat bgmodel;
int nframes;
int history;
int nmixtures;
double varThreshold;
double backgroundRatio;
double noiseSigma;
String name_;
};
template<typename VT> struct MixData
{
float sortKey;
float weight;
VT mean;
VT var;
};
static void process8uC1( const Mat& image, Mat& fgmask, double learningRate,
Mat& bgmodel, int nmixtures, double backgroundRatio,
double varThreshold, double noiseSigma )
{
int x, y, k, k1, rows = image.rows, cols = image.cols;
float alpha = (float)learningRate, T = (float)backgroundRatio, vT = (float)varThreshold;
int K = nmixtures;
MixData<float>* mptr = (MixData<float>*)bgmodel.data;
const float w0 = (float)defaultInitialWeight;
const float sk0 = (float)(w0/(defaultNoiseSigma*2));
const float var0 = (float)(defaultNoiseSigma*defaultNoiseSigma*4);
const float minVar = (float)(noiseSigma*noiseSigma);
for( y = 0; y < rows; y++ )
{
const uchar* src = image.ptr<uchar>(y);
uchar* dst = fgmask.ptr<uchar>(y);
if( alpha > 0 )
{
for( x = 0; x < cols; x++, mptr += K )
{
float wsum = 0;
float pix = src[x];
int kHit = -1, kForeground = -1;
for( k = 0; k < K; k++ )
{
float w = mptr[k].weight;
wsum += w;
if( w < FLT_EPSILON )
break;
float mu = mptr[k].mean;
float var = mptr[k].var;
float diff = pix - mu;
float d2 = diff*diff;
if( d2 < vT*var )
{
wsum -= w;
float dw = alpha*(1.f - w);
mptr[k].weight = w + dw;
mptr[k].mean = mu + alpha*diff;
var = std::max(var + alpha*(d2 - var), minVar);
mptr[k].var = var;
mptr[k].sortKey = w/std::sqrt(var);
for( k1 = k-1; k1 >= 0; k1-- )
{
if( mptr[k1].sortKey >= mptr[k1+1].sortKey )
break;
std::swap( mptr[k1], mptr[k1+1] );
}
kHit = k1+1;
break;
}
}
if( kHit < 0 ) // no appropriate gaussian mixture found at all, remove the weakest mixture and create a new one
{
kHit = k = std::min(k, K-1);
wsum += w0 - mptr[k].weight;
mptr[k].weight = w0;
mptr[k].mean = pix;
mptr[k].var = var0;
mptr[k].sortKey = sk0;
}
else
for( ; k < K; k++ )
wsum += mptr[k].weight;
float wscale = 1.f/wsum;
wsum = 0;
for( k = 0; k < K; k++ )
{
wsum += mptr[k].weight *= wscale;
mptr[k].sortKey *= wscale;
if( wsum > T && kForeground < 0 )
kForeground = k+1;
}
dst[x] = (uchar)(-(kHit >= kForeground));
}
}
else
{
for( x = 0; x < cols; x++, mptr += K )
{
float pix = src[x];
int kHit = -1, kForeground = -1;
for( k = 0; k < K; k++ )
{
if( mptr[k].weight < FLT_EPSILON )
break;
float mu = mptr[k].mean;
float var = mptr[k].var;
float diff = pix - mu;
float d2 = diff*diff;
if( d2 < vT*var )
{
kHit = k;
break;
}
}
if( kHit >= 0 )
{
float wsum = 0;
for( k = 0; k < K; k++ )
{
wsum += mptr[k].weight;
if( wsum > T )
{
kForeground = k+1;
break;
}
}
}
dst[x] = (uchar)(kHit < 0 || kHit >= kForeground ? 255 : 0);
}
}
}
}
static void process8uC3( const Mat& image, Mat& fgmask, double learningRate,
Mat& bgmodel, int nmixtures, double backgroundRatio,
double varThreshold, double noiseSigma )
{
int x, y, k, k1, rows = image.rows, cols = image.cols;
float alpha = (float)learningRate, T = (float)backgroundRatio, vT = (float)varThreshold;
int K = nmixtures;
const float w0 = (float)defaultInitialWeight;
const float sk0 = (float)(w0/(defaultNoiseSigma*2*std::sqrt(3.)));
const float var0 = (float)(defaultNoiseSigma*defaultNoiseSigma*4);
const float minVar = (float)(noiseSigma*noiseSigma);
MixData<Vec3f>* mptr = (MixData<Vec3f>*)bgmodel.data;
for( y = 0; y < rows; y++ )
{
const uchar* src = image.ptr<uchar>(y);
uchar* dst = fgmask.ptr<uchar>(y);
if( alpha > 0 )
{
for( x = 0; x < cols; x++, mptr += K )
{
float wsum = 0;
Vec3f pix(src[x*3], src[x*3+1], src[x*3+2]);
int kHit = -1, kForeground = -1;
for( k = 0; k < K; k++ )
{
float w = mptr[k].weight;
wsum += w;
if( w < FLT_EPSILON )
break;
Vec3f mu = mptr[k].mean;
Vec3f var = mptr[k].var;
Vec3f diff = pix - mu;
float d2 = diff.dot(diff);
if( d2 < vT*(var[0] + var[1] + var[2]) )
{
wsum -= w;
float dw = alpha*(1.f - w);
mptr[k].weight = w + dw;
mptr[k].mean = mu + alpha*diff;
var = Vec3f(std::max(var[0] + alpha*(diff[0]*diff[0] - var[0]), minVar),
std::max(var[1] + alpha*(diff[1]*diff[1] - var[1]), minVar),
std::max(var[2] + alpha*(diff[2]*diff[2] - var[2]), minVar));
mptr[k].var = var;
mptr[k].sortKey = w/std::sqrt(var[0] + var[1] + var[2]);
for( k1 = k-1; k1 >= 0; k1-- )
{
if( mptr[k1].sortKey >= mptr[k1+1].sortKey )
break;
std::swap( mptr[k1], mptr[k1+1] );
}
kHit = k1+1;
break;
}
}
if( kHit < 0 ) // no appropriate gaussian mixture found at all, remove the weakest mixture and create a new one
{
kHit = k = std::min(k, K-1);
wsum += w0 - mptr[k].weight;
mptr[k].weight = w0;
mptr[k].mean = pix;
mptr[k].var = Vec3f(var0, var0, var0);
mptr[k].sortKey = sk0;
}
else
for( ; k < K; k++ )
wsum += mptr[k].weight;
float wscale = 1.f/wsum;
wsum = 0;
for( k = 0; k < K; k++ )
{
wsum += mptr[k].weight *= wscale;
mptr[k].sortKey *= wscale;
if( wsum > T && kForeground < 0 )
kForeground = k+1;
}
dst[x] = (uchar)(-(kHit >= kForeground));
}
}
else
{
for( x = 0; x < cols; x++, mptr += K )
{
Vec3f pix(src[x*3], src[x*3+1], src[x*3+2]);
int kHit = -1, kForeground = -1;
for( k = 0; k < K; k++ )
{
if( mptr[k].weight < FLT_EPSILON )
break;
Vec3f mu = mptr[k].mean;
Vec3f var = mptr[k].var;
Vec3f diff = pix - mu;
float d2 = diff.dot(diff);
if( d2 < vT*(var[0] + var[1] + var[2]) )
{
kHit = k;
break;
}
}
if( kHit >= 0 )
{
float wsum = 0;
for( k = 0; k < K; k++ )
{
wsum += mptr[k].weight;
if( wsum > T )
{
kForeground = k+1;
break;
}
}
}
dst[x] = (uchar)(kHit < 0 || kHit >= kForeground ? 255 : 0);
}
}
}
}
void BackgroundSubtractorMOGImpl::apply(InputArray _image, OutputArray _fgmask, double learningRate)
{
Mat image = _image.getMat();
bool needToInitialize = nframes == 0 || learningRate >= 1 || image.size() != frameSize || image.type() != frameType;
if( needToInitialize )
initialize(image.size(), image.type());
CV_Assert( image.depth() == CV_8U );
_fgmask.create( image.size(), CV_8U );
Mat fgmask = _fgmask.getMat();
++nframes;
learningRate = learningRate >= 0 && nframes > 1 ? learningRate : 1./std::min( nframes, history );
CV_Assert(learningRate >= 0);
if( image.type() == CV_8UC1 )
process8uC1( image, fgmask, learningRate, bgmodel, nmixtures, backgroundRatio, varThreshold, noiseSigma );
else if( image.type() == CV_8UC3 )
process8uC3( image, fgmask, learningRate, bgmodel, nmixtures, backgroundRatio, varThreshold, noiseSigma );
else
CV_Error( Error::StsUnsupportedFormat, "Only 1- and 3-channel 8-bit images are supported in BackgroundSubtractorMOG" );
}
void BackgroundSubtractorMOGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){
Mat knownForegroundMask = _knownForegroundMask.getMat();
if(!_knownForegroundMask.empty())
{
CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
}
apply(_image, _fgmask, learningRate);
}
Ptr<BackgroundSubtractorMOG> createBackgroundSubtractorMOG(int history, int nmixtures,
double backgroundRatio, double noiseSigma)
{
return makePtr<BackgroundSubtractorMOGImpl>(history, nmixtures, backgroundRatio, noiseSigma);
}
}
}
/* End of file. */
+535
View File
@@ -0,0 +1,535 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
/*
* This class implements a particular BackgroundSubtraction algorithm described in "Visual Tracking of Human Visitors under
* Variable-Lighting Conditions for a Responsive Audio Art Installation," A. Godbehere,
* A. Matsukawa, K. Goldberg, American Control Conference, Montreal, June 2012.
*
* Prepared and integrated by Andrew B. Godbehere.
*/
#include "precomp.hpp"
#include "opencv2/core/utility.hpp"
#include <limits>
#include "opencv2/core/utils/logger.hpp"
namespace cv
{
namespace bgsegm
{
class BackgroundSubtractorGMGImpl CV_FINAL : public BackgroundSubtractorGMG
{
public:
BackgroundSubtractorGMGImpl()
{
/*
* Default Parameter Values. Override with algorithm "set" method.
*/
maxFeatures = 64;
learningRate = 0.025;
numInitializationFrames = 120;
quantizationLevels = 16;
backgroundPrior = 0.8;
decisionThreshold = 0.8;
smoothingRadius = 7;
updateBackgroundModel = true;
minVal_ = maxVal_ = 0;
name_ = "BackgroundSubtractor.GMG";
}
~BackgroundSubtractorGMGImpl()
{
}
/**
* Validate parameters and set up data structures for appropriate image size.
* Must call before running on data.
* @param frameSize input frame size
* @param min minimum value taken on by pixels in image sequence. Usually 0
* @param max maximum value taken on by pixels in image sequence. e.g. 1.0 or 255
*/
void initialize(Size frameSize, double minVal, double maxVal);
/**
* Performs single-frame background subtraction and builds up a statistical background image
* model.
* @param image Input image
* @param fgmask Output mask image representing foreground and background pixels
*/
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1.0) CV_OVERRIDE;
virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
/**
* Releases all inner buffers.
*/
void release();
virtual int getMaxFeatures() const CV_OVERRIDE { return maxFeatures; }
virtual void setMaxFeatures(int _maxFeatures) CV_OVERRIDE { maxFeatures = _maxFeatures; }
virtual double getDefaultLearningRate() const CV_OVERRIDE { return learningRate; }
virtual void setDefaultLearningRate(double lr) CV_OVERRIDE { learningRate = lr; }
virtual int getNumFrames() const CV_OVERRIDE { return numInitializationFrames; }
virtual void setNumFrames(int nframes) CV_OVERRIDE { numInitializationFrames = nframes; }
virtual int getQuantizationLevels() const CV_OVERRIDE { return quantizationLevels; }
virtual void setQuantizationLevels(int nlevels) CV_OVERRIDE { quantizationLevels = nlevels; }
virtual double getBackgroundPrior() const CV_OVERRIDE { return backgroundPrior; }
virtual void setBackgroundPrior(double bgprior) CV_OVERRIDE { backgroundPrior = bgprior; }
virtual int getSmoothingRadius() const CV_OVERRIDE { return smoothingRadius; }
virtual void setSmoothingRadius(int radius) CV_OVERRIDE { smoothingRadius = radius; }
virtual double getDecisionThreshold() const CV_OVERRIDE { return decisionThreshold; }
virtual void setDecisionThreshold(double thresh) CV_OVERRIDE { decisionThreshold = thresh; }
virtual bool getUpdateBackgroundModel() const CV_OVERRIDE { return updateBackgroundModel; }
virtual void setUpdateBackgroundModel(bool update) CV_OVERRIDE { updateBackgroundModel = update; }
virtual double getMinVal() const CV_OVERRIDE { return minVal_; }
virtual void setMinVal(double val) CV_OVERRIDE { minVal_ = val; }
virtual double getMaxVal() const CV_OVERRIDE { return maxVal_; }
virtual void setMaxVal(double val) CV_OVERRIDE { maxVal_ = val; }
virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE
{
backgroundImage.release();
}
virtual void write(FileStorage& fs) const CV_OVERRIDE
{
fs << "name" << name_
<< "maxFeatures" << maxFeatures
<< "defaultLearningRate" << learningRate
<< "numFrames" << numInitializationFrames
<< "quantizationLevels" << quantizationLevels
<< "backgroundPrior" << backgroundPrior
<< "decisionThreshold" << decisionThreshold
<< "smoothingRadius" << smoothingRadius
<< "updateBackgroundModel" << (int)updateBackgroundModel;
// we do not save minVal_ & maxVal_, since they depend on the image type.
}
virtual void read(const FileNode& fn) CV_OVERRIDE
{
CV_Assert( (String)fn["name"] == name_ );
maxFeatures = (int)fn["maxFeatures"];
learningRate = (double)fn["defaultLearningRate"];
numInitializationFrames = (int)fn["numFrames"];
quantizationLevels = (int)fn["quantizationLevels"];
backgroundPrior = (double)fn["backgroundPrior"];
smoothingRadius = (int)fn["smoothingRadius"];
decisionThreshold = (double)fn["decisionThreshold"];
updateBackgroundModel = (int)fn["updateBackgroundModel"] != 0;
minVal_ = maxVal_ = 0;
frameSize_ = Size();
}
//! Total number of distinct colors to maintain in histogram.
int maxFeatures;
//! Set between 0.0 and 1.0, determines how quickly features are "forgotten" from histograms.
double learningRate;
//! Number of frames of video to use to initialize histograms.
int numInitializationFrames;
//! Number of discrete levels in each channel to be used in histograms.
int quantizationLevels;
//! Prior probability that any given pixel is a background pixel. A sensitivity parameter.
double backgroundPrior;
//! Value above which pixel is determined to be FG.
double decisionThreshold;
//! Smoothing radius, in pixels, for cleaning up FG image.
int smoothingRadius;
//! Perform background model update
bool updateBackgroundModel;
private:
double maxVal_;
double minVal_;
Size frameSize_;
int frameNum_;
String name_;
Mat_<int> nfeatures_;
Mat_<int> colors_;
Mat_<float> weights_;
};
void BackgroundSubtractorGMGImpl::initialize(Size frameSize, double minVal, double maxVal)
{
CV_Assert(minVal < maxVal);
CV_Assert(maxFeatures > 0);
CV_Assert(learningRate >= 0.0 && learningRate <= 1.0);
CV_Assert(numInitializationFrames >= 1);
CV_Assert(quantizationLevels >= 1 && quantizationLevels <= 255);
CV_Assert(backgroundPrior >= 0.0 && backgroundPrior <= 1.0);
minVal_ = minVal;
maxVal_ = maxVal;
frameSize_ = frameSize;
frameNum_ = 0;
nfeatures_.create(frameSize_);
colors_.create(frameSize_.area(), maxFeatures);
weights_.create(frameSize_.area(), maxFeatures);
nfeatures_.setTo(Scalar::all(0));
}
static float findFeature(int color, const int* colors, const float* weights, int nfeatures)
{
for (int i = 0; i < nfeatures; ++i)
{
if (color == colors[i])
return weights[i];
}
// not in histogram, so return 0.
return 0.0f;
}
static void normalizeHistogram(float* weights, int nfeatures)
{
float total = 0.0f;
for (int i = 0; i < nfeatures; ++i)
total += weights[i];
if (total != 0.0f)
{
for (int i = 0; i < nfeatures; ++i)
weights[i] /= total;
}
}
static bool insertFeature(int color, float weight, int* colors, float* weights, int& nfeatures, int maxFeatures)
{
int idx = -1;
for (int i = 0; i < nfeatures; ++i)
{
if (color == colors[i])
{
// feature in histogram
weight += weights[i];
idx = i;
break;
}
}
if (idx >= 0)
{
// move feature to beginning of list
::memmove(colors + 1, colors, idx * sizeof(int));
::memmove(weights + 1, weights, idx * sizeof(float));
colors[0] = color;
weights[0] = weight;
}
else if (nfeatures == maxFeatures)
{
// discard oldest feature
::memmove(colors + 1, colors, (nfeatures - 1) * sizeof(int));
::memmove(weights + 1, weights, (nfeatures - 1) * sizeof(float));
colors[0] = color;
weights[0] = weight;
}
else
{
colors[nfeatures] = color;
weights[nfeatures] = weight;
++nfeatures;
return true;
}
return false;
}
template <typename T> struct Quantization
{
static int apply(const void* src_, int x, int cn, double minVal, double maxVal, int quantizationLevels)
{
const T* src = static_cast<const T*>(src_);
src += x * cn;
unsigned int res = 0;
for (int i = 0, shift = 0; i < cn; ++i, ++src, shift += 8)
res |= static_cast<int>((*src - minVal) * quantizationLevels / (maxVal - minVal)) << shift;
return res;
}
};
class GMG_LoopBody : public ParallelLoopBody
{
public:
GMG_LoopBody(const Mat& frame, const Mat& fgmask, const Mat_<int>& nfeatures, const Mat_<int>& colors, const Mat_<float>& weights,
int maxFeatures, double learningRate, int numInitializationFrames, int quantizationLevels, double backgroundPrior, double decisionThreshold,
double maxVal, double minVal, int frameNum, bool updateBackgroundModel) :
frame_(frame), fgmask_(fgmask), nfeatures_(nfeatures), colors_(colors), weights_(weights),
maxFeatures_(maxFeatures), learningRate_(learningRate), numInitializationFrames_(numInitializationFrames), quantizationLevels_(quantizationLevels),
backgroundPrior_(backgroundPrior), decisionThreshold_(decisionThreshold), updateBackgroundModel_(updateBackgroundModel),
maxVal_(maxVal), minVal_(minVal), frameNum_(frameNum)
{
}
void operator() (const Range& range) const CV_OVERRIDE;
private:
Mat frame_;
mutable Mat_<uchar> fgmask_;
mutable Mat_<int> nfeatures_;
mutable Mat_<int> colors_;
mutable Mat_<float> weights_;
int maxFeatures_;
double learningRate_;
int numInitializationFrames_;
int quantizationLevels_;
double backgroundPrior_;
double decisionThreshold_;
bool updateBackgroundModel_;
double maxVal_;
double minVal_;
int frameNum_;
};
void GMG_LoopBody::operator() (const Range& range) const
{
typedef int (*func_t)(const void* src_, int x, int cn, double minVal, double maxVal, int quantizationLevels);
static const func_t funcs[] =
{
Quantization<uchar>::apply,
Quantization<schar>::apply,
Quantization<ushort>::apply,
Quantization<short>::apply,
Quantization<int>::apply,
Quantization<float>::apply,
Quantization<double>::apply
};
const func_t func = funcs[frame_.depth()];
CV_Assert(func != 0);
const int cn = frame_.channels();
for (int y = range.start, featureIdx = y * frame_.cols; y < range.end; ++y)
{
const uchar* frame_row = frame_.ptr(y);
int* nfeatures_row = nfeatures_[y];
uchar* fgmask_row = fgmask_[y];
for (int x = 0; x < frame_.cols; ++x, ++featureIdx)
{
int nfeatures = nfeatures_row[x];
int* colors = colors_[featureIdx];
float* weights = weights_[featureIdx];
int newFeatureColor = func(frame_row, x, cn, minVal_, maxVal_, quantizationLevels_);
bool isForeground = false;
if (frameNum_ >= numInitializationFrames_)
{
// typical operation
const double weight = findFeature(newFeatureColor, colors, weights, nfeatures);
// see Godbehere, Matsukawa, Goldberg (2012) for reasoning behind this implementation of Bayes rule
const double posterior = (weight * backgroundPrior_) / (weight * backgroundPrior_ + (1.0 - weight) * (1.0 - backgroundPrior_));
isForeground = ((1.0 - posterior) > decisionThreshold_);
// update histogram.
if (updateBackgroundModel_)
{
for (int i = 0; i < nfeatures; ++i)
weights[i] *= (float)(1.0f - learningRate_);
bool inserted = insertFeature(newFeatureColor, (float)learningRate_, colors, weights, nfeatures, maxFeatures_);
if (inserted)
{
normalizeHistogram(weights, nfeatures);
nfeatures_row[x] = nfeatures;
}
}
}
else if (updateBackgroundModel_)
{
// training-mode update
insertFeature(newFeatureColor, 1.0f, colors, weights, nfeatures, maxFeatures_);
if (frameNum_ == numInitializationFrames_ - 1)
normalizeHistogram(weights, nfeatures);
}
fgmask_row[x] = (uchar)(-(schar)isForeground);
}
}
}
void BackgroundSubtractorGMGImpl::apply(InputArray _frame, OutputArray _fgmask, double newLearningRate)
{
Mat frame = _frame.getMat();
const int depth = frame.depth();
CV_CheckDepth(depth, (depth == CV_8U) || (depth == CV_8S) ||
(depth == CV_16U) || (depth == CV_16S) ||
(depth == CV_32S) ||
(depth == CV_32F) || (depth == CV_64F), "Unsupported depth");
CV_CheckGE(frame.channels(), 1, "Unsupported channels");
CV_CheckLE(frame.channels(), 4, "Unsupported channels");
if (newLearningRate != -1.0)
{
CV_Assert(newLearningRate >= 0.0 && newLearningRate <= 1.0);
learningRate = newLearningRate;
}
if (frame.size() != frameSize_)
{
double minval = minVal_;
double maxval = maxVal_;
if( minVal_ == 0 && maxVal_ == 0 )
{
if( depth == CV_8U ) { minval = std::numeric_limits<uint8_t>::min(); maxval = std::numeric_limits<uint8_t>::max(); }
else if( depth == CV_8S ) { minval = std::numeric_limits<int8_t>::min(); maxval = std::numeric_limits<int8_t>::max(); }
else if( depth == CV_16U ) { minval = std::numeric_limits<uint16_t>::min();maxval = std::numeric_limits<uint16_t>::max();}
else if( depth == CV_16S ) { minval = std::numeric_limits<int16_t>::min(); maxval = std::numeric_limits<int16_t>::max(); }
else if( depth == CV_32S ) { minval = std::numeric_limits<int32_t>::min(); maxval = std::numeric_limits<int32_t>::max(); }
else /* CV_32F or CV_64F */ { minval = 0.0; maxval = 1.0; }
}
initialize(frame.size(), minval, maxval);
}
_fgmask.create(frameSize_, CV_8UC1);
Mat fgmask = _fgmask.getMat();
GMG_LoopBody body(frame, fgmask, nfeatures_, colors_, weights_,
maxFeatures, learningRate, numInitializationFrames, quantizationLevels, backgroundPrior, decisionThreshold,
maxVal_, minVal_, frameNum_, updateBackgroundModel);
parallel_for_(Range(0, frame.rows), body, frame.total()/(double)(1<<16));
if (smoothingRadius > 0)
{
medianBlur(fgmask, fgmask, smoothingRadius);
}
// keep track of how many frames we have processed
++frameNum_;
}
void BackgroundSubtractorGMGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double newLearningRate){
Mat knownForegroundMask = _knownForegroundMask.getMat();
if(!_knownForegroundMask.empty())
{
CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
}
apply(_image, _fgmask, newLearningRate);
}
void BackgroundSubtractorGMGImpl::release()
{
frameSize_ = Size();
nfeatures_.release();
colors_.release();
weights_.release();
}
Ptr<BackgroundSubtractorGMG> createBackgroundSubtractorGMG(int initializationFrames, double decisionThreshold)
{
Ptr<BackgroundSubtractorGMG> bgfg = makePtr<BackgroundSubtractorGMGImpl>();
bgfg->setNumFrames(initializationFrames);
bgfg->setDecisionThreshold(decisionThreshold);
return bgfg;
}
/*
///////////////////////////////////////////////////////////////////////////////////////////////////////////
CV_INIT_ALGORITHM(BackgroundSubtractorGMG, "BackgroundSubtractor.GMG",
obj.info()->addParam(obj, "maxFeatures", obj.maxFeatures,false,0,0,
"Maximum number of features to store in histogram. Harsh enforcement of sparsity constraint.");
obj.info()->addParam(obj, "learningRate", obj.learningRate,false,0,0,
"Adaptation rate of histogram. Close to 1, slow adaptation. Close to 0, fast adaptation, features forgotten quickly.");
obj.info()->addParam(obj, "initializationFrames", obj.numInitializationFrames,false,0,0,
"Number of frames to use to initialize histograms of pixels.");
obj.info()->addParam(obj, "quantizationLevels", obj.quantizationLevels,false,0,0,
"Number of discrete colors to be used in histograms. Up-front quantization.");
obj.info()->addParam(obj, "backgroundPrior", obj.backgroundPrior,false,0,0,
"Prior probability that each individual pixel is a background pixel.");
obj.info()->addParam(obj, "smoothingRadius", obj.smoothingRadius,false,0,0,
"Radius of smoothing kernel to filter noise from FG mask image.");
obj.info()->addParam(obj, "decisionThreshold", obj.decisionThreshold,false,0,0,
"Threshold for FG decision rule. Pixel is FG if posterior probability exceeds threshold.");
obj.info()->addParam(obj, "updateBackgroundModel", obj.updateBackgroundModel,false,0,0,
"Perform background model update.");
obj.info()->addParam(obj, "minVal", obj.minVal_,false,0,0,
"Minimum of the value range (mostly for regression testing)");
obj.info()->addParam(obj, "maxVal", obj.maxVal_,false,0,0,
"Maximum of the value range (mostly for regression testing)");
);
*/
}
}
File diff suppressed because it is too large Load Diff
+432
View File
@@ -0,0 +1,432 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// (3-clause BSD License)
// For BackgroundSubtractorCNT
// (Background Subtraction based on Counting)
//
// Copyright (C) 2016, Sagi Zeevi (www.theimpossiblecode.com), all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#include <functional>
#include "opencv2/core/utils/logger.hpp"
namespace cv
{
namespace bgsegm
{
class BackgroundSubtractorCNTImpl CV_FINAL : public BackgroundSubtractorCNT
{
public:
BackgroundSubtractorCNTImpl(int minStability,
bool useHistory,
int maxStability,
bool isParallel);
// BackgroundSubtractor interface
virtual void apply(InputArray image, OutputArray fgmask, double learningRate) CV_OVERRIDE;
virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE;
int getMinPixelStability() const CV_OVERRIDE;
void setMinPixelStability(int value) CV_OVERRIDE;
int getMaxPixelStability() const CV_OVERRIDE;
void setMaxPixelStability(int value) CV_OVERRIDE;
bool getUseHistory() const CV_OVERRIDE;
void setUseHistory(bool value) CV_OVERRIDE;
bool getIsParallel() const CV_OVERRIDE;
void setIsParallel(bool value) CV_OVERRIDE;
//! the destructor
virtual ~BackgroundSubtractorCNTImpl() {}
private:
int minPixelStability;
int maxPixelStability;
int threshold;
bool useHistory;
bool isParallel;
// These 3 commented expressed in 1 'data' for faster single access
// Mat_<int> stability; // data[0] => Candidate for historyStability if pixel is ~same as in prevFrame
// Mat_<int> history; // data[1] => Color which got most hits for the past maxPixelStability frames
// Mat_<int> historyStability; // data[2] => How many hits this pixel got for the color in history
// Mat_<int> background; // data[3] => Current background as detected by algorithm
Mat_<Vec4i> data;
Mat prevFrame;
Mat fgMaskPrev;
};
BackgroundSubtractorCNTImpl::BackgroundSubtractorCNTImpl(int minStability,
bool _useHistory,
int maxStability,
bool _isParallel)
: minPixelStability(minStability),
maxPixelStability(maxStability),
threshold(5),
useHistory(_useHistory),
isParallel(_isParallel)
{
}
void BackgroundSubtractorCNTImpl::getBackgroundImage(OutputArray _backgroundImage) const
{
CV_Assert(! data.empty());
_backgroundImage.create(prevFrame.size(), CV_8U); // OutputArray usage requires this step
Mat backgroundImage = _backgroundImage.getMat();
// mixChannels requires same types to mix,
// so imixing with tmp Mat and conerting
Mat_<int> tmp(prevFrame.rows, prevFrame.cols);
int from_bg_model_to_user[] = {3, 0};
mixChannels(&data, 1, &tmp, 1, from_bg_model_to_user, 1);
tmp.convertTo(backgroundImage, CV_8U);
}
int BackgroundSubtractorCNTImpl::getMinPixelStability() const
{
return minPixelStability;
}
void BackgroundSubtractorCNTImpl::setMinPixelStability(int value)
{
CV_Assert(value > 0 && value < maxPixelStability);
minPixelStability = value;
}
int BackgroundSubtractorCNTImpl::getMaxPixelStability() const
{
return maxPixelStability;
}
void BackgroundSubtractorCNTImpl::setMaxPixelStability(int value)
{
CV_Assert(value > minPixelStability);
maxPixelStability = value;
}
bool BackgroundSubtractorCNTImpl::getUseHistory() const
{
return useHistory;
}
void BackgroundSubtractorCNTImpl::setUseHistory(bool value)
{
useHistory = value;
}
bool BackgroundSubtractorCNTImpl::getIsParallel() const
{
return isParallel;
}
void BackgroundSubtractorCNTImpl::setIsParallel(bool value)
{
isParallel = value;
}
class CNTFunctor
{
public:
virtual void operator()(Vec4i &vec, uchar currColor, uchar prevColor, uchar &fgMaskPixelRef) = 0;
//! the destructor
virtual ~CNTFunctor() {}
};
struct BGSubtractPixel : public CNTFunctor
{
BGSubtractPixel(int _minPixelStability, int _threshold,
const Mat &_frame, const Mat &_prevFrame, Mat &_fgMask)
: minPixelStability(_minPixelStability),
threshold(_threshold),
frame(_frame),
prevFrame(_prevFrame),
fgMask(_fgMask)
{}
//! the destructor
virtual ~BGSubtractPixel() {}
void operator()(Vec4i &vec, uchar currColor, uchar prevColor, uchar &fgMaskPixelRef) CV_OVERRIDE
{
int &stabilityRef = vec[0];
int &bgImgRef = vec[3];
if (abs(currColor - prevColor) < threshold)
{
++stabilityRef;
if (stabilityRef == minPixelStability)
{ // bg
--stabilityRef;
bgImgRef = prevColor;
}
else
{ // fg
fgMaskPixelRef = 255;
}
}
else
{ // fg
stabilityRef = 0;
fgMaskPixelRef = 255;
}
}
int minPixelStability;
int threshold;
const Mat &frame;
const Mat &prevFrame;
Mat &fgMask;
};
struct BGSubtractPixelWithHistory : public CNTFunctor
{
BGSubtractPixelWithHistory(int _minPixelStability, int _maxPixelStability, int _threshold,
const Mat &_frame, const Mat &_prevFrame, Mat &_fgMask)
: minPixelStability(_minPixelStability),
maxPixelStability(_maxPixelStability),
threshold(_threshold),
thresholdHistory(30),
frame(_frame),
prevFrame(_prevFrame),
fgMask(_fgMask)
{}
//! the destructor
virtual ~BGSubtractPixelWithHistory() {}
void incrStability(int &histStabilityRef)
{
if (histStabilityRef < maxPixelStability)
{
++histStabilityRef;
}
}
void decrStability(int &histStabilityRef)
{
if (histStabilityRef > 0)
{
--histStabilityRef;
}
}
void operator()(Vec4i &vec, uchar currColor, uchar prevColor, uchar &fgMaskPixelRef) CV_OVERRIDE
{
int &stabilityRef = vec[0];
int &historyColorRef = vec[1];
int &histStabilityRef = vec[2];
int &bgImgRef = vec[3];
if (abs(currColor - historyColorRef) < thresholdHistory)
{ // No change compared to history - this is maybe a background
stabilityRef = 0;
incrStability(histStabilityRef);
if (histStabilityRef <= minPixelStability)
{
fgMaskPixelRef = 255;
}
else
{
bgImgRef = historyColorRef;
}
}
else if (abs(currColor - prevColor) < threshold)
{ // No change compared to prev - this is maybe a background
incrStability(stabilityRef);
if (stabilityRef > minPixelStability)
{ // Stable color - this is maybe a background
if (stabilityRef >= histStabilityRef)
{
historyColorRef = currColor;
histStabilityRef = stabilityRef;
bgImgRef = historyColorRef;
}
else
{ // Stable but different from stable history - this is a foreground
decrStability(histStabilityRef);
fgMaskPixelRef = 255;
}
}
else
{ // This is FG.
fgMaskPixelRef = 255;
}
}
else
{ // Color changed - this is defently a foreground
stabilityRef = 0;
decrStability(histStabilityRef);
fgMaskPixelRef = 255;
}
}
int minPixelStability;
int maxPixelStability;
int threshold;
int thresholdHistory;
const Mat &frame;
const Mat &prevFrame;
Mat &fgMask;
};
class CNTInvoker : public ParallelLoopBody
{
public:
CNTInvoker(Mat_<Vec4i> &_data, Mat &_img, Mat &_prevFrame, Mat &_fgMask, CNTFunctor &_functor)
: data(_data), img(_img), prevFrame(_prevFrame), fgMask(_fgMask), functor(_functor)
{
}
// Iterate rows
void operator()(const Range& range) const CV_OVERRIDE
{
for (int r = range.start; r < range.end; ++r)
{
Vec4i* row = data.ptr<Vec4i>(r);
uchar* frameRow = img.ptr<uchar>(r);
uchar* prevFrameRow = prevFrame.ptr<uchar>(r);
uchar* fgMaskRow = fgMask.ptr<uchar>(r);
for (int c = 0; c < data.cols; ++c)
{
functor(row[c], frameRow[c], prevFrameRow[c], fgMaskRow[c]);
}
}
}
private:
Mat_<Vec4i> &data;
Mat &img;
Mat &prevFrame;
Mat &fgMask;
CNTFunctor &functor;
};
void BackgroundSubtractorCNTImpl::apply(InputArray image, OutputArray _fgmask, double learningRate)
{
CV_Assert(image.depth() == CV_8U);
Mat frameIn = image.getMat();
if(frameIn.channels() != 1)
cvtColor(frameIn, frameIn, COLOR_BGR2GRAY);
_fgmask.create(image.size(), CV_8U); // OutputArray usage requires this step
Mat fgMask = _fgmask.getMat();
bool needToInitialize = data.empty() || learningRate >= 1 || frameIn.size() != prevFrame.size();
Mat frame = frameIn.clone();
if (needToInitialize)
{ // Usually done only once
data = Mat_<Vec4i>::zeros(frame.rows, frame.cols);
prevFrame = frame;
// mixChannels requires same types to mix,
// so imixing with tmp Mat and conerting
Mat tmp;
prevFrame.convertTo(tmp, CV_32S);
int from_gray_to_history_color[] = {0,1};
mixChannels(&tmp, 1, &data, 1, from_gray_to_history_color, 1);
}
fgMask = Scalar(0);
CNTFunctor *functor;
if (useHistory && learningRate)
{
double scaleMaxStability = 1.0;
if (learningRate > 0 && learningRate < 1.0)
{
scaleMaxStability = learningRate;
}
functor = new BGSubtractPixelWithHistory(minPixelStability, int(maxPixelStability * scaleMaxStability),
threshold, frame, prevFrame, fgMask);
}
else
{
functor = new BGSubtractPixel(minPixelStability, threshold*3, frame, prevFrame, fgMask);
}
if (isParallel)
{
parallel_for_(Range(0, frame.rows),
CNTInvoker(data, frame, prevFrame, fgMask, *functor));
}
else
{
for (int r = 0; r < data.rows; ++r)
{
Vec4i* row = data.ptr<Vec4i>(r);
uchar* frameRow = frame.ptr<uchar>(r);
uchar* prevFrameRow = prevFrame.ptr<uchar>(r);
uchar* fgMaskRow = fgMask.ptr<uchar>(r);
for (int c = 0; c < data.cols; ++c)
{
(*functor)(row[c], frameRow[c], prevFrameRow[c], fgMaskRow[c]);
}
}
}
delete functor;
prevFrame = frame;
}
void BackgroundSubtractorCNTImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){
Mat knownForegroundMask = _knownForegroundMask.getMat();
if(!_knownForegroundMask.empty())
{
CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
}
apply(_image, _fgmask, learningRate);
}
Ptr<BackgroundSubtractorCNT> createBackgroundSubtractorCNT(int minPixelStability, bool useHistory, int maxStability, bool isParallel)
{
return makePtr<BackgroundSubtractorCNTImpl>(minPixelStability, useHistory, maxStability, isParallel);
}
}
}
/* End of file. */
+52
View File
@@ -0,0 +1,52 @@
/*
By downloading, copying, installing or using the software you agree to this
license. If you do not agree to this license, do not download, install,
copy or use the software.
License Agreement
For Open Source Computer Vision Library
(3-clause BSD License)
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
Third party copyrights are property of their respective owners.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the names of the copyright holders nor the names of the contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall copyright holders or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
*/
#ifndef __OPENCV_BGSEGM_PRECOMP_HPP__
#define __OPENCV_BGSEGM_PRECOMP_HPP__
#include <opencv2/bgsegm.hpp>
#include <opencv2/video.hpp>
#include <opencv2/imgproc.hpp>
#include <algorithm>
#include <cmath>
#endif
+221
View File
@@ -0,0 +1,221 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
/**
* @file synthetic_seq.cpp
* @author Vladislav Samsonov <vvladxx@gmail.com>
* @brief Synthetic frame sequence generator for testing background subtraction algorithms.
*
*/
#include "precomp.hpp"
namespace cv
{
namespace bgsegm
{
namespace
{
inline int clamp(int x, int l, int u) {
return ((x) < (l)) ? (l) : (((x) > (u)) ? (u) : (x));
}
inline int within(int a, int b, int c) {
return (((a) <= (b)) && ((b) <= (c))) ? 1 : 0;
}
void bilinearInterp(uchar* dest, double x, double y, unsigned bpp, const uchar** values) {
x = std::fmod(x, 1.0);
y = std::fmod(y, 1.0);
if (x < 0.0)
x += 1.0;
if (y < 0.0)
y += 1.0;
for (unsigned i = 0; i < bpp; i++) {
double m0 = (1.0 - x) * values[0][i] + x * values[1][i];
double m1 = (1.0 - x) * values[2][i] + x * values[3][i];
dest[i] = (uchar) ((1.0 - y) * m0 + y * m1);
}
}
// Static background is a way too easy test. We will add distortion to it.
void waveDistortion(const uchar* src, uchar* dst, int width, int height, int bypp, double amplitude, double wavelength, double phase) {
const uchar zeroes[4] = {0, 0, 0, 0};
const long rowsiz = width * bypp;
const double xhsiz = (double) width / 2.0;
const double yhsiz = (double) height / 2.0;
double xscale, yscale;
if (xhsiz < yhsiz) {
xscale = yhsiz / xhsiz;
yscale = 1.0;
}
else if (xhsiz > yhsiz) {
xscale = 1.0;
yscale = xhsiz / yhsiz;
}
else {
xscale = 1.0;
yscale = 1.0;
}
wavelength *= 2;
for (int y = 0; y < height; y++) {
uchar* dest = dst;
for (int x = 0; x < width; x++) {
const double dx = x * xscale;
const double dy = y * yscale;
const double d = sqrt (dx * dx + dy * dy);
const double amnt = amplitude * sin(((d / wavelength) * (2.0 * M_PI) + phase));
const double needx = (amnt + dx) / xscale;
const double needy = (amnt + dy) / yscale;
const int xi = clamp(int(needx), 0, width - 2);
const int yi = clamp(int(needy), 0, height - 2);
const uchar* p = src + rowsiz * yi + xi * bypp;
const int x1_in = within(0, xi, width - 1);
const int y1_in = within(0, yi, height - 1);
const int x2_in = within(0, xi + 1, width - 1);
const int y2_in = within(0, yi + 1, height - 1);
const uchar* values[4];
if (x1_in && y1_in)
values[0] = p;
else
values[0] = zeroes;
if (x2_in && y1_in)
values[1] = p + bypp;
else
values[1] = zeroes;
if (x1_in && y2_in)
values[2] = p + rowsiz;
else
values[2] = zeroes;
if (x2_in && y2_in)
values[3] = p + bypp + rowsiz;
else
values[3] = zeroes;
bilinearInterp(dest, needx, needy, bypp, values);
dest += bypp;
}
dst += rowsiz;
}
}
}
SyntheticSequenceGenerator::SyntheticSequenceGenerator(InputArray _background, InputArray _object, double _amplitude, double _wavelength, double _wavespeed, double _objspeed)
: amplitude(_amplitude), wavelength(_wavelength), wavespeed(_wavespeed), objspeed(_objspeed), timeStep(0) {
_background.getMat().copyTo(background);
_object.getMat().copyTo(object);
if (background.channels() == 1) {
cvtColor(background, background, COLOR_GRAY2BGR);
}
if (object.channels() == 1) {
cvtColor(object, object, COLOR_GRAY2BGR);
}
CV_Assert(background.channels() == 3);
CV_Assert(object.channels() == 3);
CV_Assert(background.size().width > object.size().width);
CV_Assert(background.size().height > object.size().height);
background.convertTo(background, CV_8U);
object.convertTo(object, CV_8U);
pos.x = (background.size().width - object.size().width) / 2;
pos.y = (background.size().height - object.size().height) / 2;
const double phi = rng.uniform(0.0, CV_2PI);
dir.x = std::cos(phi);
dir.y = std::sin(phi);
}
void SyntheticSequenceGenerator::getNextFrame(OutputArray _frame, OutputArray _gtMask) {
CV_Assert(!background.empty() && !object.empty());
const Size sz = background.size();
_frame.create(sz, CV_8UC3);
Mat frame = _frame.getMat();
CV_Assert(background.isContinuous() && frame.isContinuous());
waveDistortion(background.ptr(), frame.ptr(), sz.width, sz.height, 3, amplitude, wavelength, double(timeStep) * wavespeed);
const Size objSz = object.size();
object.copyTo(frame(Rect(Point2i(pos), objSz)));
while (pos.x + dir.x * objspeed < 0 || pos.x + dir.x * objspeed >= sz.width - objSz.width || pos.y + dir.y * objspeed < 0 || pos.y + dir.y * objspeed >= sz.height - objSz.height) {
const double phi = rng.uniform(0.0, CV_2PI);
dir.x = std::cos(phi);
dir.y = std::sin(phi);
}
_gtMask.create(sz, CV_8U);
Mat gtMask = _gtMask.getMat();
gtMask.setTo(cv::Scalar::all(0));
gtMask(Rect(Point2i(pos), objSz)) = 255;
pos += dir * objspeed;
++timeStep;
}
Ptr<SyntheticSequenceGenerator> createSyntheticSequenceGenerator(InputArray background, InputArray object, double amplitude, double wavelength, double wavespeed, double objspeed) {
return makePtr<SyntheticSequenceGenerator>(background, object, amplitude, wavelength, wavespeed, objspeed);
}
}
}
@@ -0,0 +1,118 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Author: andrewgodbehere
#include "test_precomp.hpp"
namespace opencv_test { namespace {
/**
* This test checks the following:
* (i) BackgroundSubtractorGMG can operate with matrices of various types and sizes
* (ii) Training mode returns empty fgmask
* (iii) End of training mode, and anomalous frame yields every pixel detected as FG
*/
typedef testing::TestWithParam<std::tuple<perf::MatDepth,int>> bgsubgmg_allTypes;
TEST_P(bgsubgmg_allTypes, accuracy)
{
const int depth = get<0>(GetParam());
const int ncn = get<1>(GetParam());
const int mtype = CV_MAKETYPE(depth, ncn);
const int width = 64;
const int height = 64;
RNG& rng = TS::ptr()->get_rng();
Ptr<BackgroundSubtractorGMG> fgbg = createBackgroundSubtractorGMG();
ASSERT_TRUE(fgbg != nullptr) << "Failed to call createBackgroundSubtractorGMG()";
/**
* Set a few parameters
*/
fgbg->setSmoothingRadius(7);
fgbg->setDecisionThreshold(0.7);
fgbg->setNumFrames(120);
/**
* Generate bounds for the values in the matrix for each type
*/
double maxd = 0, mind = 0;
/**
* Max value for simulated images picked randomly in upper half of type range
* Min value for simulated images picked randomly in lower half of type range
*/
if (depth == CV_8U)
{
uchar half = UCHAR_MAX/2;
maxd = (unsigned char)rng.uniform(half+32, UCHAR_MAX);
mind = (unsigned char)rng.uniform(0, half-32);
}
else if (depth == CV_8S)
{
maxd = (char)rng.uniform(32, CHAR_MAX);
mind = (char)rng.uniform(CHAR_MIN, -32);
}
else if (depth == CV_16U)
{
ushort half = USHRT_MAX/2;
maxd = (unsigned int)rng.uniform(half+32, USHRT_MAX);
mind = (unsigned int)rng.uniform(0, half-32);
}
else if (depth == CV_16S)
{
maxd = rng.uniform(32, SHRT_MAX);
mind = rng.uniform(SHRT_MIN, -32);
}
else if (depth == CV_32S)
{
maxd = rng.uniform(32, INT_MAX);
mind = rng.uniform(INT_MIN, -32);
}
else
{
ASSERT_TRUE( (depth == CV_32F)||(depth == CV_64F) ) << "Unsupported depth";
const double harf = 0.5;
const double bias = 0.125; // = 32/256 (Like CV_8U)
maxd = rng.uniform(harf + bias, 1.0);
mind = rng.uniform(0.0, harf - bias );
}
fgbg->setMinVal(mind);
fgbg->setMaxVal(maxd);
Mat simImage(height, width, mtype);
Mat fgmask;
const Mat fullbg(height, width, CV_8UC1, cv::Scalar(0)); // all background.
const int numLearningFrames = 120;
for (int i = 0; i < numLearningFrames; ++i)
{
/**
* Genrate simulated "image" for any type. Values always confined to upper half of range.
*/
rng.fill(simImage, RNG::UNIFORM, (mind + maxd)*0.5, maxd);
/**
* Feed simulated images into background subtractor
*/
fgbg->apply(simImage,fgmask);
EXPECT_EQ(cv::norm(fgmask, fullbg, NORM_INF), 0) << "foreground mask should be entirely background during training";
}
//! generate last image, distinct from training images
rng.fill(simImage, RNG::UNIFORM, mind, maxd);
fgbg->apply(simImage,fgmask);
const Mat fullfg(height, width, CV_8UC1, cv::Scalar(255)); // all foreground.
EXPECT_EQ(cv::norm(fgmask, fullfg, NORM_INF), 0) << "foreground mask should be entirely foreground finally";
}
INSTANTIATE_TEST_CASE_P(/**/,
bgsubgmg_allTypes,
testing::Combine(
testing::Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
testing::Values(1,2,3,4)));
}} // namespace
@@ -0,0 +1,140 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include <set>
namespace opencv_test { namespace {
static string getDataDir() { return TS::ptr()->get_data_path(); }
static string getLenaImagePath() { return getDataDir() + "shared/lena.png"; }
// Simple synthetic illumination invariance test
TEST(BackgroundSubtractor_LSBP, IlluminationInvariance)
{
RNG rng;
Mat input(100, 100, CV_32FC3);
rng.fill(input, RNG::UNIFORM, 0.0f, 0.1f);
Mat lsv1, lsv2;
cv::bgsegm::BackgroundSubtractorLSBPDesc::calcLocalSVDValues(lsv1, input);
input *= 10;
cv::bgsegm::BackgroundSubtractorLSBPDesc::calcLocalSVDValues(lsv2, input);
ASSERT_LE(cv::norm(lsv1, lsv2), 0.04f);
}
TEST(BackgroundSubtractor_LSBP, Correctness)
{
Mat input(3, 3, CV_32FC3);
float n = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j) {
input.at<Point3f>(i, j) = Point3f(n, n, n);
++n;
}
Mat lsv;
bgsegm::BackgroundSubtractorLSBPDesc::calcLocalSVDValues(lsv, input);
EXPECT_LE(std::abs(lsv.at<float>(1, 1) - 0.0903614f), 0.001f);
input = 1;
bgsegm::BackgroundSubtractorLSBPDesc::calcLocalSVDValues(lsv, input);
EXPECT_LE(std::abs(lsv.at<float>(1, 1) - 0.0f), 0.001f);
}
TEST(BackgroundSubtractor_LSBP, Discrimination)
{
Point2i LSBPSamplePoints[32];
for (int i = 0; i < 32; ++i) {
const double phi = i * CV_2PI / 32.0;
LSBPSamplePoints[i] = Point2i(int(4 * std::cos(phi)), int(4 * std::sin(phi)));
}
Mat lena = imread(getLenaImagePath());
Mat lsv;
lena.convertTo(lena, CV_32FC3);
bgsegm::BackgroundSubtractorLSBPDesc::calcLocalSVDValues(lsv, lena);
Scalar mean, var;
meanStdDev(lsv, mean, var);
EXPECT_GE(mean[0], 0.02);
EXPECT_LE(mean[0], 0.04);
EXPECT_GE(var[0], 0.03);
Mat desc;
bgsegm::BackgroundSubtractorLSBPDesc::computeFromLocalSVDValues(desc, lsv, LSBPSamplePoints);
Size sz = desc.size();
std::set<int> distinctive_elements;
for (int i = 0; i < sz.height; ++i)
for (int j = 0; j < sz.width; ++j)
distinctive_elements.insert(desc.at<int>(i, j));
EXPECT_GE(distinctive_elements.size(), 35000U);
}
static double scoreBitwiseReduce(const Mat& mask, const Mat& gtMask, uchar v1, uchar v2) {
Mat result;
cv::bitwise_and(mask == v1, gtMask == v2, result);
return cv::countNonZero(result);
}
template<typename T>
static double evaluateBGSAlgorithm(Ptr<T> bgs) {
Mat background = imread(getDataDir() + "shared/fruits.png");
Mat object = imread(getDataDir() + "shared/baboon.png");
cv::resize(object, object, Size(100, 100), 0, 0, INTER_LINEAR_EXACT);
Ptr<bgsegm::SyntheticSequenceGenerator> generator = bgsegm::createSyntheticSequenceGenerator(background, object);
double f1_mean = 0;
unsigned total = 0;
for (int frameNum = 1; frameNum <= 400; ++frameNum) {
Mat frame, gtMask;
generator->getNextFrame(frame, gtMask);
Mat mask;
bgs->apply(frame, mask);
Size sz = frame.size();
EXPECT_EQ(sz, gtMask.size());
EXPECT_EQ(gtMask.size(), mask.size());
EXPECT_EQ(mask.type(), gtMask.type());
EXPECT_EQ(mask.type(), CV_8U);
// We will give the algorithm some time for the proper background model inference.
// Almost all background subtraction algorithms have a problem with cold start and require some time for background model initialization.
// So we will not count first part of the frames in the score.
if (frameNum > 300) {
const double tp = scoreBitwiseReduce(mask, gtMask, 255, 255);
const double fp = scoreBitwiseReduce(mask, gtMask, 255, 0);
const double fn = scoreBitwiseReduce(mask, gtMask, 0, 255);
if (tp + fn + fp > 0) {
const double f1_score = 2.0 * tp / (2.0 * tp + fn + fp);
f1_mean += f1_score;
++total;
}
}
}
f1_mean /= total;
return f1_mean;
}
TEST(BackgroundSubtractor_LSBP, Accuracy)
{
EXPECT_GE(evaluateBGSAlgorithm(bgsegm::createBackgroundSubtractorGSOC()), 0.9);
EXPECT_GE(evaluateBGSAlgorithm(bgsegm::createBackgroundSubtractorLSBP()), 0.25);
}
}} // namespace
+6
View File
@@ -0,0 +1,6 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
CV_TEST_MAIN("cv")
+16
View File
@@ -0,0 +1,16 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_TEST_PRECOMP_HPP__
#define __OPENCV_TEST_PRECOMP_HPP__
#include "opencv2/ts.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/bgsegm.hpp"
namespace opencv_test {
using namespace cv::bgsegm;
}
#endif
@@ -0,0 +1,133 @@
Background Subtraction {#tutorial_bgsegm_bg_subtraction}
======================
Goal
----
In this chapter,
- We will familiarize with the background subtraction methods available in OpenCV.
Basics
------
Background subtraction is a major preprocessing step in many vision-based applications. For
example, consider the case of a visitor counter where a static camera takes the number of visitors
entering or leaving the room, or a traffic camera extracting information about the vehicles etc. In
all these cases, first you need to extract the person or vehicles alone. Technically, you need to
extract the moving foreground from static background.
If you have an image of background alone, like an image of the room without visitors, image of the road
without vehicles etc, it is an easy job. Just subtract the new image from the background. You get
the foreground objects alone. But in most of the cases, you may not have such an image, so we need
to extract the background from whatever images we have. It becomes more complicated when there are
shadows of the vehicles. Since shadows also move, simple subtraction will mark that also as
foreground. It complicates things.
Several algorithms were introduced for this purpose.
In the following, we will have a look at two algorithms from the `bgsegm` module.
### BackgroundSubtractorMOG
It is a Gaussian Mixture-based Background/Foreground Segmentation Algorithm. It was introduced in
the paper "An Improved Adaptive Background Mixture Model for Realtime Tracking with Shadow
Detection" by P. KaewTraKulPong and R. Bowden in 2001. It uses a method to model each background
pixel by a mixture of K Gaussian distributions (K = 3 to 5). The weights of the mixture represent
the time proportions that those colours stay in the scene. The probable background colours are the
ones which stay longer and more static.
While coding, we need to create a background object using the function,
**cv.bgsegm.createBackgroundSubtractorMOG()**. It has some optional parameters like length of history,
number of gaussian mixtures, threshold etc. It is all set to some default values. Then inside the
video loop, use backgroundsubtractor.apply() method to get the foreground mask.
See a simple example below:
@code{.py}
import numpy as np
import cv2 as cv
cap = cv.VideoCapture('vtest.avi')
fgbg = cv.bgsegm.createBackgroundSubtractorMOG()
while(1):
ret, frame = cap.read()
fgmask = fgbg.apply(frame)
cv.imshow('frame',fgmask)
k = cv.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv.destroyAllWindows()
@endcode
( All the results are shown at the end for comparison).
@note Documentation on the newer method **cv.createBackgroundSubtractorMOG2()** can be found here: @ref tutorial_background_subtraction
### BackgroundSubtractorGMG
This algorithm combines statistical background image estimation and per-pixel Bayesian segmentation.
It was introduced by Andrew B. Godbehere, Akihiro Matsukawa, and Ken Goldberg in their paper "Visual
Tracking of Human Visitors under Variable-Lighting Conditions for a Responsive Audio Art
Installation" in 2012. As per the paper, the system ran a successful interactive audio art
installation called “Are We There Yet?” from March 31 - July 31 2011 at the Contemporary Jewish
Museum in San Francisco, California.
It uses first few (120 by default) frames for background modelling. It employs probabilistic
foreground segmentation algorithm that identifies possible foreground objects using Bayesian
inference. The estimates are adaptive; newer observations are more heavily weighted than old
observations to accommodate variable illumination. Several morphological filtering operations like
closing and opening are done to remove unwanted noise. You will get a black window during first few
frames.
It would be better to apply morphological opening to the result to remove the noises.
@code{.py}
import numpy as np
import cv2 as cv
cap = cv.VideoCapture('vtest.avi')
kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE,(3,3))
fgbg = cv.bgsegm.createBackgroundSubtractorGMG()
while(1):
ret, frame = cap.read()
fgmask = fgbg.apply(frame)
fgmask = cv.morphologyEx(fgmask, cv.MORPH_OPEN, kernel)
cv.imshow('frame',fgmask)
k = cv.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv.destroyAllWindows()
@endcode
Results
-------
**Original Frame**
Below image shows the 200th frame of a video
![image](images/resframe.jpg)
**Result of BackgroundSubtractorMOG**
![image](images/resmog.jpg)
**Result of BackgroundSubtractorGMG**
Noise is removed with morphological opening.
![image](images/resgmg.jpg)
Additional Resources
--------------------
Exercises
---------
Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

@@ -0,0 +1,6 @@
Tutorials for bgsegm module {#tutorial_table_of_content_bgsegm}
===============================================================
- @subpage tutorial_bgsegm_bg_subtraction
In several applications, we need to extract foreground for further operations like object tracking. Background Subtraction is a well-known method in those cases.
+3
View File
@@ -0,0 +1,3 @@
set(the_description "Biologically inspired algorithms")
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef)
ocv_define_module(bioinspired opencv_core OPTIONAL opencv_highgui WRAP java objc python)
+6
View File
@@ -0,0 +1,6 @@
Biologically inspired vision models and derivated tools
=======================================================
1. A biological retina model for image spatio-temporal noise and luminance changes enhancement
2. A transient areas (spatio-temporal events) segmentation tool to use at the output of the Retina
3. High Dynamic Range (HDR >8bit images) tone mapping to (conversion to 8bit) use cas of the retina
+56
View File
@@ -0,0 +1,56 @@
@article{Benoit2010,
title={Using human visual system modeling for bio-inspired low level image processing},
author={Benoit, Alexandre and Caplier, Alice and Durette, Barth{\'e}l{\'e}my and H{\'e}rault, Jeanny},
journal={Computer vision and Image understanding},
volume={114},
number={7},
pages={758--773},
year={2010},
publisher={Elsevier}
}
@INPROCEEDINGS{Benoit2014,
author={Strat, S.T. and Benoit, A. and Lambert, P.},
booktitle={Signal Processing Conference (EUSIPCO), 2014 Proceedings of the 22nd European},
title={Retina enhanced bag of words descriptors for video classification},
year={2014},
month={Sept},
pages={1307-1311}
}
@inproceedings{Strat2013,
title={Retina enhanced SIFT descriptors for video indexing},
author={Strat, Sabin Tiberius and Benoit, Alexandre and Lambert, Patrick},
booktitle={Content-Based Multimedia Indexing (CBMI), 2013 11th International Workshop on},
pages={201--206},
year={2013},
organization={IEEE}
}
@book{Herault2010,
title={Vision: Images, Signals and Neural Networks-Models of Neural Processing in Visual Perception},
author={Jeanny, Herault},
year={2010},
publisher={World Scientific}
}
@inproceedings{Chaix2007,
title={Efficient demosaicing through recursive filtering},
author={De Lavar{\`e}ne, Brice Chaix and Alleysson, David and Durette, Barth{\'e}l{\'e}my and H{\'e}rault, Jeanny},
booktitle={Image Processing, 2007. ICIP 2007. IEEE International Conference on},
volume={2},
pages={II--189},
year={2007},
organization={IEEE}
}
@article{Meylan2007,
title={Model of retinal local adaptation for the tone mapping of color filter array images},
author={Meylan, Laurence and Alleysson, David and S{\"u}sstrunk, Sabine},
journal={JOSA A},
volume={24},
number={9},
pages={2807--2816},
year={2007},
publisher={Optical Society of America}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 894 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 KiB

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Some files were not shown because too many files have changed in this diff Show More