51#ifndef __OPENCV_OPTFLOW_SPARSE_MATCHING_GPC_HPP__
52#define __OPENCV_OPTFLOW_SPARSE_MATCHING_GPC_HPP__
54#include "opencv2/core.hpp"
55#include "opencv2/imgproc.hpp"
67 static const unsigned nFeatures = 18;
72 void markAsSeparated() { feature[0] = std::numeric_limits< double >::quiet_NaN(); }
74 bool isSeparated()
const {
return cvIsNaN( feature[0] ) != 0; }
86typedef std::vector< GPCPatchSample > GPCSamplesVector;
101 GPCSamplesVector samples;
109 const std::vector< String > >,
int descriptorType );
111 static Ptr< GPCTrainingSamples > create( InputArrayOfArrays imagesFrom, InputArrayOfArrays imagesTo, InputArrayOfArrays gt,
112 int descriptorType );
114 size_t size()
const {
return samples.size(); }
116 int type()
const {
return descriptorType; }
118 operator GPCSamplesVector &() {
return samples; }
131 bool _printProgress =
true )
164 bool operator==(
const Node &n )
const {
return coef == n.
coef && rhs == n.
rhs && left == n.left && right == n.right; }
168 typedef GPCSamplesVector::iterator SIter;
170 std::vector< Node > nodes;
173 bool trainNode(
size_t nodeId, SIter begin, SIter end,
unsigned depth );
186 bool operator==(
const GPCTree &t )
const {
return nodes == t.nodes; }
199 bool operator==(
const Trail &trail )
const {
return memcmp( leaf, trail.leaf,
sizeof( leaf ) ) == 0; }
201 bool operator<(
const Trail &trail )
const
203 for (
int i = 0; i < T - 1; ++i )
204 if ( leaf[i] != trail.leaf[i] )
205 return leaf[i] < trail.leaf[i];
206 return leaf[T - 1] < trail.leaf[T - 1];
214 const std::vector< GPCPatchDescriptor > *descr;
215 std::vector< Trail > *trails;
217 ParallelTrailsFilling &operator=(
const ParallelTrailsFilling & );
220 ParallelTrailsFilling(
const GPCForest *_forest,
const std::vector< GPCPatchDescriptor > *_descr, std::vector< Trail > *_trails )
221 : forest( _forest ), descr( _descr ), trails( _trails ){};
223 void operator()(
const Range &range )
const CV_OVERRIDE
225 for (
int t = range.start; t < range.end; ++t )
226 for (
size_t i = 0; i < descr->size(); ++i )
227 trails->at( i ).leaf[t] = forest->tree[t].findLeafForPatch( descr->at( i ) );
239 for (
int i = 0; i < T; ++i )
240 tree[i].
train( samples, params );
246 void train(
const std::vector< String > &imagesFrom,
const std::vector< String > &imagesTo,
const std::vector< String > >,
249 for (
int i = 0; i < T; ++i )
253 tree[i].train( *samples, params );
257 void train( InputArrayOfArrays imagesFrom, InputArrayOfArrays imagesTo, InputArrayOfArrays gt,
260 for (
int i = 0; i < T; ++i )
264 tree[i].train( *samples, params );
270 fs <<
"ntrees" << T <<
"trees"
272 for (
int i = 0; i < T; ++i )
285 for (
int i = 0; i < T; ++i, ++it )
295 void findCorrespondences( InputArray imgFrom, InputArray imgTo, std::vector< std::pair< Point2i, Point2i > > &corr,
304 static void dropOutliers( std::vector< std::pair< Point2i, Point2i > > &corr );
306 static void getAllDescriptorsForImage(
const Mat *imgCh, std::vector< GPCPatchDescriptor > &descr,
const GPCMatchingParams &mp,
309 static void getCoordinatesFromIndex(
size_t index,
Size sz,
int &x,
int &y );
320 imgFrom.getMat().
convertTo( from, CV_32FC3 );
321 imgTo.getMat().convertTo( to, CV_32FC3 );
325 Mat fromCh[3], toCh[3];
326 split( from, fromCh );
329 std::vector< GPCPatchDescriptor > descr;
330 GPCDetails::getAllDescriptorsForImage( fromCh, descr, params, tree[0].getDescriptorType() );
331 std::vector< Trail > trailsFrom( descr.size() ), trailsTo( descr.size() );
333 for (
size_t i = 0; i < descr.size(); ++i )
334 GPCDetails::getCoordinatesFromIndex( i, from.size(), trailsFrom[i].coord.x, trailsFrom[i].coord.y );
338 GPCDetails::getAllDescriptorsForImage( toCh, descr, params, tree[0].getDescriptorType() );
340 for (
size_t i = 0; i < descr.size(); ++i )
341 GPCDetails::getCoordinatesFromIndex( i, to.size(), trailsTo[i].coord.x, trailsTo[i].coord.y );
344 std::sort( trailsFrom.begin(), trailsFrom.end() );
345 std::sort( trailsTo.begin(), trailsTo.end() );
347 for (
size_t i = 0; i < trailsFrom.size(); ++i )
350 while ( i + 1 < trailsFrom.size() && trailsFrom[i] == trailsFrom[i + 1] )
354 typename std::vector< Trail >::const_iterator lb = std::lower_bound( trailsTo.begin(), trailsTo.end(), trailsFrom[i] );
355 if ( lb != trailsTo.end() && *lb == trailsFrom[i] && ( ( lb + 1 ) == trailsTo.end() || !( *lb == *( lb + 1 ) ) ) )
356 corr.push_back( std::make_pair( trailsFrom[i].coord, lb->coord ) );
360 GPCDetails::dropOutliers( corr );
This is a base class for all more or less complex algorithms in OpenCV
Definition: core.hpp:3091
File Storage Node class.
Definition: persistence.hpp:482
used to iterate through sequences and mappings.
Definition: persistence.hpp:634
XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or readi...
Definition: persistence.hpp:304
n-dimensional dense array class
Definition: mat.hpp:802
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
Converts an array to another data type with optional scaling.
Base class for parallel data processors
Definition: utility.hpp:577
Template class specifying a continuous subsequence (slice) of a sequence.
Definition: core/types.hpp:590
Template class for specifying the size of an image or rectangle.
Definition: core/types.hpp:316
Template class for short numerical vectors, a partial case of Matx
Definition: matx.hpp:342
Definition: sparse_matching_gpc.hpp:302
Definition: sparse_matching_gpc.hpp:192
void write(FileStorage &fs) const CV_OVERRIDE
Stores algorithm parameters in a file storage
Definition: sparse_matching_gpc.hpp:268
void train(const std::vector< String > &imagesFrom, const std::vector< String > &imagesTo, const std::vector< String > >, const GPCTrainingParams params=GPCTrainingParams())
Train the forest using individual samples for each tree. It is generally better to use this instead o...
Definition: sparse_matching_gpc.hpp:246
void train(GPCTrainingSamples &samples, const GPCTrainingParams params=GPCTrainingParams())
Train the forest using one sample set for every tree. Please, consider using the next method instead ...
Definition: sparse_matching_gpc.hpp:237
void read(const FileNode &fn) CV_OVERRIDE
Reads algorithm parameters from a file storage
Definition: sparse_matching_gpc.hpp:281
Class encapsulating training samples.
Definition: sparse_matching_gpc.hpp:99
static Ptr< GPCTrainingSamples > create(const std::vector< String > &imagesFrom, const std::vector< String > &imagesTo, const std::vector< String > >, int descriptorType)
This function can be used to extract samples from a pair of images and a ground truth flow....
Class for individual tree.
Definition: sparse_matching_gpc.hpp:155
void write(FileStorage &fs) const CV_OVERRIDE
Stores algorithm parameters in a file storage
void read(const FileNode &fn) CV_OVERRIDE
Reads algorithm parameters from a file storage
CV_EXPORTS void split(const Mat &src, Mat *mvbegin)
Divides a multi-channel array into several single-channel arrays.
CV_EXPORTS_W void sort(InputArray src, OutputArray dst, int flags)
Sorts each row or each column of a matrix.
CV_EXPORTS void parallel_for_(const Range &range, const ParallelLoopBody &body, double nstripes=-1.)
Parallel data processor
CV_INLINE int cvIsNaN(double value)
Determines if the argument is Not A Number.
Definition: fast_math.hpp:273
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails
Definition: base.hpp:342
CV_EXPORTS_W void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
@ COLOR_BGR2YCrCb
convert RGB/BGR to luma-chroma (aka YCC), color conversions
Definition: imgproc.hpp:589
void findCorrespondences(InputArray imgFrom, InputArray imgTo, std::vector< std::pair< Point2i, Point2i > > &corr, const GPCMatchingParams params=GPCMatchingParams()) const
Find correspondences between two images.
Definition: sparse_matching_gpc.hpp:313
GPCDescType
Descriptor types for the Global Patch Collider.
Definition: sparse_matching_gpc.hpp:91
@ GPC_DESCRIPTOR_DCT
Better quality but slow
Definition: sparse_matching_gpc.hpp:92
@ GPC_DESCRIPTOR_WHT
Worse quality but much faster
Definition: sparse_matching_gpc.hpp:93
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75
Definition: cvstd_wrapper.hpp:74
Class encapsulating matching parameters.
Definition: sparse_matching_gpc.hpp:144
bool useOpenCL
Whether to use OpenCL to speed up the matching.
Definition: sparse_matching_gpc.hpp:145
Definition: sparse_matching_gpc.hpp:66
Definition: sparse_matching_gpc.hpp:78
Class encapsulating training parameters.
Definition: sparse_matching_gpc.hpp:124
unsigned maxTreeDepth
Maximum tree depth to stop partitioning.
Definition: sparse_matching_gpc.hpp:125
int minNumberOfSamples
Minimum number of samples in the node to stop partitioning.
Definition: sparse_matching_gpc.hpp:126
bool printProgress
Print progress to stdout.
Definition: sparse_matching_gpc.hpp:128
int descriptorType
Type of descriptors to use.
Definition: sparse_matching_gpc.hpp:127
Definition: sparse_matching_gpc.hpp:158
Vec< double, GPCPatchDescriptor::nFeatures > coef
Hyperplane coefficients
Definition: sparse_matching_gpc.hpp:159
double rhs
Bias term of the hyperplane
Definition: sparse_matching_gpc.hpp:160