Direct Graphical Models  v.1.7.0
Decode.cpp
1 #include "Decode.h"
2 #include "Graph.h"
3 #include "macroses.h"
4 
5 namespace DirectGraphicalModels
6 {
7  vec_byte_t CDecode::decode(const CGraph &graph, Mat &lossMatrix)
8  {
9  size_t nNodes = graph.getNumNodes(); // number of nodes
10  vec_byte_t res(nNodes);
11  Mat pot;
12  bool ifLossMat = !lossMatrix.empty();
13 
14  // Getting optimal state
15  for (size_t n = 0; n < nNodes; n++) { // all nodes
16  graph.getNode(n, pot);
17  if (ifLossMat) gemm(lossMatrix, pot, 1.0, Mat(), 0.0, pot);
18 
19  Point extremumLoc;
20  if (ifLossMat) minMaxLoc(pot, NULL, NULL, &extremumLoc, NULL);
21  else minMaxLoc(pot, NULL, NULL, NULL, &extremumLoc);
22  res[n] = static_cast<byte>(extremumLoc.y);
23  } // n
24 
25  return res;
26  }
27 
29  {
30  Mat res(nStates, nStates, CV_32FC1, Scalar(1.0f));
31  for (byte i = 0; i < nStates; i++) res.at<float>(i,i) = 0.0f;
32  return res;
33  }
34 }
virtual vec_byte_t decode(Mat &lossMatrix=EmptyMat) const
Approximate decoding.
Definition: Decode.h:38
virtual size_t getNumNodes(void) const =0
Returns the number of nodes in the graph.
virtual void getNode(size_t node, Mat &pot) const =0
Returns the node potential.
Interface class for graphical models.
Definition: Graph.h:14
static Mat getDefaultLossMatrix(byte nStates)
Returns a default loss matrix .
Definition: Decode.cpp:28