Direct Graphical Models  v.1.7.0
GraphDense.cpp
1 #include "GraphDense.h"
2 #include "macroses.h"
3 
4 namespace DirectGraphicalModels
5 {
6  // Add a new node to the graph with specified potentional
7  size_t CGraphDense::addNode(const Mat &pot)
8  {
9  size_t res = getNumNodes();
10  m_nodePotentials.push_back(pot.empty() ? Mat(1, getNumStates(), CV_32FC1, Scalar(1.0f / getNumStates())) : pot.t());
11  return res;
12  }
13 
14  // Set or change the potential of node idx
15  void CGraphDense::setNode(size_t node, const Mat &pot)
16  {
17  // Assertions
18  DGM_ASSERT_MSG(node < getNumNodes(), "Node %zu is out of range %zu", node, getNumNodes());
19  DGM_ASSERT_MSG((pot.cols == 1) && (pot.rows == getNumStates()), "Potential size (%d x %d) does not match (%d x %d)", pot.cols, pot.rows, 1, getNumStates());
20  DGM_ASSERT_MSG(pot.type() == CV_32FC1, "Potential type is not CV_32FC1");
21 
22  m_nodePotentials.row(static_cast<int>(node)) = pot.t();
23  }
24 
25  void CGraphDense::setNodes(size_t start_node, const Mat &pots)
26  {
27  // Assertions
28  DGM_ASSERT_MSG(start_node + pots.rows < getNumNodes(), "Node %zu is out of range %zu", start_node + pots.rows, getNumNodes());
29  DGM_ASSERT_MSG(pots.cols == getNumStates(), "Potential size (%d) does not match (%d)", pots.cols, getNumStates());
30  DGM_ASSERT_MSG(pots.type() == CV_32FC1, "Potentials type is not CV_32FC1");
31 
32  pots.copyTo(m_nodePotentials(Rect(0, static_cast<int>(start_node), getNumStates(), pots.rows)));
33  }
34 
35  // Return node potential vector
36  void CGraphDense::getNode(size_t node, Mat &pot) const
37  {
38  DGM_ASSERT_MSG(node < getNumNodes(), "Node %zu is out of range %zu", node, getNumNodes());
39  if (pot.empty() || pot.cols != 1 || pot.rows != getNumStates() || pot.type() != CV_32FC1)
40  pot = Mat(getNumStates(), 1, CV_32FC1);
41 
42  const float *pPot = m_nodePotentials.ptr<float>(static_cast<int>(node));
43  for (byte s = 0; s < getNumStates(); s++)
44  pot.at<float>(s, 0) = pPot[s];
45  }
46 
47  void CGraphDense::getNodes(size_t start_node, size_t num_nodes, Mat &pots) const
48  {
49  if (!num_nodes) num_nodes = getNumNodes() - start_node;
50  DGM_ASSERT_MSG(start_node + num_nodes <= getNumNodes(), "The given ranges exceed the number of nodes(%zu)", getNumNodes());
51 // if (pots.empty() || pots.cols != getNumStates() || pots.rows != num_nodes || pots.type() != CV_32FC1)
52 // pots = Mat(static_cast<int>(num_nodes), getNumStates(), CV_32FC1);
53  m_nodePotentials(Rect(0, static_cast<int>(start_node), getNumStates(), static_cast<int>(num_nodes))).copyTo(pots);
54  }
55 
56  void CGraphDense::getChildNodes(size_t node, vec_size_t &vNodes) const
57  {
58  DGM_ASSERT_MSG(node < getNumNodes(), "Node %zu is out of range %zu", node, getNumNodes());
59  if (!vNodes.empty()) vNodes.clear();
60  for (size_t i = 0; i < getNumNodes(); i++)
61  if (i != node)
62  vNodes.push_back(i);
63  }
64 }
byte getNumStates(void) const
Returns number of states (classes)
Definition: Graph.h:99
size_t getNumNodes(void) const override
Returns the number of nodes in the graph.
Definition: GraphDense.h:40
size_t addNode(const Mat &pot=EmptyMat) override
Adds an additional node (with specified potentional)
Definition: GraphDense.cpp:7
Mat m_nodePotentials
The container for the node potentials: Mat(nNodes, nStates, CV_32FC1), i.e. every row is a node poten...
Definition: GraphDense.h:64
void setNodes(size_t start_node, const Mat &pots) override
Fills the graph nodes with new potentials.
Definition: GraphDense.cpp:25
void getNode(size_t node, Mat &pot) const override
Returns the node potential.
Definition: GraphDense.cpp:36
void setNode(size_t node, const Mat &pot) override
Sets or changes the potential of node.
Definition: GraphDense.cpp:15
void getChildNodes(size_t node, vec_size_t &vNodes) const override
Returns the set of IDs of the child nodes of the argument node.
Definition: GraphDense.cpp:56
void getNodes(size_t start_node, size_t num_nodes, Mat &pots) const override
Returns the node potentials.
Definition: GraphDense.cpp:47