From 9f4f6f0ab8940b879da5407c06ab682681204a48 Mon Sep 17 00:00:00 2001 From: Josh Klontz Date: Tue, 31 Aug 2021 12:45:50 -0600 Subject: [PATCH] removed plugins not supported by opencv4 --- openbr/plugins/classification/forest.cpp | 284 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- openbr/plugins/cmake/opencv4.cmake | 4 ---- openbr/plugins/imgproc/custom_sift.cpp | 464 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- openbr/plugins/imgproc/keypointdescriptor.cpp | 67 ------------------------------------------------------------------- openbr/plugins/metadata/keypointdetector.cpp | 71 ----------------------------------------------------------------------- 5 files changed, 0 insertions(+), 890 deletions(-) delete mode 100644 openbr/plugins/classification/forest.cpp delete mode 100644 openbr/plugins/cmake/opencv4.cmake delete mode 100644 openbr/plugins/imgproc/custom_sift.cpp delete mode 100644 openbr/plugins/imgproc/keypointdescriptor.cpp delete mode 100644 openbr/plugins/metadata/keypointdetector.cpp diff --git a/openbr/plugins/classification/forest.cpp b/openbr/plugins/classification/forest.cpp deleted file mode 100644 index d033635..0000000 --- a/openbr/plugins/classification/forest.cpp +++ /dev/null @@ -1,284 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Copyright 2012 The MITRE Corporation * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include -#include - -using namespace cv; - -namespace br -{ - -/*! - * \ingroup transforms - * \brief Wraps OpenCV's random trees framework - * \author Scott Klum \cite sklum - * \br_link http://docs.opencv.org/modules/ml/doc/random_trees.html - * \br_property bool classification If true the labels are expected to be categorical. Otherwise they are expected to be numerical. Default is true. - * \br_property float splitPercentage Used to calculate the minimum number of samples per split in a random tree. The minimum number of samples is calculated as the number of samples x splitPercentage. Default is 0.01. - * \br_property int maxDepth The maximum depth of each decision tree. Default is std::numeric_limits::max() and typically should be set by the user. - * \br_property int maxTrees The maximum number of trees in the forest. Default is 10. - * \br_property float forestAccuracy A sufficient accuracy for the forest for training to terminate. Used if termCrit is EPS or Both. Default is 0.1. - * \br_property bool returnConfidence If both classification and returnConfidence are use a fuzzy class label as the output of the forest. Default is true. - * \br_property bool overwriteMat If true set dst to be a 1x1 Mat with the forest response as its value. Otherwise append the forest response to metadata using outputVariable as a key. Default is true. - * \br_property QString inputVariable The metadata key for each templates label. Default is "Label". - * \br_property QString outputVariable The metadata key for the forest response if overwriteMat is false. Default is "". - * \br_property bool weight If true and classification is true the random forest will use prior accuracies. Default is false. - * \br_property enum termCrit Termination criteria for training the random forest. Options are Iter, EPS and Both. Iter terminates when the maximum number of trees is reached. EPS terminates when forestAccuracy is met. Both terminates when either is true. Default is Iter. - */ -class ForestTransform : public Transform -{ - Q_OBJECT - - void init() - { - forest = ml::RTrees::create(); - - if (outputVariable.isEmpty()) - outputVariable = inputVariable; - } - - void train(const TemplateList &data) - { - trainForest(data); - } - - void project(const Template &src, Template &dst) const - { - dst = src; - - float response; - if (classification && returnConfidence) { - // Fuzzy class label - response = forest.predict_prob(src.m().reshape(1,1)); - } else { - response = forest.predict(src.m().reshape(1,1)); - } - - if (overwriteMat) { - dst.m() = Mat(1, 1, CV_32F); - dst.m().at(0, 0) = response; - } else { - dst.file.set(outputVariable, response); - } - } - - void load(QDataStream &stream) - { - OpenCVUtils::loadModel(forest,stream); - } - - void store(QDataStream &stream) const - { - OpenCVUtils::storeModel(forest,stream); - } - -protected: - Q_ENUMS(TerminationCriteria) - Q_PROPERTY(bool classification READ get_classification WRITE set_classification RESET reset_classification STORED false) - Q_PROPERTY(float splitPercentage READ get_splitPercentage WRITE set_splitPercentage RESET reset_splitPercentage STORED false) - Q_PROPERTY(int maxDepth READ get_maxDepth WRITE set_maxDepth RESET reset_maxDepth STORED false) - Q_PROPERTY(int maxTrees READ get_maxTrees WRITE set_maxTrees RESET reset_maxTrees STORED false) - Q_PROPERTY(float forestAccuracy READ get_forestAccuracy WRITE set_forestAccuracy RESET reset_forestAccuracy STORED false) - Q_PROPERTY(bool returnConfidence READ get_returnConfidence WRITE set_returnConfidence RESET reset_returnConfidence STORED false) - Q_PROPERTY(bool overwriteMat READ get_overwriteMat WRITE set_overwriteMat RESET reset_overwriteMat STORED false) - Q_PROPERTY(QString inputVariable READ get_inputVariable WRITE set_inputVariable RESET reset_inputVariable STORED false) - Q_PROPERTY(QString outputVariable READ get_outputVariable WRITE set_outputVariable RESET reset_outputVariable STORED false) - Q_PROPERTY(bool weight READ get_weight WRITE set_weight RESET reset_weight STORED false) - Q_PROPERTY(TerminationCriteria termCrit READ get_termCrit WRITE set_termCrit RESET reset_termCrit STORED false) - -public: - enum TerminationCriteria { Iter = TermCriteria::ITER, - EPS = TermCriteria::EPS, - Both = TermCriteria::EPS | TermCriteria::ITER}; - -protected: - BR_PROPERTY(bool, classification, true) - BR_PROPERTY(float, splitPercentage, .01) - BR_PROPERTY(int, maxDepth, std::numeric_limits::max()) - BR_PROPERTY(int, maxTrees, 10) - BR_PROPERTY(float, forestAccuracy, .1) - BR_PROPERTY(bool, returnConfidence, true) - BR_PROPERTY(bool, overwriteMat, true) - BR_PROPERTY(QString, inputVariable, "Label") - BR_PROPERTY(QString, outputVariable, "") - BR_PROPERTY(bool, weight, false) - BR_PROPERTY(TerminationCriteria, termCrit, Iter) - - Ptr forest; - - void trainForest(const TemplateList &data) - { - Mat samples = OpenCVUtils::toMat(data.data()); - Mat labels = OpenCVUtils::toMat(File::get(data, inputVariable)); - - Mat types = Mat(samples.cols + 1, 1, CV_8U); - types.setTo(Scalar(ml::VAR_NUMERICAL)); - - if (classification) { - types.at(samples.cols, 0) = ml::VAR_CATEGORICAL; - } else { - types.at(samples.cols, 0) = ml::VAR_NUMERICAL; - } - - forest->setMaxDepth(maxDepth); - forest->setMinSampleCount(data.size() * splitPercentage); - forest->setRegressionAccuracy(0); - forest->setUseSurrogates(false); - forest->setMaxCategories(2); - forest->setCalculateVarImportance(false); - forest->setActiveVarCount(0); - forest->setTermCriteria(TermCriteria(termCrit, 1000, forestAccuracy)); - - bool usePrior = classification && weight; - if (usePrior) { - int nonZero = countNonZero(labels); - - cv::Mat priors(1, 2, CV_32FC1); - priors.at(0, 0) = 1; - priors.at(0, 1) = (float)(samples.rows-nonZero)/nonZero; - - forest->setPriors(priors); - } - - forest->train(ml::TrainData::create(samples, ml::ROW_SAMPLE, labels, noArray(), noArray(), noArray(), types)); - - if (Globals->verbose) { - qDebug() << "Number of trees:" << forest->getRoots().size(); - - if (classification) { - QTime timer; - timer.start(); - int correctClassification = 0; - float regressionError = 0; - for (int i=0; ipredict(samples.row(i), noArray(), ml::StatModel::RAW_OUTPUT); - float label = forest->predict(samples.row(i); - if (label == labels.at(i,0)) { - correctClassification++; - } - regressionError += fabs(prediction-labels.at(i,0)); - } - - qDebug("Time to classify %d samples: %d ms\n \ - Classification Accuracy: %f\n \ - MAE: %f\n \ - Sample dimensionality: %d", - samples.rows,timer.elapsed(),(float)correctClassification/samples.rows,regressionError/samples.rows,samples.cols); - } - } - } -}; - -BR_REGISTER(Transform, ForestTransform) - -/*! - * \ingroup transforms - * \brief Wraps OpenCV's random trees framework to induce features - * \author Scott Klum \cite sklum - * \br_link https://lirias.kuleuven.be/bitstream/123456789/316661/1/icdm11-camready.pdf - * \br_property bool useRegressionValue SCOTT FILL ME IN. - */ -class ForestInductionTransform : public ForestTransform -{ - Q_OBJECT - Q_PROPERTY(bool useRegressionValue READ get_useRegressionValue WRITE set_useRegressionValue RESET reset_useRegressionValue STORED false) - BR_PROPERTY(bool, useRegressionValue, false) - - int totalSize; - QList< QList > nodes; - - void fillNodes() - { - for (int i=0; i()); - const CvDTreeNode* node = forest.get_tree(i)->get_root(); - - // traverse the tree and save all the nodes in depth-first order - for(;;) - { - CvDTreeNode* parent; - for(;;) - { - if( !node->left ) - break; - node = node->left; - } - - nodes.last().append(node); - - for( parent = node->parent; parent && parent->right == node; - node = parent, parent = parent->parent ) - ; - - if( !parent ) - break; - - node = parent->right; - } - - totalSize += nodes.last().size(); - } - } - - void train(const TemplateList &data) - { - trainForest(data); - if (!useRegressionValue) fillNodes(); - } - - void project(const Template &src, Template &dst) const - { - dst = src; - - Mat responses; - - if (useRegressionValue) { - responses = Mat::zeros(forest.get_tree_count(),1,CV_32F); - for (int i=0; i(i,0) = forest.get_tree(i)->predict(src.m().reshape(1,1))->value; - } - } else { - responses = Mat::zeros(totalSize,1,CV_32F); - int offset = 0; - for (int i=0; ipredict(src.m().reshape(1,1))); - responses.at(offset+index,0) = 1; - offset += nodes[i].size(); - } - } - - dst.m() = responses; - } - - void load(QDataStream &stream) - { - OpenCVUtils::loadModel(forest,stream); - if (!useRegressionValue) fillNodes(); - - } - - void store(QDataStream &stream) const - { - OpenCVUtils::storeModel(forest,stream); - } -}; - -BR_REGISTER(Transform, ForestInductionTransform) - -} // namespace br - -#include "classification/forest.moc" diff --git a/openbr/plugins/cmake/opencv4.cmake b/openbr/plugins/cmake/opencv4.cmake deleted file mode 100644 index 92a5273..0000000 --- a/openbr/plugins/cmake/opencv4.cmake +++ /dev/null @@ -1,4 +0,0 @@ -set(BR_EXCLUDED_PLUGINS ${BR_EXCLUDED_PLUGINS} plugins/classification/forest.cpp - plugins/imgproc/custom_sift.cpp - plugins/imgproc/keypointdescriptor.cpp - plugins/metadata/keypointdetector.cpp) diff --git a/openbr/plugins/imgproc/custom_sift.cpp b/openbr/plugins/imgproc/custom_sift.cpp deleted file mode 100644 index 429fa86..0000000 --- a/openbr/plugins/imgproc/custom_sift.cpp +++ /dev/null @@ -1,464 +0,0 @@ -/*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) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009, 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 the copyright holders 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*/ - -/**********************************************************************************************\ - Implementation of SIFT is based on the code from http://blogs.oregonstate.edu/hess/code/sift/ - Below is the original copyright. - -// Copyright (c) 2006-2010, Rob Hess -// All rights reserved. - -// The following patent has been issued for methods embodied in this -// software: "Method and apparatus for identifying scale invariant features -// in an image and use of same for locating an object in an image," David -// G. Lowe, US Patent 6,711,293 (March 23, 2004). Provisional application -// filed March 8, 1999. Asignee: The University of British Columbia. For -// further details, contact David Lowe (lowe@cs.ubc.ca) or the -// University-Industry Liaison Office of the University of British -// Columbia. - -// Note that restrictions imposed by this patent (and possibly others) -// exist independently of and may be in conflict with the freedoms granted -// in this license, which refers to copyright of the program, not patents -// for any methods that it implements. Both copyright and patent law must -// be obeyed to legally use and redistribute this program and it is not the -// purpose of this license to induce you to infringe any patents or other -// property right claims or to contest validity of any such claims. If you -// redistribute or use the program, then this license merely protects you -// from committing copyright infringement. It does not protect you from -// committing patent infringement. So, before you do anything with this -// program, make sure that you have permission to do so not merely in terms -// of copyright, but also in terms of patent law. - -// Please note that this license is not to be understood as a guarantee -// either. If you use the program according to this license, but in -// conflict with patent law, it does not mean that the licensor will refund -// you for any losses that you incur if you are sued for your patent -// infringement. - -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// * Redistributions of source code must retain the above copyright and -// patent notices, this list of conditions and the following -// disclaimer. -// * Redistributions 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. -// * Neither the name of Oregon State University nor the names of its -// contributors may 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 COPYRIGHT -// HOLDER 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. -\**********************************************************************************************/ - -#include -#include -#include -#include - -using namespace cv; - -/******************************* Defs and macros *****************************/ - -// determines the size of a single descriptor orientation histogram -static const float SIFT_DESCR_SCL_FCTR = 3.f; - -// threshold on magnitude of elements of descriptor vector -static const float SIFT_DESCR_MAG_THR = 0.2f; - -// factor used to convert floating-point descriptor to unsigned char -static const float SIFT_INT_DESCR_FCTR = 512.f; - -// intermediate type used for DoG pyramids -typedef short sift_wt; -static const int SIFT_FIXPT_SCALE = 48; - -static inline void unpackOctave(const KeyPoint &kpt, int &octave, int &layer, float &scale) -{ - octave = kpt.octave & 255; - layer = (kpt.octave >> 8) & 255; - octave = octave < 128 ? octave : (-128 | octave); - scale = octave >= 0 ? 1.f/(1 << octave) : (float)(1 << -octave); -} - -static Mat createInitialImage( const Mat& img, bool doubleImageSize, float sigma) -{ - Mat gray, gray_fpt; - if( img.channels() == 3 || img.channels() == 4 ) - cvtColor(img, gray, COLOR_BGR2GRAY); - else - img.copyTo(gray); - gray.convertTo(gray_fpt, DataType::type, SIFT_FIXPT_SCALE, 0); - - float sig_diff; - - if( doubleImageSize ) - { - sig_diff = sqrtf( std::max(sigma * sigma, 0.01f) ); - Mat dbl; - resize(gray_fpt, dbl, Size(gray.cols*2, gray.rows*2), 0, 0, INTER_LINEAR); - GaussianBlur(dbl, dbl, Size(), sig_diff, sig_diff); - return dbl; - } - else - { - sig_diff = sqrtf( std::max(sigma * sigma, 0.01f) ); - GaussianBlur(gray_fpt, gray_fpt, Size(), sig_diff, sig_diff); - return gray_fpt; - } -} - - -static void buildGaussianPyramid( const Mat& base, vector& pyr, int nOctaves, int nOctaveLayers, double sigma ) -{ - vector sig(nOctaveLayers + 3); - pyr.resize(nOctaves*(nOctaveLayers + 3)); - - // precompute Gaussian sigmas using the following formula: - // \sigma_{total}^2 = \sigma_{i}^2 + \sigma_{i-1}^2 - sig[0] = sigma; - double k = pow( 2., 1. / nOctaveLayers ); - for( int i = 1; i < nOctaveLayers + 3; i++ ) - { - double sig_prev = pow(k, (double)(i-1))*sigma; - double sig_total = sig_prev*k; - sig[i] = std::sqrt(sig_total*sig_total - sig_prev*sig_prev); - } - - for( int o = 0; o < nOctaves; o++ ) - { - for( int i = 0; i < nOctaveLayers + 3; i++ ) - { - Mat& dst = pyr[o*(nOctaveLayers + 3) + i]; - if( o == 0 && i == 0 ) - dst = base; - // base of new octave is halved image from end of previous octave - else if( i == 0 ) - { - const Mat& src = pyr[(o-1)*(nOctaveLayers + 3) + nOctaveLayers]; - resize(src, dst, Size(src.cols/2, src.rows/2), - 0, 0, INTER_NEAREST); - } - else - { - const Mat& src = pyr[o*(nOctaveLayers + 3) + i-1]; - GaussianBlur(src, dst, Size(), sig[i], sig[i]); - } - } - } -} - - -static void buildDoGPyramid( const vector& gpyr, vector& dogpyr, int nOctaveLayers ) -{ - int nOctaves = (int)gpyr.size()/(nOctaveLayers + 3); - dogpyr.resize( nOctaves*(nOctaveLayers + 2) ); - - for( int o = 0; o < nOctaves; o++ ) - { - for( int i = 0; i < nOctaveLayers + 2; i++ ) - { - const Mat& src1 = gpyr[o*(nOctaveLayers + 3) + i]; - const Mat& src2 = gpyr[o*(nOctaveLayers + 3) + i + 1]; - Mat& dst = dogpyr[o*(nOctaveLayers + 2) + i]; - subtract(src2, src1, dst, noArray(), DataType::type); - } - } -} - -static void calcSIFTDescriptor( const Mat& img, Point2f ptf, float ori, float scl, - int d, int n, float* dst ) -{ - Point pt(cvRound(ptf.x), cvRound(ptf.y)); - float cos_t = cosf(ori*(float)(CV_PI/180)); - float sin_t = sinf(ori*(float)(CV_PI/180)); - float bins_per_rad = n / 360.f; - float exp_scale = -1.f/(d * d * 0.5f); - float hist_width = SIFT_DESCR_SCL_FCTR * scl; - int radius = cvRound(hist_width * 1.4142135623730951f * (d + 1) * 0.5f); - // Clip the radius to the diagonal of the image to avoid autobuffer too large exception - radius = std::min(radius, (int) sqrt((double) img.cols*img.cols + img.rows*img.rows)); - cos_t /= hist_width; - sin_t /= hist_width; - - int i, j, k, len = (radius*2+1)*(radius*2+1), histlen = (d+2)*(d+2)*(n+2); - int rows = img.rows, cols = img.cols; - - AutoBuffer buf(len*6 + histlen); - float *X = buf, *Y = X + len, *Mag = Y, *Ori = Mag + len, *W = Ori + len; - float *RBin = W + len, *CBin = RBin + len, *hist = CBin + len; - - for( i = 0; i < d+2; i++ ) - { - for( j = 0; j < d+2; j++ ) - for( k = 0; k < n+2; k++ ) - hist[(i*(d+2) + j)*(n+2) + k] = 0.; - } - - for( i = -radius, k = 0; i <= radius; i++ ) - for( j = -radius; j <= radius; j++ ) - { - // Calculate sample's histogram array coords rotated relative to ori. - // Subtract 0.5 so samples that fall e.g. in the center of row 1 (i.e. - // r_rot = 1.5) have full weight placed in row 1 after interpolation. - float c_rot = j * cos_t - i * sin_t; - float r_rot = j * sin_t + i * cos_t; - float rbin = r_rot + d/2 - 0.5f; - float cbin = c_rot + d/2 - 0.5f; - int r = pt.y + i, c = pt.x + j; - - if( rbin > -1 && rbin < d && cbin > -1 && cbin < d && - r > 0 && r < rows - 1 && c > 0 && c < cols - 1 ) - { - float dx = (float)(img.at(r, c+1) - img.at(r, c-1)); - float dy = (float)(img.at(r-1, c) - img.at(r+1, c)); - X[k] = dx; Y[k] = dy; RBin[k] = rbin; CBin[k] = cbin; - W[k] = (c_rot * c_rot + r_rot * r_rot)*exp_scale; - k++; - } - } - - len = k; - fastAtan2(Y, X, Ori, len, true); - magnitude(X, Y, Mag, len); - exp(W, W, len); - - for( k = 0; k < len; k++ ) - { - float rbin = RBin[k], cbin = CBin[k]; - float obin = (Ori[k] - ori)*bins_per_rad; - float mag = Mag[k]*W[k]; - - int r0 = cvFloor( rbin ); - int c0 = cvFloor( cbin ); - int o0 = cvFloor( obin ); - rbin -= r0; - cbin -= c0; - obin -= o0; - - if( o0 < 0 ) - o0 += n; - if( o0 >= n ) - o0 -= n; - - // histogram update using tri-linear interpolation - float v_r1 = mag*rbin, v_r0 = mag - v_r1; - float v_rc11 = v_r1*cbin, v_rc10 = v_r1 - v_rc11; - float v_rc01 = v_r0*cbin, v_rc00 = v_r0 - v_rc01; - float v_rco111 = v_rc11*obin, v_rco110 = v_rc11 - v_rco111; - float v_rco101 = v_rc10*obin, v_rco100 = v_rc10 - v_rco101; - float v_rco011 = v_rc01*obin, v_rco010 = v_rc01 - v_rco011; - float v_rco001 = v_rc00*obin, v_rco000 = v_rc00 - v_rco001; - - int idx = ((r0+1)*(d+2) + c0+1)*(n+2) + o0; - hist[idx] += v_rco000; - hist[idx+1] += v_rco001; - hist[idx+(n+2)] += v_rco010; - hist[idx+(n+3)] += v_rco011; - hist[idx+(d+2)*(n+2)] += v_rco100; - hist[idx+(d+2)*(n+2)+1] += v_rco101; - hist[idx+(d+3)*(n+2)] += v_rco110; - hist[idx+(d+3)*(n+2)+1] += v_rco111; - } - - // finalize histogram, since the orientation histograms are circular - for( i = 0; i < d; i++ ) - for( j = 0; j < d; j++ ) - { - int idx = ((i+1)*(d+2) + (j+1))*(n+2); - hist[idx] += hist[idx+n]; - hist[idx+1] += hist[idx+n+1]; - for( k = 0; k < n; k++ ) - dst[(i*d + j)*n + k] = hist[idx+k]; - } - // copy histogram to the descriptor, - // apply hysteresis thresholding - // and scale the result, so that it can be easily converted - // to byte array - float nrm2 = 0; - len = d*d*n; - for( k = 0; k < len; k++ ) - nrm2 += dst[k]*dst[k]; - float thr = std::sqrt(nrm2)*SIFT_DESCR_MAG_THR; - for( i = 0, nrm2 = 0; i < k; i++ ) - { - float val = std::min(dst[i], thr); - dst[i] = val; - nrm2 += val*val; - } - nrm2 = SIFT_INT_DESCR_FCTR/std::max(std::sqrt(nrm2), FLT_EPSILON); - -#if 1 - for( k = 0; k < len; k++ ) - { - dst[k] = saturate_cast(dst[k]*nrm2); - } -#else - float nrm1 = 0; - for( k = 0; k < len; k++ ) - { - dst[k] *= nrm2; - nrm1 += dst[k]; - } - nrm1 = 1.f/std::max(nrm1, FLT_EPSILON); - for( k = 0; k < len; k++ ) - { - dst[k] = std::sqrt(dst[k] * nrm1);//saturate_cast(std::sqrt(dst[k] * nrm1)*SIFT_INT_DESCR_FCTR); - } -#endif -} - -static void calcDescriptors(const vector& gpyr, const vector& keypoints, - Mat& descriptors, int nOctaveLayers, int firstOctave, int n /* bins */, int d /* width */) -{ - for( size_t i = 0; i < keypoints.size(); i++ ) - { - KeyPoint kpt = keypoints[i]; - int octave, layer; - float scale; - unpackOctave(kpt, octave, layer, scale); - CV_Assert(octave >= firstOctave && layer <= nOctaveLayers+2); - float size=kpt.size*scale; - Point2f ptf(kpt.pt.x*scale, kpt.pt.y*scale); - const Mat& img = gpyr[(octave - firstOctave)*(nOctaveLayers + 3) + layer]; - - float angle = 360.f - kpt.angle; - if(std::abs(angle - 360.f) < FLT_EPSILON) - angle = 0.f; - calcSIFTDescriptor(img, ptf, angle, size*0.5f, d, n, descriptors.ptr((int)i)); - } -} - -static int descriptorSize(int bins, int width) -{ - return width*width*bins; -} - -static void extractSIFT(const Mat &image, vector &keypoints, Mat &descriptors, int nOctaveLayers, double sigma, int bins, int width) -{ - if( image.empty() || image.depth() != CV_8U ) - CV_Error( CV_StsBadArg, "image is empty or has incorrect depth (!=CV_8U)" ); - - int firstOctave = 0, actualNOctaves = 0, actualNLayers = 0, maxOctave = INT_MIN; - for (size_t i=0; i= -1 && actualNLayers <= nOctaveLayers ); - actualNOctaves = maxOctave - firstOctave + 1; - - const Mat base = createInitialImage(image, firstOctave < 0, (float)sigma); - const int nOctaves = actualNOctaves > 0 ? actualNOctaves : cvRound(log( (double)std::min( base.cols, base.rows ) ) / log(2.) - 2) - firstOctave; - - vector gpyr, dogpyr; - buildGaussianPyramid(base, gpyr, nOctaves, nOctaveLayers, sigma); - buildDoGPyramid(gpyr, dogpyr, nOctaveLayers); - - descriptors.create((int)keypoints.size(), descriptorSize(bins, width), CV_32F); - calcDescriptors(gpyr, keypoints, descriptors, nOctaveLayers, firstOctave, bins, width); -} - -#include "openbr/plugins/openbr_internal.h" - -namespace br -{ - -/*! - * \ingroup transforms - * \brief Specialize wrapper OpenCV SIFT wrapper - * \author Josh Klontz \cite jklontz - */ -class CustomSIFTTransform : public UntrainableTransform -{ - Q_OBJECT - Q_PROPERTY(int size READ get_size WRITE set_size RESET reset_size STORED false) - Q_PROPERTY(QList sizes READ get_sizes WRITE set_sizes RESET reset_sizes STORED false) - Q_PROPERTY(int bins READ get_bins WRITE set_bins RESET reset_bins STORED false) - Q_PROPERTY(int width READ get_width WRITE set_width RESET reset_width STORED false) - BR_PROPERTY(int, size, 1) - BR_PROPERTY(QList, sizes, QList()) - BR_PROPERTY(int, bins, 8) - BR_PROPERTY(int, width, 4) - - void init() - { - if (sizes.empty()) - sizes.append(size); - } - - void project(const Template &src, Template &dst) const - { - std::vector keyPoints; - foreach (const QPointF &val, src.file.points()) - foreach (const int sz, sizes) - keyPoints.push_back(KeyPoint(val.x(), val.y(), sz)); - - Mat m; - extractSIFT(src, keyPoints, m, 3, 1.6f, bins, width); - m.setTo(0, m<0); // SIFT returns large negative values when it goes off the edge of the image. - dst += m; - } -}; - -BR_REGISTER(Transform, CustomSIFTTransform) - -} - -#include "custom_sift.moc" diff --git a/openbr/plugins/imgproc/keypointdescriptor.cpp b/openbr/plugins/imgproc/keypointdescriptor.cpp deleted file mode 100644 index e0f7d5f..0000000 --- a/openbr/plugins/imgproc/keypointdescriptor.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Copyright 2012 The MITRE Corporation * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include - -#include - -using namespace cv; - -namespace br -{ - -/*! - * \ingroup transforms - * \brief Wraps OpenCV Key Point Descriptor - * \br_link http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_feature_detectors.html - * \author Josh Klontz \cite jklontz - */ -class KeyPointDescriptorTransform : public UntrainableTransform -{ - Q_OBJECT - Q_PROPERTY(QString descriptor READ get_descriptor WRITE set_descriptor RESET reset_descriptor STORED false) - Q_PROPERTY(int size READ get_size WRITE set_size RESET reset_size STORED false) - BR_PROPERTY(QString, descriptor, "SIFT") - BR_PROPERTY(int, size, -1) - - Ptr descriptorExtractor; - - void init() - { - descriptorExtractor = DescriptorExtractor::create(descriptor.toStdString()); - if (descriptorExtractor.empty()) - qFatal("Failed to create DescriptorExtractor: %s", qPrintable(descriptor)); - } - - void project(const Template &src, Template &dst) const - { - std::vector keyPoints; - if (size == -1) - foreach (const QRectF &ROI, src.file.rects()) - keyPoints.push_back(KeyPoint(ROI.x()+ROI.width()/2, ROI.y()+ROI.height()/2, (ROI.width() + ROI.height())/2)); - else - foreach (const QPointF &landmark, src.file.points()) - keyPoints.push_back(KeyPoint(landmark.x(), landmark.y(), size)); - if (keyPoints.empty()) return; - descriptorExtractor->compute(src, keyPoints, dst); - } -}; - -BR_REGISTER(Transform, KeyPointDescriptorTransform) - -} // namespace br - -#include "imgproc/keypointdescriptor.moc" diff --git a/openbr/plugins/metadata/keypointdetector.cpp b/openbr/plugins/metadata/keypointdetector.cpp deleted file mode 100644 index 0d5acb5..0000000 --- a/openbr/plugins/metadata/keypointdetector.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Copyright 2012 The MITRE Corporation * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include - -#include -#include - -using namespace cv; - -namespace br -{ - -/*! - * \ingroup transforms - * \brief Wraps OpenCV Key Point Detector - * \br_link http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_feature_detectors.html - * \author Josh Klontz \cite jklontz - */ -class KeyPointDetectorTransform : public UntrainableTransform -{ - Q_OBJECT - Q_PROPERTY(QString detector READ get_detector WRITE set_detector RESET reset_detector STORED false) - BR_PROPERTY(QString, detector, "SIFT") - - Ptr featureDetector; - - void init() - { - featureDetector = FeatureDetector::create(detector.toStdString()); - if (featureDetector.empty()) - qFatal("Failed to create KeyPointDetector: %s", qPrintable(detector)); - } - - void project(const Template &src, Template &dst) const - { - dst = src; - - std::vector keyPoints; - try { - featureDetector->detect(src, keyPoints); - } catch (...) { - qWarning("Key point detection failed for file %s", qPrintable(src.file.name)); - dst.file.fte = true; - } - - QList rects; - foreach (const KeyPoint &keyPoint, keyPoints) - rects.append(Rect(keyPoint.pt.x-keyPoint.size/2, keyPoint.pt.y-keyPoint.size/2, keyPoint.size, keyPoint.size)); - dst.file.setRects(OpenCVUtils::fromRects(rects)); - } -}; - -BR_REGISTER(Transform, KeyPointDetectorTransform) - -} // namespace br - -#include "metadata/keypointdetector.moc" -- libgit2 0.21.4