iRoCS Toolbox  1.1.0
helpers-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 HELPERS_INL_HH
24 #define HELPERS_INL_HH
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.hh>
28 #endif
29 
30 #include <vector>
31 #include <map>
32 #include <list>
33 #include <set>
34 #include <blitz/array.h>
35 
36 namespace segmentation {
37 
38 
39 
51 template<typename T>
52 std::vector<blitz::Array<T, 2> > collectSelectedClusterProfiles(
53  const blitz::Array<T, 2>& profiles,
54  const std::vector< ptrdiff_t >& labels,
55  const std::set< ptrdiff_t >& selected_clusters) {
56 
57  using std::vector;
58  using std::list;
59  using std::map;
60  typedef list<blitz::Array<float, 1> > ClusterList;
61 
62  // restructure the profiles by cluster id
63  // only consider selected clusters
64  map<const ptrdiff_t, ClusterList > profiles_by_cluster;
65  // build list of profiles for each cluster
66  for (ptrdiff_t row = 0; row < profiles.extent(0); ++row) {
67  ptrdiff_t label = labels[row];
68  // if selected
69  if (selected_clusters.find(label) != selected_clusters.end()) {
70  ClusterList &cluster_list = profiles_by_cluster[label];
71  blitz::Array<float, 1> profile = profiles(row, blitz::Range::all());
72  cluster_list.push_back(profile);
73  }
74  }
75 
76  // now build blitz Array with profiles for each selected cluster
77  std::vector<blitz::Array<T, 2> > selected_profiles(selected_clusters.size());
78  ptrdiff_t num_samples = profiles.extent(1);
79  int i = 0;
80  for(std::set<ptrdiff_t>::const_iterator it = selected_clusters.begin();
81  it != selected_clusters.end(); ++it, ++i){
82  ptrdiff_t label = *it;
83  ClusterList &cluster_list = profiles_by_cluster[label];
84  blitz::Array<float, 2> &cluster_profiles = selected_profiles[i];
85  cluster_profiles.resize(cluster_list.size(), num_samples);
86  int row = 0;
87  for (ClusterList::const_iterator it = cluster_list.begin();
88  it != cluster_list.end(); ++it, ++row) {
89  cluster_profiles(row, blitz::Range::all()) = *it;
90  }
91  }
92  return selected_profiles;
93 }
94 
95 }
96 
97 #endif
std::vector< blitz::Array< T, 2 > > collectSelectedClusterProfiles(const blitz::Array< T, 2 > &profiles, const std::vector< ptrdiff_t > &labels, const std::set< ptrdiff_t > &selected_clusters)
Builds a vector of blitz Arrays (profiles) for selected clusters each element of the vector correspon...
Definition: helpers-inl.hh:52
bool all(blitz::TinyMatrix< bool, NRows, NColumns > const &matrix)
all() reduction for boolean blitz::TinyMatrix.