Direct Graphical Models  v.1.7.0
GraphDenseExt.cpp
1 #include "GraphDenseExt.h"
2 #include "GraphDense.h"
3 #include "EdgeModelPotts.h"
4 #include "macroses.h"
5 
6 namespace DirectGraphicalModels
7 {
8  void CGraphDenseExt::buildGraph(Size graphSize)
9  {
10  m_size = graphSize;
11 
13 
14  // 2D default potentials
15  Mat pots(graphSize, CV_32FC(m_graph.getNumStates()));
16  pots.setTo(1.0f / m_graph.getNumStates());
17  m_graph.addNodes(pots.clone().reshape(1, pots.cols * pots.rows));
18  }
19 
20  void CGraphDenseExt::setGraph(const Mat &pots)
21  {
22  m_size = pots.size();
23 
24  if (m_graph.getNumNodes() == pots.cols * pots.rows)
25  m_graph.setNodes(0, pots.clone().reshape(1, pots.cols * pots.rows));
26  else {
28  m_graph.addNodes(pots.clone().reshape(1, pots.cols * pots.rows));
29  }
30  }
31 
32  void CGraphDenseExt::addGaussianEdgeModel(Vec2f sigma, float weight, const std::function<void(const Mat& src, Mat& dst)> &semiMetricFunction)
33  {
34  Mat features;
35  Mat feature(1, 2, CV_32FC1);
36  float *pFeature = feature.ptr<float>(0);
37  for (int y = 0; y < m_size.height; y++)
38  for (int x = 0; x < m_size.width; x++) {
39  pFeature[0] = x / sigma.val[0];
40  pFeature[1] = y / sigma.val[1];
41  features.push_back(feature);
42  } // x
43 
44  m_graph.addEdgeModel(std::make_shared<CEdgeModelPotts>(features, weight, semiMetricFunction));
45  }
46 
47  void CGraphDenseExt::addBilateralEdgeModel(const Mat &featureVectors, Vec2f sigma, float sigma_opt, float weight, const std::function<void(const Mat& src, Mat& dst)> &semiMetricFunction)
48  {
49  const word nFeatures = featureVectors.channels();
50 
51  DGM_ASSERT_MSG(featureVectors.size() == m_size, "Resilution of the train image does not equal to the graph size");
52  Mat features;
53  Mat feature(1, 2 + nFeatures, CV_32FC1);
54  float *pFeature = feature.ptr<float>(0);
55  for (int y = 0; y < m_size.height; y++) {
56  const byte *pFv = featureVectors.ptr<byte>(y);
57  for (int x = 0; x < m_size.width; x++) {
58  pFeature[0] = x / sigma.val[0];
59  pFeature[1] = y / sigma.val[1];
60  for (word f = 0; f < nFeatures; f++)
61  pFeature[2 + f] = pFv[nFeatures * x + f] / sigma_opt;
62  features.push_back(feature);
63  } // x
64  } // y
65  m_graph.addEdgeModel(std::make_shared<CEdgeModelPotts>(features, weight, semiMetricFunction));
66  }
67 
68  void CGraphDenseExt::addBilateralEdgeModel(const vec_mat_t &featureVectors, Vec2f sigma, float sigma_opt, float weight, const std::function<void(const Mat& src, Mat& dst)> &semiMetricFunction)
69  {
70  const word nFeatures = static_cast<word>(featureVectors.size());
71 
72  DGM_ASSERT_MSG(!featureVectors.empty(), "The train image is empty");
73  DGM_ASSERT_MSG(featureVectors[0].size() == m_size, "Resilution of the train image does not equal to the graph size");
74  Mat features;
75  Mat feature(1, 2 + nFeatures, CV_32FC1);
76  float *pFeature = feature.ptr<float>(0);
77  for (int y = 0; y < m_size.height; y++) {
78  byte const **pFv = new const byte *[nFeatures];
79  for (word f = 0; f < nFeatures; f++) pFv[f] = featureVectors[f].ptr<byte>(y);
80  for (int x = 0; x < m_size.width; x++) {
81  pFeature[0] = x / sigma.val[0];
82  pFeature[1] = y / sigma.val[1];
83  for (int f = 0; f < nFeatures; f++)
84  pFeature[2 + f] = pFv[f][x] / sigma_opt;
85  features.push_back(feature);
86  } // x
87  } // y
88  m_graph.addEdgeModel(std::make_shared<CEdgeModelPotts>(features, weight, semiMetricFunction));
89  }
90 }
byte getNumStates(void) const
Returns number of states (classes)
Definition: Graph.h:99
void buildGraph(Size graphSize) override
Builds a 2D graph of size corresponding to the image resolution.
size_t getNumNodes(void) const override
Returns the number of nodes in the graph.
Definition: GraphDense.h:40
void addNodes(const Mat &pots) override
Adds the graph nodes with potentials.
Definition: GraphDense.h:29
void addEdgeModel(const ptr_edgeModel_t &pEdgeModel)
Adds an edge model.
Definition: GraphDense.h:49
void setNodes(size_t start_node, const Mat &pots) override
Fills the graph nodes with new potentials.
Definition: GraphDense.cpp:25
void reset(void) override
Resets the graph.
Definition: GraphDense.h:26
CGraphDense & m_graph
The graph.
Definition: GraphDenseExt.h:96
Size m_size
Size of the 2D graph.
Definition: GraphDenseExt.h:97
void addGaussianEdgeModel(Vec2f sigma, float weight=1.0f, const std::function< void(const Mat &src, Mat &dst)> &semiMetricFunction={})
Add a Gaussian potential model with standard deviation sigma.
void setGraph(const Mat &pots) override
Fills an existing 2D graph with potentials or builds a new 2D graph of size corresponding to pots...
void addBilateralEdgeModel(const Mat &featureVectors, Vec2f sigma, float sigma_opt=1.0f, float weight=1.0f, const std::function< void(const Mat &src, Mat &dst)> &semiMetricFunction={})
Add a Bilateral pairwise potential with spacial standard deviations sigma and color standard deviatio...