int ScaleEnergyFunction::GetDataCostReverseThreshold(int labelId, int nodeId, int penaltyCost)
{
	CvPoint point = _mapping2D->GetMappedPoint(nodeId);
	
	DoublePoint mappedPoint = _labelMapping->GetMappedPoint(labelId, point);

	if(mappedPoint.x < 0 || mappedPoint.x > _inputSize.width - 1)
		return penaltyCost;
	if(mappedPoint.y < 0 || mappedPoint.y > _inputSize.height - 1)
		return penaltyCost;


	if(!IsSatisfiedBoundary(point.x, mappedPoint.x, _inputSize.width, _outputSize.width))
		return penaltyCost;
	if(!IsSatisfiedBoundary(point.y, mappedPoint.y, _inputSize.height, _outputSize.height))
		return penaltyCost;

	CvScalar saliency = GetInterpolatedValue(mappedPoint, _saliency);
	
	double scale = _labelMapping->GetScale(labelId);
	
	int scaleId = _labelMapping->GetScaleId(labelId);

	//DisplayImage(_saliency, "saliency");

	int value = saliency.val[0] + saliency.val[1] + saliency.val[2];
	int threshold = 150 * 3;
	 
	if(value > threshold)
		value = threshold + (value - threshold) * scale;
	else
		value = threshold - (threshold - value) * scale;

	return value;
}
int ScaleEnergyFunction::GetDataCostReverseSaliency(int labelId, int nodeId, int penaltyCost)
{
	CvPoint point = _mapping2D->GetMappedPoint(nodeId);
	
	DoublePoint mappedPoint = _labelMapping->GetMappedPoint(labelId, point);

	if(mappedPoint.x < 0 || mappedPoint.x > _inputSize.width - 1)
		return penaltyCost;
	if(mappedPoint.y < 0 || mappedPoint.y > _inputSize.height - 1)
		return penaltyCost;


	if(!IsSatisfiedBoundary(point.x, mappedPoint.x, _inputSize.width, _outputSize.width))
		return penaltyCost;
	if(!IsSatisfiedBoundary(point.y, mappedPoint.y, _inputSize.height, _outputSize.height))
		return penaltyCost;

	CvScalar saliency = GetInterpolatedValue(mappedPoint, _saliency);
	
	double scale = _labelMapping->GetScale(labelId);
	
	int scaleId = _labelMapping->GetScaleId(labelId);

	//DisplayImage(_saliency, "saliency");

	if(scaleId == 0)
		return saliency.val[0] + saliency.val[1] + saliency.val[2];
	else
		return _maxSaliency - (saliency.val[0] + saliency.val[1] + saliency.val[2]);
}
int ScaleEnergyFunction::GetDataCostAreaCost(int labelId, int nodeId, int penaltyCost)
{
	CvPoint point = _mapping2D->GetMappedPoint(nodeId);
	
	DoublePoint mappedPoint = _labelMapping->GetMappedPoint(labelId, point);

	if(mappedPoint.x < 0 || mappedPoint.x > _inputSize.width - 1)
		return penaltyCost;
	if(mappedPoint.y < 0 || mappedPoint.y > _inputSize.height - 1)
		return penaltyCost;


	if(!IsSatisfiedBoundary(point.x, mappedPoint.x, _inputSize.width, _outputSize.width))
		return penaltyCost * 2;
	if(!IsSatisfiedBoundary(point.y, mappedPoint.y, _inputSize.height, _outputSize.height))
		return penaltyCost * 2;

	CvScalar saliency = GetInterpolatedValue(mappedPoint, _saliency);
		 
	int scaleId = _labelMapping->GetScaleId(labelId);
	double scaleX = _labelMapping->GetScaleX(scaleId);
	double scaleY = _labelMapping->GetScaleY(scaleId);
	int distortionCost = GetDistortionCost(point, _image, labelId, 3, scaleX, scaleY);
	int areaCost = 255 * scaleX * scaleY;

	int energy = (_distortionWeight * distortionCost + _areaWeight *  areaCost) / _smoothWeight;
	//printf("investigate: %i %i \n", nodeId, labelId);
	return energy;
	// return (saliency.val[0] + saliency.val[1] + saliency.val[2]) * scale;
}
int ScaleEnergyFunction::GetDistortionCost(CvPoint point, IplImage* image, int label, int patch_size, double scaleX, double scaleY)
{
	vector<CvScalar>* scaled_points = new vector<CvScalar>(patch_size * patch_size);
	for(int i = 0; i < patch_size; i++)
		for(int j = 0; j < patch_size; j++)
		{
			DoublePoint currPoint = _labelMapping->GetMappedPoint(label, cvPoint(point.x + i, point.y + j));				
			
			(*scaled_points)[i * patch_size + j] = GetInterpolatedValue(currPoint, image);			
		}
	
		
	int scaled_patch_size = patch_size / scaleX * scaleY;
	
	DoublePoint mappedPoint = _labelMapping->GetMappedPoint(label, point);
	int x = (int)mappedPoint.x;
	int y = (int)mappedPoint.y;

	int minDistortion = 10000000;
	for(int i = x; i < x + scaled_patch_size - patch_size + 1; i++)
		for(int j = y; j < y + scaled_patch_size - patch_size + 1; j++)
		{
			 
			int distortion = GetDistortionCostPatch(scaled_points, cvPoint(i,j), image, patch_size);
			if(distortion < minDistortion)
				minDistortion = distortion;
		}

		//scaled_points->clear();
		delete scaled_points;

	return minDistortion;
}
int ScaleEnergyFunction::GetSmoothCostPreventCrossingPatch(int labelId1, int labelId2, int nodeId1, int nodeId2, int penaltyCost, int threshold, int patchsize)
{	
	CvPoint point1 = _mapping2D->GetMappedPoint(nodeId1);
	CvPoint point2 = _mapping2D->GetMappedPoint(nodeId2);

	int energy = 0; 



	DoublePoint mappedPoint1 = _labelMapping->GetMappedPoint(labelId1, point1);
	DoublePoint mappedPoint2 = _labelMapping->GetMappedPoint(labelId2, point2);
	
	if(!IsInside(mappedPoint1, _inputSize) || !IsInside(mappedPoint2, _inputSize))
		return penaltyCost;

	int scaleId1 = _labelMapping->GetScaleId(labelId1);
	int scaleId2 = _labelMapping->GetScaleId(labelId2);
	if(scaleId1 != scaleId2)
	{
		CvScalar saliency1 = GetInterpolatedValue(mappedPoint1, _saliency);		
		CvScalar saliency2 = GetInterpolatedValue(mappedPoint2, _saliency);

		if(saliency1.val[0] + saliency1.val[1] + saliency1.val[2] > threshold
			&& saliency2.val[0] + saliency2.val[1] + saliency2.val[2] > threshold)
			return penaltyCost;
	}


	DoublePoint mappedNeighbor1 = _labelMapping->GetMappedPoint(labelId1, point2);
	DoublePoint mappedNeighbor2 = _labelMapping->GetMappedPoint(labelId2, point1);

	if(!IsInside(mappedNeighbor1, _inputSize) || !IsInside(mappedNeighbor2, _inputSize))
		return penaltyCost;

	energy += GetPatchDifference(point2, point2, labelId1, labelId2, _image, _labelMapping, patchsize);
	energy += GetPatchDifference(point1, point1, labelId1, labelId2, _image, _labelMapping, patchsize);
	energy += GetPatchDifference(point2, point2, labelId1, labelId2, _gradient, _labelMapping, patchsize);
	energy += GetPatchDifference(point1, point1, labelId1, labelId2, _gradient, _labelMapping, patchsize);

	return energy;
}
Example #6
0
void Fluid::Advect(
    float timeStep,
    const View2f& velocities,
    const View<SampleType, 1>& u,
    View<SampleType, 1>* uAdvected) const {
  const int width = Width();
  const int height = Height();
  for (int y = 0; y < height; ++y) {
    for (int x = 0; x < width; ++x) {
      // Trace back in time by following the velocity field.
      const Vector2 pos = Vector2(x, y) - timeStep * _gridScale * FlipY(GetValue(x, y, velocities));
      const SampleType uNew = GetInterpolatedValue(pos, u);
      SetValue(x, y, uNew, uAdvected);
    }
  }
}
// normal data cost with boundary constraint
int ScaleEnergyFunction::GetDataCostOrigin(int labelId, int nodeId, int penaltyCost)
{
	CvPoint point = _mapping2D->GetMappedPoint(nodeId);
	
	DoublePoint mappedPoint = _labelMapping->GetMappedPoint(labelId, point);

	if(mappedPoint.x < 0 || mappedPoint.x > _inputSize.width - 1)
		return penaltyCost;
	if(mappedPoint.y < 0 || mappedPoint.y > _inputSize.height - 1)
		return penaltyCost;


	if(!IsSatisfiedBoundary(point.x, mappedPoint.x, _inputSize.width, _outputSize.width))
		return penaltyCost;
	if(!IsSatisfiedBoundary(point.y, mappedPoint.y, _inputSize.height, _outputSize.height))
		return penaltyCost;

	CvScalar saliency = GetInterpolatedValue(mappedPoint, _saliency);
	 
	return saliency.val[0] + saliency.val[1] + saliency.val[2];
}