示例#1
0
void GraphComparator::_calculateRasterCost(cv::Mat& mat)
{
  ProbablePathCalculator ppc;
  ppc.setRandomNoise(0.0);
  ppc.setRandomPatches(0.0, 1);
  vector<float> friction(_width * _height, 5.0 * _pixelSize);
  Tgs::Image<float> cost(_width, _height);

  for (int y = 0; y < _height; y++)
  {
    float* row = mat.ptr<float>(y);
    for (int x = 0; x < _width; x++)
    {
      cost.pixel(x, y) = row[x];
    }
  }

  ppc.setFriction(_height, _width, friction);
  cost = ppc.updateCostSurface(cost);

  for (int y = 0; y < _height; y++)
  {
    float* row = mat.ptr<float>(y);
    for (int x = 0; x < _width; x++)
    {
      row[x] = cost.pixel(x, y);
    }
  }
}
  void testUpdateCostSurface()
  {
    int w = 5, h = 5;
    vector<float> friction;
    friction.resize(w * h, 1);
    Image<float> cost;
    cost.resize(w, h, -1);
    cost.pixel(1, 0) = 3;

    std::string tmp = "[[4, 3, 4, 5, 6]\n"
      "[4.41421, 4, 4.41421, 5.41421, 6.41421]\n"
      "[5.41421, 5, 5.41421, 5.82843, 6.82843]\n"
      "[6.41421, 6, 6.41421, 6.82843, 7.24264]\n"
      "[7.41421, 7, 7.41421, 7.82843, 8.24264]\n"
      "]";

    {
      ProbablePathCalculator uut;
      uut.setFriction(w, h, friction);
      uut.setRandomNoise(0.0);
      uut.updateCostSurface(cost);
      stringstream strm;
      strm << cost;
      CPPUNIT_ASSERT_EQUAL(tmp, strm.str());
    }

    {
      cost.resize(w, h, 10);
      cost.pixel(1, 0) = 3;

      ProbablePathCalculator uut;
      uut.setFriction(w, h, friction);
      uut.setRandomNoise(0.0);
      uut.updateCostSurface(cost);
      stringstream strm;
      strm << cost;
    }
  }