File size: 4,661 Bytes
dffe123 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | // This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "../../precomp.hpp"
#include "opencv2/video/detail/tracking.detail.hpp"
#include "tracker_mil_state.hpp"
namespace cv {
namespace detail {
inline namespace tracking {
/**
* TrackerStateEstimatorMILBoosting::TrackerMILTargetState
*/
TrackerStateEstimatorMILBoosting::TrackerMILTargetState::TrackerMILTargetState(const Point2f& position, int width, int height, bool foreground,
const Mat& features)
{
setTargetPosition(position);
setTargetWidth(width);
setTargetHeight(height);
setTargetFg(foreground);
setFeatures(features);
}
void TrackerStateEstimatorMILBoosting::TrackerMILTargetState::setTargetFg(bool foreground)
{
isTarget = foreground;
}
void TrackerStateEstimatorMILBoosting::TrackerMILTargetState::setFeatures(const Mat& features)
{
targetFeatures = features;
}
bool TrackerStateEstimatorMILBoosting::TrackerMILTargetState::isTargetFg() const
{
return isTarget;
}
Mat TrackerStateEstimatorMILBoosting::TrackerMILTargetState::getFeatures() const
{
return targetFeatures;
}
TrackerStateEstimatorMILBoosting::TrackerStateEstimatorMILBoosting(int nFeatures)
{
className = "BOOSTING";
trained = false;
numFeatures = nFeatures;
}
TrackerStateEstimatorMILBoosting::~TrackerStateEstimatorMILBoosting()
{
}
void TrackerStateEstimatorMILBoosting::setCurrentConfidenceMap(ConfidenceMap& confidenceMap)
{
currentConfidenceMap.clear();
currentConfidenceMap = confidenceMap;
}
uint TrackerStateEstimatorMILBoosting::max_idx(const std::vector<float>& v)
{
const float* findPtr = &(*std::max_element(v.begin(), v.end()));
const float* beginPtr = &(*v.begin());
return (uint)(findPtr - beginPtr);
}
Ptr<TrackerTargetState> TrackerStateEstimatorMILBoosting::estimateImpl(const std::vector<ConfidenceMap>& /*confidenceMaps*/)
{
//run ClfMilBoost classify in order to compute next location
if (currentConfidenceMap.empty())
return Ptr<TrackerTargetState>();
Mat positiveStates;
Mat negativeStates;
prepareData(currentConfidenceMap, positiveStates, negativeStates);
std::vector<float> prob = boostMILModel.classify(positiveStates);
int bestind = max_idx(prob);
//float resp = prob[bestind];
return currentConfidenceMap.at(bestind).first;
}
void TrackerStateEstimatorMILBoosting::prepareData(const ConfidenceMap& confidenceMap, Mat& positive, Mat& negative)
{
int posCounter = 0;
int negCounter = 0;
for (size_t i = 0; i < confidenceMap.size(); i++)
{
Ptr<TrackerMILTargetState> currentTargetState = confidenceMap.at(i).first.staticCast<TrackerMILTargetState>();
CV_DbgAssert(currentTargetState);
if (currentTargetState->isTargetFg())
posCounter++;
else
negCounter++;
}
positive.create(posCounter, numFeatures, CV_32FC1);
negative.create(negCounter, numFeatures, CV_32FC1);
//TODO change with mat fast access
//initialize trainData (positive and negative)
int pc = 0;
int nc = 0;
for (size_t i = 0; i < confidenceMap.size(); i++)
{
Ptr<TrackerMILTargetState> currentTargetState = confidenceMap.at(i).first.staticCast<TrackerMILTargetState>();
Mat stateFeatures = currentTargetState->getFeatures();
if (currentTargetState->isTargetFg())
{
for (int j = 0; j < stateFeatures.rows; j++)
{
//fill the positive trainData with the value of the feature j for sample i
positive.at<float>(pc, j) = stateFeatures.at<float>(j, 0);
}
pc++;
}
else
{
for (int j = 0; j < stateFeatures.rows; j++)
{
//fill the negative trainData with the value of the feature j for sample i
negative.at<float>(nc, j) = stateFeatures.at<float>(j, 0);
}
nc++;
}
}
}
void TrackerStateEstimatorMILBoosting::updateImpl(std::vector<ConfidenceMap>& confidenceMaps)
{
if (!trained)
{
//this is the first time that the classifier is built
//init MIL
boostMILModel.init();
trained = true;
}
ConfidenceMap lastConfidenceMap = confidenceMaps.back();
Mat positiveStates;
Mat negativeStates;
prepareData(lastConfidenceMap, positiveStates, negativeStates);
//update MIL
boostMILModel.update(positiveStates, negativeStates);
}
}}} // namespace cv::detail::tracking
|