Direct Graphical Models  v.1.7.0
PDFHistogram2D.cpp
1 #include "PDFHistogram2D.h"
2 
3 namespace DirectGraphicalModels
4 {
5 // Constructor
7 {
8  memset(m_data, 0, 256 * 256 * sizeof(long));
9 }
10 
11 // Destructor
13 { }
14 
16 {
17  memset(m_data, 0, 256 * 256 * sizeof(long));
18  m_nPoints = 0;
19 }
20 
21 void CPDFHistogram2D::addPoint(Scalar point)
22 {
23  byte x = static_cast<byte>(MIN(255, MAX(0, point[0])));
24  byte y = static_cast<byte>(MIN(255, MAX(0, point[1])));
25  m_data[x][y]++;
26  m_nPoints++;
27 }
28 
29 double CPDFHistogram2D::getDensity(Scalar point)
30 {
31  byte x = static_cast<byte>(MIN(255, MAX(0, point[0])));
32  byte y = static_cast<byte>(MIN(255, MAX(0, point[1])));
33  return m_nPoints ? static_cast<double>(m_data[x][y]) / m_nPoints : 0;
34 }
35 
37 {
38  long tmp[256][256];
39  for (int iter = 0; iter < nIt; iter++) {
40  memcpy(tmp, m_data, 256 * 256 * sizeof(long));
41  for (int x = 1; x < 255; x++)
42  for (int y = 1; y < 255; y++)
43  m_data[x][y] = static_cast<long>(0.125 * (tmp[x][y-1] + tmp[x-1][y] + 4 * tmp[x][y] + tmp[x+1][y] + tmp[x][y+1]));
44  } // iterations
45 }
46 
47 void CPDFHistogram2D::saveFile(FILE *pFile) const
48 {
49  fwrite(&m_data, sizeof(long), 256 * 256, pFile);
50  fwrite(&m_nPoints, sizeof(long), 1, pFile);
51 }
52 
53 void CPDFHistogram2D::loadFile(FILE *pFile)
54 {
55  fread(&m_data, sizeof(long), 256 * 256, pFile);
56  fread(&m_nPoints, sizeof(long), 1, pFile);
57 }
58 }
virtual void reset(void)
Resets class variables.
virtual void saveFile(FILE *pFile) const
Saves the random model into the file.
long m_nPoints
The number of samples, added with the addPoint() function.
Definition: IPDF.h:54
void smooth(int nIt=1)
Performs the gaussian smoothing on histogram.
Interface class for Probability Density Function (PDF)
Definition: IPDF.h:16
virtual void loadFile(FILE *pFile)
Loads the random model from the file.
virtual void addPoint(Scalar point)
Adds a sample point for PDF estimation.
virtual double getDensity(Scalar point)
Returns the probability density value for the argument point.