mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53:03 +04:00
Merge remote-tracking branch 'remotes/origin/master' into tvl1_chambolle
Conflicts: modules/video/doc/motion_analysis_and_object_tracking.rst
This commit is contained in:
@@ -214,135 +214,6 @@ Unlike :ocv:func:`findHomography` and :ocv:func:`estimateRigidTransform`, the fu
|
||||
:ocv:func:`findHomography`
|
||||
|
||||
|
||||
updateMotionHistory
|
||||
-----------------------
|
||||
Updates the motion history image by a moving silhouette.
|
||||
|
||||
.. ocv:function:: void updateMotionHistory( InputArray silhouette, InputOutputArray mhi, double timestamp, double duration )
|
||||
|
||||
.. ocv:pyfunction:: cv2.updateMotionHistory(silhouette, mhi, timestamp, duration) -> mhi
|
||||
|
||||
.. ocv:cfunction:: void cvUpdateMotionHistory( const CvArr* silhouette, CvArr* mhi, double timestamp, double duration )
|
||||
|
||||
:param silhouette: Silhouette mask that has non-zero pixels where the motion occurs.
|
||||
|
||||
:param mhi: Motion history image that is updated by the function (single-channel, 32-bit floating-point).
|
||||
|
||||
:param timestamp: Current time in milliseconds or other units.
|
||||
|
||||
:param duration: Maximal duration of the motion track in the same units as ``timestamp`` .
|
||||
|
||||
The function updates the motion history image as follows:
|
||||
|
||||
.. math::
|
||||
|
||||
\texttt{mhi} (x,y)= \forkthree{\texttt{timestamp}}{if $\texttt{silhouette}(x,y) \ne 0$}{0}{if $\texttt{silhouette}(x,y) = 0$ and $\texttt{mhi} < (\texttt{timestamp} - \texttt{duration})$}{\texttt{mhi}(x,y)}{otherwise}
|
||||
|
||||
That is, MHI pixels where the motion occurs are set to the current ``timestamp`` , while the pixels where the motion happened last time a long time ago are cleared.
|
||||
|
||||
The function, together with
|
||||
:ocv:func:`calcMotionGradient` and
|
||||
:ocv:func:`calcGlobalOrientation` , implements a motion templates technique described in
|
||||
[Davis97]_ and [Bradski00]_.
|
||||
See also the OpenCV sample ``motempl.c`` that demonstrates the use of all the motion template functions.
|
||||
|
||||
|
||||
calcMotionGradient
|
||||
----------------------
|
||||
Calculates a gradient orientation of a motion history image.
|
||||
|
||||
.. ocv:function:: void calcMotionGradient( InputArray mhi, OutputArray mask, OutputArray orientation, double delta1, double delta2, int apertureSize=3 )
|
||||
|
||||
.. ocv:pyfunction:: cv2.calcMotionGradient(mhi, delta1, delta2[, mask[, orientation[, apertureSize]]]) -> mask, orientation
|
||||
|
||||
.. ocv:cfunction:: void cvCalcMotionGradient( const CvArr* mhi, CvArr* mask, CvArr* orientation, double delta1, double delta2, int aperture_size=3 )
|
||||
|
||||
:param mhi: Motion history single-channel floating-point image.
|
||||
|
||||
:param mask: Output mask image that has the type ``CV_8UC1`` and the same size as ``mhi`` . Its non-zero elements mark pixels where the motion gradient data is correct.
|
||||
|
||||
:param orientation: Output motion gradient orientation image that has the same type and the same size as ``mhi`` . Each pixel of the image is a motion orientation, from 0 to 360 degrees.
|
||||
|
||||
:param delta1: Minimal (or maximal) allowed difference between ``mhi`` values within a pixel neighborhood.
|
||||
|
||||
:param delta2: Maximal (or minimal) allowed difference between ``mhi`` values within a pixel neighborhood. That is, the function finds the minimum ( :math:`m(x,y)` ) and maximum ( :math:`M(x,y)` ) ``mhi`` values over :math:`3 \times 3` neighborhood of each pixel and marks the motion orientation at :math:`(x, y)` as valid only if
|
||||
|
||||
.. math::
|
||||
|
||||
\min ( \texttt{delta1} , \texttt{delta2} ) \le M(x,y)-m(x,y) \le \max ( \texttt{delta1} , \texttt{delta2} ).
|
||||
|
||||
:param apertureSize: Aperture size of the :ocv:func:`Sobel` operator.
|
||||
|
||||
The function calculates a gradient orientation at each pixel
|
||||
:math:`(x, y)` as:
|
||||
|
||||
.. math::
|
||||
|
||||
\texttt{orientation} (x,y)= \arctan{\frac{d\texttt{mhi}/dy}{d\texttt{mhi}/dx}}
|
||||
|
||||
In fact,
|
||||
:ocv:func:`fastAtan2` and
|
||||
:ocv:func:`phase` are used so that the computed angle is measured in degrees and covers the full range 0..360. Also, the ``mask`` is filled to indicate pixels where the computed angle is valid.
|
||||
|
||||
.. note::
|
||||
|
||||
* (Python) An example on how to perform a motion template technique can be found at opencv_source_code/samples/python2/motempl.py
|
||||
|
||||
calcGlobalOrientation
|
||||
-------------------------
|
||||
Calculates a global motion orientation in a selected region.
|
||||
|
||||
.. ocv:function:: double calcGlobalOrientation( InputArray orientation, InputArray mask, InputArray mhi, double timestamp, double duration )
|
||||
|
||||
.. ocv:pyfunction:: cv2.calcGlobalOrientation(orientation, mask, mhi, timestamp, duration) -> retval
|
||||
|
||||
.. ocv:cfunction:: double cvCalcGlobalOrientation( const CvArr* orientation, const CvArr* mask, const CvArr* mhi, double timestamp, double duration )
|
||||
|
||||
:param orientation: Motion gradient orientation image calculated by the function :ocv:func:`calcMotionGradient` .
|
||||
|
||||
:param mask: Mask image. It may be a conjunction of a valid gradient mask, also calculated by :ocv:func:`calcMotionGradient` , and the mask of a region whose direction needs to be calculated.
|
||||
|
||||
:param mhi: Motion history image calculated by :ocv:func:`updateMotionHistory` .
|
||||
|
||||
:param timestamp: Timestamp passed to :ocv:func:`updateMotionHistory` .
|
||||
|
||||
:param duration: Maximum duration of a motion track in milliseconds, passed to :ocv:func:`updateMotionHistory` .
|
||||
|
||||
The function calculates an average
|
||||
motion direction in the selected region and returns the angle between
|
||||
0 degrees and 360 degrees. The average direction is computed from
|
||||
the weighted orientation histogram, where a recent motion has a larger
|
||||
weight and the motion occurred in the past has a smaller weight, as recorded in ``mhi`` .
|
||||
|
||||
|
||||
|
||||
|
||||
segmentMotion
|
||||
-------------
|
||||
Splits a motion history image into a few parts corresponding to separate independent motions (for example, left hand, right hand).
|
||||
|
||||
.. ocv:function:: void segmentMotion(InputArray mhi, OutputArray segmask, vector<Rect>& boundingRects, double timestamp, double segThresh)
|
||||
|
||||
.. ocv:pyfunction:: cv2.segmentMotion(mhi, timestamp, segThresh[, segmask]) -> segmask, boundingRects
|
||||
|
||||
.. ocv:cfunction:: CvSeq* cvSegmentMotion( const CvArr* mhi, CvArr* seg_mask, CvMemStorage* storage, double timestamp, double seg_thresh )
|
||||
|
||||
:param mhi: Motion history image.
|
||||
|
||||
:param segmask: Image where the found mask should be stored, single-channel, 32-bit floating-point.
|
||||
|
||||
:param boundingRects: Vector containing ROIs of motion connected components.
|
||||
|
||||
:param timestamp: Current time in milliseconds or other units.
|
||||
|
||||
:param segThresh: Segmentation threshold that is recommended to be equal to the interval between motion history "steps" or greater.
|
||||
|
||||
|
||||
The function finds all of the motion segments and marks them in ``segmask`` with individual values (1,2,...). It also computes a vector with ROIs of motion connected components. After that the motion direction for every component can be calculated with :ocv:func:`calcGlobalOrientation` using the extracted mask of the particular component.
|
||||
|
||||
|
||||
|
||||
|
||||
CamShift
|
||||
--------
|
||||
Finds an object center, size, and orientation.
|
||||
@@ -994,52 +865,6 @@ Sets the prior probability that each individual pixel is a background pixel.
|
||||
.. ocv:function:: void BackgroundSubtractorGMG::setBackgroundPrior(double bgprior)
|
||||
|
||||
|
||||
calcOpticalFlowSF
|
||||
-----------------
|
||||
Calculate an optical flow using "SimpleFlow" algorithm.
|
||||
|
||||
.. ocv:function:: void calcOpticalFlowSF( InputArray from, InputArray to, OutputArray flow, int layers, int averaging_block_size, int max_flow )
|
||||
|
||||
.. ocv:function:: calcOpticalFlowSF( InputArray from, InputArray to, OutputArray flow, int layers, int averaging_block_size, int max_flow, double sigma_dist, double sigma_color, int postprocess_window, double sigma_dist_fix, double sigma_color_fix, double occ_thr, int upscale_averaging_radius, double upscale_sigma_dist, double upscale_sigma_color, double speed_up_thr )
|
||||
|
||||
:param prev: First 8-bit 3-channel image.
|
||||
|
||||
:param next: Second 8-bit 3-channel image of the same size as ``prev``
|
||||
|
||||
:param flow: computed flow image that has the same size as ``prev`` and type ``CV_32FC2``
|
||||
|
||||
:param layers: Number of layers
|
||||
|
||||
:param averaging_block_size: Size of block through which we sum up when calculate cost function for pixel
|
||||
|
||||
:param max_flow: maximal flow that we search at each level
|
||||
|
||||
:param sigma_dist: vector smooth spatial sigma parameter
|
||||
|
||||
:param sigma_color: vector smooth color sigma parameter
|
||||
|
||||
:param postprocess_window: window size for postprocess cross bilateral filter
|
||||
|
||||
:param sigma_dist_fix: spatial sigma for postprocess cross bilateralf filter
|
||||
|
||||
:param sigma_color_fix: color sigma for postprocess cross bilateral filter
|
||||
|
||||
:param occ_thr: threshold for detecting occlusions
|
||||
|
||||
:param upscale_averaging_radius: window size for bilateral upscale operation
|
||||
|
||||
:param upscale_sigma_dist: spatial sigma for bilateral upscale operation
|
||||
|
||||
:param upscale_sigma_color: color sigma for bilateral upscale operation
|
||||
|
||||
:param speed_up_thr: threshold to detect point with irregular flow - where flow should be recalculated after upscale
|
||||
|
||||
See [Tao2012]_. And site of project - http://graphics.berkeley.edu/papers/Tao-SAN-2012-05/.
|
||||
|
||||
.. note::
|
||||
|
||||
* An example using the simpleFlow algorithm can be found at opencv_source_code/samples/cpp/simpleflow_demo.cpp
|
||||
|
||||
createOptFlow_DualTVL1
|
||||
----------------------
|
||||
"Dual TV L1" Optical Flow Algorithm.
|
||||
@@ -1079,11 +904,6 @@ createOptFlow_DualTVL1
|
||||
|
||||
Stopping criterion iterations number used in the numerical scheme.
|
||||
|
||||
.. ocv:member:: double gamma
|
||||
|
||||
Parameter used for motion estimation. It adds a variable allowing for illumination variations
|
||||
Set this parameter to 1 if you have varying illumination.
|
||||
See: [Chambolle2011]_
|
||||
|
||||
DenseOpticalFlow::calc
|
||||
--------------------------
|
||||
@@ -1111,13 +931,6 @@ Releases all inner buffers.
|
||||
|
||||
.. [Bradski98] Bradski, G.R. "Computer Vision Face Tracking for Use in a Perceptual User Interface", Intel, 1998
|
||||
|
||||
.. [Bradski00] Davis, J.W. and Bradski, G.R. "Motion Segmentation and Pose Recognition with Motion History Gradients", WACV00, 2000
|
||||
|
||||
.. [Chambolle2011] Chambolle, A. and Pock, T. "A First-Order Primal-Dual Algorithm for Convex Problems with Applications to Imaging"
|
||||
Journal of Mathematical imaging and vision, 2011, Vol 40 issue 1, pp 120-145
|
||||
|
||||
.. [Davis97] Davis, J.W. and Bobick, A.F. "The Representation and Recognition of Action Using Temporal Templates", CVPR97, 1997
|
||||
|
||||
.. [EP08] Evangelidis, G.D. and Psarakis E.Z. "Parametric Image Alignment using Enhanced Correlation Coefficient Maximization", IEEE Transactions on PAMI, vol. 32, no. 10, 2008
|
||||
|
||||
.. [Farneback2003] Gunnar Farneback, Two-frame motion estimation based on polynomial expansion, Lecture Notes in Computer Science, 2003, (2749), , 363-370.
|
||||
@@ -1132,8 +945,6 @@ Releases all inner buffers.
|
||||
|
||||
.. [Welch95] Greg Welch and Gary Bishop "An Introduction to the Kalman Filter", 1995
|
||||
|
||||
.. [Tao2012] Michael Tao, Jiamin Bai, Pushmeet Kohli and Sylvain Paris. SimpleFlow: A Non-iterative, Sublinear Optical Flow Algorithm. Computer Graphics Forum (Eurographics 2012)
|
||||
|
||||
.. [Zach2007] C. Zach, T. Pock and H. Bischof. "A Duality Based Approach for Realtime TV-L1 Optical Flow", In Proceedings of Pattern Recognition (DAGM), Heidelberg, Germany, pp. 214-223, 2007
|
||||
|
||||
.. [Zivkovic2004] Z. Zivkovic. "Improved adaptive Gausian mixture model for background subtraction", International Conference Pattern Recognition, UK, August, 2004, http://www.zoranz.net/Publications/zivkovic2004ICPR.pdf. The code is very fast and performs also shadow detection. Number of Gausssian components is adapted per pixel.
|
||||
|
||||
@@ -66,39 +66,6 @@ public:
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm
|
||||
|
||||
The class implements the following algorithm:
|
||||
"An improved adaptive background mixture model for real-time tracking with shadow detection"
|
||||
P. KadewTraKuPong and R. Bowden,
|
||||
Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
|
||||
http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
|
||||
|
||||
*/
|
||||
class CV_EXPORTS_W BackgroundSubtractorMOG : public BackgroundSubtractor
|
||||
{
|
||||
public:
|
||||
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;
|
||||
};
|
||||
|
||||
CV_EXPORTS_W Ptr<BackgroundSubtractorMOG>
|
||||
createBackgroundSubtractorMOG(int history=200, int nmixtures=5,
|
||||
double backgroundRatio=0.7, double noiseSigma=0);
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
The class implements the following algorithm:
|
||||
"Improved adaptive Gausian mixture model for background subtraction"
|
||||
@@ -189,51 +156,6 @@ CV_EXPORTS_W Ptr<BackgroundSubtractorKNN>
|
||||
createBackgroundSubtractorKNN(int history=500, double dist2Threshold=400.0,
|
||||
bool detectShadows=true);
|
||||
|
||||
/**
|
||||
* Background Subtractor module. 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:
|
||||
CV_WRAP virtual int getMaxFeatures() const = 0;
|
||||
CV_WRAP virtual void setMaxFeatures(int maxFeatures) = 0;
|
||||
|
||||
CV_WRAP virtual double getDefaultLearningRate() const = 0;
|
||||
CV_WRAP virtual void setDefaultLearningRate(double lr) = 0;
|
||||
|
||||
CV_WRAP virtual int getNumFrames() const = 0;
|
||||
CV_WRAP virtual void setNumFrames(int nframes) = 0;
|
||||
|
||||
CV_WRAP virtual int getQuantizationLevels() const = 0;
|
||||
CV_WRAP virtual void setQuantizationLevels(int nlevels) = 0;
|
||||
|
||||
CV_WRAP virtual double getBackgroundPrior() const = 0;
|
||||
CV_WRAP virtual void setBackgroundPrior(double bgprior) = 0;
|
||||
|
||||
CV_WRAP virtual int getSmoothingRadius() const = 0;
|
||||
CV_WRAP virtual void setSmoothingRadius(int radius) = 0;
|
||||
|
||||
CV_WRAP virtual double getDecisionThreshold() const = 0;
|
||||
CV_WRAP virtual void setDecisionThreshold(double thresh) = 0;
|
||||
|
||||
CV_WRAP virtual bool getUpdateBackgroundModel() const = 0;
|
||||
CV_WRAP virtual void setUpdateBackgroundModel(bool update) = 0;
|
||||
|
||||
CV_WRAP virtual double getMinVal() const = 0;
|
||||
CV_WRAP virtual void setMinVal(double val) = 0;
|
||||
|
||||
CV_WRAP virtual double getMaxVal() const = 0;
|
||||
CV_WRAP virtual void setMaxVal(double val) = 0;
|
||||
};
|
||||
|
||||
|
||||
CV_EXPORTS_W Ptr<BackgroundSubtractorGMG> createBackgroundSubtractorGMG(int initializationFrames=120,
|
||||
double decisionThreshold=0.8);
|
||||
|
||||
} // cv
|
||||
|
||||
#endif
|
||||
|
||||
@@ -55,28 +55,6 @@ enum { OPTFLOW_USE_INITIAL_FLOW = 4,
|
||||
OPTFLOW_FARNEBACK_GAUSSIAN = 256
|
||||
};
|
||||
|
||||
enum { MOTION_TRANSLATION = 0,
|
||||
MOTION_EUCLIDEAN = 1,
|
||||
MOTION_AFFINE = 2,
|
||||
MOTION_HOMOGRAPHY = 3
|
||||
};
|
||||
|
||||
//! updates motion history image using the current silhouette
|
||||
CV_EXPORTS_W void updateMotionHistory( InputArray silhouette, InputOutputArray mhi,
|
||||
double timestamp, double duration );
|
||||
|
||||
//! computes the motion gradient orientation image from the motion history image
|
||||
CV_EXPORTS_W void calcMotionGradient( InputArray mhi, OutputArray mask, OutputArray orientation,
|
||||
double delta1, double delta2, int apertureSize = 3 );
|
||||
|
||||
//! computes the global orientation of the selected motion history image part
|
||||
CV_EXPORTS_W double calcGlobalOrientation( InputArray orientation, InputArray mask, InputArray mhi,
|
||||
double timestamp, double duration );
|
||||
|
||||
CV_EXPORTS_W void segmentMotion( InputArray mhi, OutputArray segmask,
|
||||
CV_OUT std::vector<Rect>& boundingRects,
|
||||
double timestamp, double segThresh );
|
||||
|
||||
//! updates the object tracking window using CAMSHIFT algorithm
|
||||
CV_EXPORTS_W RotatedRect CamShift( InputArray probImage, CV_IN_OUT Rect& window,
|
||||
TermCriteria criteria );
|
||||
@@ -109,6 +87,15 @@ CV_EXPORTS_W void calcOpticalFlowFarneback( InputArray prev, InputArray next, In
|
||||
// that maps one 2D point set to another or one image to another.
|
||||
CV_EXPORTS_W Mat estimateRigidTransform( InputArray src, InputArray dst, bool fullAffine );
|
||||
|
||||
|
||||
enum
|
||||
{
|
||||
MOTION_TRANSLATION = 0,
|
||||
MOTION_EUCLIDEAN = 1,
|
||||
MOTION_AFFINE = 2,
|
||||
MOTION_HOMOGRAPHY = 3
|
||||
};
|
||||
|
||||
//! estimates the best-fit Translation, Euclidean, Affine or Perspective Transformation
|
||||
// with respect to Enhanced Correlation Coefficient criterion that maps one image to
|
||||
// another (area-based alignment)
|
||||
@@ -120,20 +107,6 @@ CV_EXPORTS_W double findTransformECC( InputArray templateImage, InputArray input
|
||||
InputOutputArray warpMatrix, int motionType = MOTION_AFFINE,
|
||||
TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 50, 0.001));
|
||||
|
||||
|
||||
//! computes dense optical flow using Simple Flow algorithm
|
||||
CV_EXPORTS_W void calcOpticalFlowSF( InputArray from, InputArray to, OutputArray flow,
|
||||
int layers, int averaging_block_size, int max_flow);
|
||||
|
||||
CV_EXPORTS_W void calcOpticalFlowSF( InputArray from, InputArray to, OutputArray flow, int layers,
|
||||
int averaging_block_size, int max_flow,
|
||||
double sigma_dist, double sigma_color, int postprocess_window,
|
||||
double sigma_dist_fix, double sigma_color_fix, double occ_thr,
|
||||
int upscale_averaging_radius, double upscale_sigma_dist,
|
||||
double upscale_sigma_color, double speed_up_thr );
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
Kalman filter.
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "perf_precomp.hpp"
|
||||
#include "../perf_precomp.hpp"
|
||||
#include "opencv2/ts/ocl_perf.hpp"
|
||||
|
||||
#ifdef HAVE_OPENCL
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
// Copyright (C) 2014, Advanced Micro Devices, Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
|
||||
#include "perf_precomp.hpp"
|
||||
#include "../perf_precomp.hpp"
|
||||
#include "opencv2/ts/ocl_perf.hpp"
|
||||
|
||||
#ifdef HAVE_OPENCL
|
||||
#if 0 //def HAVE_OPENCL
|
||||
|
||||
namespace cvtest {
|
||||
namespace ocl {
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "perf_precomp.hpp"
|
||||
#include "../perf_precomp.hpp"
|
||||
#include "opencv2/ts/ocl_perf.hpp"
|
||||
|
||||
using std::tr1::make_tuple;
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "perf_precomp.hpp"
|
||||
#include "../perf_precomp.hpp"
|
||||
#include "opencv2/ts/ocl_perf.hpp"
|
||||
|
||||
using std::tr1::make_tuple;
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "perf_precomp.hpp"
|
||||
#include "../perf_precomp.hpp"
|
||||
#include "opencv2/ts/ocl_perf.hpp"
|
||||
|
||||
using std::tr1::make_tuple;
|
||||
|
||||
@@ -1,850 +0,0 @@
|
||||
/*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 "opencv2/imgproc/imgproc_c.h"
|
||||
#include "opencv2/video/tracking_c.h"
|
||||
|
||||
// to be moved to legacy
|
||||
|
||||
static int icvMinimalPyramidSize( CvSize imgSize )
|
||||
{
|
||||
return cvAlign(imgSize.width,8) * imgSize.height / 3;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
icvInitPyramidalAlgorithm( const CvMat* imgA, const CvMat* imgB,
|
||||
CvMat* pyrA, CvMat* pyrB,
|
||||
int level, CvTermCriteria * criteria,
|
||||
int max_iters, int flags,
|
||||
uchar *** imgI, uchar *** imgJ,
|
||||
int **step, CvSize** size,
|
||||
double **scale, cv::AutoBuffer<uchar>* buffer )
|
||||
{
|
||||
const int ALIGN = 8;
|
||||
int pyrBytes, bufferBytes = 0, elem_size;
|
||||
int level1 = level + 1;
|
||||
|
||||
int i;
|
||||
CvSize imgSize, levelSize;
|
||||
|
||||
*imgI = *imgJ = 0;
|
||||
*step = 0;
|
||||
*scale = 0;
|
||||
*size = 0;
|
||||
|
||||
/* check input arguments */
|
||||
if( ((flags & CV_LKFLOW_PYR_A_READY) != 0 && !pyrA) ||
|
||||
((flags & CV_LKFLOW_PYR_B_READY) != 0 && !pyrB) )
|
||||
CV_Error( CV_StsNullPtr, "Some of the precomputed pyramids are missing" );
|
||||
|
||||
if( level < 0 )
|
||||
CV_Error( CV_StsOutOfRange, "The number of pyramid levels is negative" );
|
||||
|
||||
switch( criteria->type )
|
||||
{
|
||||
case CV_TERMCRIT_ITER:
|
||||
criteria->epsilon = 0.f;
|
||||
break;
|
||||
case CV_TERMCRIT_EPS:
|
||||
criteria->max_iter = max_iters;
|
||||
break;
|
||||
case CV_TERMCRIT_ITER | CV_TERMCRIT_EPS:
|
||||
break;
|
||||
default:
|
||||
assert( 0 );
|
||||
CV_Error( CV_StsBadArg, "Invalid termination criteria" );
|
||||
}
|
||||
|
||||
/* compare squared values */
|
||||
criteria->epsilon *= criteria->epsilon;
|
||||
|
||||
/* set pointers and step for every level */
|
||||
pyrBytes = 0;
|
||||
|
||||
imgSize = cvGetSize(imgA);
|
||||
elem_size = CV_ELEM_SIZE(imgA->type);
|
||||
levelSize = imgSize;
|
||||
|
||||
for( i = 1; i < level1; i++ )
|
||||
{
|
||||
levelSize.width = (levelSize.width + 1) >> 1;
|
||||
levelSize.height = (levelSize.height + 1) >> 1;
|
||||
|
||||
int tstep = cvAlign(levelSize.width,ALIGN) * elem_size;
|
||||
pyrBytes += tstep * levelSize.height;
|
||||
}
|
||||
|
||||
assert( pyrBytes <= imgSize.width * imgSize.height * elem_size * 4 / 3 );
|
||||
|
||||
/* buffer_size = <size for patches> + <size for pyramids> */
|
||||
bufferBytes = (int)((level1 >= 0) * ((pyrA->data.ptr == 0) +
|
||||
(pyrB->data.ptr == 0)) * pyrBytes +
|
||||
(sizeof(imgI[0][0]) * 2 + sizeof(step[0][0]) +
|
||||
sizeof(size[0][0]) + sizeof(scale[0][0])) * level1);
|
||||
|
||||
buffer->allocate( bufferBytes );
|
||||
|
||||
*imgI = (uchar **) (uchar*)(*buffer);
|
||||
*imgJ = *imgI + level1;
|
||||
*step = (int *) (*imgJ + level1);
|
||||
*scale = (double *) (*step + level1);
|
||||
*size = (CvSize *)(*scale + level1);
|
||||
|
||||
imgI[0][0] = imgA->data.ptr;
|
||||
imgJ[0][0] = imgB->data.ptr;
|
||||
step[0][0] = imgA->step;
|
||||
scale[0][0] = 1;
|
||||
size[0][0] = imgSize;
|
||||
|
||||
if( level > 0 )
|
||||
{
|
||||
uchar *bufPtr = (uchar *) (*size + level1);
|
||||
uchar *ptrA = pyrA->data.ptr;
|
||||
uchar *ptrB = pyrB->data.ptr;
|
||||
|
||||
if( !ptrA )
|
||||
{
|
||||
ptrA = bufPtr;
|
||||
bufPtr += pyrBytes;
|
||||
}
|
||||
|
||||
if( !ptrB )
|
||||
ptrB = bufPtr;
|
||||
|
||||
levelSize = imgSize;
|
||||
|
||||
/* build pyramids for both frames */
|
||||
for( i = 1; i <= level; i++ )
|
||||
{
|
||||
int levelBytes;
|
||||
CvMat prev_level, next_level;
|
||||
|
||||
levelSize.width = (levelSize.width + 1) >> 1;
|
||||
levelSize.height = (levelSize.height + 1) >> 1;
|
||||
|
||||
size[0][i] = levelSize;
|
||||
step[0][i] = cvAlign( levelSize.width, ALIGN ) * elem_size;
|
||||
scale[0][i] = scale[0][i - 1] * 0.5;
|
||||
|
||||
levelBytes = step[0][i] * levelSize.height;
|
||||
imgI[0][i] = (uchar *) ptrA;
|
||||
ptrA += levelBytes;
|
||||
|
||||
if( !(flags & CV_LKFLOW_PYR_A_READY) )
|
||||
{
|
||||
prev_level = cvMat( size[0][i-1].height, size[0][i-1].width, CV_8UC1 );
|
||||
next_level = cvMat( size[0][i].height, size[0][i].width, CV_8UC1 );
|
||||
cvSetData( &prev_level, imgI[0][i-1], step[0][i-1] );
|
||||
cvSetData( &next_level, imgI[0][i], step[0][i] );
|
||||
cvPyrDown( &prev_level, &next_level );
|
||||
}
|
||||
|
||||
imgJ[0][i] = (uchar *) ptrB;
|
||||
ptrB += levelBytes;
|
||||
|
||||
if( !(flags & CV_LKFLOW_PYR_B_READY) )
|
||||
{
|
||||
prev_level = cvMat( size[0][i-1].height, size[0][i-1].width, CV_8UC1 );
|
||||
next_level = cvMat( size[0][i].height, size[0][i].width, CV_8UC1 );
|
||||
cvSetData( &prev_level, imgJ[0][i-1], step[0][i-1] );
|
||||
cvSetData( &next_level, imgJ[0][i], step[0][i] );
|
||||
cvPyrDown( &prev_level, &next_level );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* compute dI/dx and dI/dy */
|
||||
static void
|
||||
icvCalcIxIy_32f( const float* src, int src_step, float* dstX, float* dstY, int dst_step,
|
||||
CvSize src_size, const float* smooth_k, float* buffer0 )
|
||||
{
|
||||
int src_width = src_size.width, dst_width = src_size.width-2;
|
||||
int x, height = src_size.height - 2;
|
||||
float* buffer1 = buffer0 + src_width;
|
||||
|
||||
src_step /= sizeof(src[0]);
|
||||
dst_step /= sizeof(dstX[0]);
|
||||
|
||||
for( ; height--; src += src_step, dstX += dst_step, dstY += dst_step )
|
||||
{
|
||||
const float* src2 = src + src_step;
|
||||
const float* src3 = src + src_step*2;
|
||||
|
||||
for( x = 0; x < src_width; x++ )
|
||||
{
|
||||
float t0 = (src3[x] + src[x])*smooth_k[0] + src2[x]*smooth_k[1];
|
||||
float t1 = src3[x] - src[x];
|
||||
buffer0[x] = t0; buffer1[x] = t1;
|
||||
}
|
||||
|
||||
for( x = 0; x < dst_width; x++ )
|
||||
{
|
||||
float t0 = buffer0[x+2] - buffer0[x];
|
||||
float t1 = (buffer1[x] + buffer1[x+2])*smooth_k[0] + buffer1[x+1]*smooth_k[1];
|
||||
dstX[x] = t0; dstY[x] = t1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#undef CV_8TO32F
|
||||
#define CV_8TO32F(a) (a)
|
||||
|
||||
static const void*
|
||||
icvAdjustRect( const void* srcptr, int src_step, int pix_size,
|
||||
CvSize src_size, CvSize win_size,
|
||||
CvPoint ip, CvRect* pRect )
|
||||
{
|
||||
CvRect rect;
|
||||
const char* src = (const char*)srcptr;
|
||||
|
||||
if( ip.x >= 0 )
|
||||
{
|
||||
src += ip.x*pix_size;
|
||||
rect.x = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
rect.x = -ip.x;
|
||||
if( rect.x > win_size.width )
|
||||
rect.x = win_size.width;
|
||||
}
|
||||
|
||||
if( ip.x + win_size.width < src_size.width )
|
||||
rect.width = win_size.width;
|
||||
else
|
||||
{
|
||||
rect.width = src_size.width - ip.x - 1;
|
||||
if( rect.width < 0 )
|
||||
{
|
||||
src += rect.width*pix_size;
|
||||
rect.width = 0;
|
||||
}
|
||||
assert( rect.width <= win_size.width );
|
||||
}
|
||||
|
||||
if( ip.y >= 0 )
|
||||
{
|
||||
src += ip.y * src_step;
|
||||
rect.y = 0;
|
||||
}
|
||||
else
|
||||
rect.y = -ip.y;
|
||||
|
||||
if( ip.y + win_size.height < src_size.height )
|
||||
rect.height = win_size.height;
|
||||
else
|
||||
{
|
||||
rect.height = src_size.height - ip.y - 1;
|
||||
if( rect.height < 0 )
|
||||
{
|
||||
src += rect.height*src_step;
|
||||
rect.height = 0;
|
||||
}
|
||||
}
|
||||
|
||||
*pRect = rect;
|
||||
return src - rect.x*pix_size;
|
||||
}
|
||||
|
||||
|
||||
static CvStatus CV_STDCALL icvGetRectSubPix_8u32f_C1R
|
||||
( const uchar* src, int src_step, CvSize src_size,
|
||||
float* dst, int dst_step, CvSize win_size, CvPoint2D32f center )
|
||||
{
|
||||
CvPoint ip;
|
||||
float a12, a22, b1, b2;
|
||||
float a, b;
|
||||
double s = 0;
|
||||
int i, j;
|
||||
|
||||
center.x -= (win_size.width-1)*0.5f;
|
||||
center.y -= (win_size.height-1)*0.5f;
|
||||
|
||||
ip.x = cvFloor( center.x );
|
||||
ip.y = cvFloor( center.y );
|
||||
|
||||
if( win_size.width <= 0 || win_size.height <= 0 )
|
||||
return CV_BADRANGE_ERR;
|
||||
|
||||
a = center.x - ip.x;
|
||||
b = center.y - ip.y;
|
||||
a = MAX(a,0.0001f);
|
||||
a12 = a*(1.f-b);
|
||||
a22 = a*b;
|
||||
b1 = 1.f - b;
|
||||
b2 = b;
|
||||
s = (1. - a)/a;
|
||||
|
||||
src_step /= sizeof(src[0]);
|
||||
dst_step /= sizeof(dst[0]);
|
||||
|
||||
if( 0 <= ip.x && ip.x + win_size.width < src_size.width &&
|
||||
0 <= ip.y && ip.y + win_size.height < src_size.height )
|
||||
{
|
||||
// extracted rectangle is totally inside the image
|
||||
src += ip.y * src_step + ip.x;
|
||||
|
||||
#if 0
|
||||
if( icvCopySubpix_8u32f_C1R_p &&
|
||||
icvCopySubpix_8u32f_C1R_p( src, src_step, dst,
|
||||
dst_step*sizeof(dst[0]), win_size, a, b ) >= 0 )
|
||||
return CV_OK;
|
||||
#endif
|
||||
|
||||
for( ; win_size.height--; src += src_step, dst += dst_step )
|
||||
{
|
||||
float prev = (1 - a)*(b1*CV_8TO32F(src[0]) + b2*CV_8TO32F(src[src_step]));
|
||||
for( j = 0; j < win_size.width; j++ )
|
||||
{
|
||||
float t = a12*CV_8TO32F(src[j+1]) + a22*CV_8TO32F(src[j+1+src_step]);
|
||||
dst[j] = prev + t;
|
||||
prev = (float)(t*s);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CvRect r;
|
||||
|
||||
src = (const uchar*)icvAdjustRect( src, src_step*sizeof(*src),
|
||||
sizeof(*src), src_size, win_size,ip, &r);
|
||||
|
||||
for( i = 0; i < win_size.height; i++, dst += dst_step )
|
||||
{
|
||||
const uchar *src2 = src + src_step;
|
||||
|
||||
if( i < r.y || i >= r.height )
|
||||
src2 -= src_step;
|
||||
|
||||
for( j = 0; j < r.x; j++ )
|
||||
{
|
||||
float s0 = CV_8TO32F(src[r.x])*b1 +
|
||||
CV_8TO32F(src2[r.x])*b2;
|
||||
|
||||
dst[j] = (float)(s0);
|
||||
}
|
||||
|
||||
if( j < r.width )
|
||||
{
|
||||
float prev = (1 - a)*(b1*CV_8TO32F(src[j]) + b2*CV_8TO32F(src2[j]));
|
||||
|
||||
for( ; j < r.width; j++ )
|
||||
{
|
||||
float t = a12*CV_8TO32F(src[j+1]) + a22*CV_8TO32F(src2[j+1]);
|
||||
dst[j] = prev + t;
|
||||
prev = (float)(t*s);
|
||||
}
|
||||
}
|
||||
|
||||
for( ; j < win_size.width; j++ )
|
||||
{
|
||||
float s0 = CV_8TO32F(src[r.width])*b1 +
|
||||
CV_8TO32F(src2[r.width])*b2;
|
||||
|
||||
dst[j] = (float)(s0);
|
||||
}
|
||||
|
||||
if( i < r.height )
|
||||
src = src2;
|
||||
}
|
||||
}
|
||||
|
||||
return CV_OK;
|
||||
}
|
||||
|
||||
|
||||
#define ICV_32F8U(x) ((uchar)cvRound(x))
|
||||
|
||||
#define ICV_DEF_GET_QUADRANGLE_SUB_PIX_FUNC( flavor, srctype, dsttype, worktype, cast_macro, cvt ) \
|
||||
static CvStatus CV_STDCALL icvGetQuadrangleSubPix_##flavor##_C1R \
|
||||
( const srctype * src, int src_step, CvSize src_size, \
|
||||
dsttype *dst, int dst_step, CvSize win_size, const float *matrix ) \
|
||||
{ \
|
||||
int x, y; \
|
||||
double dx = (win_size.width - 1)*0.5; \
|
||||
double dy = (win_size.height - 1)*0.5; \
|
||||
double A11 = matrix[0], A12 = matrix[1], A13 = matrix[2]-A11*dx-A12*dy; \
|
||||
double A21 = matrix[3], A22 = matrix[4], A23 = matrix[5]-A21*dx-A22*dy; \
|
||||
\
|
||||
src_step /= sizeof(srctype); \
|
||||
dst_step /= sizeof(dsttype); \
|
||||
\
|
||||
for( y = 0; y < win_size.height; y++, dst += dst_step ) \
|
||||
{ \
|
||||
double xs = A12*y + A13; \
|
||||
double ys = A22*y + A23; \
|
||||
double xe = A11*(win_size.width-1) + A12*y + A13; \
|
||||
double ye = A21*(win_size.width-1) + A22*y + A23; \
|
||||
\
|
||||
if( (unsigned)(cvFloor(xs)-1) < (unsigned)(src_size.width - 3) && \
|
||||
(unsigned)(cvFloor(ys)-1) < (unsigned)(src_size.height - 3) && \
|
||||
(unsigned)(cvFloor(xe)-1) < (unsigned)(src_size.width - 3) && \
|
||||
(unsigned)(cvFloor(ye)-1) < (unsigned)(src_size.height - 3)) \
|
||||
{ \
|
||||
for( x = 0; x < win_size.width; x++ ) \
|
||||
{ \
|
||||
int ixs = cvFloor( xs ); \
|
||||
int iys = cvFloor( ys ); \
|
||||
const srctype *ptr = src + src_step*iys + ixs; \
|
||||
double a = xs - ixs, b = ys - iys, a1 = 1.f - a; \
|
||||
worktype p0 = cvt(ptr[0])*a1 + cvt(ptr[1])*a; \
|
||||
worktype p1 = cvt(ptr[src_step])*a1 + cvt(ptr[src_step+1])*a; \
|
||||
xs += A11; \
|
||||
ys += A21; \
|
||||
\
|
||||
dst[x] = cast_macro(p0 + b * (p1 - p0)); \
|
||||
} \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
for( x = 0; x < win_size.width; x++ ) \
|
||||
{ \
|
||||
int ixs = cvFloor( xs ), iys = cvFloor( ys ); \
|
||||
double a = xs - ixs, b = ys - iys, a1 = 1.f - a; \
|
||||
const srctype *ptr0, *ptr1; \
|
||||
worktype p0, p1; \
|
||||
xs += A11; ys += A21; \
|
||||
\
|
||||
if( (unsigned)iys < (unsigned)(src_size.height-1) ) \
|
||||
ptr0 = src + src_step*iys, ptr1 = ptr0 + src_step; \
|
||||
else \
|
||||
ptr0 = ptr1 = src + (iys < 0 ? 0 : src_size.height-1)*src_step; \
|
||||
\
|
||||
if( (unsigned)ixs < (unsigned)(src_size.width-1) ) \
|
||||
{ \
|
||||
p0 = cvt(ptr0[ixs])*a1 + cvt(ptr0[ixs+1])*a; \
|
||||
p1 = cvt(ptr1[ixs])*a1 + cvt(ptr1[ixs+1])*a; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
ixs = ixs < 0 ? 0 : src_size.width - 1; \
|
||||
p0 = cvt(ptr0[ixs]); p1 = cvt(ptr1[ixs]); \
|
||||
} \
|
||||
dst[x] = cast_macro(p0 + b * (p1 - p0)); \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return CV_OK; \
|
||||
}
|
||||
|
||||
ICV_DEF_GET_QUADRANGLE_SUB_PIX_FUNC( 8u32f, uchar, float, double, cv::saturate_cast<float>, CV_8TO32F )
|
||||
|
||||
/* Affine tracking algorithm */
|
||||
|
||||
CV_IMPL void
|
||||
cvCalcAffineFlowPyrLK( const void* arrA, const void* arrB,
|
||||
void* pyrarrA, void* pyrarrB,
|
||||
const CvPoint2D32f * featuresA,
|
||||
CvPoint2D32f * featuresB,
|
||||
float *matrices, int count,
|
||||
CvSize winSize, int level,
|
||||
char *status, float *error,
|
||||
CvTermCriteria criteria, int flags )
|
||||
{
|
||||
const int MAX_ITERS = 100;
|
||||
|
||||
cv::AutoBuffer<char> _status;
|
||||
cv::AutoBuffer<uchar> buffer;
|
||||
cv::AutoBuffer<uchar> pyr_buffer;
|
||||
|
||||
CvMat stubA, *imgA = (CvMat*)arrA;
|
||||
CvMat stubB, *imgB = (CvMat*)arrB;
|
||||
CvMat pstubA, *pyrA = (CvMat*)pyrarrA;
|
||||
CvMat pstubB, *pyrB = (CvMat*)pyrarrB;
|
||||
|
||||
static const float smoothKernel[] = { 0.09375, 0.3125, 0.09375 }; /* 3/32, 10/32, 3/32 */
|
||||
|
||||
int bufferBytes = 0;
|
||||
|
||||
uchar **imgI = 0;
|
||||
uchar **imgJ = 0;
|
||||
int *step = 0;
|
||||
double *scale = 0;
|
||||
CvSize* size = 0;
|
||||
|
||||
float *patchI;
|
||||
float *patchJ;
|
||||
float *Ix;
|
||||
float *Iy;
|
||||
|
||||
int i, j, k, l;
|
||||
|
||||
CvSize patchSize = cvSize( winSize.width * 2 + 1, winSize.height * 2 + 1 );
|
||||
int patchLen = patchSize.width * patchSize.height;
|
||||
int patchStep = patchSize.width * sizeof( patchI[0] );
|
||||
|
||||
CvSize srcPatchSize = cvSize( patchSize.width + 2, patchSize.height + 2 );
|
||||
int srcPatchLen = srcPatchSize.width * srcPatchSize.height;
|
||||
int srcPatchStep = srcPatchSize.width * sizeof( patchI[0] );
|
||||
CvSize imgSize;
|
||||
float eps = (float)MIN(winSize.width, winSize.height);
|
||||
|
||||
imgA = cvGetMat( imgA, &stubA );
|
||||
imgB = cvGetMat( imgB, &stubB );
|
||||
|
||||
if( CV_MAT_TYPE( imgA->type ) != CV_8UC1 )
|
||||
CV_Error( CV_StsUnsupportedFormat, "" );
|
||||
|
||||
if( !CV_ARE_TYPES_EQ( imgA, imgB ))
|
||||
CV_Error( CV_StsUnmatchedFormats, "" );
|
||||
|
||||
if( !CV_ARE_SIZES_EQ( imgA, imgB ))
|
||||
CV_Error( CV_StsUnmatchedSizes, "" );
|
||||
|
||||
if( imgA->step != imgB->step )
|
||||
CV_Error( CV_StsUnmatchedSizes, "imgA and imgB must have equal steps" );
|
||||
|
||||
if( !matrices )
|
||||
CV_Error( CV_StsNullPtr, "" );
|
||||
|
||||
imgSize = cv::Size(imgA->cols, imgA->rows);
|
||||
|
||||
if( pyrA )
|
||||
{
|
||||
pyrA = cvGetMat( pyrA, &pstubA );
|
||||
|
||||
if( pyrA->step*pyrA->height < icvMinimalPyramidSize( imgSize ) )
|
||||
CV_Error( CV_StsBadArg, "pyramid A has insufficient size" );
|
||||
}
|
||||
else
|
||||
{
|
||||
pyrA = &pstubA;
|
||||
pyrA->data.ptr = 0;
|
||||
}
|
||||
|
||||
if( pyrB )
|
||||
{
|
||||
pyrB = cvGetMat( pyrB, &pstubB );
|
||||
|
||||
if( pyrB->step*pyrB->height < icvMinimalPyramidSize( imgSize ) )
|
||||
CV_Error( CV_StsBadArg, "pyramid B has insufficient size" );
|
||||
}
|
||||
else
|
||||
{
|
||||
pyrB = &pstubB;
|
||||
pyrB->data.ptr = 0;
|
||||
}
|
||||
|
||||
if( count == 0 )
|
||||
return;
|
||||
|
||||
/* check input arguments */
|
||||
if( !featuresA || !featuresB || !matrices )
|
||||
CV_Error( CV_StsNullPtr, "" );
|
||||
|
||||
if( winSize.width <= 1 || winSize.height <= 1 )
|
||||
CV_Error( CV_StsOutOfRange, "the search window is too small" );
|
||||
|
||||
if( count < 0 )
|
||||
CV_Error( CV_StsOutOfRange, "" );
|
||||
|
||||
icvInitPyramidalAlgorithm( imgA, imgB,
|
||||
pyrA, pyrB, level, &criteria, MAX_ITERS, flags,
|
||||
&imgI, &imgJ, &step, &size, &scale, &pyr_buffer );
|
||||
|
||||
/* buffer_size = <size for patches> + <size for pyramids> */
|
||||
bufferBytes = (srcPatchLen + patchLen*3)*sizeof(patchI[0]) + (36*2 + 6)*sizeof(double);
|
||||
|
||||
buffer.allocate(bufferBytes);
|
||||
|
||||
if( !status )
|
||||
{
|
||||
_status.allocate(count);
|
||||
status = _status;
|
||||
}
|
||||
|
||||
patchI = (float *)(uchar*)buffer;
|
||||
patchJ = patchI + srcPatchLen;
|
||||
Ix = patchJ + patchLen;
|
||||
Iy = Ix + patchLen;
|
||||
|
||||
if( status )
|
||||
memset( status, 1, count );
|
||||
|
||||
if( !(flags & CV_LKFLOW_INITIAL_GUESSES) )
|
||||
{
|
||||
memcpy( featuresB, featuresA, count * sizeof( featuresA[0] ));
|
||||
for( i = 0; i < count * 4; i += 4 )
|
||||
{
|
||||
matrices[i] = matrices[i + 3] = 1.f;
|
||||
matrices[i + 1] = matrices[i + 2] = 0.f;
|
||||
}
|
||||
}
|
||||
|
||||
for( i = 0; i < count; i++ )
|
||||
{
|
||||
featuresB[i].x = (float)(featuresB[i].x * scale[level] * 0.5);
|
||||
featuresB[i].y = (float)(featuresB[i].y * scale[level] * 0.5);
|
||||
}
|
||||
|
||||
/* do processing from top pyramid level (smallest image)
|
||||
to the bottom (original image) */
|
||||
for( l = level; l >= 0; l-- )
|
||||
{
|
||||
CvSize levelSize = size[l];
|
||||
int levelStep = step[l];
|
||||
|
||||
/* find flow for each given point at the particular level */
|
||||
for( i = 0; i < count; i++ )
|
||||
{
|
||||
CvPoint2D32f u;
|
||||
float Av[6];
|
||||
double G[36];
|
||||
double meanI = 0, meanJ = 0;
|
||||
int x, y;
|
||||
int pt_status = status[i];
|
||||
CvMat mat;
|
||||
|
||||
if( !pt_status )
|
||||
continue;
|
||||
|
||||
Av[0] = matrices[i*4];
|
||||
Av[1] = matrices[i*4+1];
|
||||
Av[3] = matrices[i*4+2];
|
||||
Av[4] = matrices[i*4+3];
|
||||
|
||||
Av[2] = featuresB[i].x += featuresB[i].x;
|
||||
Av[5] = featuresB[i].y += featuresB[i].y;
|
||||
|
||||
u.x = (float) (featuresA[i].x * scale[l]);
|
||||
u.y = (float) (featuresA[i].y * scale[l]);
|
||||
|
||||
if( u.x < -eps || u.x >= levelSize.width+eps ||
|
||||
u.y < -eps || u.y >= levelSize.height+eps ||
|
||||
icvGetRectSubPix_8u32f_C1R( imgI[l], levelStep,
|
||||
levelSize, patchI, srcPatchStep, srcPatchSize, u ) < 0 )
|
||||
{
|
||||
/* point is outside the image. take the next */
|
||||
if( l == 0 )
|
||||
status[i] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
icvCalcIxIy_32f( patchI, srcPatchStep, Ix, Iy,
|
||||
(srcPatchSize.width-2)*sizeof(patchI[0]), srcPatchSize,
|
||||
smoothKernel, patchJ );
|
||||
|
||||
/* repack patchI (remove borders) */
|
||||
for( k = 0; k < patchSize.height; k++ )
|
||||
memcpy( patchI + k * patchSize.width,
|
||||
patchI + (k + 1) * srcPatchSize.width + 1, patchStep );
|
||||
|
||||
memset( G, 0, sizeof( G ));
|
||||
|
||||
/* calculate G matrix */
|
||||
for( y = -winSize.height, k = 0; y <= winSize.height; y++ )
|
||||
{
|
||||
for( x = -winSize.width; x <= winSize.width; x++, k++ )
|
||||
{
|
||||
double ixix = ((double) Ix[k]) * Ix[k];
|
||||
double ixiy = ((double) Ix[k]) * Iy[k];
|
||||
double iyiy = ((double) Iy[k]) * Iy[k];
|
||||
|
||||
double xx, xy, yy;
|
||||
|
||||
G[0] += ixix;
|
||||
G[1] += ixiy;
|
||||
G[2] += x * ixix;
|
||||
G[3] += y * ixix;
|
||||
G[4] += x * ixiy;
|
||||
G[5] += y * ixiy;
|
||||
|
||||
// G[6] == G[1]
|
||||
G[7] += iyiy;
|
||||
// G[8] == G[4]
|
||||
// G[9] == G[5]
|
||||
G[10] += x * iyiy;
|
||||
G[11] += y * iyiy;
|
||||
|
||||
xx = x * x;
|
||||
xy = x * y;
|
||||
yy = y * y;
|
||||
|
||||
// G[12] == G[2]
|
||||
// G[13] == G[8] == G[4]
|
||||
G[14] += xx * ixix;
|
||||
G[15] += xy * ixix;
|
||||
G[16] += xx * ixiy;
|
||||
G[17] += xy * ixiy;
|
||||
|
||||
// G[18] == G[3]
|
||||
// G[19] == G[9]
|
||||
// G[20] == G[15]
|
||||
G[21] += yy * ixix;
|
||||
// G[22] == G[17]
|
||||
G[23] += yy * ixiy;
|
||||
|
||||
// G[24] == G[4]
|
||||
// G[25] == G[10]
|
||||
// G[26] == G[16]
|
||||
// G[27] == G[22]
|
||||
G[28] += xx * iyiy;
|
||||
G[29] += xy * iyiy;
|
||||
|
||||
// G[30] == G[5]
|
||||
// G[31] == G[11]
|
||||
// G[32] == G[17]
|
||||
// G[33] == G[23]
|
||||
// G[34] == G[29]
|
||||
G[35] += yy * iyiy;
|
||||
|
||||
meanI += patchI[k];
|
||||
}
|
||||
}
|
||||
|
||||
meanI /= patchSize.width*patchSize.height;
|
||||
|
||||
G[8] = G[4];
|
||||
G[9] = G[5];
|
||||
G[22] = G[17];
|
||||
|
||||
// fill part of G below its diagonal
|
||||
for( y = 1; y < 6; y++ )
|
||||
for( x = 0; x < y; x++ )
|
||||
G[y * 6 + x] = G[x * 6 + y];
|
||||
|
||||
cvInitMatHeader( &mat, 6, 6, CV_64FC1, G );
|
||||
|
||||
if( cvInvert( &mat, &mat, CV_SVD ) < 1e-4 )
|
||||
{
|
||||
/* bad matrix. take the next point */
|
||||
if( l == 0 )
|
||||
status[i] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
for( j = 0; j < criteria.max_iter; j++ )
|
||||
{
|
||||
double b[6] = {0,0,0,0,0,0}, eta[6];
|
||||
double t0, t1, s = 0;
|
||||
|
||||
if( Av[2] < -eps || Av[2] >= levelSize.width+eps ||
|
||||
Av[5] < -eps || Av[5] >= levelSize.height+eps ||
|
||||
icvGetQuadrangleSubPix_8u32f_C1R( imgJ[l], levelStep,
|
||||
levelSize, patchJ, patchStep, patchSize, Av ) < 0 )
|
||||
{
|
||||
pt_status = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
for( y = -winSize.height, k = 0, meanJ = 0; y <= winSize.height; y++ )
|
||||
for( x = -winSize.width; x <= winSize.width; x++, k++ )
|
||||
meanJ += patchJ[k];
|
||||
|
||||
meanJ = meanJ / (patchSize.width * patchSize.height) - meanI;
|
||||
|
||||
for( y = -winSize.height, k = 0; y <= winSize.height; y++ )
|
||||
{
|
||||
for( x = -winSize.width; x <= winSize.width; x++, k++ )
|
||||
{
|
||||
double t = patchI[k] - patchJ[k] + meanJ;
|
||||
double ixt = Ix[k] * t;
|
||||
double iyt = Iy[k] * t;
|
||||
|
||||
s += t;
|
||||
|
||||
b[0] += ixt;
|
||||
b[1] += iyt;
|
||||
b[2] += x * ixt;
|
||||
b[3] += y * ixt;
|
||||
b[4] += x * iyt;
|
||||
b[5] += y * iyt;
|
||||
}
|
||||
}
|
||||
|
||||
for( k = 0; k < 6; k++ )
|
||||
eta[k] = G[k*6]*b[0] + G[k*6+1]*b[1] + G[k*6+2]*b[2] +
|
||||
G[k*6+3]*b[3] + G[k*6+4]*b[4] + G[k*6+5]*b[5];
|
||||
|
||||
Av[2] = (float)(Av[2] + Av[0] * eta[0] + Av[1] * eta[1]);
|
||||
Av[5] = (float)(Av[5] + Av[3] * eta[0] + Av[4] * eta[1]);
|
||||
|
||||
t0 = Av[0] * (1 + eta[2]) + Av[1] * eta[4];
|
||||
t1 = Av[0] * eta[3] + Av[1] * (1 + eta[5]);
|
||||
Av[0] = (float)t0;
|
||||
Av[1] = (float)t1;
|
||||
|
||||
t0 = Av[3] * (1 + eta[2]) + Av[4] * eta[4];
|
||||
t1 = Av[3] * eta[3] + Av[4] * (1 + eta[5]);
|
||||
Av[3] = (float)t0;
|
||||
Av[4] = (float)t1;
|
||||
|
||||
if( eta[0] * eta[0] + eta[1] * eta[1] < criteria.epsilon )
|
||||
break;
|
||||
}
|
||||
|
||||
if( pt_status != 0 || l == 0 )
|
||||
{
|
||||
status[i] = (char)pt_status;
|
||||
featuresB[i].x = Av[2];
|
||||
featuresB[i].y = Av[5];
|
||||
|
||||
matrices[i*4] = Av[0];
|
||||
matrices[i*4+1] = Av[1];
|
||||
matrices[i*4+2] = Av[3];
|
||||
matrices[i*4+3] = Av[4];
|
||||
}
|
||||
|
||||
if( pt_status && l == 0 && error )
|
||||
{
|
||||
/* calc error */
|
||||
double err = 0;
|
||||
|
||||
for( y = 0, k = 0; y < patchSize.height; y++ )
|
||||
{
|
||||
for( x = 0; x < patchSize.width; x++, k++ )
|
||||
{
|
||||
double t = patchI[k] - patchJ[k] + meanJ;
|
||||
err += t * t;
|
||||
}
|
||||
}
|
||||
error[i] = (float)std::sqrt(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,472 +0,0 @@
|
||||
/*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>
|
||||
|
||||
// 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
|
||||
{
|
||||
|
||||
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 : 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);
|
||||
|
||||
//! 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 AlgorithmInfo* info() const { return 0; }
|
||||
|
||||
virtual void getBackgroundImage(OutputArray) const
|
||||
{
|
||||
CV_Error( Error::StsNotImplemented, "" );
|
||||
}
|
||||
|
||||
virtual int getHistory() const { return history; }
|
||||
virtual void setHistory(int _nframes) { history = _nframes; }
|
||||
|
||||
virtual int getNMixtures() const { return nmixtures; }
|
||||
virtual void setNMixtures(int nmix) { nmixtures = nmix; }
|
||||
|
||||
virtual double getBackgroundRatio() const { return backgroundRatio; }
|
||||
virtual void setBackgroundRatio(double _backgroundRatio) { backgroundRatio = _backgroundRatio; }
|
||||
|
||||
virtual double getNoiseSigma() const { return noiseSigma; }
|
||||
virtual void setNoiseSigma(double _noiseSigma) { noiseSigma = _noiseSigma; }
|
||||
|
||||
virtual void write(FileStorage& fs) const
|
||||
{
|
||||
fs << "name" << name_
|
||||
<< "history" << history
|
||||
<< "nmixtures" << nmixtures
|
||||
<< "backgroundRatio" << backgroundRatio
|
||||
<< "noiseSigma" << noiseSigma;
|
||||
}
|
||||
|
||||
virtual void read(const FileNode& fn)
|
||||
{
|
||||
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" );
|
||||
}
|
||||
|
||||
Ptr<BackgroundSubtractorMOG> createBackgroundSubtractorMOG(int history, int nmixtures,
|
||||
double backgroundRatio, double noiseSigma)
|
||||
{
|
||||
return makePtr<BackgroundSubtractorMOGImpl>(history, nmixtures, backgroundRatio, noiseSigma);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* End of file. */
|
||||
@@ -83,7 +83,7 @@
|
||||
///////////*/
|
||||
|
||||
#include "precomp.hpp"
|
||||
#include "opencl_kernels.hpp"
|
||||
#include "opencl_kernels_video.hpp"
|
||||
|
||||
namespace cv
|
||||
{
|
||||
@@ -840,9 +840,9 @@ void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask,
|
||||
|
||||
parallel_for_(Range(0, image.rows),
|
||||
MOG2Invoker(image, fgmask,
|
||||
(GMM*)bgmodel.data,
|
||||
(float*)(bgmodel.data + sizeof(GMM)*nmixtures*image.rows*image.cols),
|
||||
bgmodelUsedModes.data, nmixtures, (float)learningRate,
|
||||
bgmodel.ptr<GMM>(),
|
||||
(float*)(bgmodel.ptr() + sizeof(GMM)*nmixtures*image.rows*image.cols),
|
||||
bgmodelUsedModes.ptr(), nmixtures, (float)learningRate,
|
||||
(float)varThreshold,
|
||||
backgroundRatio, varThresholdGen,
|
||||
fVarInit, fVarMin, fVarMax, float(-learningRate*fCT), fTau,
|
||||
@@ -864,7 +864,7 @@ void BackgroundSubtractorMOG2Impl::getBackgroundImage(OutputArray backgroundImag
|
||||
CV_Assert(nchannels == 1 || nchannels == 3);
|
||||
Mat meanBackground(frameSize, CV_MAKETYPE(CV_8U, nchannels), Scalar::all(0));
|
||||
int firstGaussianIdx = 0;
|
||||
const GMM* gmm = (GMM*)bgmodel.data;
|
||||
const GMM* gmm = bgmodel.ptr<GMM>();
|
||||
const float* mean = reinterpret_cast<const float*>(gmm + frameSize.width*frameSize.height*nmixtures);
|
||||
std::vector<float> meanVal(nchannels, 0.f);
|
||||
for(int row=0; row<meanBackground.rows; row++)
|
||||
|
||||
@@ -1,522 +0,0 @@
|
||||
/*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 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.
|
||||
*
|
||||
* Prepared and integrated by Andrew B. Godbehere.
|
||||
*/
|
||||
|
||||
#include "precomp.hpp"
|
||||
#include <limits>
|
||||
|
||||
namespace cv
|
||||
{
|
||||
|
||||
class BackgroundSubtractorGMGImpl : 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()
|
||||
{
|
||||
}
|
||||
|
||||
virtual AlgorithmInfo* info() const { return 0; }
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Releases all inner buffers.
|
||||
*/
|
||||
void release();
|
||||
|
||||
virtual int getMaxFeatures() const { return maxFeatures; }
|
||||
virtual void setMaxFeatures(int _maxFeatures) { maxFeatures = _maxFeatures; }
|
||||
|
||||
virtual double getDefaultLearningRate() const { return learningRate; }
|
||||
virtual void setDefaultLearningRate(double lr) { learningRate = lr; }
|
||||
|
||||
virtual int getNumFrames() const { return numInitializationFrames; }
|
||||
virtual void setNumFrames(int nframes) { numInitializationFrames = nframes; }
|
||||
|
||||
virtual int getQuantizationLevels() const { return quantizationLevels; }
|
||||
virtual void setQuantizationLevels(int nlevels) { quantizationLevels = nlevels; }
|
||||
|
||||
virtual double getBackgroundPrior() const { return backgroundPrior; }
|
||||
virtual void setBackgroundPrior(double bgprior) { backgroundPrior = bgprior; }
|
||||
|
||||
virtual int getSmoothingRadius() const { return smoothingRadius; }
|
||||
virtual void setSmoothingRadius(int radius) { smoothingRadius = radius; }
|
||||
|
||||
virtual double getDecisionThreshold() const { return decisionThreshold; }
|
||||
virtual void setDecisionThreshold(double thresh) { decisionThreshold = thresh; }
|
||||
|
||||
virtual bool getUpdateBackgroundModel() const { return updateBackgroundModel; }
|
||||
virtual void setUpdateBackgroundModel(bool update) { updateBackgroundModel = update; }
|
||||
|
||||
virtual double getMinVal() const { return minVal_; }
|
||||
virtual void setMinVal(double val) { minVal_ = val; }
|
||||
|
||||
virtual double getMaxVal() const { return maxVal_; }
|
||||
virtual void setMaxVal(double val) { maxVal_ = val; }
|
||||
|
||||
virtual void getBackgroundImage(OutputArray) const
|
||||
{
|
||||
CV_Error( Error::StsNotImplemented, "" );
|
||||
}
|
||||
|
||||
virtual void write(FileStorage& fs) const
|
||||
{
|
||||
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_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_<unsigned int> colors_;
|
||||
Mat_<float> weights_;
|
||||
|
||||
Mat buf_;
|
||||
};
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
float findFeature(unsigned int color, const unsigned 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
bool insertFeature(unsigned int color, float weight, unsigned 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(unsigned 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(unsigned 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;
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T> struct Quantization
|
||||
{
|
||||
static unsigned 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_<unsigned 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;
|
||||
|
||||
private:
|
||||
Mat frame_;
|
||||
|
||||
mutable Mat_<uchar> fgmask_;
|
||||
|
||||
mutable Mat_<int> nfeatures_;
|
||||
mutable Mat_<unsigned 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 unsigned 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];
|
||||
unsigned int* colors = colors_[featureIdx];
|
||||
float* weights = weights_[featureIdx];
|
||||
|
||||
unsigned 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();
|
||||
|
||||
CV_Assert(frame.depth() == CV_8U || frame.depth() == CV_16U || frame.depth() == CV_32F);
|
||||
CV_Assert(frame.channels() == 1 || frame.channels() == 3 || frame.channels() == 4);
|
||||
|
||||
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 )
|
||||
{
|
||||
minval = 0;
|
||||
maxval = frame.depth() == CV_8U ? 255.0 : frame.depth() == CV_16U ? std::numeric_limits<ushort>::max() : 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, buf_, smoothingRadius);
|
||||
swap(fgmask, buf_);
|
||||
}
|
||||
|
||||
// keep track of how many frames we have processed
|
||||
++frameNum_;
|
||||
}
|
||||
|
||||
void BackgroundSubtractorGMGImpl::release()
|
||||
{
|
||||
frameSize_ = Size();
|
||||
|
||||
nfeatures_.release();
|
||||
colors_.release();
|
||||
weights_.release();
|
||||
buf_.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)");
|
||||
);
|
||||
*/
|
||||
|
||||
}
|
||||
@@ -87,76 +87,6 @@ cvCamShift( const void* imgProb, CvRect windowIn,
|
||||
return rr.size.width*rr.size.height > 0.f ? 1 : -1;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////// Motion Templates ////////////////////////////
|
||||
|
||||
CV_IMPL void
|
||||
cvUpdateMotionHistory( const void* silhouette, void* mhimg,
|
||||
double timestamp, double mhi_duration )
|
||||
{
|
||||
cv::Mat silh = cv::cvarrToMat(silhouette), mhi = cv::cvarrToMat(mhimg);
|
||||
cv::updateMotionHistory(silh, mhi, timestamp, mhi_duration);
|
||||
}
|
||||
|
||||
|
||||
CV_IMPL void
|
||||
cvCalcMotionGradient( const CvArr* mhimg, CvArr* maskimg,
|
||||
CvArr* orientation,
|
||||
double delta1, double delta2,
|
||||
int aperture_size )
|
||||
{
|
||||
cv::Mat mhi = cv::cvarrToMat(mhimg);
|
||||
const cv::Mat mask = cv::cvarrToMat(maskimg), orient = cv::cvarrToMat(orientation);
|
||||
cv::calcMotionGradient(mhi, mask, orient, delta1, delta2, aperture_size);
|
||||
}
|
||||
|
||||
|
||||
CV_IMPL double
|
||||
cvCalcGlobalOrientation( const void* orientation, const void* maskimg, const void* mhimg,
|
||||
double curr_mhi_timestamp, double mhi_duration )
|
||||
{
|
||||
cv::Mat mhi = cv::cvarrToMat(mhimg);
|
||||
cv::Mat mask = cv::cvarrToMat(maskimg), orient = cv::cvarrToMat(orientation);
|
||||
return cv::calcGlobalOrientation(orient, mask, mhi, curr_mhi_timestamp, mhi_duration);
|
||||
}
|
||||
|
||||
|
||||
CV_IMPL CvSeq*
|
||||
cvSegmentMotion( const CvArr* mhimg, CvArr* segmaskimg, CvMemStorage* storage,
|
||||
double timestamp, double segThresh )
|
||||
{
|
||||
cv::Mat mhi = cv::cvarrToMat(mhimg);
|
||||
const cv::Mat segmask = cv::cvarrToMat(segmaskimg);
|
||||
std::vector<cv::Rect> brs;
|
||||
cv::segmentMotion(mhi, segmask, brs, timestamp, segThresh);
|
||||
CvSeq* seq = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvConnectedComp), storage);
|
||||
|
||||
CvConnectedComp comp;
|
||||
memset(&comp, 0, sizeof(comp));
|
||||
for( size_t i = 0; i < brs.size(); i++ )
|
||||
{
|
||||
cv::Rect roi = brs[i];
|
||||
float compLabel = (float)(i+1);
|
||||
int x, y, area = 0;
|
||||
|
||||
cv::Mat part = segmask(roi);
|
||||
for( y = 0; y < roi.height; y++ )
|
||||
{
|
||||
const float* partptr = part.ptr<float>(y);
|
||||
for( x = 0; x < roi.width; x++ )
|
||||
area += partptr[x] == compLabel;
|
||||
}
|
||||
|
||||
comp.value = cv::Scalar(compLabel);
|
||||
comp.rect = roi;
|
||||
comp.area = area;
|
||||
cvSeqPush(seq, &comp);
|
||||
}
|
||||
|
||||
return seq;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////// Kalman ///////////////////////////////
|
||||
|
||||
CV_IMPL CvKalman*
|
||||
|
||||
@@ -297,7 +297,12 @@ static void update_warping_matrix_ECC (Mat& map_matrix, const Mat& update, const
|
||||
mapPtr[5] += updatePtr[7];
|
||||
}
|
||||
if (motionType == MOTION_EUCLIDEAN) {
|
||||
double new_theta = acos(mapPtr[0]) + updatePtr[0];
|
||||
double new_theta = updatePtr[0];
|
||||
if (mapPtr[3]>0)
|
||||
new_theta += acos(mapPtr[0]);
|
||||
|
||||
if (mapPtr[3]<0)
|
||||
new_theta -= acos(mapPtr[0]);
|
||||
|
||||
mapPtr[2] += updatePtr[1];
|
||||
mapPtr[5] += updatePtr[2];
|
||||
|
||||
@@ -84,7 +84,7 @@ const Mat& KalmanFilter::predict(const Mat& control)
|
||||
// update the state: x'(k) = A*x(k)
|
||||
statePre = transitionMatrix*statePost;
|
||||
|
||||
if( control.data )
|
||||
if( !control.empty() )
|
||||
// x'(k) = x'(k) + B*u(k)
|
||||
statePre += controlMatrix*control;
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
#include <float.h>
|
||||
#include <stdio.h>
|
||||
#include "lkpyramid.hpp"
|
||||
#include "opencl_kernels.hpp"
|
||||
#include "opencl_kernels_video.hpp"
|
||||
|
||||
#define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n))
|
||||
|
||||
@@ -311,11 +311,11 @@ void cv::detail::LKTrackerInvoker::operator()(const Range& range) const
|
||||
int x, y;
|
||||
for( y = 0; y < winSize.height; y++ )
|
||||
{
|
||||
const uchar* src = (const uchar*)I.data + (y + iprevPt.y)*stepI + iprevPt.x*cn;
|
||||
const deriv_type* dsrc = (const deriv_type*)derivI.data + (y + iprevPt.y)*dstep + iprevPt.x*cn2;
|
||||
const uchar* src = I.ptr() + (y + iprevPt.y)*stepI + iprevPt.x*cn;
|
||||
const deriv_type* dsrc = derivI.ptr<deriv_type>() + (y + iprevPt.y)*dstep + iprevPt.x*cn2;
|
||||
|
||||
deriv_type* Iptr = (deriv_type*)(IWinBuf.data + y*IWinBuf.step);
|
||||
deriv_type* dIptr = (deriv_type*)(derivIWinBuf.data + y*derivIWinBuf.step);
|
||||
deriv_type* Iptr = IWinBuf.ptr<deriv_type>(y);
|
||||
deriv_type* dIptr = derivIWinBuf.ptr<deriv_type>(y);
|
||||
|
||||
x = 0;
|
||||
|
||||
@@ -541,9 +541,9 @@ void cv::detail::LKTrackerInvoker::operator()(const Range& range) const
|
||||
|
||||
for( y = 0; y < winSize.height; y++ )
|
||||
{
|
||||
const uchar* Jptr = (const uchar*)J.data + (y + inextPt.y)*stepJ + inextPt.x*cn;
|
||||
const deriv_type* Iptr = (const deriv_type*)(IWinBuf.data + y*IWinBuf.step);
|
||||
const deriv_type* dIptr = (const deriv_type*)(derivIWinBuf.data + y*derivIWinBuf.step);
|
||||
const uchar* Jptr = J.ptr() + (y + inextPt.y)*stepJ + inextPt.x*cn;
|
||||
const deriv_type* Iptr = IWinBuf.ptr<deriv_type>(y);
|
||||
const deriv_type* dIptr = derivIWinBuf.ptr<deriv_type>(y);
|
||||
|
||||
x = 0;
|
||||
|
||||
@@ -725,8 +725,8 @@ void cv::detail::LKTrackerInvoker::operator()(const Range& range) const
|
||||
|
||||
for( y = 0; y < winSize.height; y++ )
|
||||
{
|
||||
const uchar* Jptr = (const uchar*)J.data + (y + inextPoint.y)*stepJ + inextPoint.x*cn;
|
||||
const deriv_type* Iptr = (const deriv_type*)(IWinBuf.data + y*IWinBuf.step);
|
||||
const uchar* Jptr = J.ptr() + (y + inextPoint.y)*stepJ + inextPoint.x*cn;
|
||||
const deriv_type* Iptr = IWinBuf.ptr<deriv_type>(y);
|
||||
|
||||
for( x = 0; x < winSize.width*cn; x++ )
|
||||
{
|
||||
@@ -1120,13 +1120,13 @@ void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg,
|
||||
Mat nextPtsMat = _nextPts.getMat();
|
||||
CV_Assert( nextPtsMat.checkVector(2, CV_32F, true) == npoints );
|
||||
|
||||
const Point2f* prevPts = (const Point2f*)prevPtsMat.data;
|
||||
Point2f* nextPts = (Point2f*)nextPtsMat.data;
|
||||
const Point2f* prevPts = prevPtsMat.ptr<Point2f>();
|
||||
Point2f* nextPts = nextPtsMat.ptr<Point2f>();
|
||||
|
||||
_status.create((int)npoints, 1, CV_8U, -1, true);
|
||||
Mat statusMat = _status.getMat(), errMat;
|
||||
CV_Assert( statusMat.isContinuous() );
|
||||
uchar* status = statusMat.data;
|
||||
uchar* status = statusMat.ptr();
|
||||
float* err = 0;
|
||||
|
||||
for( i = 0; i < npoints; i++ )
|
||||
@@ -1137,7 +1137,7 @@ void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg,
|
||||
_err.create((int)npoints, 1, CV_32F, -1, true);
|
||||
errMat = _err.getMat();
|
||||
CV_Assert( errMat.isContinuous() );
|
||||
err = (float*)errMat.data;
|
||||
err = errMat.ptr<float>();
|
||||
}
|
||||
|
||||
std::vector<Mat> prevPyr, nextPyr;
|
||||
@@ -1230,7 +1230,7 @@ void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg,
|
||||
{
|
||||
Size imgSize = prevPyr[level * lvlStep1].size();
|
||||
Mat _derivI( imgSize.height + winSize.height*2,
|
||||
imgSize.width + winSize.width*2, derivIBuf.type(), derivIBuf.data );
|
||||
imgSize.width + winSize.width*2, derivIBuf.type(), derivIBuf.ptr() );
|
||||
derivI = _derivI(Rect(winSize.width, winSize.height, imgSize.width, imgSize.height));
|
||||
calcSharrDeriv(prevPyr[level * lvlStep1], derivI);
|
||||
copyMakeBorder(derivI, _derivI, winSize.height, winSize.height, winSize.width, winSize.width, BORDER_CONSTANT|BORDER_ISOLATED);
|
||||
|
||||
@@ -1,416 +0,0 @@
|
||||
/*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.
|
||||
//
|
||||
//
|
||||
// Intel License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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 "opencl_kernels.hpp"
|
||||
|
||||
#ifdef HAVE_OPENCL
|
||||
|
||||
namespace cv {
|
||||
|
||||
static bool ocl_updateMotionHistory( InputArray _silhouette, InputOutputArray _mhi,
|
||||
float timestamp, float delbound )
|
||||
{
|
||||
ocl::Kernel k("updateMotionHistory", ocl::video::updatemotionhistory_oclsrc);
|
||||
if (k.empty())
|
||||
return false;
|
||||
|
||||
UMat silh = _silhouette.getUMat(), mhi = _mhi.getUMat();
|
||||
|
||||
k.args(ocl::KernelArg::ReadOnlyNoSize(silh), ocl::KernelArg::ReadWrite(mhi),
|
||||
timestamp, delbound);
|
||||
|
||||
size_t globalsize[2] = { silh.cols, silh.rows };
|
||||
return k.run(2, globalsize, NULL, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void cv::updateMotionHistory( InputArray _silhouette, InputOutputArray _mhi,
|
||||
double timestamp, double duration )
|
||||
{
|
||||
CV_Assert( _silhouette.type() == CV_8UC1 && _mhi.type() == CV_32FC1 );
|
||||
CV_Assert( _silhouette.sameSize(_mhi) );
|
||||
|
||||
float ts = (float)timestamp;
|
||||
float delbound = (float)(timestamp - duration);
|
||||
|
||||
CV_OCL_RUN(_mhi.isUMat() && _mhi.dims() <= 2,
|
||||
ocl_updateMotionHistory(_silhouette, _mhi, ts, delbound))
|
||||
|
||||
Mat silh = _silhouette.getMat(), mhi = _mhi.getMat();
|
||||
Size size = silh.size();
|
||||
#if defined(HAVE_IPP)
|
||||
int silhstep = (int)silh.step, mhistep = (int)mhi.step;
|
||||
#endif
|
||||
|
||||
if( silh.isContinuous() && mhi.isContinuous() )
|
||||
{
|
||||
size.width *= size.height;
|
||||
size.height = 1;
|
||||
#if defined(HAVE_IPP)
|
||||
silhstep = (int)silh.total();
|
||||
mhistep = (int)mhi.total() * sizeof(Ipp32f);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(HAVE_IPP)
|
||||
IppStatus status = ippiUpdateMotionHistory_8u32f_C1IR((const Ipp8u *)silh.data, silhstep, (Ipp32f *)mhi.data, mhistep,
|
||||
ippiSize(size.width, size.height), (Ipp32f)timestamp, (Ipp32f)duration);
|
||||
if (status >= 0)
|
||||
return;
|
||||
#endif
|
||||
|
||||
#if CV_SSE2
|
||||
volatile bool useSIMD = cv::checkHardwareSupport(CV_CPU_SSE2);
|
||||
#endif
|
||||
|
||||
for(int y = 0; y < size.height; y++ )
|
||||
{
|
||||
const uchar* silhData = silh.ptr<uchar>(y);
|
||||
float* mhiData = mhi.ptr<float>(y);
|
||||
int x = 0;
|
||||
|
||||
#if CV_SSE2
|
||||
if( useSIMD )
|
||||
{
|
||||
__m128 ts4 = _mm_set1_ps(ts), db4 = _mm_set1_ps(delbound);
|
||||
for( ; x <= size.width - 8; x += 8 )
|
||||
{
|
||||
__m128i z = _mm_setzero_si128();
|
||||
__m128i s = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(silhData + x)), z);
|
||||
__m128 s0 = _mm_cvtepi32_ps(_mm_unpacklo_epi16(s, z)), s1 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(s, z));
|
||||
__m128 v0 = _mm_loadu_ps(mhiData + x), v1 = _mm_loadu_ps(mhiData + x + 4);
|
||||
__m128 fz = _mm_setzero_ps();
|
||||
|
||||
v0 = _mm_and_ps(v0, _mm_cmpge_ps(v0, db4));
|
||||
v1 = _mm_and_ps(v1, _mm_cmpge_ps(v1, db4));
|
||||
|
||||
__m128 m0 = _mm_and_ps(_mm_xor_ps(v0, ts4), _mm_cmpneq_ps(s0, fz));
|
||||
__m128 m1 = _mm_and_ps(_mm_xor_ps(v1, ts4), _mm_cmpneq_ps(s1, fz));
|
||||
|
||||
v0 = _mm_xor_ps(v0, m0);
|
||||
v1 = _mm_xor_ps(v1, m1);
|
||||
|
||||
_mm_storeu_ps(mhiData + x, v0);
|
||||
_mm_storeu_ps(mhiData + x + 4, v1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
for( ; x < size.width; x++ )
|
||||
{
|
||||
float val = mhiData[x];
|
||||
val = silhData[x] ? ts : val < delbound ? 0 : val;
|
||||
mhiData[x] = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void cv::calcMotionGradient( InputArray _mhi, OutputArray _mask,
|
||||
OutputArray _orientation,
|
||||
double delta1, double delta2,
|
||||
int aperture_size )
|
||||
{
|
||||
static int runcase = 0; runcase++;
|
||||
|
||||
Mat mhi = _mhi.getMat();
|
||||
Size size = mhi.size();
|
||||
|
||||
_mask.create(size, CV_8U);
|
||||
_orientation.create(size, CV_32F);
|
||||
|
||||
Mat mask = _mask.getMat();
|
||||
Mat orient = _orientation.getMat();
|
||||
|
||||
if( aperture_size < 3 || aperture_size > 7 || (aperture_size & 1) == 0 )
|
||||
CV_Error( Error::StsOutOfRange, "aperture_size must be 3, 5 or 7" );
|
||||
|
||||
if( delta1 <= 0 || delta2 <= 0 )
|
||||
CV_Error( Error::StsOutOfRange, "both delta's must be positive" );
|
||||
|
||||
if( mhi.type() != CV_32FC1 )
|
||||
CV_Error( Error::StsUnsupportedFormat,
|
||||
"MHI must be single-channel floating-point images" );
|
||||
|
||||
if( orient.data == mhi.data )
|
||||
{
|
||||
_orientation.release();
|
||||
_orientation.create(size, CV_32F);
|
||||
orient = _orientation.getMat();
|
||||
}
|
||||
|
||||
if( delta1 > delta2 )
|
||||
std::swap(delta1, delta2);
|
||||
|
||||
float gradient_epsilon = 1e-4f * aperture_size * aperture_size;
|
||||
float min_delta = (float)delta1;
|
||||
float max_delta = (float)delta2;
|
||||
|
||||
Mat dX_min, dY_max;
|
||||
|
||||
// calc Dx and Dy
|
||||
Sobel( mhi, dX_min, CV_32F, 1, 0, aperture_size, 1, 0, BORDER_REPLICATE );
|
||||
Sobel( mhi, dY_max, CV_32F, 0, 1, aperture_size, 1, 0, BORDER_REPLICATE );
|
||||
|
||||
int x, y;
|
||||
|
||||
if( mhi.isContinuous() && orient.isContinuous() && mask.isContinuous() )
|
||||
{
|
||||
size.width *= size.height;
|
||||
size.height = 1;
|
||||
}
|
||||
|
||||
// calc gradient
|
||||
for( y = 0; y < size.height; y++ )
|
||||
{
|
||||
const float* dX_min_row = dX_min.ptr<float>(y);
|
||||
const float* dY_max_row = dY_max.ptr<float>(y);
|
||||
float* orient_row = orient.ptr<float>(y);
|
||||
uchar* mask_row = mask.ptr<uchar>(y);
|
||||
|
||||
fastAtan2(dY_max_row, dX_min_row, orient_row, size.width, true);
|
||||
|
||||
// make orientation zero where the gradient is very small
|
||||
for( x = 0; x < size.width; x++ )
|
||||
{
|
||||
float dY = dY_max_row[x];
|
||||
float dX = dX_min_row[x];
|
||||
|
||||
if( std::abs(dX) < gradient_epsilon && std::abs(dY) < gradient_epsilon )
|
||||
{
|
||||
mask_row[x] = (uchar)0;
|
||||
orient_row[x] = 0.f;
|
||||
}
|
||||
else
|
||||
mask_row[x] = (uchar)1;
|
||||
}
|
||||
}
|
||||
|
||||
erode( mhi, dX_min, noArray(), Point(-1,-1), (aperture_size-1)/2, BORDER_REPLICATE );
|
||||
dilate( mhi, dY_max, noArray(), Point(-1,-1), (aperture_size-1)/2, BORDER_REPLICATE );
|
||||
|
||||
// mask off pixels which have little motion difference in their neighborhood
|
||||
for( y = 0; y < size.height; y++ )
|
||||
{
|
||||
const float* dX_min_row = dX_min.ptr<float>(y);
|
||||
const float* dY_max_row = dY_max.ptr<float>(y);
|
||||
float* orient_row = orient.ptr<float>(y);
|
||||
uchar* mask_row = mask.ptr<uchar>(y);
|
||||
|
||||
for( x = 0; x < size.width; x++ )
|
||||
{
|
||||
float d0 = dY_max_row[x] - dX_min_row[x];
|
||||
|
||||
if( mask_row[x] == 0 || d0 < min_delta || max_delta < d0 )
|
||||
{
|
||||
mask_row[x] = (uchar)0;
|
||||
orient_row[x] = 0.f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double cv::calcGlobalOrientation( InputArray _orientation, InputArray _mask,
|
||||
InputArray _mhi, double /*timestamp*/,
|
||||
double duration )
|
||||
{
|
||||
Mat orient = _orientation.getMat(), mask = _mask.getMat(), mhi = _mhi.getMat();
|
||||
Size size = mhi.size();
|
||||
|
||||
CV_Assert( mask.type() == CV_8U && orient.type() == CV_32F && mhi.type() == CV_32F );
|
||||
CV_Assert( mask.size() == size && orient.size() == size );
|
||||
CV_Assert( duration > 0 );
|
||||
|
||||
int histSize = 12;
|
||||
float _ranges[] = { 0.f, 360.f };
|
||||
const float* ranges = _ranges;
|
||||
Mat hist;
|
||||
|
||||
calcHist(&orient, 1, 0, mask, hist, 1, &histSize, &ranges);
|
||||
|
||||
// find the maximum index (the dominant orientation)
|
||||
Point baseOrientPt;
|
||||
minMaxLoc(hist, 0, 0, 0, &baseOrientPt);
|
||||
float fbaseOrient = (baseOrientPt.x + baseOrientPt.y)*360.f/histSize;
|
||||
|
||||
// override timestamp with the maximum value in MHI
|
||||
double timestamp = 0;
|
||||
minMaxLoc( mhi, 0, ×tamp, 0, 0, mask );
|
||||
|
||||
// find the shift relative to the dominant orientation as weighted sum of relative angles
|
||||
float a = (float)(254. / 255. / duration);
|
||||
float b = (float)(1. - timestamp * a);
|
||||
float delbound = (float)(timestamp - duration);
|
||||
|
||||
if( mhi.isContinuous() && mask.isContinuous() && orient.isContinuous() )
|
||||
{
|
||||
size.width *= size.height;
|
||||
size.height = 1;
|
||||
}
|
||||
|
||||
/*
|
||||
a = 254/(255*dt)
|
||||
b = 1 - t*a = 1 - 254*t/(255*dur) =
|
||||
(255*dt - 254*t)/(255*dt) =
|
||||
(dt - (t - dt)*254)/(255*dt);
|
||||
--------------------------------------------------------
|
||||
ax + b = 254*x/(255*dt) + (dt - (t - dt)*254)/(255*dt) =
|
||||
(254*x + dt - (t - dt)*254)/(255*dt) =
|
||||
((x - (t - dt))*254 + dt)/(255*dt) =
|
||||
(((x - (t - dt))/dt)*254 + 1)/255 = (((x - low_time)/dt)*254 + 1)/255
|
||||
*/
|
||||
float shiftOrient = 0, shiftWeight = 0;
|
||||
for( int y = 0; y < size.height; y++ )
|
||||
{
|
||||
const float* mhiptr = mhi.ptr<float>(y);
|
||||
const float* oriptr = orient.ptr<float>(y);
|
||||
const uchar* maskptr = mask.ptr<uchar>(y);
|
||||
|
||||
for( int x = 0; x < size.width; x++ )
|
||||
{
|
||||
if( maskptr[x] != 0 && mhiptr[x] > delbound )
|
||||
{
|
||||
/*
|
||||
orient in 0..360, base_orient in 0..360
|
||||
-> (rel_angle = orient - base_orient) in -360..360.
|
||||
rel_angle is translated to -180..180
|
||||
*/
|
||||
float weight = mhiptr[x] * a + b;
|
||||
float relAngle = oriptr[x] - fbaseOrient;
|
||||
|
||||
relAngle += (relAngle < -180 ? 360 : 0);
|
||||
relAngle += (relAngle > 180 ? -360 : 0);
|
||||
|
||||
if( fabs(relAngle) < 45 )
|
||||
{
|
||||
shiftOrient += weight * relAngle;
|
||||
shiftWeight += weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add the dominant orientation and the relative shift
|
||||
if( shiftWeight == 0 )
|
||||
shiftWeight = 0.01f;
|
||||
|
||||
fbaseOrient += shiftOrient / shiftWeight;
|
||||
fbaseOrient -= (fbaseOrient < 360 ? 0 : 360);
|
||||
fbaseOrient += (fbaseOrient >= 0 ? 0 : 360);
|
||||
|
||||
return fbaseOrient;
|
||||
}
|
||||
|
||||
|
||||
void cv::segmentMotion(InputArray _mhi, OutputArray _segmask,
|
||||
std::vector<Rect>& boundingRects,
|
||||
double timestamp, double segThresh)
|
||||
{
|
||||
Mat mhi = _mhi.getMat();
|
||||
|
||||
_segmask.create(mhi.size(), CV_32F);
|
||||
Mat segmask = _segmask.getMat();
|
||||
segmask = Scalar::all(0);
|
||||
|
||||
CV_Assert( mhi.type() == CV_32F );
|
||||
CV_Assert( segThresh >= 0 );
|
||||
|
||||
Mat mask = Mat::zeros( mhi.rows + 2, mhi.cols + 2, CV_8UC1 );
|
||||
|
||||
int x, y;
|
||||
|
||||
// protect zero mhi pixels from floodfill.
|
||||
for( y = 0; y < mhi.rows; y++ )
|
||||
{
|
||||
const float* mhiptr = mhi.ptr<float>(y);
|
||||
uchar* maskptr = mask.ptr<uchar>(y+1) + 1;
|
||||
|
||||
for( x = 0; x < mhi.cols; x++ )
|
||||
{
|
||||
if( mhiptr[x] == 0 )
|
||||
maskptr[x] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
float ts = (float)timestamp;
|
||||
float comp_idx = 1.f;
|
||||
|
||||
for( y = 0; y < mhi.rows; y++ )
|
||||
{
|
||||
float* mhiptr = mhi.ptr<float>(y);
|
||||
uchar* maskptr = mask.ptr<uchar>(y+1) + 1;
|
||||
|
||||
for( x = 0; x < mhi.cols; x++ )
|
||||
{
|
||||
if( mhiptr[x] == ts && maskptr[x] == 0 )
|
||||
{
|
||||
Rect cc;
|
||||
floodFill( mhi, mask, Point(x,y), Scalar::all(0),
|
||||
&cc, Scalar::all(segThresh), Scalar::all(segThresh),
|
||||
FLOODFILL_MASK_ONLY + 2*256 + 4 );
|
||||
|
||||
for( int y1 = 0; y1 < cc.height; y1++ )
|
||||
{
|
||||
float* segmaskptr = segmask.ptr<float>(cc.y + y1) + cc.x;
|
||||
uchar* maskptr1 = mask.ptr<uchar>(cc.y + y1 + 1) + cc.x + 1;
|
||||
|
||||
for( int x1 = 0; x1 < cc.width; x1++ )
|
||||
{
|
||||
if( maskptr1[x1] > 1 )
|
||||
{
|
||||
maskptr1[x1] = 1;
|
||||
segmaskptr[x1] = comp_idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
comp_idx += 1.f;
|
||||
boundingRects.push_back(cc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* End of file. */
|
||||
@@ -1,27 +0,0 @@
|
||||
// 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.
|
||||
|
||||
// Copyright (C) 2014, Advanced Micro Devices, Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
|
||||
__kernel void updateMotionHistory(__global const uchar * silh, int silh_step, int silh_offset,
|
||||
__global uchar * mhiptr, int mhi_step, int mhi_offset, int mhi_rows, int mhi_cols,
|
||||
float timestamp, float delbound)
|
||||
{
|
||||
int x = get_global_id(0);
|
||||
int y = get_global_id(1);
|
||||
|
||||
if (x < mhi_cols && y < mhi_rows)
|
||||
{
|
||||
int silh_index = mad24(y, silh_step, silh_offset + x);
|
||||
int mhi_index = mad24(y, mhi_step, mhi_offset + x * (int)sizeof(float));
|
||||
|
||||
silh += silh_index;
|
||||
__global float * mhi = (__global float *)(mhiptr + mhi_index);
|
||||
|
||||
float val = mhi[0];
|
||||
val = silh[0] ? timestamp : val < delbound ? 0 : val;
|
||||
mhi[0] = val;
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,11 @@
|
||||
//M*/
|
||||
|
||||
#include "precomp.hpp"
|
||||
#include "opencl_kernels.hpp"
|
||||
#include "opencl_kernels_video.hpp"
|
||||
|
||||
#if defined __APPLE__ || defined ANDROID
|
||||
#define SMALL_LOCALSIZE
|
||||
#endif
|
||||
|
||||
//
|
||||
// 2D dense optical flow algorithm from the following paper:
|
||||
@@ -130,8 +134,8 @@ FarnebackPolyExp( const Mat& src, Mat& dst, int n, double sigma )
|
||||
for( y = 0; y < height; y++ )
|
||||
{
|
||||
float g0 = g[0], g1, g2;
|
||||
float *srow0 = (float*)(src.data + src.step*y), *srow1 = 0;
|
||||
float *drow = (float*)(dst.data + dst.step*y);
|
||||
const float *srow0 = src.ptr<float>(y), *srow1 = 0;
|
||||
float *drow = dst.ptr<float>(y);
|
||||
|
||||
// vertical part of convolution
|
||||
for( x = 0; x < width; x++ )
|
||||
@@ -143,8 +147,8 @@ FarnebackPolyExp( const Mat& src, Mat& dst, int n, double sigma )
|
||||
for( k = 1; k <= n; k++ )
|
||||
{
|
||||
g0 = g[k]; g1 = xg[k]; g2 = xxg[k];
|
||||
srow0 = (float*)(src.data + src.step*std::max(y-k,0));
|
||||
srow1 = (float*)(src.data + src.step*std::min(y+k,height-1));
|
||||
srow0 = src.ptr<float>(std::max(y-k,0));
|
||||
srow1 = src.ptr<float>(std::min(y+k,height-1));
|
||||
|
||||
for( x = 0; x < width; x++ )
|
||||
{
|
||||
@@ -216,16 +220,16 @@ FarnebackUpdateMatrices( const Mat& _R0, const Mat& _R1, const Mat& _flow, Mat&
|
||||
static const float border[BORDER] = {0.14f, 0.14f, 0.4472f, 0.4472f, 0.4472f};
|
||||
|
||||
int x, y, width = _flow.cols, height = _flow.rows;
|
||||
const float* R1 = (float*)_R1.data;
|
||||
const float* R1 = _R1.ptr<float>();
|
||||
size_t step1 = _R1.step/sizeof(R1[0]);
|
||||
|
||||
matM.create(height, width, CV_32FC(5));
|
||||
|
||||
for( y = _y0; y < _y1; y++ )
|
||||
{
|
||||
const float* flow = (float*)(_flow.data + y*_flow.step);
|
||||
const float* R0 = (float*)(_R0.data + y*_R0.step);
|
||||
float* M = (float*)(matM.data + y*matM.step);
|
||||
const float* flow = _flow.ptr<float>(y);
|
||||
const float* R0 = _R0.ptr<float>(y);
|
||||
float* M = matM.ptr<float>(y);
|
||||
|
||||
for( x = 0; x < width; x++ )
|
||||
{
|
||||
@@ -321,13 +325,13 @@ FarnebackUpdateFlow_Blur( const Mat& _R0, const Mat& _R1,
|
||||
double* vsum = _vsum + (m+1)*5;
|
||||
|
||||
// init vsum
|
||||
const float* srow0 = (const float*)matM.data;
|
||||
const float* srow0 = matM.ptr<float>();
|
||||
for( x = 0; x < width*5; x++ )
|
||||
vsum[x] = srow0[x]*(m+2);
|
||||
|
||||
for( y = 1; y < m; y++ )
|
||||
{
|
||||
srow0 = (float*)(matM.data + matM.step*std::min(y,height-1));
|
||||
srow0 = matM.ptr<float>(std::min(y,height-1));
|
||||
for( x = 0; x < width*5; x++ )
|
||||
vsum[x] += srow0[x];
|
||||
}
|
||||
@@ -336,10 +340,10 @@ FarnebackUpdateFlow_Blur( const Mat& _R0, const Mat& _R1,
|
||||
for( y = 0; y < height; y++ )
|
||||
{
|
||||
double g11, g12, g22, h1, h2;
|
||||
float* flow = (float*)(_flow.data + _flow.step*y);
|
||||
float* flow = _flow.ptr<float>(y);
|
||||
|
||||
srow0 = (const float*)(matM.data + matM.step*std::max(y-m-1,0));
|
||||
const float* srow1 = (const float*)(matM.data + matM.step*std::min(y+m,height-1));
|
||||
srow0 = matM.ptr<float>(std::max(y-m-1,0));
|
||||
const float* srow1 = matM.ptr<float>(std::min(y+m,height-1));
|
||||
|
||||
// vertical blur
|
||||
for( x = 0; x < width*5; x++ )
|
||||
@@ -443,13 +447,13 @@ FarnebackUpdateFlow_GaussianBlur( const Mat& _R0, const Mat& _R1,
|
||||
for( y = 0; y < height; y++ )
|
||||
{
|
||||
double g11, g12, g22, h1, h2;
|
||||
float* flow = (float*)(_flow.data + _flow.step*y);
|
||||
float* flow = _flow.ptr<float>(y);
|
||||
|
||||
// vertical blur
|
||||
for( i = 0; i <= m; i++ )
|
||||
{
|
||||
srow[m-i] = (const float*)(matM.data + matM.step*std::max(y-i,0));
|
||||
srow[m+i] = (const float*)(matM.data + matM.step*std::min(y+i,height-1));
|
||||
srow[m-i] = matM.ptr<float>(std::max(y-i,0));
|
||||
srow[m+i] = matM.ptr<float>(std::min(y+i,height-1));
|
||||
}
|
||||
|
||||
x = 0;
|
||||
@@ -836,7 +840,7 @@ private:
|
||||
|
||||
bool gaussianBlurOcl(const UMat &src, int ksizeHalf, UMat &dst)
|
||||
{
|
||||
#ifdef ANDROID
|
||||
#ifdef SMALL_LOCALSIZE
|
||||
size_t localsize[2] = { 128, 1};
|
||||
#else
|
||||
size_t localsize[2] = { 256, 1};
|
||||
@@ -863,7 +867,7 @@ private:
|
||||
bool gaussianBlur5Ocl(const UMat &src, int ksizeHalf, UMat &dst)
|
||||
{
|
||||
int height = src.rows / 5;
|
||||
#ifdef ANDROID
|
||||
#ifdef SMALL_LOCALSIZE
|
||||
size_t localsize[2] = { 128, 1};
|
||||
#else
|
||||
size_t localsize[2] = { 256, 1};
|
||||
@@ -888,7 +892,7 @@ private:
|
||||
}
|
||||
bool polynomialExpansionOcl(const UMat &src, UMat &dst)
|
||||
{
|
||||
#ifdef ANDROID
|
||||
#ifdef SMALL_LOCALSIZE
|
||||
size_t localsize[2] = { 128, 1};
|
||||
#else
|
||||
size_t localsize[2] = { 256, 1};
|
||||
@@ -925,7 +929,7 @@ private:
|
||||
bool boxFilter5Ocl(const UMat &src, int ksizeHalf, UMat &dst)
|
||||
{
|
||||
int height = src.rows / 5;
|
||||
#ifdef ANDROID
|
||||
#ifdef SMALL_LOCALSIZE
|
||||
size_t localsize[2] = { 128, 1};
|
||||
#else
|
||||
size_t localsize[2] = { 256, 1};
|
||||
@@ -952,7 +956,7 @@ private:
|
||||
|
||||
bool updateFlowOcl(const UMat &M, UMat &flowx, UMat &flowy)
|
||||
{
|
||||
#ifdef ANDROID
|
||||
#ifdef SMALL_LOCALSIZE
|
||||
size_t localsize[2] = { 32, 4};
|
||||
#else
|
||||
size_t localsize[2] = { 32, 8};
|
||||
@@ -976,7 +980,7 @@ private:
|
||||
}
|
||||
bool updateMatricesOcl(const UMat &flowx, const UMat &flowy, const UMat &R0, const UMat &R1, UMat &M)
|
||||
{
|
||||
#ifdef ANDROID
|
||||
#ifdef SMALL_LOCALSIZE
|
||||
size_t localsize[2] = { 32, 4};
|
||||
#else
|
||||
size_t localsize[2] = { 32, 8};
|
||||
@@ -1118,7 +1122,7 @@ void cv::calcOpticalFlowFarneback( InputArray _prev0, InputArray _next0,
|
||||
else
|
||||
flow = flow0;
|
||||
|
||||
if( !prevFlow.data )
|
||||
if( prevFlow.empty() )
|
||||
{
|
||||
if( flags & OPTFLOW_USE_INITIAL_FLOW )
|
||||
{
|
||||
|
||||
@@ -1,673 +0,0 @@
|
||||
/*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-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., 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"
|
||||
|
||||
//
|
||||
// 2D dense optical flow algorithm from the following paper:
|
||||
// Michael Tao, Jiamin Bai, Pushmeet Kohli, and Sylvain Paris.
|
||||
// "SimpleFlow: A Non-iterative, Sublinear Optical Flow Algorithm"
|
||||
// Computer Graphics Forum (Eurographics 2012)
|
||||
// http://graphics.berkeley.edu/papers/Tao-SAN-2012-05/
|
||||
//
|
||||
|
||||
namespace cv
|
||||
{
|
||||
|
||||
static const uchar MASK_TRUE_VALUE = (uchar)255;
|
||||
|
||||
inline static float dist(const Vec3b& p1, const Vec3b& p2) {
|
||||
return (float)((p1[0] - p2[0]) * (p1[0] - p2[0]) +
|
||||
(p1[1] - p2[1]) * (p1[1] - p2[1]) +
|
||||
(p1[2] - p2[2]) * (p1[2] - p2[2]));
|
||||
}
|
||||
|
||||
inline static float dist(const Vec2f& p1, const Vec2f& p2) {
|
||||
return (p1[0] - p2[0]) * (p1[0] - p2[0]) +
|
||||
(p1[1] - p2[1]) * (p1[1] - p2[1]);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline static T min(T t1, T t2, T t3) {
|
||||
return (t1 <= t2 && t1 <= t3) ? t1 : min(t2, t3);
|
||||
}
|
||||
|
||||
static void removeOcclusions(const Mat& flow,
|
||||
const Mat& flow_inv,
|
||||
float occ_thr,
|
||||
Mat& confidence) {
|
||||
const int rows = flow.rows;
|
||||
const int cols = flow.cols;
|
||||
if (!confidence.data) {
|
||||
confidence = Mat::zeros(rows, cols, CV_32F);
|
||||
}
|
||||
for (int r = 0; r < rows; ++r) {
|
||||
for (int c = 0; c < cols; ++c) {
|
||||
if (dist(flow.at<Vec2f>(r, c), -flow_inv.at<Vec2f>(r, c)) > occ_thr) {
|
||||
confidence.at<float>(r, c) = 0;
|
||||
} else {
|
||||
confidence.at<float>(r, c) = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void wd(Mat& d, int top_shift, int bottom_shift, int left_shift, int right_shift, float sigma) {
|
||||
for (int dr = -top_shift, r = 0; dr <= bottom_shift; ++dr, ++r) {
|
||||
for (int dc = -left_shift, c = 0; dc <= right_shift; ++dc, ++c) {
|
||||
d.at<float>(r, c) = (float)-(dr*dr + dc*dc);
|
||||
}
|
||||
}
|
||||
d *= 1.0 / (2.0 * sigma * sigma);
|
||||
exp(d, d);
|
||||
}
|
||||
|
||||
static void wc(const Mat& image, Mat& d, int r0, int c0,
|
||||
int top_shift, int bottom_shift, int left_shift, int right_shift, float sigma) {
|
||||
const Vec3b centeral_point = image.at<Vec3b>(r0, c0);
|
||||
int left_border = c0-left_shift, right_border = c0+right_shift;
|
||||
for (int dr = r0-top_shift, r = 0; dr <= r0+bottom_shift; ++dr, ++r) {
|
||||
const Vec3b *row = image.ptr<Vec3b>(dr);
|
||||
float *d_row = d.ptr<float>(r);
|
||||
for (int dc = left_border, c = 0; dc <= right_border; ++dc, ++c) {
|
||||
d_row[c] = -dist(centeral_point, row[dc]);
|
||||
}
|
||||
}
|
||||
d *= 1.0 / (2.0 * sigma * sigma);
|
||||
exp(d, d);
|
||||
}
|
||||
|
||||
static void crossBilateralFilter(const Mat& image,
|
||||
const Mat& edge_image,
|
||||
const Mat confidence,
|
||||
Mat& dst, int d,
|
||||
float sigma_color, float sigma_space,
|
||||
bool flag=false) {
|
||||
const int rows = image.rows;
|
||||
const int cols = image.cols;
|
||||
Mat image_extended, edge_image_extended, confidence_extended;
|
||||
copyMakeBorder(image, image_extended, d, d, d, d, BORDER_DEFAULT);
|
||||
copyMakeBorder(edge_image, edge_image_extended, d, d, d, d, BORDER_DEFAULT);
|
||||
copyMakeBorder(confidence, confidence_extended, d, d, d, d, BORDER_CONSTANT, Scalar(0));
|
||||
Mat weights_space(2*d+1, 2*d+1, CV_32F);
|
||||
wd(weights_space, d, d, d, d, sigma_space);
|
||||
Mat weights(2*d+1, 2*d+1, CV_32F);
|
||||
Mat weighted_sum(2*d+1, 2*d+1, CV_32F);
|
||||
|
||||
std::vector<Mat> image_extended_channels;
|
||||
split(image_extended, image_extended_channels);
|
||||
|
||||
for (int row = 0; row < rows; ++row) {
|
||||
for (int col = 0; col < cols; ++col) {
|
||||
wc(edge_image_extended, weights, row+d, col+d, d, d, d, d, sigma_color);
|
||||
|
||||
Range window_rows(row,row+2*d+1);
|
||||
Range window_cols(col,col+2*d+1);
|
||||
|
||||
multiply(weights, confidence_extended(window_rows, window_cols), weights);
|
||||
multiply(weights, weights_space, weights);
|
||||
float weights_sum = (float)sum(weights)[0];
|
||||
|
||||
for (int ch = 0; ch < 2; ++ch) {
|
||||
multiply(weights, image_extended_channels[ch](window_rows, window_cols), weighted_sum);
|
||||
float total_sum = (float)sum(weighted_sum)[0];
|
||||
|
||||
dst.at<Vec2f>(row, col)[ch] = (flag && fabs(weights_sum) < 1e-9)
|
||||
? image.at<float>(row, col)
|
||||
: total_sum / weights_sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void calcConfidence(const Mat& prev,
|
||||
const Mat& next,
|
||||
const Mat& flow,
|
||||
Mat& confidence,
|
||||
int max_flow) {
|
||||
const int rows = prev.rows;
|
||||
const int cols = prev.cols;
|
||||
confidence = Mat::zeros(rows, cols, CV_32F);
|
||||
|
||||
for (int r0 = 0; r0 < rows; ++r0) {
|
||||
for (int c0 = 0; c0 < cols; ++c0) {
|
||||
Vec2f flow_at_point = flow.at<Vec2f>(r0, c0);
|
||||
int u0 = cvRound(flow_at_point[0]);
|
||||
if (r0 + u0 < 0) { u0 = -r0; }
|
||||
if (r0 + u0 >= rows) { u0 = rows - 1 - r0; }
|
||||
int v0 = cvRound(flow_at_point[1]);
|
||||
if (c0 + v0 < 0) { v0 = -c0; }
|
||||
if (c0 + v0 >= cols) { v0 = cols - 1 - c0; }
|
||||
|
||||
const int top_row_shift = -std::min(r0 + u0, max_flow);
|
||||
const int bottom_row_shift = std::min(rows - 1 - (r0 + u0), max_flow);
|
||||
const int left_col_shift = -std::min(c0 + v0, max_flow);
|
||||
const int right_col_shift = std::min(cols - 1 - (c0 + v0), max_flow);
|
||||
|
||||
bool first_flow_iteration = true;
|
||||
float sum_e = 0, min_e = 0;
|
||||
|
||||
for (int u = top_row_shift; u <= bottom_row_shift; ++u) {
|
||||
for (int v = left_col_shift; v <= right_col_shift; ++v) {
|
||||
float e = dist(prev.at<Vec3b>(r0, c0), next.at<Vec3b>(r0 + u0 + u, c0 + v0 + v));
|
||||
if (first_flow_iteration) {
|
||||
sum_e = e;
|
||||
min_e = e;
|
||||
first_flow_iteration = false;
|
||||
} else {
|
||||
sum_e += e;
|
||||
min_e = std::min(min_e, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
int windows_square = (bottom_row_shift - top_row_shift + 1) *
|
||||
(right_col_shift - left_col_shift + 1);
|
||||
confidence.at<float>(r0, c0) = (windows_square == 0) ? 0
|
||||
: sum_e / windows_square - min_e;
|
||||
CV_Assert(confidence.at<float>(r0, c0) >= 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void calcOpticalFlowSingleScaleSF(const Mat& prev_extended,
|
||||
const Mat& next_extended,
|
||||
const Mat& mask,
|
||||
Mat& flow,
|
||||
int averaging_radius,
|
||||
int max_flow,
|
||||
float sigma_dist,
|
||||
float sigma_color) {
|
||||
const int averaging_radius_2 = averaging_radius << 1;
|
||||
const int rows = prev_extended.rows - averaging_radius_2;
|
||||
const int cols = prev_extended.cols - averaging_radius_2;
|
||||
|
||||
Mat weight_window(averaging_radius_2 + 1, averaging_radius_2 + 1, CV_32F);
|
||||
Mat space_weight_window(averaging_radius_2 + 1, averaging_radius_2 + 1, CV_32F);
|
||||
|
||||
wd(space_weight_window, averaging_radius, averaging_radius, averaging_radius, averaging_radius, sigma_dist);
|
||||
|
||||
for (int r0 = 0; r0 < rows; ++r0) {
|
||||
for (int c0 = 0; c0 < cols; ++c0) {
|
||||
if (!mask.at<uchar>(r0, c0)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: do smth with this creepy staff
|
||||
Vec2f flow_at_point = flow.at<Vec2f>(r0, c0);
|
||||
int u0 = cvRound(flow_at_point[0]);
|
||||
if (r0 + u0 < 0) { u0 = -r0; }
|
||||
if (r0 + u0 >= rows) { u0 = rows - 1 - r0; }
|
||||
int v0 = cvRound(flow_at_point[1]);
|
||||
if (c0 + v0 < 0) { v0 = -c0; }
|
||||
if (c0 + v0 >= cols) { v0 = cols - 1 - c0; }
|
||||
|
||||
const int top_row_shift = -std::min(r0 + u0, max_flow);
|
||||
const int bottom_row_shift = std::min(rows - 1 - (r0 + u0), max_flow);
|
||||
const int left_col_shift = -std::min(c0 + v0, max_flow);
|
||||
const int right_col_shift = std::min(cols - 1 - (c0 + v0), max_flow);
|
||||
|
||||
float min_cost = FLT_MAX, best_u = (float)u0, best_v = (float)v0;
|
||||
|
||||
wc(prev_extended, weight_window, r0 + averaging_radius, c0 + averaging_radius,
|
||||
averaging_radius, averaging_radius, averaging_radius, averaging_radius, sigma_color);
|
||||
multiply(weight_window, space_weight_window, weight_window);
|
||||
|
||||
const int prev_extended_top_window_row = r0;
|
||||
const int prev_extended_left_window_col = c0;
|
||||
|
||||
for (int u = top_row_shift; u <= bottom_row_shift; ++u) {
|
||||
const int next_extended_top_window_row = r0 + u0 + u;
|
||||
for (int v = left_col_shift; v <= right_col_shift; ++v) {
|
||||
const int next_extended_left_window_col = c0 + v0 + v;
|
||||
|
||||
float cost = 0;
|
||||
for (int r = 0; r <= averaging_radius_2; ++r) {
|
||||
const Vec3b *prev_extended_window_row = prev_extended.ptr<Vec3b>(prev_extended_top_window_row + r);
|
||||
const Vec3b *next_extended_window_row = next_extended.ptr<Vec3b>(next_extended_top_window_row + r);
|
||||
const float* weight_window_row = weight_window.ptr<float>(r);
|
||||
for (int c = 0; c <= averaging_radius_2; ++c) {
|
||||
cost += weight_window_row[c] *
|
||||
dist(prev_extended_window_row[prev_extended_left_window_col + c],
|
||||
next_extended_window_row[next_extended_left_window_col + c]);
|
||||
}
|
||||
}
|
||||
// cost should be divided by sum(weight_window), but because
|
||||
// we interested only in min(cost) and sum(weight_window) is constant
|
||||
// for every point - we remove it
|
||||
|
||||
if (cost < min_cost) {
|
||||
min_cost = cost;
|
||||
best_u = (float)(u + u0);
|
||||
best_v = (float)(v + v0);
|
||||
}
|
||||
}
|
||||
}
|
||||
flow.at<Vec2f>(r0, c0) = Vec2f(best_u, best_v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Mat upscaleOpticalFlow(int new_rows,
|
||||
int new_cols,
|
||||
const Mat& image,
|
||||
const Mat& confidence,
|
||||
Mat& flow,
|
||||
int averaging_radius,
|
||||
float sigma_dist,
|
||||
float sigma_color) {
|
||||
crossBilateralFilter(flow, image, confidence, flow, averaging_radius, sigma_color, sigma_dist, true);
|
||||
Mat new_flow;
|
||||
resize(flow, new_flow, Size(new_cols, new_rows), 0, 0, INTER_NEAREST);
|
||||
new_flow *= 2;
|
||||
return new_flow;
|
||||
}
|
||||
|
||||
static Mat calcIrregularityMat(const Mat& flow, int radius) {
|
||||
const int rows = flow.rows;
|
||||
const int cols = flow.cols;
|
||||
Mat irregularity = Mat::zeros(rows, cols, CV_32F);
|
||||
for (int r = 0; r < rows; ++r) {
|
||||
const int start_row = std::max(0, r - radius);
|
||||
const int end_row = std::min(rows - 1, r + radius);
|
||||
for (int c = 0; c < cols; ++c) {
|
||||
const int start_col = std::max(0, c - radius);
|
||||
const int end_col = std::min(cols - 1, c + radius);
|
||||
for (int dr = start_row; dr <= end_row; ++dr) {
|
||||
for (int dc = start_col; dc <= end_col; ++dc) {
|
||||
const float diff = dist(flow.at<Vec2f>(r, c), flow.at<Vec2f>(dr, dc));
|
||||
if (diff > irregularity.at<float>(r, c)) {
|
||||
irregularity.at<float>(r, c) = diff;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return irregularity;
|
||||
}
|
||||
|
||||
static void selectPointsToRecalcFlow(const Mat& flow,
|
||||
int irregularity_metric_radius,
|
||||
float speed_up_thr,
|
||||
int curr_rows,
|
||||
int curr_cols,
|
||||
const Mat& prev_speed_up,
|
||||
Mat& speed_up,
|
||||
Mat& mask) {
|
||||
const int prev_rows = flow.rows;
|
||||
const int prev_cols = flow.cols;
|
||||
|
||||
Mat is_flow_regular = calcIrregularityMat(flow, irregularity_metric_radius)
|
||||
< speed_up_thr;
|
||||
Mat done = Mat::zeros(prev_rows, prev_cols, CV_8U);
|
||||
speed_up = Mat::zeros(curr_rows, curr_cols, CV_8U);
|
||||
mask = Mat::zeros(curr_rows, curr_cols, CV_8U);
|
||||
|
||||
for (int r = 0; r < is_flow_regular.rows; ++r) {
|
||||
for (int c = 0; c < is_flow_regular.cols; ++c) {
|
||||
if (!done.at<uchar>(r, c)) {
|
||||
if (is_flow_regular.at<uchar>(r, c) &&
|
||||
2*r + 1 < curr_rows && 2*c + 1< curr_cols) {
|
||||
|
||||
bool all_flow_in_region_regular = true;
|
||||
int speed_up_at_this_point = prev_speed_up.at<uchar>(r, c);
|
||||
int step = (1 << speed_up_at_this_point) - 1;
|
||||
int prev_top = r;
|
||||
int prev_bottom = std::min(r + step, prev_rows - 1);
|
||||
int prev_left = c;
|
||||
int prev_right = std::min(c + step, prev_cols - 1);
|
||||
|
||||
for (int rr = prev_top; rr <= prev_bottom; ++rr) {
|
||||
for (int cc = prev_left; cc <= prev_right; ++cc) {
|
||||
done.at<uchar>(rr, cc) = 1;
|
||||
if (!is_flow_regular.at<uchar>(rr, cc)) {
|
||||
all_flow_in_region_regular = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int curr_top = std::min(2 * r, curr_rows - 1);
|
||||
int curr_bottom = std::min(2*(r + step) + 1, curr_rows - 1);
|
||||
int curr_left = std::min(2 * c, curr_cols - 1);
|
||||
int curr_right = std::min(2*(c + step) + 1, curr_cols - 1);
|
||||
|
||||
if (all_flow_in_region_regular &&
|
||||
curr_top != curr_bottom &&
|
||||
curr_left != curr_right) {
|
||||
mask.at<uchar>(curr_top, curr_left) = MASK_TRUE_VALUE;
|
||||
mask.at<uchar>(curr_bottom, curr_left) = MASK_TRUE_VALUE;
|
||||
mask.at<uchar>(curr_top, curr_right) = MASK_TRUE_VALUE;
|
||||
mask.at<uchar>(curr_bottom, curr_right) = MASK_TRUE_VALUE;
|
||||
for (int rr = curr_top; rr <= curr_bottom; ++rr) {
|
||||
for (int cc = curr_left; cc <= curr_right; ++cc) {
|
||||
speed_up.at<uchar>(rr, cc) = (uchar)(speed_up_at_this_point + 1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int rr = curr_top; rr <= curr_bottom; ++rr) {
|
||||
for (int cc = curr_left; cc <= curr_right; ++cc) {
|
||||
mask.at<uchar>(rr, cc) = MASK_TRUE_VALUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
done.at<uchar>(r, c) = 1;
|
||||
for (int dr = 0; dr <= 1; ++dr) {
|
||||
int nr = 2*r + dr;
|
||||
for (int dc = 0; dc <= 1; ++dc) {
|
||||
int nc = 2*c + dc;
|
||||
if (nr < curr_rows && nc < curr_cols) {
|
||||
mask.at<uchar>(nr, nc) = MASK_TRUE_VALUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline float extrapolateValueInRect(int height, int width,
|
||||
float v11, float v12,
|
||||
float v21, float v22,
|
||||
int r, int c) {
|
||||
if (r == 0 && c == 0) { return v11;}
|
||||
if (r == 0 && c == width) { return v12;}
|
||||
if (r == height && c == 0) { return v21;}
|
||||
if (r == height && c == width) { return v22;}
|
||||
|
||||
CV_Assert(height > 0 && width > 0);
|
||||
float qr = float(r) / height;
|
||||
float pr = 1.0f - qr;
|
||||
float qc = float(c) / width;
|
||||
float pc = 1.0f - qc;
|
||||
|
||||
return v11*pr*pc + v12*pr*qc + v21*qr*pc + v22*qc*qr;
|
||||
}
|
||||
|
||||
static void extrapolateFlow(Mat& flow,
|
||||
const Mat& speed_up) {
|
||||
const int rows = flow.rows;
|
||||
const int cols = flow.cols;
|
||||
Mat done = Mat::zeros(rows, cols, CV_8U);
|
||||
for (int r = 0; r < rows; ++r) {
|
||||
for (int c = 0; c < cols; ++c) {
|
||||
if (!done.at<uchar>(r, c) && speed_up.at<uchar>(r, c) > 1) {
|
||||
int step = (1 << speed_up.at<uchar>(r, c)) - 1;
|
||||
int top = r;
|
||||
int bottom = std::min(r + step, rows - 1);
|
||||
int left = c;
|
||||
int right = std::min(c + step, cols - 1);
|
||||
|
||||
int height = bottom - top;
|
||||
int width = right - left;
|
||||
for (int rr = top; rr <= bottom; ++rr) {
|
||||
for (int cc = left; cc <= right; ++cc) {
|
||||
done.at<uchar>(rr, cc) = 1;
|
||||
Vec2f flow_at_point;
|
||||
Vec2f top_left = flow.at<Vec2f>(top, left);
|
||||
Vec2f top_right = flow.at<Vec2f>(top, right);
|
||||
Vec2f bottom_left = flow.at<Vec2f>(bottom, left);
|
||||
Vec2f bottom_right = flow.at<Vec2f>(bottom, right);
|
||||
|
||||
flow_at_point[0] = extrapolateValueInRect(height, width,
|
||||
top_left[0], top_right[0],
|
||||
bottom_left[0], bottom_right[0],
|
||||
rr-top, cc-left);
|
||||
|
||||
flow_at_point[1] = extrapolateValueInRect(height, width,
|
||||
top_left[1], top_right[1],
|
||||
bottom_left[1], bottom_right[1],
|
||||
rr-top, cc-left);
|
||||
flow.at<Vec2f>(rr, cc) = flow_at_point;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void buildPyramidWithResizeMethod(const Mat& src,
|
||||
std::vector<Mat>& pyramid,
|
||||
int layers,
|
||||
int interpolation_type) {
|
||||
pyramid.push_back(src);
|
||||
for (int i = 1; i <= layers; ++i) {
|
||||
Mat prev = pyramid[i - 1];
|
||||
if (prev.rows <= 1 || prev.cols <= 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
Mat next;
|
||||
resize(prev, next, Size((prev.cols + 1) / 2, (prev.rows + 1) / 2), 0, 0, interpolation_type);
|
||||
pyramid.push_back(next);
|
||||
}
|
||||
}
|
||||
|
||||
CV_EXPORTS_W void calcOpticalFlowSF(InputArray _from,
|
||||
InputArray _to,
|
||||
OutputArray _resulted_flow,
|
||||
int layers,
|
||||
int averaging_radius,
|
||||
int max_flow,
|
||||
double sigma_dist,
|
||||
double sigma_color,
|
||||
int postprocess_window,
|
||||
double sigma_dist_fix,
|
||||
double sigma_color_fix,
|
||||
double occ_thr,
|
||||
int upscale_averaging_radius,
|
||||
double upscale_sigma_dist,
|
||||
double upscale_sigma_color,
|
||||
double speed_up_thr)
|
||||
{
|
||||
Mat from = _from.getMat();
|
||||
Mat to = _to.getMat();
|
||||
|
||||
std::vector<Mat> pyr_from_images;
|
||||
std::vector<Mat> pyr_to_images;
|
||||
|
||||
buildPyramidWithResizeMethod(from, pyr_from_images, layers - 1, INTER_CUBIC);
|
||||
buildPyramidWithResizeMethod(to, pyr_to_images, layers - 1, INTER_CUBIC);
|
||||
|
||||
CV_Assert((int)pyr_from_images.size() == layers && (int)pyr_to_images.size() == layers);
|
||||
|
||||
Mat curr_from, curr_to, prev_from, prev_to;
|
||||
Mat curr_from_extended, curr_to_extended;
|
||||
|
||||
curr_from = pyr_from_images[layers - 1];
|
||||
curr_to = pyr_to_images[layers - 1];
|
||||
|
||||
copyMakeBorder(curr_from, curr_from_extended,
|
||||
averaging_radius, averaging_radius, averaging_radius, averaging_radius,
|
||||
BORDER_DEFAULT);
|
||||
copyMakeBorder(curr_to, curr_to_extended,
|
||||
averaging_radius, averaging_radius, averaging_radius, averaging_radius,
|
||||
BORDER_DEFAULT);
|
||||
|
||||
Mat mask = Mat::ones(curr_from.size(), CV_8U);
|
||||
Mat mask_inv = Mat::ones(curr_from.size(), CV_8U);
|
||||
|
||||
Mat flow = Mat::zeros(curr_from.size(), CV_32FC2);
|
||||
Mat flow_inv = Mat::zeros(curr_to.size(), CV_32FC2);
|
||||
|
||||
Mat confidence;
|
||||
Mat confidence_inv;
|
||||
|
||||
|
||||
calcOpticalFlowSingleScaleSF(curr_from_extended,
|
||||
curr_to_extended,
|
||||
mask,
|
||||
flow,
|
||||
averaging_radius,
|
||||
max_flow,
|
||||
(float)sigma_dist,
|
||||
(float)sigma_color);
|
||||
|
||||
calcOpticalFlowSingleScaleSF(curr_to_extended,
|
||||
curr_from_extended,
|
||||
mask_inv,
|
||||
flow_inv,
|
||||
averaging_radius,
|
||||
max_flow,
|
||||
(float)sigma_dist,
|
||||
(float)sigma_color);
|
||||
|
||||
removeOcclusions(flow,
|
||||
flow_inv,
|
||||
(float)occ_thr,
|
||||
confidence);
|
||||
|
||||
removeOcclusions(flow_inv,
|
||||
flow,
|
||||
(float)occ_thr,
|
||||
confidence_inv);
|
||||
|
||||
Mat speed_up = Mat::zeros(curr_from.size(), CV_8U);
|
||||
Mat speed_up_inv = Mat::zeros(curr_from.size(), CV_8U);
|
||||
|
||||
for (int curr_layer = layers - 2; curr_layer >= 0; --curr_layer) {
|
||||
curr_from = pyr_from_images[curr_layer];
|
||||
curr_to = pyr_to_images[curr_layer];
|
||||
prev_from = pyr_from_images[curr_layer + 1];
|
||||
prev_to = pyr_to_images[curr_layer + 1];
|
||||
|
||||
copyMakeBorder(curr_from, curr_from_extended,
|
||||
averaging_radius, averaging_radius, averaging_radius, averaging_radius,
|
||||
BORDER_DEFAULT);
|
||||
copyMakeBorder(curr_to, curr_to_extended,
|
||||
averaging_radius, averaging_radius, averaging_radius, averaging_radius,
|
||||
BORDER_DEFAULT);
|
||||
|
||||
const int curr_rows = curr_from.rows;
|
||||
const int curr_cols = curr_from.cols;
|
||||
|
||||
Mat new_speed_up, new_speed_up_inv;
|
||||
|
||||
selectPointsToRecalcFlow(flow,
|
||||
averaging_radius,
|
||||
(float)speed_up_thr,
|
||||
curr_rows,
|
||||
curr_cols,
|
||||
speed_up,
|
||||
new_speed_up,
|
||||
mask);
|
||||
|
||||
selectPointsToRecalcFlow(flow_inv,
|
||||
averaging_radius,
|
||||
(float)speed_up_thr,
|
||||
curr_rows,
|
||||
curr_cols,
|
||||
speed_up_inv,
|
||||
new_speed_up_inv,
|
||||
mask_inv);
|
||||
|
||||
speed_up = new_speed_up;
|
||||
speed_up_inv = new_speed_up_inv;
|
||||
|
||||
flow = upscaleOpticalFlow(curr_rows,
|
||||
curr_cols,
|
||||
prev_from,
|
||||
confidence,
|
||||
flow,
|
||||
upscale_averaging_radius,
|
||||
(float)upscale_sigma_dist,
|
||||
(float)upscale_sigma_color);
|
||||
|
||||
flow_inv = upscaleOpticalFlow(curr_rows,
|
||||
curr_cols,
|
||||
prev_to,
|
||||
confidence_inv,
|
||||
flow_inv,
|
||||
upscale_averaging_radius,
|
||||
(float)upscale_sigma_dist,
|
||||
(float)upscale_sigma_color);
|
||||
|
||||
calcConfidence(curr_from, curr_to, flow, confidence, max_flow);
|
||||
calcOpticalFlowSingleScaleSF(curr_from_extended,
|
||||
curr_to_extended,
|
||||
mask,
|
||||
flow,
|
||||
averaging_radius,
|
||||
max_flow,
|
||||
(float)sigma_dist,
|
||||
(float)sigma_color);
|
||||
|
||||
calcConfidence(curr_to, curr_from, flow_inv, confidence_inv, max_flow);
|
||||
calcOpticalFlowSingleScaleSF(curr_to_extended,
|
||||
curr_from_extended,
|
||||
mask_inv,
|
||||
flow_inv,
|
||||
averaging_radius,
|
||||
max_flow,
|
||||
(float)sigma_dist,
|
||||
(float)sigma_color);
|
||||
|
||||
extrapolateFlow(flow, speed_up);
|
||||
extrapolateFlow(flow_inv, speed_up_inv);
|
||||
|
||||
//TODO: should we remove occlusions for the last stage?
|
||||
removeOcclusions(flow, flow_inv, (float)occ_thr, confidence);
|
||||
removeOcclusions(flow_inv, flow, (float)occ_thr, confidence_inv);
|
||||
}
|
||||
|
||||
crossBilateralFilter(flow, curr_from, confidence, flow,
|
||||
postprocess_window, (float)sigma_color_fix, (float)sigma_dist_fix);
|
||||
|
||||
GaussianBlur(flow, flow, Size(3, 3), 5);
|
||||
|
||||
_resulted_flow.create(flow.size(), CV_32FC2);
|
||||
Mat resulted_flow = _resulted_flow.getMat();
|
||||
int from_to[] = {0,1 , 1,0};
|
||||
mixChannels(&flow, 1, &resulted_flow, 1, from_to, 2);
|
||||
}
|
||||
|
||||
CV_EXPORTS_W void calcOpticalFlowSF(InputArray from,
|
||||
InputArray to,
|
||||
OutputArray flow,
|
||||
int layers,
|
||||
int averaging_block_size,
|
||||
int max_flow) {
|
||||
calcOpticalFlowSF(from, to, flow, layers, averaging_block_size, max_flow,
|
||||
4.1, 25.5, 18, 55.0, 25.5, 0.35, 18, 55.0, 25.5, 10);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -73,7 +73,7 @@
|
||||
*/
|
||||
|
||||
#include "precomp.hpp"
|
||||
#include "opencl_kernels.hpp"
|
||||
#include "opencl_kernels_video.hpp"
|
||||
|
||||
#include <limits>
|
||||
#include <iomanip>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "test_precomp.hpp"
|
||||
#include "../test_precomp.hpp"
|
||||
#include "opencv2/ts/ocl_test.hpp"
|
||||
|
||||
#ifdef HAVE_OPENCL
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
// 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.
|
||||
|
||||
// Copyright (C) 2014, Advanced Micro Devices, Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
#include "opencv2/ts/ocl_test.hpp"
|
||||
|
||||
#ifdef HAVE_OPENCL
|
||||
|
||||
namespace cvtest {
|
||||
namespace ocl {
|
||||
|
||||
PARAM_TEST_CASE(UpdateMotionHistory, bool)
|
||||
{
|
||||
double timestamp, duration;
|
||||
bool use_roi;
|
||||
|
||||
TEST_DECLARE_INPUT_PARAMETER(silhouette);
|
||||
TEST_DECLARE_OUTPUT_PARAMETER(mhi);
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
use_roi = GET_PARAM(0);
|
||||
}
|
||||
|
||||
virtual void generateTestData()
|
||||
{
|
||||
Size roiSize = randomSize(1, MAX_VALUE);
|
||||
Border silhouetteBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
|
||||
randomSubMat(silhouette, silhouette_roi, roiSize, silhouetteBorder, CV_8UC1, -11, 11);
|
||||
|
||||
Border mhiBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
|
||||
randomSubMat(mhi, mhi_roi, roiSize, mhiBorder, CV_32FC1, 0, 1);
|
||||
|
||||
timestamp = randomDouble(0, 1);
|
||||
duration = randomDouble(0, 1);
|
||||
if (timestamp < duration)
|
||||
std::swap(timestamp, duration);
|
||||
|
||||
UMAT_UPLOAD_INPUT_PARAMETER(silhouette);
|
||||
UMAT_UPLOAD_OUTPUT_PARAMETER(mhi);
|
||||
}
|
||||
};
|
||||
|
||||
OCL_TEST_P(UpdateMotionHistory, Mat)
|
||||
{
|
||||
for (int j = 0; j < test_loop_times; j++)
|
||||
{
|
||||
generateTestData();
|
||||
|
||||
OCL_OFF(cv::updateMotionHistory(silhouette_roi, mhi_roi, timestamp, duration));
|
||||
OCL_ON(cv::updateMotionHistory(usilhouette_roi, umhi_roi, timestamp, duration));
|
||||
|
||||
OCL_EXPECT_MATS_NEAR(mhi, 0);
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////// Instantiation /////////////////////////////////////////
|
||||
|
||||
OCL_INSTANTIATE_TEST_CASE_P(Video, UpdateMotionHistory, Values(false, true));
|
||||
|
||||
} } // namespace cvtest::ocl
|
||||
|
||||
#endif // HAVE_OPENCL
|
||||
@@ -41,7 +41,7 @@
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
#include "../test_precomp.hpp"
|
||||
#include "opencv2/ts/ocl_test.hpp"
|
||||
|
||||
#ifdef HAVE_OPENCL
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
#include "../test_precomp.hpp"
|
||||
#include "opencv2/ts/ocl_test.hpp"
|
||||
|
||||
#ifdef HAVE_OPENCL
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
//M*/
|
||||
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
#include "../test_precomp.hpp"
|
||||
#include "opencv2/ts/ocl_test.hpp"
|
||||
|
||||
|
||||
|
||||
@@ -1,137 +0,0 @@
|
||||
/*
|
||||
* BackgroundSubtractorGBH_test.cpp
|
||||
*
|
||||
* Created on: Jun 14, 2012
|
||||
* Author: andrewgodbehere
|
||||
*/
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
|
||||
using namespace cv;
|
||||
|
||||
class CV_BackgroundSubtractorTest : public cvtest::BaseTest
|
||||
{
|
||||
public:
|
||||
CV_BackgroundSubtractorTest();
|
||||
protected:
|
||||
void run(int);
|
||||
};
|
||||
|
||||
CV_BackgroundSubtractorTest::CV_BackgroundSubtractorTest()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
void CV_BackgroundSubtractorTest::run(int)
|
||||
{
|
||||
int code = cvtest::TS::OK;
|
||||
RNG& rng = ts->get_rng();
|
||||
int type = ((unsigned int)rng)%7; //!< pick a random type, 0 - 6, defined in types_c.h
|
||||
int channels = 1 + ((unsigned int)rng)%4; //!< random number of channels from 1 to 4.
|
||||
int channelsAndType = CV_MAKETYPE(type,channels);
|
||||
int width = 2 + ((unsigned int)rng)%98; //!< Mat will be 2 to 100 in width and height
|
||||
int height = 2 + ((unsigned int)rng)%98;
|
||||
|
||||
Ptr<BackgroundSubtractorGMG> fgbg = createBackgroundSubtractorGMG();
|
||||
Mat fgmask;
|
||||
|
||||
if (!fgbg)
|
||||
CV_Error(Error::StsError,"Failed to create Algorithm\n");
|
||||
|
||||
/**
|
||||
* 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 (type == 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 (type == CV_8S)
|
||||
{
|
||||
maxd = (char)rng.uniform(32, CHAR_MAX);
|
||||
mind = (char)rng.uniform(CHAR_MIN, -32);
|
||||
}
|
||||
else if (type == 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 (type == CV_16S)
|
||||
{
|
||||
maxd = rng.uniform(32, SHRT_MAX);
|
||||
mind = rng.uniform(SHRT_MIN, -32);
|
||||
}
|
||||
else if (type == CV_32S)
|
||||
{
|
||||
maxd = rng.uniform(32, INT_MAX);
|
||||
mind = rng.uniform(INT_MIN, -32);
|
||||
}
|
||||
else if (type == CV_32F)
|
||||
{
|
||||
maxd = rng.uniform(32.0f, FLT_MAX);
|
||||
mind = rng.uniform(-FLT_MAX, -32.0f);
|
||||
}
|
||||
else if (type == CV_64F)
|
||||
{
|
||||
maxd = rng.uniform(32.0, DBL_MAX);
|
||||
mind = rng.uniform(-DBL_MAX, -32.0);
|
||||
}
|
||||
|
||||
fgbg->setMinVal(mind);
|
||||
fgbg->setMaxVal(maxd);
|
||||
|
||||
Mat simImage = Mat::zeros(height, width, channelsAndType);
|
||||
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);
|
||||
Mat fullbg = Mat::zeros(simImage.rows, simImage.cols, CV_8U);
|
||||
|
||||
//! fgmask should be entirely background during training
|
||||
code = cvtest::cmpEps2( ts, fgmask, fullbg, 0, false, "The training foreground mask" );
|
||||
if (code < 0)
|
||||
ts->set_failed_test_info( code );
|
||||
}
|
||||
//! generate last image, distinct from training images
|
||||
rng.fill(simImage, RNG::UNIFORM, mind, maxd);
|
||||
|
||||
fgbg->apply(simImage,fgmask);
|
||||
//! now fgmask should be entirely foreground
|
||||
Mat fullfg = 255*Mat::ones(simImage.rows, simImage.cols, CV_8U);
|
||||
code = cvtest::cmpEps2( ts, fgmask, fullfg, 255, false, "The final foreground mask" );
|
||||
if (code < 0)
|
||||
{
|
||||
ts->set_failed_test_info( code );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(VIDEO_BGSUBGMG, accuracy) { CV_BackgroundSubtractorTest test; test.safe_run(); }
|
||||
@@ -1,500 +0,0 @@
|
||||
/*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.
|
||||
//
|
||||
//
|
||||
// Intel License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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 "test_precomp.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
///////////////////// base MHI class ///////////////////////
|
||||
class CV_MHIBaseTest : public cvtest::ArrayTest
|
||||
{
|
||||
public:
|
||||
CV_MHIBaseTest();
|
||||
|
||||
protected:
|
||||
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
|
||||
void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
|
||||
int prepare_test_case( int test_case_idx );
|
||||
double timestamp, duration, max_log_duration;
|
||||
int mhi_i, mhi_ref_i;
|
||||
double silh_ratio;
|
||||
};
|
||||
|
||||
|
||||
CV_MHIBaseTest::CV_MHIBaseTest()
|
||||
{
|
||||
timestamp = duration = 0;
|
||||
max_log_duration = 9;
|
||||
mhi_i = mhi_ref_i = -1;
|
||||
|
||||
silh_ratio = 0.25;
|
||||
}
|
||||
|
||||
|
||||
void CV_MHIBaseTest::get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high )
|
||||
{
|
||||
cvtest::ArrayTest::get_minmax_bounds( i, j, type, low, high );
|
||||
if( i == INPUT && CV_MAT_DEPTH(type) == CV_8U )
|
||||
{
|
||||
low = Scalar::all(cvRound(-1./silh_ratio)+2.);
|
||||
high = Scalar::all(2);
|
||||
}
|
||||
else if( i == mhi_i || i == mhi_ref_i )
|
||||
{
|
||||
low = Scalar::all(-exp(max_log_duration));
|
||||
high = Scalar::all(0.);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CV_MHIBaseTest::get_test_array_types_and_sizes( int test_case_idx,
|
||||
vector<vector<Size> >& sizes, vector<vector<int> >& types )
|
||||
{
|
||||
RNG& rng = ts->get_rng();
|
||||
cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
|
||||
|
||||
types[INPUT][0] = CV_8UC1;
|
||||
types[mhi_i][0] = types[mhi_ref_i][0] = CV_32FC1;
|
||||
duration = exp(cvtest::randReal(rng)*max_log_duration);
|
||||
timestamp = duration + cvtest::randReal(rng)*30.-10.;
|
||||
}
|
||||
|
||||
|
||||
int CV_MHIBaseTest::prepare_test_case( int test_case_idx )
|
||||
{
|
||||
int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
|
||||
if( code > 0 )
|
||||
{
|
||||
Mat& mat = test_mat[mhi_i][0];
|
||||
mat += Scalar::all(duration);
|
||||
cv::max(mat, 0, mat);
|
||||
if( mhi_i != mhi_ref_i )
|
||||
{
|
||||
Mat& mat0 = test_mat[mhi_ref_i][0];
|
||||
cvtest::copy( mat, mat0 );
|
||||
}
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
///////////////////// update motion history ////////////////////////////
|
||||
|
||||
static void test_updateMHI( const Mat& silh, Mat& mhi, double timestamp, double duration )
|
||||
{
|
||||
int i, j;
|
||||
float delbound = (float)(timestamp - duration);
|
||||
for( i = 0; i < mhi.rows; i++ )
|
||||
{
|
||||
const uchar* silh_row = silh.ptr(i);
|
||||
float* mhi_row = mhi.ptr<float>(i);
|
||||
|
||||
for( j = 0; j < mhi.cols; j++ )
|
||||
{
|
||||
if( silh_row[j] )
|
||||
mhi_row[j] = (float)timestamp;
|
||||
else if( mhi_row[j] < delbound )
|
||||
mhi_row[j] = 0.f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class CV_UpdateMHITest : public CV_MHIBaseTest
|
||||
{
|
||||
public:
|
||||
CV_UpdateMHITest();
|
||||
|
||||
protected:
|
||||
double get_success_error_level( int test_case_idx, int i, int j );
|
||||
void run_func();
|
||||
void prepare_to_validation( int );
|
||||
};
|
||||
|
||||
|
||||
CV_UpdateMHITest::CV_UpdateMHITest()
|
||||
{
|
||||
test_array[INPUT].push_back(NULL);
|
||||
test_array[INPUT_OUTPUT].push_back(NULL);
|
||||
test_array[REF_INPUT_OUTPUT].push_back(NULL);
|
||||
mhi_i = INPUT_OUTPUT; mhi_ref_i = REF_INPUT_OUTPUT;
|
||||
}
|
||||
|
||||
|
||||
double CV_UpdateMHITest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void CV_UpdateMHITest::run_func()
|
||||
{
|
||||
cv::updateMotionHistory( test_mat[INPUT][0], test_mat[INPUT_OUTPUT][0], timestamp, duration);
|
||||
}
|
||||
|
||||
|
||||
void CV_UpdateMHITest::prepare_to_validation( int /*test_case_idx*/ )
|
||||
{
|
||||
//CvMat m0 = test_mat[REF_INPUT_OUTPUT][0];
|
||||
test_updateMHI( test_mat[INPUT][0], test_mat[REF_INPUT_OUTPUT][0], timestamp, duration );
|
||||
}
|
||||
|
||||
|
||||
///////////////////// calc motion gradient ////////////////////////////
|
||||
|
||||
static void test_MHIGradient( const Mat& mhi, Mat& mask, Mat& orientation,
|
||||
double delta1, double delta2, int aperture_size )
|
||||
{
|
||||
Point anchor( aperture_size/2, aperture_size/2 );
|
||||
double limit = 1e-4*aperture_size*aperture_size;
|
||||
|
||||
Mat dx, dy, min_mhi, max_mhi;
|
||||
|
||||
Mat kernel = cvtest::calcSobelKernel2D( 1, 0, aperture_size );
|
||||
cvtest::filter2D( mhi, dx, CV_32F, kernel, anchor, 0, BORDER_REPLICATE );
|
||||
kernel = cvtest::calcSobelKernel2D( 0, 1, aperture_size );
|
||||
cvtest::filter2D( mhi, dy, CV_32F, kernel, anchor, 0, BORDER_REPLICATE );
|
||||
|
||||
kernel = Mat::ones(aperture_size, aperture_size, CV_8U);
|
||||
cvtest::erode(mhi, min_mhi, kernel, anchor, 0, BORDER_REPLICATE);
|
||||
cvtest::dilate(mhi, max_mhi, kernel, anchor, 0, BORDER_REPLICATE);
|
||||
|
||||
if( delta1 > delta2 )
|
||||
{
|
||||
std::swap( delta1, delta2 );
|
||||
}
|
||||
|
||||
for( int i = 0; i < mhi.rows; i++ )
|
||||
{
|
||||
uchar* mask_row = mask.ptr(i);
|
||||
float* orient_row = orientation.ptr<float>(i);
|
||||
const float* dx_row = dx.ptr<float>(i);
|
||||
const float* dy_row = dy.ptr<float>(i);
|
||||
const float* min_row = min_mhi.ptr<float>(i);
|
||||
const float* max_row = max_mhi.ptr<float>(i);
|
||||
|
||||
for( int j = 0; j < mhi.cols; j++ )
|
||||
{
|
||||
double delta = max_row[j] - min_row[j];
|
||||
double _dx = dx_row[j], _dy = dy_row[j];
|
||||
|
||||
if( delta1 <= delta && delta <= delta2 &&
|
||||
(fabs(_dx) > limit || fabs(_dy) > limit) )
|
||||
{
|
||||
mask_row[j] = 1;
|
||||
double angle = atan2( _dy, _dx ) * (180/CV_PI);
|
||||
if( angle < 0 )
|
||||
angle += 360.;
|
||||
orient_row[j] = (float)angle;
|
||||
}
|
||||
else
|
||||
{
|
||||
mask_row[j] = 0;
|
||||
orient_row[j] = 0.f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class CV_MHIGradientTest : public CV_MHIBaseTest
|
||||
{
|
||||
public:
|
||||
CV_MHIGradientTest();
|
||||
|
||||
protected:
|
||||
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
|
||||
double get_success_error_level( int test_case_idx, int i, int j );
|
||||
void run_func();
|
||||
void prepare_to_validation( int );
|
||||
|
||||
double delta1, delta2, delta_range_log;
|
||||
int aperture_size;
|
||||
};
|
||||
|
||||
|
||||
CV_MHIGradientTest::CV_MHIGradientTest()
|
||||
{
|
||||
mhi_i = mhi_ref_i = INPUT;
|
||||
test_array[INPUT].push_back(NULL);
|
||||
test_array[OUTPUT].push_back(NULL);
|
||||
test_array[OUTPUT].push_back(NULL);
|
||||
test_array[REF_OUTPUT].push_back(NULL);
|
||||
test_array[REF_OUTPUT].push_back(NULL);
|
||||
delta1 = delta2 = 0;
|
||||
aperture_size = 0;
|
||||
delta_range_log = 4;
|
||||
}
|
||||
|
||||
|
||||
void CV_MHIGradientTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
|
||||
{
|
||||
RNG& rng = ts->get_rng();
|
||||
CV_MHIBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
|
||||
|
||||
types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_8UC1;
|
||||
types[OUTPUT][1] = types[REF_OUTPUT][1] = CV_32FC1;
|
||||
delta1 = exp(cvtest::randReal(rng)*delta_range_log + 1.);
|
||||
delta2 = exp(cvtest::randReal(rng)*delta_range_log + 1.);
|
||||
aperture_size = (cvtest::randInt(rng)%3)*2+3;
|
||||
//duration = exp(cvtest::randReal(rng)*max_log_duration);
|
||||
//timestamp = duration + cvtest::randReal(rng)*30.-10.;
|
||||
}
|
||||
|
||||
|
||||
double CV_MHIGradientTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int j )
|
||||
{
|
||||
return j == 0 ? 0 : 2e-1;
|
||||
}
|
||||
|
||||
|
||||
void CV_MHIGradientTest::run_func()
|
||||
{
|
||||
cv::calcMotionGradient(test_mat[INPUT][0], test_mat[OUTPUT][0],
|
||||
test_mat[OUTPUT][1], delta1, delta2, aperture_size );
|
||||
//cvCalcMotionGradient( test_array[INPUT][0], test_array[OUTPUT][0],
|
||||
// test_array[OUTPUT][1], delta1, delta2, aperture_size );
|
||||
}
|
||||
|
||||
|
||||
void CV_MHIGradientTest::prepare_to_validation( int /*test_case_idx*/ )
|
||||
{
|
||||
test_MHIGradient( test_mat[INPUT][0], test_mat[REF_OUTPUT][0],
|
||||
test_mat[REF_OUTPUT][1], delta1, delta2, aperture_size );
|
||||
test_mat[REF_OUTPUT][0] += Scalar::all(1);
|
||||
test_mat[OUTPUT][0] += Scalar::all(1);
|
||||
}
|
||||
|
||||
|
||||
////////////////////// calc global orientation /////////////////////////
|
||||
|
||||
static double test_calcGlobalOrientation( const Mat& orient, const Mat& mask,
|
||||
const Mat& mhi, double timestamp, double duration )
|
||||
{
|
||||
const int HIST_SIZE = 12;
|
||||
int y, x;
|
||||
int histogram[HIST_SIZE];
|
||||
int max_bin = 0;
|
||||
|
||||
double base_orientation = 0, delta_orientation = 0, weight = 0;
|
||||
double low_time, global_orientation;
|
||||
|
||||
memset( histogram, 0, sizeof( histogram ));
|
||||
timestamp = 0;
|
||||
|
||||
for( y = 0; y < orient.rows; y++ )
|
||||
{
|
||||
const float* orient_data = orient.ptr<float>(y);
|
||||
const uchar* mask_data = mask.ptr(y);
|
||||
const float* mhi_data = mhi.ptr<float>(y);
|
||||
for( x = 0; x < orient.cols; x++ )
|
||||
if( mask_data[x] )
|
||||
{
|
||||
int bin = cvFloor( (orient_data[x]*HIST_SIZE)/360 );
|
||||
histogram[bin < 0 ? 0 : bin >= HIST_SIZE ? HIST_SIZE-1 : bin]++;
|
||||
if( mhi_data[x] > timestamp )
|
||||
timestamp = mhi_data[x];
|
||||
}
|
||||
}
|
||||
|
||||
low_time = timestamp - duration;
|
||||
|
||||
for( x = 1; x < HIST_SIZE; x++ )
|
||||
{
|
||||
if( histogram[x] > histogram[max_bin] )
|
||||
max_bin = x;
|
||||
}
|
||||
|
||||
base_orientation = ((double)max_bin*360)/HIST_SIZE;
|
||||
|
||||
for( y = 0; y < orient.rows; y++ )
|
||||
{
|
||||
const float* orient_data = orient.ptr<float>(y);
|
||||
const float* mhi_data = mhi.ptr<float>(y);
|
||||
const uchar* mask_data = mask.ptr(y);
|
||||
|
||||
for( x = 0; x < orient.cols; x++ )
|
||||
{
|
||||
if( mask_data[x] && mhi_data[x] > low_time )
|
||||
{
|
||||
double diff = orient_data[x] - base_orientation;
|
||||
double delta_weight = (((mhi_data[x] - low_time)/duration)*254 + 1)/255;
|
||||
|
||||
if( diff < -180 ) diff += 360;
|
||||
if( diff > 180 ) diff -= 360;
|
||||
|
||||
if( delta_weight > 0 && fabs(diff) < 45 )
|
||||
{
|
||||
delta_orientation += diff*delta_weight;
|
||||
weight += delta_weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( weight == 0 )
|
||||
global_orientation = base_orientation;
|
||||
else
|
||||
{
|
||||
global_orientation = base_orientation + delta_orientation/weight;
|
||||
if( global_orientation < 0 ) global_orientation += 360;
|
||||
if( global_orientation > 360 ) global_orientation -= 360;
|
||||
}
|
||||
|
||||
return global_orientation;
|
||||
}
|
||||
|
||||
|
||||
class CV_MHIGlobalOrientTest : public CV_MHIBaseTest
|
||||
{
|
||||
public:
|
||||
CV_MHIGlobalOrientTest();
|
||||
|
||||
protected:
|
||||
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
|
||||
void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
|
||||
double get_success_error_level( int test_case_idx, int i, int j );
|
||||
int validate_test_results( int test_case_idx );
|
||||
void run_func();
|
||||
double angle, min_angle, max_angle;
|
||||
};
|
||||
|
||||
|
||||
CV_MHIGlobalOrientTest::CV_MHIGlobalOrientTest()
|
||||
{
|
||||
mhi_i = mhi_ref_i = INPUT;
|
||||
test_array[INPUT].push_back(NULL);
|
||||
test_array[INPUT].push_back(NULL);
|
||||
test_array[INPUT].push_back(NULL);
|
||||
min_angle = max_angle = 0;
|
||||
}
|
||||
|
||||
|
||||
void CV_MHIGlobalOrientTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
|
||||
{
|
||||
RNG& rng = ts->get_rng();
|
||||
CV_MHIBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
|
||||
Size size = sizes[INPUT][0];
|
||||
|
||||
size.width = MAX( size.width, 16 );
|
||||
size.height = MAX( size.height, 16 );
|
||||
sizes[INPUT][0] = sizes[INPUT][1] = sizes[INPUT][2] = size;
|
||||
|
||||
types[INPUT][1] = CV_8UC1; // mask
|
||||
types[INPUT][2] = CV_32FC1; // orientation
|
||||
|
||||
min_angle = cvtest::randReal(rng)*359.9;
|
||||
max_angle = cvtest::randReal(rng)*359.9;
|
||||
if( min_angle >= max_angle )
|
||||
{
|
||||
std::swap( min_angle, max_angle);
|
||||
}
|
||||
max_angle += 0.1;
|
||||
duration = exp(cvtest::randReal(rng)*max_log_duration);
|
||||
timestamp = duration + cvtest::randReal(rng)*30.-10.;
|
||||
}
|
||||
|
||||
|
||||
void CV_MHIGlobalOrientTest::get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high )
|
||||
{
|
||||
CV_MHIBaseTest::get_minmax_bounds( i, j, type, low, high );
|
||||
if( i == INPUT && j == 2 )
|
||||
{
|
||||
low = Scalar::all(min_angle);
|
||||
high = Scalar::all(max_angle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
double CV_MHIGlobalOrientTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
|
||||
{
|
||||
return 15;
|
||||
}
|
||||
|
||||
|
||||
void CV_MHIGlobalOrientTest::run_func()
|
||||
{
|
||||
//angle = cvCalcGlobalOrientation( test_array[INPUT][2], test_array[INPUT][1],
|
||||
// test_array[INPUT][0], timestamp, duration );
|
||||
angle = cv::calcGlobalOrientation(test_mat[INPUT][2], test_mat[INPUT][1],
|
||||
test_mat[INPUT][0], timestamp, duration );
|
||||
}
|
||||
|
||||
|
||||
int CV_MHIGlobalOrientTest::validate_test_results( int test_case_idx )
|
||||
{
|
||||
//printf("%d. rows=%d, cols=%d, nzmask=%d\n", test_case_idx, test_mat[INPUT][1].rows, test_mat[INPUT][1].cols,
|
||||
// cvCountNonZero(test_array[INPUT][1]));
|
||||
|
||||
double ref_angle = test_calcGlobalOrientation( test_mat[INPUT][2], test_mat[INPUT][1],
|
||||
test_mat[INPUT][0], timestamp, duration );
|
||||
double err_level = get_success_error_level( test_case_idx, 0, 0 );
|
||||
int code = cvtest::TS::OK;
|
||||
int nz = countNonZero( test_mat[INPUT][1] );
|
||||
|
||||
if( nz > 32 && !(min_angle - err_level <= angle &&
|
||||
max_angle + err_level >= angle) &&
|
||||
!(min_angle - err_level <= angle+360 &&
|
||||
max_angle + err_level >= angle+360) )
|
||||
{
|
||||
ts->printf( cvtest::TS::LOG, "The angle=%g is outside (%g,%g) range\n",
|
||||
angle, min_angle - err_level, max_angle + err_level );
|
||||
code = cvtest::TS::FAIL_BAD_ACCURACY;
|
||||
}
|
||||
else if( fabs(angle - ref_angle) > err_level &&
|
||||
fabs(360 - fabs(angle - ref_angle)) > err_level )
|
||||
{
|
||||
ts->printf( cvtest::TS::LOG, "The angle=%g differs too much from reference value=%g\n",
|
||||
angle, ref_angle );
|
||||
code = cvtest::TS::FAIL_BAD_ACCURACY;
|
||||
}
|
||||
|
||||
if( code < 0 )
|
||||
ts->set_failed_test_info( code );
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
TEST(Video_MHIUpdate, accuracy) { CV_UpdateMHITest test; test.safe_run(); }
|
||||
TEST(Video_MHIGradient, accuracy) { CV_MHIGradientTest test; test.safe_run(); }
|
||||
TEST(Video_MHIGlobalOrient, accuracy) { CV_MHIGlobalOrientTest test; test.safe_run(); }
|
||||
@@ -1,190 +0,0 @@
|
||||
/*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.
|
||||
//
|
||||
//
|
||||
// Intel License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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 "test_precomp.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ///////////////////// simpleflow_test ///////////////////////// */
|
||||
|
||||
class CV_SimpleFlowTest : public cvtest::BaseTest
|
||||
{
|
||||
public:
|
||||
CV_SimpleFlowTest();
|
||||
protected:
|
||||
void run(int);
|
||||
};
|
||||
|
||||
|
||||
CV_SimpleFlowTest::CV_SimpleFlowTest() {}
|
||||
|
||||
static bool readOpticalFlowFromFile(FILE* file, cv::Mat& flow) {
|
||||
char header[5];
|
||||
if (fread(header, 1, 4, file) < 4 && (string)header != "PIEH") {
|
||||
return false;
|
||||
}
|
||||
|
||||
int cols, rows;
|
||||
if (fread(&cols, sizeof(int), 1, file) != 1||
|
||||
fread(&rows, sizeof(int), 1, file) != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
flow = cv::Mat::zeros(rows, cols, CV_32FC2);
|
||||
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
for (int j = 0; j < cols; ++j) {
|
||||
cv::Vec2f flow_at_point;
|
||||
if (fread(&(flow_at_point[0]), sizeof(float), 1, file) != 1 ||
|
||||
fread(&(flow_at_point[1]), sizeof(float), 1, file) != 1) {
|
||||
return false;
|
||||
}
|
||||
flow.at<cv::Vec2f>(i, j) = flow_at_point;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool isFlowCorrect(float u) {
|
||||
return !cvIsNaN(u) && (fabs(u) < 1e9);
|
||||
}
|
||||
|
||||
static float calc_rmse(cv::Mat flow1, cv::Mat flow2) {
|
||||
float sum = 0;
|
||||
int counter = 0;
|
||||
const int rows = flow1.rows;
|
||||
const int cols = flow1.cols;
|
||||
|
||||
for (int y = 0; y < rows; ++y) {
|
||||
for (int x = 0; x < cols; ++x) {
|
||||
cv::Vec2f flow1_at_point = flow1.at<cv::Vec2f>(y, x);
|
||||
cv::Vec2f flow2_at_point = flow2.at<cv::Vec2f>(y, x);
|
||||
|
||||
float u1 = flow1_at_point[0];
|
||||
float v1 = flow1_at_point[1];
|
||||
float u2 = flow2_at_point[0];
|
||||
float v2 = flow2_at_point[1];
|
||||
|
||||
if (isFlowCorrect(u1) && isFlowCorrect(u2) && isFlowCorrect(v1) && isFlowCorrect(v2)) {
|
||||
sum += (u1-u2)*(u1-u2) + (v1-v2)*(v1-v2);
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (float)sqrt(sum / (1e-9 + counter));
|
||||
}
|
||||
|
||||
void CV_SimpleFlowTest::run(int) {
|
||||
const float MAX_RMSE = 0.6f;
|
||||
const string frame1_path = ts->get_data_path() + "optflow/RubberWhale1.png";
|
||||
const string frame2_path = ts->get_data_path() + "optflow/RubberWhale2.png";
|
||||
const string gt_flow_path = ts->get_data_path() + "optflow/RubberWhale.flo";
|
||||
|
||||
cv::Mat frame1 = cv::imread(frame1_path);
|
||||
cv::Mat frame2 = cv::imread(frame2_path);
|
||||
|
||||
if (frame1.empty()) {
|
||||
ts->printf(cvtest::TS::LOG, "could not read image %s\n", frame2_path.c_str());
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame2.empty()) {
|
||||
ts->printf(cvtest::TS::LOG, "could not read image %s\n", frame2_path.c_str());
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame1.rows != frame2.rows && frame1.cols != frame2.cols) {
|
||||
ts->printf(cvtest::TS::LOG, "images should be of equal sizes (%s and %s)",
|
||||
frame1_path.c_str(), frame2_path.c_str());
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame1.type() != 16 || frame2.type() != 16) {
|
||||
ts->printf(cvtest::TS::LOG, "images should be of equal type CV_8UC3 (%s and %s)",
|
||||
frame1_path.c_str(), frame2_path.c_str());
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
|
||||
return;
|
||||
}
|
||||
|
||||
cv::Mat flow_gt;
|
||||
|
||||
FILE* gt_flow_file = fopen(gt_flow_path.c_str(), "rb");
|
||||
if (gt_flow_file == NULL) {
|
||||
ts->printf(cvtest::TS::LOG, "could not read ground-thuth flow from file %s",
|
||||
gt_flow_path.c_str());
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!readOpticalFlowFromFile(gt_flow_file, flow_gt)) {
|
||||
ts->printf(cvtest::TS::LOG, "error while reading flow data from file %s",
|
||||
gt_flow_path.c_str());
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
|
||||
return;
|
||||
}
|
||||
fclose(gt_flow_file);
|
||||
|
||||
cv::Mat flow;
|
||||
cv::calcOpticalFlowSF(frame1, frame2, flow, 3, 2, 4);
|
||||
|
||||
float rmse = calc_rmse(flow_gt, flow);
|
||||
|
||||
ts->printf(cvtest::TS::LOG, "Optical flow estimation RMSE for SimpleFlow algorithm : %lf\n",
|
||||
rmse);
|
||||
|
||||
if (rmse > MAX_RMSE) {
|
||||
ts->printf( cvtest::TS::LOG,
|
||||
"Too big rmse error : %lf ( >= %lf )\n", rmse, MAX_RMSE);
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST(Video_OpticalFlowSimpleFlow, accuracy) { CV_SimpleFlowTest test; test.safe_run(); }
|
||||
|
||||
/* End of file. */
|
||||
@@ -52,12 +52,13 @@ namespace
|
||||
{
|
||||
// first four bytes, should be the same in little endian
|
||||
const float FLO_TAG_FLOAT = 202021.25f; // check for this when READING the file
|
||||
const char FLO_TAG_STRING[] = "PIEH"; // use this when WRITING the file
|
||||
|
||||
#ifdef DUMP
|
||||
// binary file format for flow data specified here:
|
||||
// http://vision.middlebury.edu/flow/data/
|
||||
void writeOpticalFlowToFile(const Mat_<Point2f>& flow, const string& fileName)
|
||||
{
|
||||
const char FLO_TAG_STRING[] = "PIEH"; // use this when WRITING the file
|
||||
ofstream file(fileName.c_str(), ios_base::binary);
|
||||
|
||||
file << FLO_TAG_STRING;
|
||||
@@ -76,6 +77,7 @@ namespace
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// binary file format for flow data specified here:
|
||||
// http://vision.middlebury.edu/flow/data/
|
||||
|
||||
Reference in New Issue
Block a user