From 64533009b35a293a980d28f1476d1828ad287b33 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 3 Aug 2017 13:03:08 +0300 Subject: [PATCH] akaze: optimize allocations --- modules/features2d/src/kaze/AKAZEFeatures.cpp | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/modules/features2d/src/kaze/AKAZEFeatures.cpp b/modules/features2d/src/kaze/AKAZEFeatures.cpp index dff9e440e3..9ae21641d9 100644 --- a/modules/features2d/src/kaze/AKAZEFeatures.cpp +++ b/modules/features2d/src/kaze/AKAZEFeatures.cpp @@ -1679,12 +1679,10 @@ void Upright_MLDB_Full_Descriptor_Invoker::Get_Upright_MLDB_Full_Descriptor(cons const AKAZEOptions & options = *options_; const std::vector& evolution = *evolution_; - // Matrices for the M-LDB descriptor - Mat values[3] = { - Mat(4, options.descriptor_channels, CV_32FC1), - Mat(9, options.descriptor_channels, CV_32FC1), - Mat(16, options.descriptor_channels, CV_32FC1) - }; + // Buffer for the M-LDB descriptor + const int max_channels = 3; + CV_Assert(options.descriptor_channels <= max_channels); + float values[16*max_channels]; // Get the information from the keypoint ratio = (float)(1 << kpt.octave); @@ -1739,7 +1737,7 @@ void Upright_MLDB_Full_Descriptor_Invoker::Get_Upright_MLDB_Full_Descriptor(cons dx /= nsamples; dy /= nsamples; - float *val = values[z].ptr(dcount2); + float *val = &values[dcount2*max_channels]; *(val) = di; *(val+1) = dx; *(val+2) = dy; @@ -1751,8 +1749,8 @@ void Upright_MLDB_Full_Descriptor_Invoker::Get_Upright_MLDB_Full_Descriptor(cons const int num = (z + 2) * (z + 2); for (int i = 0; i < num; i++) { for (int j = i + 1; j < num; j++) { - const float * valI = values[z].ptr(i); - const float * valJ = values[z].ptr(j); + const float * valI = &values[i*max_channels]; + const float * valJ = &values[j*max_channels]; for (int k = 0; k < 3; ++k) { if (*(valI + k) > *(valJ + k)) { desc[dcount1 / 8] |= (1 << (dcount1 % 8)); @@ -1931,7 +1929,11 @@ void MLDB_Descriptor_Subset_Invoker::Get_MLDB_Descriptor_Subset(const KeyPoint& float si = sin(angle); // Allocate memory for the matrix of values - Mat values((4 + 9 + 16)*options.descriptor_channels, 1, CV_32FC1); + // Buffer for the M-LDB descriptor + const int max_channels = 3; + const int channels = options.descriptor_channels; + CV_Assert(channels <= max_channels); + float values[(4 + 9 + 16)*max_channels]; // Sample everything, but only do the comparisons const int pattern_size = options.descriptor_pattern_size; @@ -1978,23 +1980,23 @@ void MLDB_Descriptor_Subset_Invoker::Get_MLDB_Descriptor_Subset(const KeyPoint& } } - *(values.ptr(options.descriptor_channels*i)) = di; + float* pValues = &values[channels * i]; + pValues[0] = di; - if (options.descriptor_channels == 2) { - *(values.ptr(options.descriptor_channels*i + 1)) = dx; + if (channels == 2) { + pValues[1] = dx; } - else if (options.descriptor_channels == 3) { - *(values.ptr(options.descriptor_channels*i + 1)) = dx; - *(values.ptr(options.descriptor_channels*i + 2)) = dy; + else if (channels == 3) { + pValues[1] = dx; + pValues[2] = dy; } } // Do the comparisons - const float *vals = values.ptr(0); const int *comps = descriptorBits_.ptr(0); for (int i = 0; i vals[comps[2 * i + 1]]) { + if (values[comps[2 * i]] > values[comps[2 * i + 1]]) { desc[i / 8] |= (1 << (i % 8)); } else { desc[i / 8] &= ~(1 << (i % 8));