parent
b8588f845f
commit
a6215264ed
6 changed files with 86 additions and 91 deletions
@ -0,0 +1,23 @@ |
||||
// 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 "tracking_utils.hpp" |
||||
|
||||
using namespace cv; |
||||
|
||||
double tracking_internal::computeNCC(const Mat& patch1, const Mat& patch2) |
||||
{ |
||||
CV_Assert( patch1.rows == patch2.rows ); |
||||
CV_Assert( patch1.cols == patch2.cols ); |
||||
|
||||
int N = patch1.rows * patch1.cols; |
||||
double s1 = sum(patch1)(0); |
||||
double s2 = sum(patch2)(0); |
||||
double n1 = norm(patch1, NORM_L2SQR); |
||||
double n2 = norm(patch2, NORM_L2SQR); |
||||
double prod=patch1.dot(patch2); |
||||
double sq1 = sqrt(std::max(0.0, n1 - 1.0 * s1 * s1 / N)); |
||||
double sq2 = sqrt(std::max(0.0, n2 - 1.0 * s2 * s2 / N)); |
||||
return (sq2 == 0) ? sq1 / abs(sq1) : (prod - s1 * s2 / N) / sq1 / sq2; |
||||
} |
@ -0,0 +1,47 @@ |
||||
// 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.
|
||||
#ifndef __OPENCV_TRACKING_UTILS_HPP__ |
||||
|
||||
#include "precomp.hpp" |
||||
#include <algorithm> |
||||
|
||||
namespace cv { |
||||
namespace tracking_internal |
||||
{ |
||||
/** Computes normalized corellation coefficient between the two patches (they should be
|
||||
* of the same size).*/ |
||||
double computeNCC(const Mat& patch1, const Mat& patch2); |
||||
|
||||
template<typename T> |
||||
T getMedianAndDoPartition(std::vector<T>& values) |
||||
{ |
||||
size_t size = values.size(); |
||||
if(size%2==0) |
||||
{ |
||||
std::nth_element(values.begin(), values.begin() + size/2-1, values.end()); |
||||
T firstMedian = values[size/2-1]; |
||||
|
||||
std::nth_element(values.begin(), values.begin() + size/2, values.end()); |
||||
T secondMedian = values[size/2]; |
||||
|
||||
return (firstMedian + secondMedian) / (T)2; |
||||
} |
||||
else |
||||
{ |
||||
size_t medianIndex = (size - 1) / 2; |
||||
std::nth_element(values.begin(), values.begin() + medianIndex, values.end()); |
||||
|
||||
return values[medianIndex]; |
||||
} |
||||
} |
||||
|
||||
template<typename T> |
||||
T getMedian(const std::vector<T>& values) |
||||
{ |
||||
std::vector<T> copy(values); |
||||
return getMedianAndDoPartition(copy); |
||||
} |
||||
} |
||||
} |
||||
#endif |
Loading…
Reference in new issue