Exemplo n.º 1
0
void CmSaliencyRC::RegionContrast(const vector<Region> &regs, CMat &color3fv, Mat& regSal1d, double sigmaDist)
{	
	Mat_<float> cDistCache1f = Mat::zeros(color3fv.cols, color3fv.cols, CV_32F);{
		Vec3f* pColor = (Vec3f*)color3fv.data;
		for(int i = 0; i < cDistCache1f.rows; i++)
			for(int j= i+1; j < cDistCache1f.cols; j++)
				cDistCache1f[i][j] = cDistCache1f[j][i] = vecDist<float, 3>(pColor[i], pColor[j]);
	}

	int regNum = (int)regs.size();
	Mat_<double> rDistCache1d = Mat::zeros(regNum, regNum, CV_64F);
	regSal1d = Mat::zeros(1, regNum, CV_64F);
	double* regSal = (double*)regSal1d.data;
	for (int i = 0; i < regNum; i++){
		const Point2d &rc = regs[i].centroid;
		for (int j = 0; j < regNum; j++){
			if(i<j) {
				double dd = 0;
				const vector<CostfIdx> &c1 = regs[i].freIdx, &c2 = regs[j].freIdx;
				for (size_t m = 0; m < c1.size(); m++)
					for (size_t n = 0; n < c2.size(); n++)
						dd += cDistCache1f[c1[m].second][c2[n].second] * c1[m].first * c2[n].first;
				rDistCache1d[j][i] = rDistCache1d[i][j] = dd * exp(-pntSqrDist(rc, regs[j].centroid)/sigmaDist); 
			}
			regSal[i] += regs[j].pixNum * rDistCache1d[i][j];
		}
		regSal[i] *= exp(-9.0 * (sqr(regs[i].ad2c.x) + sqr(regs[i].ad2c.y)));
	}
}
// the weight sum is 1 for all regions
void RegionSaliency::RegionContrastLocallyDebiased(const vector<Region> &regs, const Mat &color3fv, Mat& regSal1d, double sigmaDist)
{	
  // color distance cache
	Mat_<float> cDistCache1f = Mat::zeros(color3fv.cols, color3fv.cols, CV_32F);
  {
		Vec3f* pColor = (Vec3f*)color3fv.data;
		for(int i = 0; i < cDistCache1f.rows; i++)
			for(int j= i+1; j < cDistCache1f.cols; j++)
				cDistCache1f[i][j] = cDistCache1f[j][i] = vecDist3(pColor[i], pColor[j]);
	}
  
  // spatial distance cache
  int regNum = (int)regs.size();
  Mat_<double> sDistCache1d = Mat::zeros(regNum, regNum, CV_64F);
  Mat_<double> sDistCache1dSum = Mat::zeros(regNum, 1, CV_64F);
  {
    for (int i = 0; i < regNum; i++)
    {
      for (int j = 0; j < regNum; j++)
      {
        sDistCache1d[j][i]     = sDistCache1d[i][j] = exp(-pntSqrDist(regs[i].centroid, regs[j].centroid)/sigmaDist); 
        sDistCache1dSum[i][0] += sDistCache1d[j][i];
      }
    }
  }
  // normalize the distance sum
  {
    for (int i = 0; i < regNum; i++)
    {
      for (int j = 0; j < regNum; j++)
      {
        sDistCache1d[j][i] /= sDistCache1dSum[i][0];
      }
    }
  }

	Mat_<double> rDistCache1d = Mat::zeros(regNum, regNum, CV_64F);
	regSal1d = Mat::zeros(1, regNum, CV_64F);
	double* regSal = (double*)regSal1d.data;
	for (int i = 0; i < regNum; i++){
		for (int j = 0; j < regNum; j++){
			if(i<j) {
				double dd = 0;
				const vector<CostfIdx> &c1 = regs[i].freIdx, &c2 = regs[j].freIdx;
				for (size_t m = 0; m < c1.size(); m++)
					for (size_t n = 0; n < c2.size(); n++)
						dd += cDistCache1f[c1[m].second][c2[n].second] * c1[m].first * c2[n].first;
				rDistCache1d[j][i] = rDistCache1d[i][j] = dd * sDistCache1d[i][j]; 
			}
			regSal[i] += regs[j].pixNum * rDistCache1d[i][j];
		}
	}
}