コード例 #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);
    }
  }
}
コード例 #2
0
  void test1()
  {
    ProbablePathCalculator uut;

//     GdalRasterLoader loader("C:\\GIS_Data\\sa_test_data\\nova\\NOVA_Friction.img");
//     vector<float> friction;
//     friction.reserve(loader.getHeight() * loader.getWidth());
//     for (int row = 0; row < loader.getHeight(); row++)
//     {
//       for (int col = 0; col < loader.getWidth(); col++)
//       {
//         friction.push_back(loader.getValueByPixel(row, col, 0));
//       }
//     }

    vector<float> friction;
    int w = 50, h = 50;
    friction.reserve(w * h);
    for (int row = 0; row < h; row++)
    {
      for (int col = 0; col < w; col++)
      {
        friction.push_back(1);
      }
    }

    uut.setFriction(w, h, friction);

    std::vector<PpPoint> source;
    // lower left
    source.push_back(PpPoint(2, 40));
    std::vector<PpPoint> destination;
    // upper right
    destination.push_back(PpPoint(14, 34));
    destination.push_back(PpPoint(2, 12));
    uut.setSources(source);
    uut.setDestinations(destination);
    std::vector<int> result;
    uut.setRandomNoise(0.0);
    uut.calculateProbablePaths(1);
    result = uut.getRouteCounts();
    vector<float> costSurface = uut.getCostSurface();

    uut.setRandomPatches(0.1f, 10);
    uut.calculateProbablePaths(5);
    result = uut.getRouteCounts();

    CPPUNIT_ASSERT_EQUAL(friction.size(), result.size());

    int rMax = std::numeric_limits<int>::min(), rMin = std::numeric_limits<int>::max();
    float fMax = -1e10, fMin = 1e10;
    float cMax = -1e10, cMin = 1e10;
    for (unsigned int i = 0; i < result.size(); i++)
    {
      rMax = std::max(rMax, result[i]);
      rMin = std::min(rMin, result[i]);
      fMax = std::max(fMax, friction[i]);
      fMin = std::min(fMin, friction[i]);
      cMax = std::max(cMax, costSurface[i]);
      cMin = std::min(cMin, costSurface[i]);
    }
//     qDebug() << fMin << fMax;
//     qDebug() << rMin << rMax;
//     QImage img(loader.getWidth(), loader.getHeight(), QImage::Format_RGB32);
// 
//     int* bits = (int*)img.bits();
//     for (unsigned int i = 0; i < result.size(); i++)
//     {
//       int r = std::min((int)(result[i] / rMax * 255.0), 255);
//       int f = 255 - friction[i] / fMax * 255.0;
//       int c = std::min((int)(costSurface[i] / cMax * 255.0), 255);
// 
//       if (result[i] < 0)
//       {
//         bits[i] = qRgb(0, 255, 0);
//       }
//       else if (friction[i] < 0)
//       {
//         bits[i] = qRgb(255, 0, 0);
//       }
//       else
//       {
//         int rr = 0;
//         if (r > 0)
//         {
//           rr = 100;
//         }
//         bits[i] = qRgb(rr, r, c);
//       }
//     }
//     img.save("ProbablePathCalculatorTest.png", "PNG");
  }