Direct Graphical Models  v.1.7.0
Prior.cpp
1 #include "Prior.h"
2 
3 namespace DirectGraphicalModels
4 {
5  // Constructor
6  CPrior::CPrior(byte nStates, RandomModelType type) : CBaseRandomModel(nStates), m_type(type)
7  {
8  const int size[] = { m_nStates, m_nStates, m_nStates};
9  m_histogramPrior = Mat(type, size, CV_32SC1);
10  m_histogramPrior.setTo(0);
11  }
12 
13  // Destructor
15  {
16  m_histogramPrior.release();
17  }
18 
19  void CPrior::reset(void)
20  {
21  m_histogramPrior.setTo(0);
22  }
23 
24  Mat CPrior::getPrior(float weight) const
25  {
26  if (sum(m_histogramPrior)[0] < 1) // if addXXXGroundTruth() was not called
27  return Mat(m_nStates, m_nStates, CV_32FC1, Scalar(1.0f)); // return uniform distribution
28 
29  Mat res = calculatePrior();
30  if (weight != 1.0f) res.convertTo(res, res.type(), weight);
31  return res;
32  }
33 
34  void CPrior::saveFile(FILE *pFile) const
35  {
36  switch(m_type) {
37  case RM_UNARY: //---------------------------------------------------------------------------------
38  for (byte y = 0; y < m_nStates; y++)
39  fwrite(&m_histogramPrior.at<int>(y, 0), sizeof(int), 1, pFile);
40  break;
41  case RM_PAIRWISE: //---------------------------------------------------------------------------------
42  for (byte y = 0; y < m_nStates; y++)
43  for (byte x = 0; x < m_nStates; x++)
44  fwrite(&m_histogramPrior.at<int>(y, x), sizeof(int), 1, pFile);
45  break;
46  case RM_TRIPLET: //---------------------------------------------------------------------------------
47  for (byte z = 0; z < m_nStates; z++)
48  for (byte y = 0; y < m_nStates; y++)
49  for (byte x = 0; x < m_nStates; x++)
50  fwrite(&m_histogramPrior.at<int>(z, y, x), sizeof(int), 1, pFile);
51  break;
52  }
53  }
54 
55  void CPrior::loadFile(FILE *pFile)
56  {
57  switch(m_type) {
58  case RM_UNARY: //---------------------------------------------------------------------------------
59  for (byte y = 0; y < m_nStates; y++)
60  fread(&m_histogramPrior.at<int>(y, 0), sizeof(int), 1, pFile);
61  break;
62  case RM_PAIRWISE: //---------------------------------------------------------------------------------
63  for (byte y = 0; y < m_nStates; y++)
64  for (byte x = 0; x < m_nStates; x++)
65  fread(&m_histogramPrior.at<int>(y, x), sizeof(int), 1, pFile);
66  break;
67  case RM_TRIPLET: //---------------------------------------------------------------------------------
68  for (byte z = 0; z < m_nStates; z++)
69  for (byte y = 0; y < m_nStates; y++)
70  for (byte x = 0; x < m_nStates; x++)
71  fread(&m_histogramPrior.at<int>(z, y, x), sizeof(int), 1, pFile);
72  break;
73  }
74  }
75 }
Unary random model: no iteraction between nodes.
virtual void loadFile(FILE *pFile)
Loads the random model from the file.
Definition: Prior.cpp:55
RandomModelType
Random model types.
Mat m_histogramPrior
The class cooccurance histogram.
Definition: Prior.h:51
Base abstract class for random model training.
void reset(void)
Resets class variables.
Definition: Prior.cpp:19
virtual void saveFile(FILE *pFile) const
Saves the random model into the file.
Definition: Prior.cpp:34
Mat getPrior(float weight=1.0f) const
Returns the prior probabilies.
Definition: Prior.cpp:24
CPrior(byte nStates, RandomModelType type)
Constructor.
Definition: Prior.cpp:6
virtual Mat calculatePrior(void) const =0
Calculates the prior probabilies.
Pairwise random model: maximum two nodes in the cliques.
Triplet random model: maximum tree nodes in the cliques.
byte m_nStates
The number of states (classes)
RandomModelType m_type
Type of the random model (RandomModelType)
Definition: Prior.h:55