Commit faf5f2306bf25f67d4980120d22c0380565cd0cc
1 parent
ab48a22f
remove consolidatedetections
Showing
1 changed file
with
0 additions
and
159 deletions
openbr/plugins/metadata/consolidatedetections.cpp deleted
| 1 | -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
| 2 | - * Copyright 2012 The MITRE Corporation * | ||
| 3 | - * * | ||
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); * | ||
| 5 | - * you may not use this file except in compliance with the License. * | ||
| 6 | - * You may obtain a copy of the License at * | ||
| 7 | - * * | ||
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 * | ||
| 9 | - * * | ||
| 10 | - * Unless required by applicable law or agreed to in writing, software * | ||
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, * | ||
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
| 13 | - * See the License for the specific language governing permissions and * | ||
| 14 | - * limitations under the License. * | ||
| 15 | - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
| 16 | - | ||
| 17 | -#include <Eigen/Dense> | ||
| 18 | - | ||
| 19 | -#include <openbr/plugins/openbr_internal.h> | ||
| 20 | -#include <openbr/core/opencvutils.h> | ||
| 21 | - | ||
| 22 | -using namespace Eigen; | ||
| 23 | -using namespace cv; | ||
| 24 | - | ||
| 25 | -namespace br | ||
| 26 | -{ | ||
| 27 | - | ||
| 28 | -/*! | ||
| 29 | - * \ingroup transforms | ||
| 30 | - * \brief Consolidate redundant/overlapping detections. | ||
| 31 | - * \author Brendan Klare \cite bklare | ||
| 32 | - */ | ||
| 33 | -class ConsolidateDetectionsTransform : public UntrainableMetadataTransform | ||
| 34 | -{ | ||
| 35 | - Q_OBJECT | ||
| 36 | - | ||
| 37 | - void projectMetadata(const File &src, File &dst) const | ||
| 38 | - { | ||
| 39 | - dst = src; | ||
| 40 | - if (!dst.contains("Confidences")) | ||
| 41 | - return; | ||
| 42 | - | ||
| 43 | - //Compute overlap between rectangles and create discrete Laplacian matrix | ||
| 44 | - QList<Rect> rects = OpenCVUtils::toRects(src.rects()); | ||
| 45 | - int n = rects.size(); | ||
| 46 | - if (n == 0) | ||
| 47 | - return; | ||
| 48 | - MatrixXf laplace(n,n); | ||
| 49 | - for (int i = 0; i < n; i++) { | ||
| 50 | - laplace(i,i) = 0; | ||
| 51 | - } | ||
| 52 | - for (int i = 0; i < n; i++){ | ||
| 53 | - for (int j = i + 1; j < n; j++) { | ||
| 54 | - float overlap = (float)((rects[i] & rects[j]).area()) / (float)max(rects[i].area(), rects[j].area()); | ||
| 55 | - if (overlap > 0.5) { | ||
| 56 | - laplace(i,j) = -1.0; | ||
| 57 | - laplace(j,i) = -1.0; | ||
| 58 | - laplace(i,i) = laplace(i,i) + 1.0; | ||
| 59 | - laplace(j,j) = laplace(j,j) + 1.0; | ||
| 60 | - } else { | ||
| 61 | - laplace(i,j) = 0; | ||
| 62 | - laplace(j,i) = 0; | ||
| 63 | - } | ||
| 64 | - } | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - // Compute eigendecomposition | ||
| 68 | - SelfAdjointEigenSolver<Eigen::MatrixXf> eSolver(laplace); | ||
| 69 | - MatrixXf allEVals = eSolver.eigenvalues(); | ||
| 70 | - MatrixXf allEVecs = eSolver.eigenvectors(); | ||
| 71 | - | ||
| 72 | - //Keep eigenvectors with zero eigenvalues | ||
| 73 | - int nRegions = 0; | ||
| 74 | - for (int i = 0; i < n; i++) { | ||
| 75 | - if (fabs(allEVals(i)) < 1e-4) { | ||
| 76 | - nRegions++; | ||
| 77 | - } | ||
| 78 | - } | ||
| 79 | - MatrixXf regionVecs(n, nRegions); | ||
| 80 | - for (int i = 0, cnt = 0; i < n; i++) { | ||
| 81 | - if (fabs(allEVals(i)) < 1e-4) | ||
| 82 | - regionVecs.col(cnt++) = allEVecs.col(i); | ||
| 83 | - } | ||
| 84 | - | ||
| 85 | - //Determine membership for each consolidated location | ||
| 86 | - // and compute average of regions. This is determined by | ||
| 87 | - // finding which eigenvector has the highest magnitude for | ||
| 88 | - // each input dimension. Each input dimension corresponds to | ||
| 89 | - // one of the input rect region. Thus, each eigenvector represents | ||
| 90 | - // a set of overlaping regions. | ||
| 91 | - float *midX = new float[nRegions]; | ||
| 92 | - float *midY = new float[nRegions]; | ||
| 93 | - float *avgWidth = new float[nRegions]; | ||
| 94 | - float *avgHeight = new float[nRegions]; | ||
| 95 | - float *confs = new float[nRegions]; | ||
| 96 | - int *cnts = new int[nRegions]; | ||
| 97 | - int mx; | ||
| 98 | - int mxIdx; | ||
| 99 | - for (int i = 0 ; i < nRegions; i++) { | ||
| 100 | - midX[i] = 0; | ||
| 101 | - midY[i] = 0; | ||
| 102 | - avgWidth[i] = 0; | ||
| 103 | - avgHeight[i] = 0; | ||
| 104 | - confs[i] = 0; | ||
| 105 | - cnts[i] = 0; | ||
| 106 | - } | ||
| 107 | - | ||
| 108 | - QList<float> confidences = dst.getList<float>("Confidences"); | ||
| 109 | - for (int i = 0; i < n; i++) { | ||
| 110 | - mx = 0.0; | ||
| 111 | - mxIdx = -1; | ||
| 112 | - | ||
| 113 | - for (int j = 0; j < nRegions; j++) { | ||
| 114 | - if (fabs(regionVecs(i,j)) > mx) { | ||
| 115 | - mx = fabs(regionVecs(i,j)); | ||
| 116 | - mxIdx = j; | ||
| 117 | - } | ||
| 118 | - } | ||
| 119 | - | ||
| 120 | - Rect curRect = rects[i]; | ||
| 121 | - midX[mxIdx] += ((float)curRect.x + (float)curRect.width / 2.0); | ||
| 122 | - midY[mxIdx] += ((float)curRect.y + (float)curRect.height / 2.0); | ||
| 123 | - avgWidth[mxIdx] += (float) curRect.width; | ||
| 124 | - avgHeight[mxIdx] += (float) curRect.height; | ||
| 125 | - confs[mxIdx] += confidences[i]; | ||
| 126 | - cnts[mxIdx]++; | ||
| 127 | - } | ||
| 128 | - | ||
| 129 | - QList<Rect> consolidatedRects; | ||
| 130 | - QList<float> consolidatedConfidences; | ||
| 131 | - for (int i = 0; i < nRegions; i++) { | ||
| 132 | - float cntF = (float) cnts[i]; | ||
| 133 | - if (cntF > 0) { | ||
| 134 | - int x = qRound((midX[i] / cntF) - (avgWidth[i] / cntF) / 2.0); | ||
| 135 | - int y = qRound((midY[i] / cntF) - (avgHeight[i] / cntF) / 2.0); | ||
| 136 | - int w = qRound(avgWidth[i] / cntF); | ||
| 137 | - int h = qRound(avgHeight[i] / cntF); | ||
| 138 | - consolidatedRects.append(Rect(x,y,w,h)); | ||
| 139 | - consolidatedConfidences.append(confs[i] / cntF); | ||
| 140 | - } | ||
| 141 | - } | ||
| 142 | - | ||
| 143 | - delete [] midX; | ||
| 144 | - delete [] midY; | ||
| 145 | - delete [] avgWidth; | ||
| 146 | - delete [] avgHeight; | ||
| 147 | - delete [] confs; | ||
| 148 | - delete [] cnts; | ||
| 149 | - | ||
| 150 | - dst.setRects(consolidatedRects); | ||
| 151 | - dst.setList<float>("Confidences", consolidatedConfidences); | ||
| 152 | - } | ||
| 153 | -}; | ||
| 154 | - | ||
| 155 | -BR_REGISTER(Transform, ConsolidateDetectionsTransform) | ||
| 156 | - | ||
| 157 | -} // namespace br | ||
| 158 | - | ||
| 159 | -#include "metadata/consolidatedetections.moc" |