Yash Chainani | 5458dea | 2022-06-29 21:05:02 -0700 | [diff] [blame] | 1 | /*M/////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. |
| 4 | // |
| 5 | // By downloading, copying, installing or using the software you agree to this |
| 6 | license. |
| 7 | // If you do not agree to this license, do not download, install, |
| 8 | // copy or use the software. |
| 9 | // |
| 10 | // |
| 11 | // License Agreement |
| 12 | // For Open Source Computer Vision Library |
| 13 | // |
| 14 | // Copyright (C) 2008, Willow Garage Inc., all rights reserved. |
| 15 | // Third party copyrights are property of their respective owners. |
| 16 | // |
| 17 | // Redistribution and use in source and binary forms, with or without |
| 18 | modification, |
| 19 | // are permitted provided that the following conditions are met: |
| 20 | // |
| 21 | // * Redistribution's of source code must retain the above copyright notice, |
| 22 | // this list of conditions and the following disclaimer. |
| 23 | // |
| 24 | // * Redistribution's in binary form must reproduce the above copyright |
| 25 | notice, |
| 26 | // this list of conditions and the following disclaimer in the documentation |
| 27 | // and/or other materials provided with the distribution. |
| 28 | // |
| 29 | // * The name of Intel Corporation may not be used to endorse or promote |
| 30 | products |
| 31 | // derived from this software without specific prior written permission. |
| 32 | // |
| 33 | // This software is provided by the copyright holders and contributors "as is" |
| 34 | and |
| 35 | // any express or implied warranties, including, but not limited to, the implied |
| 36 | // warranties of merchantability and fitness for a particular purpose are |
| 37 | disclaimed. |
| 38 | // In no event shall the Intel Corporation or contributors be liable for any |
| 39 | direct, |
| 40 | // indirect, incidental, special, exemplary, or consequential damages |
| 41 | // (including, but not limited to, procurement of substitute goods or services; |
| 42 | // loss of use, data, or profits; or business interruption) however caused |
| 43 | // and on any theory of liability, whether in contract, strict liability, |
| 44 | // or tort (including negligence or otherwise) arising in any way out of |
| 45 | // the use of this software, even if advised of the possibility of such damage. |
| 46 | // |
| 47 | //M*/ |
| 48 | |
| 49 | /* |
| 50 | OpenCV wrapper of reference implementation of |
| 51 | [1] Fast Explicit Diffusion for Accelerated Features in Nonlinear Scale Spaces. |
| 52 | Pablo F. Alcantarilla, J. Nuevo and Adrien Bartoli. |
| 53 | In British Machine Vision Conference (BMVC), Bristol, UK, September 2013 |
| 54 | http://www.robesafe.com/personal/pablo.alcantarilla/papers/Alcantarilla13bmvc.pdf |
| 55 | @author Eugene Khvedchenya <ekhvedchenya@gmail.com> |
| 56 | */ |
| 57 | |
| 58 | #include "akaze.h" // Define AKAZE2; included in place of <opencv2/features2d.hpp> |
| 59 | |
| 60 | #include <iostream> |
| 61 | #include <opencv2/core.hpp> |
| 62 | #include <opencv2/imgproc.hpp> |
| 63 | |
| 64 | #include "AKAZEFeatures.h" |
| 65 | |
| 66 | namespace cv { |
| 67 | using namespace std; |
| 68 | |
| 69 | class AKAZE_Impl2 : public AKAZE2 { |
| 70 | public: |
| 71 | AKAZE_Impl2(int _descriptor_type, int _descriptor_size, |
| 72 | int _descriptor_channels, float _threshold, int _octaves, |
| 73 | int _sublevels, int _diffusivity) |
| 74 | : descriptor(_descriptor_type), |
| 75 | descriptor_channels(_descriptor_channels), |
| 76 | descriptor_size(_descriptor_size), |
| 77 | threshold(_threshold), |
| 78 | octaves(_octaves), |
| 79 | sublevels(_sublevels), |
| 80 | diffusivity(_diffusivity), |
| 81 | img_width(-1), |
| 82 | img_height(-1) { |
| 83 | cout << "AKAZE_Impl2 constructor called" << endl; |
| 84 | } |
| 85 | |
| 86 | virtual ~AKAZE_Impl2() {} |
| 87 | |
| 88 | void setDescriptorType(int dtype_) { |
| 89 | descriptor = dtype_; |
| 90 | impl.release(); |
| 91 | } |
| 92 | int getDescriptorType() const { return descriptor; } |
| 93 | |
| 94 | void setDescriptorSize(int dsize_) { |
| 95 | descriptor_size = dsize_; |
| 96 | impl.release(); |
| 97 | } |
| 98 | int getDescriptorSize() const { return descriptor_size; } |
| 99 | |
| 100 | void setDescriptorChannels(int dch_) { |
| 101 | descriptor_channels = dch_; |
| 102 | impl.release(); |
| 103 | } |
| 104 | int getDescriptorChannels() const { return descriptor_channels; } |
| 105 | |
| 106 | void setThreshold(double th_) { |
| 107 | threshold = (float)th_; |
| 108 | if (!impl.empty()) impl->setThreshold(th_); |
| 109 | } |
| 110 | double getThreshold() const { return threshold; } |
| 111 | |
| 112 | void setNOctaves(int octaves_) { |
| 113 | octaves = octaves_; |
| 114 | impl.release(); |
| 115 | } |
| 116 | int getNOctaves() const { return octaves; } |
| 117 | |
| 118 | void setNOctaveLayers(int octaveLayers_) { |
| 119 | sublevels = octaveLayers_; |
| 120 | impl.release(); |
| 121 | } |
| 122 | int getNOctaveLayers() const { return sublevels; } |
| 123 | |
| 124 | void setDiffusivity(int diff_) { |
| 125 | diffusivity = diff_; |
| 126 | if (!impl.empty()) impl->setDiffusivity(diff_); |
| 127 | } |
| 128 | int getDiffusivity() const { return diffusivity; } |
| 129 | |
| 130 | // returns the descriptor size in bytes |
| 131 | int descriptorSize() const { |
| 132 | switch (descriptor) { |
| 133 | case DESCRIPTOR_KAZE: |
| 134 | case DESCRIPTOR_KAZE_UPRIGHT: |
| 135 | return 64; |
| 136 | |
| 137 | case DESCRIPTOR_MLDB: |
| 138 | case DESCRIPTOR_MLDB_UPRIGHT: |
| 139 | // We use the full length binary descriptor -> 486 bits |
| 140 | if (descriptor_size == 0) { |
| 141 | int t = (6 + 36 + 120) * descriptor_channels; |
| 142 | return (int)ceil(t / 8.); |
| 143 | } else { |
| 144 | // We use the random bit selection length binary descriptor |
| 145 | return (int)ceil(descriptor_size / 8.); |
| 146 | } |
| 147 | |
| 148 | default: |
| 149 | return -1; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // returns the descriptor type |
| 154 | int descriptorType() const { |
| 155 | switch (descriptor) { |
| 156 | case DESCRIPTOR_KAZE: |
| 157 | case DESCRIPTOR_KAZE_UPRIGHT: |
| 158 | return CV_32F; |
| 159 | |
| 160 | case DESCRIPTOR_MLDB: |
| 161 | case DESCRIPTOR_MLDB_UPRIGHT: |
| 162 | return CV_8U; |
| 163 | |
| 164 | default: |
| 165 | return -1; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // returns the default norm type |
| 170 | int defaultNorm() const { |
| 171 | switch (descriptor) { |
| 172 | case DESCRIPTOR_KAZE: |
| 173 | case DESCRIPTOR_KAZE_UPRIGHT: |
| 174 | return NORM_L2; |
| 175 | |
| 176 | case DESCRIPTOR_MLDB: |
| 177 | case DESCRIPTOR_MLDB_UPRIGHT: |
| 178 | return NORM_HAMMING; |
| 179 | |
| 180 | default: |
| 181 | return -1; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void detectAndCompute(InputArray image, InputArray mask, |
| 186 | std::vector<KeyPoint>& keypoints, |
| 187 | OutputArray descriptors, bool useProvidedKeypoints) { |
| 188 | Mat img = image.getMat(); |
| 189 | |
| 190 | if (img_width != img.cols) { |
| 191 | img_width = img.cols; |
| 192 | impl.release(); |
| 193 | } |
| 194 | |
| 195 | if (img_height != img.rows) { |
| 196 | img_height = img.rows; |
| 197 | impl.release(); |
| 198 | } |
| 199 | |
| 200 | if (impl.empty()) { |
| 201 | AKAZEOptionsV2 options; |
| 202 | options.descriptor = descriptor; |
| 203 | options.descriptor_channels = descriptor_channels; |
| 204 | options.descriptor_size = descriptor_size; |
| 205 | options.img_width = img_width; |
| 206 | options.img_height = img_height; |
| 207 | options.dthreshold = threshold; |
| 208 | options.omax = octaves; |
| 209 | options.nsublevels = sublevels; |
| 210 | options.diffusivity = diffusivity; |
| 211 | |
| 212 | impl = makePtr<AKAZEFeaturesV2>(options); |
| 213 | } |
| 214 | |
| 215 | impl->Create_Nonlinear_Scale_Space(img); |
| 216 | |
| 217 | if (!useProvidedKeypoints) { |
| 218 | impl->Feature_Detection(keypoints); |
| 219 | } |
| 220 | |
| 221 | if (!mask.empty()) { |
| 222 | KeyPointsFilter::runByPixelsMask(keypoints, mask.getMat()); |
| 223 | } |
| 224 | |
| 225 | if (descriptors.needed()) { |
| 226 | Mat& desc = descriptors.getMatRef(); |
| 227 | impl->Compute_Descriptors(keypoints, desc); |
| 228 | |
| 229 | CV_Assert((!desc.rows || desc.cols == descriptorSize())); |
| 230 | CV_Assert((!desc.rows || (desc.type() == descriptorType()))); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void write(FileStorage& fs) const { |
| 235 | fs << "descriptor" << descriptor; |
| 236 | fs << "descriptor_channels" << descriptor_channels; |
| 237 | fs << "descriptor_size" << descriptor_size; |
| 238 | fs << "threshold" << threshold; |
| 239 | fs << "octaves" << octaves; |
| 240 | fs << "sublevels" << sublevels; |
| 241 | fs << "diffusivity" << diffusivity; |
| 242 | } |
| 243 | |
| 244 | void read(const FileNode& fn) { |
| 245 | descriptor = (int)fn["descriptor"]; |
| 246 | descriptor_channels = (int)fn["descriptor_channels"]; |
| 247 | descriptor_size = (int)fn["descriptor_size"]; |
| 248 | threshold = (float)fn["threshold"]; |
| 249 | octaves = (int)fn["octaves"]; |
| 250 | sublevels = (int)fn["sublevels"]; |
| 251 | diffusivity = (int)fn["diffusivity"]; |
| 252 | } |
| 253 | |
| 254 | Ptr<AKAZEFeaturesV2> impl; |
| 255 | int descriptor; |
| 256 | int descriptor_channels; |
| 257 | int descriptor_size; |
| 258 | float threshold; |
| 259 | int octaves; |
| 260 | int sublevels; |
| 261 | int diffusivity; |
| 262 | int img_width; |
| 263 | int img_height; |
| 264 | }; |
| 265 | |
| 266 | Ptr<AKAZE2> AKAZE2::create(int descriptor_type, int descriptor_size, |
| 267 | int descriptor_channels, float threshold, |
| 268 | int octaves, int sublevels, int diffusivity) { |
| 269 | return makePtr<AKAZE_Impl2>(descriptor_type, descriptor_size, |
| 270 | descriptor_channels, threshold, octaves, |
| 271 | sublevels, diffusivity); |
| 272 | } |
| 273 | } // namespace cv |