iRoCS Toolbox  1.1.0
SegmentationModel-inl.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2015 Margret Keuper, Thorsten Falk
4  *
5  * Image Analysis Lab, University of Freiburg, Germany
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  **************************************************************************/
22 
23 #ifndef LIBSEGMENTATION_SRC_SEGMENTATION_MODEL_HH
24 #define LIBSEGMENTATION_SRC_SEGMENTATION_MODEL_HH
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.hh>
28 #endif
29 
30 #include "SegmentationModel.hh"
31 #include "NormalPDF.hh"
33 
34 namespace segmentation {
35 
36 
37 template<typename T>
39  pdfs_.push_back(pdf);
40 }
41 
42 template<typename T>
44  for (size_t i = 0; i < pdfs_.size(); ++i)
45  delete pdfs_[i];
46 }
47 
48 template<typename T>
50  pdfs_.clear();
51 }
52 
53 template<typename T>
55  return pdfs_.size();
56 }
57 
58 template<typename T>
59 void SegmentationModel<T>::load(const std::string &filename) {
60  clear();
61  BlitzH5File file(filename, BlitzH5File::ReadOnly);
62  // sampling
63  file.readAttribute(num_samples_, "num_samples", "/libsegmentation");
64  file.readAttribute(sampling_dist_um_, "sampling_dist_um", "/libsegmentation");
65 
66  // pdfs
67  size_t num_pdfs;
68  file.readAttribute(num_pdfs, "num_pdfs", "/libsegmentation");
69  pdfs_.resize(num_pdfs);
70  for (size_t i = 0; i < num_pdfs; ++i) {
71  std::stringstream pdf_prefix;
72  pdf_prefix << "/libsegmentation/pdf_" << i;
73 
74  blitz::Array<T, 2> information;
75  blitz::Array<T, 1> mean;
76  file.readDataset(mean, pdf_prefix.str() + "/mean");
77  file.readDataset(information, pdf_prefix.str() + "/information");
78  pdfs_[i] = new segmentation::NormalPDF<T>(mean, information);
79  }
80 }
81 template<typename T>
82 void SegmentationModel<T>::save(const std::string &filename) {
83  BlitzH5File file(filename, BlitzH5File::Replace);
84 
85  // sampling
86  file.writeAttribute(num_samples_, "num_samples", "/libsegmentation");
87  file.writeAttribute(
88  sampling_dist_um_, "sampling_dist_um", "/libsegmentation");
89  // write pdfs
90  file.writeAttribute(pdfs_.size(), "num_pdfs", "/libsegmentation");
91  for (size_t i = 0; i < pdfs_.size(); ++i) {
92  std::stringstream pdf_prefix;
93  pdf_prefix << "/libsegmentation/pdf_" << i;
94  const segmentation::NormalPDF<T> *pdf = pdfs_[i];
95  file.writeDataset(pdf->mean_, pdf_prefix.str() + "/mean");
96  file.writeDataset(pdf->covariance_, pdf_prefix.str() + "/information");
97  }
98 }
99 
100 }
101 #endif //LIBSEGMENTATION_SRC_SEGMENTATION_MODEL_HH
void save(const std::string &filename)
loads model from hdf5-file
~SegmentationModel()
A segmentation model ist just a list of Normal PDFs and the parameters that where used for sampling...
blitz::Array< T, 1 > mean_
Definition: NormalPDF.hh:51
void writeAttribute(DataT const &in, std::string const &attName, std::string const &objectName="/")
Writes an attribute.
void appendPdf(NormalPDF< T > *pdf)
Lightweight alternative to libBlitzHDF5 providing its basic functionality.
void writeDataset(DataT const &data, std::string const &name)
Writes a simple data set without chunking or compression.
void readAttribute(DataT &data, std::string const &attName, std::string const &objectName="/") const
Reads an attribute.
void load(const std::string &filename)
saves model as hdf5-file
Encapsulates a normal pdf.
Definition: NormalPDF.hh:40
blitz::Array< T, 2 > covariance_
Definition: NormalPDF.hh:52
void readDataset(FixedNumericT &data, std::string const &name) const
Reads a simple, fixed size numeric data set.