akaze.cpp 8.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/*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>
*/

51
#include "precomp.hpp"
52 53 54
#include "kaze/AKAZEFeatures.h"

#include <iostream>
55 56 57

namespace cv
{
58
    using namespace std;
59

60 61 62 63
    class AKAZE_Impl : public AKAZE
    {
    public:
        AKAZE_Impl(int _descriptor_type, int _descriptor_size, int _descriptor_channels,
64
                 float _threshold, int _octaves, int _sublevels, int _diffusivity)
65
        : descriptor(_descriptor_type)
66
        , descriptor_channels(_descriptor_channels)
67
        , descriptor_size(_descriptor_size)
68 69 70 71
        , threshold(_threshold)
        , octaves(_octaves)
        , sublevels(_sublevels)
        , diffusivity(_diffusivity)
72
        {
73
        }
74

75 76
        virtual ~AKAZE_Impl()
        {
77

78 79
        }

80 81 82 83 84 85 86 87 88
        void setDescriptorType(int dtype) { descriptor = dtype; }
        int getDescriptorType() const { return descriptor; }

        void setDescriptorSize(int dsize) { descriptor_size = dsize; }
        int getDescriptorSize() const { return descriptor_size; }

        void setDescriptorChannels(int dch) { descriptor_channels = dch; }
        int getDescriptorChannels() const { return descriptor_channels; }

89
        void setThreshold(double threshold_) { threshold = (float)threshold_; }
90 91 92 93 94 95 96 97 98 99 100
        double getThreshold() const { return threshold; }

        void setNOctaves(int octaves_) { octaves = octaves_; }
        int getNOctaves() const { return octaves; }

        void setNOctaveLayers(int octaveLayers_) { sublevels = octaveLayers_; }
        int getNOctaveLayers() const { return sublevels; }

        void setDiffusivity(int diff_) { diffusivity = diff_; }
        int getDiffusivity() const { return diffusivity; }

101 102
        // returns the descriptor size in bytes
        int descriptorSize() const
103
        {
104 105 106 107 108 109 110 111 112 113 114 115
            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;
116
                    return divUp(t, 8);
117 118 119 120
                }
                else
                {
                    // We use the random bit selection length binary descriptor
121
                    return divUp(descriptor_size, 8);
122
                }
123 124 125

            default:
                return -1;
126
            }
127 128
        }

129 130
        // returns the descriptor type
        int descriptorType() const
131
        {
132 133 134 135 136
            switch (descriptor)
            {
            case DESCRIPTOR_KAZE:
            case DESCRIPTOR_KAZE_UPRIGHT:
                    return CV_32F;
137

138 139 140
            case DESCRIPTOR_MLDB:
            case DESCRIPTOR_MLDB_UPRIGHT:
                    return CV_8U;
141

142 143 144
                default:
                    return -1;
            }
145 146
        }

147 148 149 150 151 152 153 154
        // returns the default norm type
        int defaultNorm() const
        {
            switch (descriptor)
            {
            case DESCRIPTOR_KAZE:
            case DESCRIPTOR_KAZE_UPRIGHT:
                return NORM_L2;
155

156 157 158
            case DESCRIPTOR_MLDB:
            case DESCRIPTOR_MLDB_UPRIGHT:
                return NORM_HAMMING;
159

160 161 162
            default:
                return -1;
            }
163 164
        }

165 166 167 168
        void detectAndCompute(InputArray image, InputArray mask,
                              std::vector<KeyPoint>& keypoints,
                              OutputArray descriptors,
                              bool useProvidedKeypoints)
169
        {
170 171
            CV_INSTRUMENT_REGION()

172
            CV_Assert( ! image.empty() );
173 174 175 176 177

            AKAZEOptions options;
            options.descriptor = descriptor;
            options.descriptor_channels = descriptor_channels;
            options.descriptor_size = descriptor_size;
178 179
            options.img_width = image.cols();
            options.img_height = image.rows();
180 181 182 183 184 185
            options.dthreshold = threshold;
            options.omax = octaves;
            options.nsublevels = sublevels;
            options.diffusivity = diffusivity;

            AKAZEFeatures impl(options);
186
            impl.Create_Nonlinear_Scale_Space(image);
187 188 189 190 191

            if (!useProvidedKeypoints)
            {
                impl.Feature_Detection(keypoints);
            }
192

193 194 195 196
            if (!mask.empty())
            {
                KeyPointsFilter::runByPixelsMask(keypoints, mask.getMat());
            }
197

198
            if(descriptors.needed())
199
            {
200
                impl.Compute_Descriptors(keypoints, descriptors);
201

202 203
                CV_Assert((descriptors.empty() || descriptors.cols() == descriptorSize()));
                CV_Assert((descriptors.empty() || (descriptors.type() == descriptorType())));
204
            }
205 206
        }

207 208
        void write(FileStorage& fs) const
        {
209
            writeFormat(fs);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
            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;
        }

        void read(const FileNode& fn)
        {
            descriptor = (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 = (int)fn["diffusivity"];
        }

230 231 232 233 234 235 236 237 238 239 240 241 242
        int descriptor;
        int descriptor_channels;
        int descriptor_size;
        float threshold;
        int octaves;
        int sublevels;
        int diffusivity;
    };

    Ptr<AKAZE> AKAZE::create(int descriptor_type,
                             int descriptor_size, int descriptor_channels,
                             float threshold, int octaves,
                             int sublevels, int diffusivity)
243
    {
244 245
        return makePtr<AKAZE_Impl>(descriptor_type, descriptor_size, descriptor_channels,
                                   threshold, octaves, sublevels, diffusivity);
246
    }
247 248 249 250 251 252

    String AKAZE::getDefaultName() const
    {
        return (Feature2D::getDefaultName() + ".AKAZE");
    }

253
}