opencv/modules/features2d/src/akaze.cpp

254 lines
9.1 KiB
C++
Raw Normal View History

/*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) 2008, 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 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*/
/*
OpenCV wrapper of reference implementation of
[1] Fast Explicit Diffusion for Accelerated Features in Nonlinear Scale Spaces.
Pablo F. Alcantarilla, J. Nuevo and Adrien Bartoli.
In British Machine Vision Conference (BMVC), Bristol, UK, September 2013
http://www.robesafe.com/personal/pablo.alcantarilla/papers/Alcantarilla13bmvc.pdf
@author Eugene Khvedchenya <ekhvedchenya@gmail.com>
*/
2014-04-05 15:25:46 +08:00
#include "precomp.hpp"
2014-07-30 22:02:08 +08:00
#include "kaze/AKAZEFeatures.h"
#include <iostream>
2014-04-05 15:25:46 +08:00
namespace cv
{
using namespace std;
2014-04-05 15:25:46 +08:00
class AKAZE_Impl : public AKAZE
{
public:
AKAZE_Impl(DescriptorType _descriptor_type, int _descriptor_size, int _descriptor_channels,
float _threshold, int _octaves, int _sublevels, KAZE::DiffusivityType _diffusivity)
2014-05-10 03:21:26 +08:00
: descriptor(_descriptor_type)
, descriptor_channels(_descriptor_channels)
2014-04-05 15:25:46 +08:00
, descriptor_size(_descriptor_size)
2014-07-30 22:02:08 +08:00
, threshold(_threshold)
, octaves(_octaves)
, sublevels(_sublevels)
, diffusivity(_diffusivity)
2014-04-05 15:25:46 +08:00
{
}
2018-03-15 21:16:54 +08:00
virtual ~AKAZE_Impl() CV_OVERRIDE
{
2014-04-05 15:25:46 +08:00
}
void setDescriptorType(DescriptorType dtype) CV_OVERRIDE{ descriptor = dtype; }
DescriptorType getDescriptorType() const CV_OVERRIDE{ return descriptor; }
2018-03-15 21:16:54 +08:00
void setDescriptorSize(int dsize) CV_OVERRIDE { descriptor_size = dsize; }
int getDescriptorSize() const CV_OVERRIDE { return descriptor_size; }
2018-03-15 21:16:54 +08:00
void setDescriptorChannels(int dch) CV_OVERRIDE { descriptor_channels = dch; }
int getDescriptorChannels() const CV_OVERRIDE { return descriptor_channels; }
2018-03-15 21:16:54 +08:00
void setThreshold(double threshold_) CV_OVERRIDE { threshold = (float)threshold_; }
double getThreshold() const CV_OVERRIDE { return threshold; }
2018-03-15 21:16:54 +08:00
void setNOctaves(int octaves_) CV_OVERRIDE { octaves = octaves_; }
int getNOctaves() const CV_OVERRIDE { return octaves; }
2018-03-15 21:16:54 +08:00
void setNOctaveLayers(int octaveLayers_) CV_OVERRIDE { sublevels = octaveLayers_; }
int getNOctaveLayers() const CV_OVERRIDE { return sublevels; }
void setDiffusivity(KAZE::DiffusivityType diff_) CV_OVERRIDE{ diffusivity = diff_; }
KAZE::DiffusivityType getDiffusivity() const CV_OVERRIDE{ return diffusivity; }
// returns the descriptor size in bytes
2018-03-15 21:16:54 +08:00
int descriptorSize() const CV_OVERRIDE
2014-04-05 15:25:46 +08:00
{
switch (descriptor)
{
case DESCRIPTOR_KAZE:
case DESCRIPTOR_KAZE_UPRIGHT:
return 64;
case DESCRIPTOR_MLDB:
case DESCRIPTOR_MLDB_UPRIGHT:
// We use the full length binary descriptor -> 486 bits
if (descriptor_size == 0)
{
int t = (6 + 36 + 120) * descriptor_channels;
return divUp(t, 8);
}
else
{
// We use the random bit selection length binary descriptor
return divUp(descriptor_size, 8);
}
default:
return -1;
}
2014-04-05 15:25:46 +08:00
}
// returns the descriptor type
2018-03-15 21:16:54 +08:00
int descriptorType() const CV_OVERRIDE
2014-04-05 15:25:46 +08:00
{
switch (descriptor)
{
case DESCRIPTOR_KAZE:
case DESCRIPTOR_KAZE_UPRIGHT:
return CV_32F;
case DESCRIPTOR_MLDB:
case DESCRIPTOR_MLDB_UPRIGHT:
return CV_8U;
default:
return -1;
}
2014-04-05 15:25:46 +08:00
}
// returns the default norm type
2018-03-15 21:16:54 +08:00
int defaultNorm() const CV_OVERRIDE
{
switch (descriptor)
{
case DESCRIPTOR_KAZE:
case DESCRIPTOR_KAZE_UPRIGHT:
return NORM_L2;
2014-04-05 15:25:46 +08:00
case DESCRIPTOR_MLDB:
case DESCRIPTOR_MLDB_UPRIGHT:
return NORM_HAMMING;
2014-04-05 15:25:46 +08:00
default:
return -1;
}
2014-04-05 15:25:46 +08:00
}
void detectAndCompute(InputArray image, InputArray mask,
std::vector<KeyPoint>& keypoints,
OutputArray descriptors,
2018-03-15 21:16:54 +08:00
bool useProvidedKeypoints) CV_OVERRIDE
2014-04-05 15:25:46 +08:00
{
CV_INSTRUMENT_REGION();
CV_Assert( ! image.empty() );
AKAZEOptions options;
options.descriptor = descriptor;
options.descriptor_channels = descriptor_channels;
options.descriptor_size = descriptor_size;
options.img_width = image.cols();
options.img_height = image.rows();
options.dthreshold = threshold;
options.omax = octaves;
options.nsublevels = sublevels;
options.diffusivity = diffusivity;
AKAZEFeatures impl(options);
impl.Create_Nonlinear_Scale_Space(image);
if (!useProvidedKeypoints)
{
impl.Feature_Detection(keypoints);
}
2014-04-05 15:25:46 +08:00
if (!mask.empty())
{
KeyPointsFilter::runByPixelsMask(keypoints, mask.getMat());
}
2014-04-05 15:25:46 +08:00
if(descriptors.needed())
{
Merge pull request #8951 from hrnr:akaze_part2 [GSOC] Speeding-up AKAZE, part #2 (#8951) * feature2d: instrument more functions used in AKAZE * rework Compute_Determinant_Hessian_Response * this takes 84% of time of Feature_Detection * run everything in parallel * compute Scharr kernels just once * compute sigma more efficiently * allocate all matrices in evolution without zeroing * features2d: add one bigger image to tests * now test have images: 600x768, 900x600 and 1385x700 to cover different resolutions * explicitly zero Lx and Ly * add Lflow and Lstep to evolution as in original AKAZE code * reworked computing keypoints orientation integrated faster function from https://github.com/h2suzuki/fast_akaze * use standard fastAtan2 instead of getAngle * compute keypoints orientation in parallel * fix visual studio warnings * replace some wrapped functions with direct calls to OpenCV functions * improved readability for people familiar with opencv * do not same image twice in base level * rework diffusity stencil * use one pass stencil for diffusity from https://github.com/h2suzuki/fast_akaze * improve locality in Create_Scale_Space * always compute determinat od hessian and spacial derivatives * this needs to be computed always as we need derivatives while computing descriptors * fixed tests of AKAZE with KAZE descriptors which have been affected by this Currently it computes all first and second order derivatives together and the determiant of the hessian. For descriptors it would be enough to compute just first order derivates, but it is not probably worth it optimize for scenario where descriptors and keypoints are computed separately, since it is already very inefficient. When computing keypoint and descriptors together it is faster to do it the current way (preserves locality). * parallelize non linear diffusion computation * do multiplication right in the nlp diffusity kernel * rework kfactor computation * get rid of sharing buffers when creating scale space pyramid, the performace impact is neglegible * features2d: initialize TBB scheduler in perf tests * ensures more stable output * more reasonable profiles, since the first call of parallel_for_ is not getting big performace hit * compute_kfactor: interleave finding of maximum and computing distance * no need to go twice through the data * start to use UMats in AKAZE to leverage OpenCl in the future * fixed bug that prevented computing determinant for scale pyramid of size 1 (just the base image) * all descriptors now support writing to uninitialized memory * use InputArray and OutputArray for input image and descriptors, allows to make use UMAt that user passes to us * enable use of all existing ocl paths in AKAZE * all parts that uses ocl-enabled functions should use ocl by now * imgproc: fix dispatching of IPP version when OCL is disabled * when OCL is disabled IPP version should be always prefered (even when the dst is UMat) * get rid of copy in DeterminantHessian response * this slows CPU version considerably * do no run in parallel when running with OCL * store derivations as UMat in pyramid * enables OCL path computing of determint hessian * will allow to compute descriptors on GPU in the future * port diffusivity to OCL * diffusivity itself is not a blocker, but this saves us downloading and uploading derivations * implement kernel for nonlinear scalar diffusion step * download the pyramid from GPU just once we don't want to downlaod matrices ad hoc from gpu when the function in AKAZE needs it. There is a HUGE mapping overhead and without shared memory support a LOT of unnecessary transfers. This maps/downloads matrices just once. * fix bug with uninitialized values in non linear diffusion * this was causing spurious segfaults in stitching tests due to propagation of NaNs * added new test, which checks for NaNs (added new debug asserts for NaNs) * valgrind now says everything is ok * add nonlinear diffusion step OCL implementation * Lt in pyramid changed to UMat, it will be downlaoded from GPU along with Lx, Ly * fix bug in pm_g2 kernel. OpenCV mangles dimensions passed to OpenCL, so we need to check for boundaries in each OCL kernel. * port computing of determinant to OCL * computing of determinant is not a blocker, but with this change we don't need to download all spatial derivatives to CPU, we only download determinant * make Ldet in the pyramid UMat, download it from CPU together with the other parts of the pyramid * add profiling macros * fix visual studio warning * instrument non_linear_diffusion * remove changes I have made to TEvolution * TEvolution is used only in KAZE now * Revert "features2d: initialize TBB scheduler in perf tests" This reverts commit ba81e2a711ae009ce3c5459775627b6423112669.
2017-08-01 20:46:01 +08:00
impl.Compute_Descriptors(keypoints, descriptors);
2014-04-05 15:25:46 +08:00
Merge pull request #8951 from hrnr:akaze_part2 [GSOC] Speeding-up AKAZE, part #2 (#8951) * feature2d: instrument more functions used in AKAZE * rework Compute_Determinant_Hessian_Response * this takes 84% of time of Feature_Detection * run everything in parallel * compute Scharr kernels just once * compute sigma more efficiently * allocate all matrices in evolution without zeroing * features2d: add one bigger image to tests * now test have images: 600x768, 900x600 and 1385x700 to cover different resolutions * explicitly zero Lx and Ly * add Lflow and Lstep to evolution as in original AKAZE code * reworked computing keypoints orientation integrated faster function from https://github.com/h2suzuki/fast_akaze * use standard fastAtan2 instead of getAngle * compute keypoints orientation in parallel * fix visual studio warnings * replace some wrapped functions with direct calls to OpenCV functions * improved readability for people familiar with opencv * do not same image twice in base level * rework diffusity stencil * use one pass stencil for diffusity from https://github.com/h2suzuki/fast_akaze * improve locality in Create_Scale_Space * always compute determinat od hessian and spacial derivatives * this needs to be computed always as we need derivatives while computing descriptors * fixed tests of AKAZE with KAZE descriptors which have been affected by this Currently it computes all first and second order derivatives together and the determiant of the hessian. For descriptors it would be enough to compute just first order derivates, but it is not probably worth it optimize for scenario where descriptors and keypoints are computed separately, since it is already very inefficient. When computing keypoint and descriptors together it is faster to do it the current way (preserves locality). * parallelize non linear diffusion computation * do multiplication right in the nlp diffusity kernel * rework kfactor computation * get rid of sharing buffers when creating scale space pyramid, the performace impact is neglegible * features2d: initialize TBB scheduler in perf tests * ensures more stable output * more reasonable profiles, since the first call of parallel_for_ is not getting big performace hit * compute_kfactor: interleave finding of maximum and computing distance * no need to go twice through the data * start to use UMats in AKAZE to leverage OpenCl in the future * fixed bug that prevented computing determinant for scale pyramid of size 1 (just the base image) * all descriptors now support writing to uninitialized memory * use InputArray and OutputArray for input image and descriptors, allows to make use UMAt that user passes to us * enable use of all existing ocl paths in AKAZE * all parts that uses ocl-enabled functions should use ocl by now * imgproc: fix dispatching of IPP version when OCL is disabled * when OCL is disabled IPP version should be always prefered (even when the dst is UMat) * get rid of copy in DeterminantHessian response * this slows CPU version considerably * do no run in parallel when running with OCL * store derivations as UMat in pyramid * enables OCL path computing of determint hessian * will allow to compute descriptors on GPU in the future * port diffusivity to OCL * diffusivity itself is not a blocker, but this saves us downloading and uploading derivations * implement kernel for nonlinear scalar diffusion step * download the pyramid from GPU just once we don't want to downlaod matrices ad hoc from gpu when the function in AKAZE needs it. There is a HUGE mapping overhead and without shared memory support a LOT of unnecessary transfers. This maps/downloads matrices just once. * fix bug with uninitialized values in non linear diffusion * this was causing spurious segfaults in stitching tests due to propagation of NaNs * added new test, which checks for NaNs (added new debug asserts for NaNs) * valgrind now says everything is ok * add nonlinear diffusion step OCL implementation * Lt in pyramid changed to UMat, it will be downlaoded from GPU along with Lx, Ly * fix bug in pm_g2 kernel. OpenCV mangles dimensions passed to OpenCL, so we need to check for boundaries in each OCL kernel. * port computing of determinant to OCL * computing of determinant is not a blocker, but with this change we don't need to download all spatial derivatives to CPU, we only download determinant * make Ldet in the pyramid UMat, download it from CPU together with the other parts of the pyramid * add profiling macros * fix visual studio warning * instrument non_linear_diffusion * remove changes I have made to TEvolution * TEvolution is used only in KAZE now * Revert "features2d: initialize TBB scheduler in perf tests" This reverts commit ba81e2a711ae009ce3c5459775627b6423112669.
2017-08-01 20:46:01 +08:00
CV_Assert((descriptors.empty() || descriptors.cols() == descriptorSize()));
CV_Assert((descriptors.empty() || (descriptors.type() == descriptorType())));
}
2014-04-05 15:25:46 +08:00
}
2018-03-15 21:16:54 +08:00
void write(FileStorage& fs) const CV_OVERRIDE
{
writeFormat(fs);
fs << "descriptor" << descriptor;
fs << "descriptor_channels" << descriptor_channels;
fs << "descriptor_size" << descriptor_size;
fs << "threshold" << threshold;
fs << "octaves" << octaves;
fs << "sublevels" << sublevels;
fs << "diffusivity" << diffusivity;
}
2018-03-15 21:16:54 +08:00
void read(const FileNode& fn) CV_OVERRIDE
{
descriptor = static_cast<DescriptorType>((int)fn["descriptor"]);
descriptor_channels = (int)fn["descriptor_channels"];
descriptor_size = (int)fn["descriptor_size"];
threshold = (float)fn["threshold"];
octaves = (int)fn["octaves"];
sublevels = (int)fn["sublevels"];
diffusivity = static_cast<KAZE::DiffusivityType>((int)fn["diffusivity"]);
}
DescriptorType descriptor;
int descriptor_channels;
int descriptor_size;
float threshold;
int octaves;
int sublevels;
KAZE::DiffusivityType diffusivity;
};
Ptr<AKAZE> AKAZE::create(DescriptorType descriptor_type,
int descriptor_size, int descriptor_channels,
float threshold, int octaves,
int sublevels, KAZE::DiffusivityType diffusivity)
2014-04-05 15:25:46 +08:00
{
return makePtr<AKAZE_Impl>(descriptor_type, descriptor_size, descriptor_channels,
threshold, octaves, sublevels, diffusivity);
2014-04-05 15:25:46 +08:00
}
String AKAZE::getDefaultName() const
{
return (Feature2D::getDefaultName() + ".AKAZE");
}
2014-07-30 22:02:08 +08:00
}