|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html |
| 4 | +/* |
| 5 | + * MIT License |
| 6 | + * |
| 7 | + * Copyright (c) 2018 Stephanie Lowry |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in all |
| 17 | + * copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | + * SOFTWARE. |
| 26 | + */ |
| 27 | +#include <cmath> |
| 28 | +#include "Logos.hpp" |
| 29 | +#include <opencv2/core.hpp> |
| 30 | + |
| 31 | +namespace logos |
| 32 | +{ |
| 33 | +Logos::Logos() |
| 34 | +{ |
| 35 | + LogosParameters defaultParams; |
| 36 | + init(defaultParams); |
| 37 | +} |
| 38 | + |
| 39 | +Logos::Logos(const LogosParameters& p) |
| 40 | +{ |
| 41 | + init(p); |
| 42 | +} |
| 43 | + |
| 44 | +void Logos::init(const LogosParameters& p) |
| 45 | +{ |
| 46 | + setParams(p); |
| 47 | + LB = static_cast<float>(-CV_PI); |
| 48 | + BINSIZE = logosParams.GLOBALORILIMIT/3; |
| 49 | + BINNUMBER = static_cast<unsigned int>(ceil(2*CV_PI/BINSIZE)); |
| 50 | + bins.resize(BINNUMBER); |
| 51 | + std::fill(bins.begin(), bins.end(), 0); |
| 52 | +} |
| 53 | + |
| 54 | +int Logos::estimateMatches(std::vector<Point*> vP1, std::vector<Point*> vP2, std::vector<PointPair*>& globalmatches) |
| 55 | +{ |
| 56 | + matches.clear(); |
| 57 | + |
| 58 | + // for each point |
| 59 | + int count1 = 0; |
| 60 | + |
| 61 | + for (std::vector<Point*>::iterator pit1 = vP1.begin(); pit1 != vP1.end(); ++pit1, count1++) |
| 62 | + { |
| 63 | + (*pit1)->nearestNeighbours(vP1, count1, getNum1()); |
| 64 | + int count2 = 0; |
| 65 | + |
| 66 | + // find possible matches |
| 67 | + for (std::vector<Point*>::iterator pit2 = vP2.begin(); pit2 != vP2.end(); ++pit2, count2++) |
| 68 | + { |
| 69 | + if ((*pit1)->getLabel() != (*pit2)->getLabel()) |
| 70 | + { |
| 71 | + continue; |
| 72 | + } |
| 73 | + // this is a possible match in Image 2 |
| 74 | + // get nearest neighbours |
| 75 | + (*pit2)->nearestNeighbours(vP2, count2, getNum2()); |
| 76 | + |
| 77 | + PointPair* ptpr = new PointPair(*pit1, *pit2); |
| 78 | + ptpr->addPositions(count1, count2); |
| 79 | + ptpr->computeLocalSupport(pp, getNum2()); |
| 80 | + |
| 81 | + // calc matches |
| 82 | + int support = 0; |
| 83 | + for (std::vector<PointPair*>::const_iterator it = pp.begin(); it < pp.end(); ++it) |
| 84 | + { |
| 85 | + Match m(ptpr, *it); |
| 86 | + if (evaluateMatch(m)) |
| 87 | + { |
| 88 | + support++; |
| 89 | + } |
| 90 | + } |
| 91 | + for (size_t i = 0; i < pp.size(); i++) |
| 92 | + { |
| 93 | + delete pp[i]; |
| 94 | + } |
| 95 | + pp.clear(); |
| 96 | + if (support > 0) |
| 97 | + { |
| 98 | + ptpr->setSupport(support); |
| 99 | + matches.push_back(ptpr); |
| 100 | + updateBin(ptpr->getRelOri()); |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + delete ptpr; |
| 105 | + ptpr = NULL; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // do global orientation |
| 111 | + double maxang = calcGlobalOrientation(); |
| 112 | + |
| 113 | + // find which matches are within global orientation limit |
| 114 | + int numinliers = 0; |
| 115 | + globalmatches.clear(); |
| 116 | + for (std::vector<PointPair*>::iterator it = matches.begin(); it != matches.end(); ++it) |
| 117 | + { |
| 118 | + if (std::fabs((*it)->getRelOri() - maxang) < logosParams.GLOBALORILIMIT) |
| 119 | + { |
| 120 | + numinliers++; |
| 121 | + globalmatches.push_back(*it); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + delete *it; |
| 126 | + *it = NULL; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return numinliers; |
| 131 | +} |
| 132 | + |
| 133 | +bool Logos::evaluateMatch(const Match& m) const |
| 134 | +{ |
| 135 | + return ((m.getRelOrientation() < getIntraOriLimit()) && |
| 136 | + (m.getRelScale() < getIntraScaleLimit()) && |
| 137 | + (m.getInterOrientation() < getInterOriLimit()) && |
| 138 | + (m.getInterScale() < getInterScaleLimit())); |
| 139 | +} |
| 140 | + |
| 141 | +void Logos::updateBin(float input) |
| 142 | +{ |
| 143 | + unsigned int binnumber = static_cast<unsigned int>(cvFloor((input-LB) / BINSIZE)); |
| 144 | + // compare binnumber to BINNUMBER |
| 145 | + if (binnumber < BINNUMBER) |
| 146 | + { |
| 147 | + bins[binnumber]++; |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + bins[BINNUMBER-1]++; |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +float Logos::calcGlobalOrientation() |
| 156 | +{ |
| 157 | + // find max bin |
| 158 | + // check BINNUMBER is big enough |
| 159 | + if (BINNUMBER < 3) |
| 160 | + { |
| 161 | + return 0; |
| 162 | + } |
| 163 | + |
| 164 | + std::vector<int> bins2(BINNUMBER); |
| 165 | + int maxval = 0; |
| 166 | + unsigned int maxix = 0; |
| 167 | + bins2[0] = bins[0] + bins[1] + bins[BINNUMBER-1]; |
| 168 | + maxval = bins2[0]; |
| 169 | + for (unsigned int i = 1; i < BINNUMBER; i++) |
| 170 | + { |
| 171 | + if (i == BINNUMBER-1) |
| 172 | + { |
| 173 | + bins2[i] = bins[i]+bins[i-1]+bins[0]; |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + bins2[i] = bins[i]+bins[i-1]+bins[i+1]; |
| 178 | + } |
| 179 | + if (bins2[i] > maxval) |
| 180 | + { |
| 181 | + maxval = bins2[i]; |
| 182 | + maxix = i; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + // convert to an angle |
| 187 | + return LB + maxix*BINSIZE + BINSIZE/2; |
| 188 | +} |
| 189 | +} |
0 commit comments