void ColorTracker::extractForegroundHistogram(int x1, int y1, int x2, int y2, Histogram & hist) { hist.clear(); std::vector<int> data1; std::vector<int> data2; std::vector<int> data3; std::vector<double> weights; int numData = (y2-y1)*(x2-x1); if (numData <= 0){ return; } data1.reserve(numData); data2.reserve(numData); data3.reserve(numData); weights.reserve(numData); double w2 = (x2-x1)/2; double h2 = (y2-y1)/2; double cx = x1 + w2; double cy = y1 + h2; double wh_i = 1.0/(w2*1.4142+1); //sqrt(2) double hh_i = 1.0/(h2*1.4142+1); for (int y = y1; y < y2+1; ++y){ const uchar * M1 = im1.ptr<uchar>(y); const uchar * M2 = im2.ptr<uchar>(y); const uchar * M3 = im3.ptr<uchar>(y); double tmp_y = std::pow((cy-y)*hh_i,2); for (int x = x1; x < x2+1; ++x){ data1.push_back(M1[x]); data2.push_back(M2[x]); data3.push_back(M3[x]); weights.push_back(kernelProfile_Epanechnikov(std::pow((cx-x)*wh_i,2) + tmp_y)); } } hist.clear(); hist.insertValues(data1, data2, data3, weights); }
void ColorTracker::extractBackgroundHistogram(int x1, int y1, int x2, int y2, Histogram & hist) { int offsetX = (x2-x1)/2; int offsetY = (y2-y1)/2; int rowMin = std::max(0, (int)(y1-offsetY)); int rowMax = std::min(im1.rows, (int)(y2+offsetY+1)); int colMin = std::max(0, (int)(x1-offsetX)); int colMax = std::min(im1.cols, (int)(x2+offsetX+1)); int numData = (rowMax-rowMin)*(colMax-colMin) - (y2-y1)*(x2-x1); if (numData < 1) numData = (rowMax-rowMin)*(colMax-colMin)/2 + 1; std::vector<int> d1, d2, d3; std::vector<double> weights; d1.reserve(numData); d2.reserve(numData); d3.reserve(numData); for (int y = rowMin; y < rowMax; ++y){ const uchar * M1 = im1.ptr<uchar>(y); const uchar * M2 = im2.ptr<uchar>(y); const uchar * M3 = im3.ptr<uchar>(y); for (int x = colMin; x < colMax; ++x){ if (x >= x1 && x <= x2 && y >= y1 && y <= y2) continue; d1.push_back(M1[x]); d2.push_back(M2[x]); d3.push_back(M3[x]); } } hist.clear(); hist.insertValues(d1, d2, d3, weights); }