LArOpenCV  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
Public Member Functions | Private Attributes | List of all members
larcv::ImageClusterManager Class Reference

Class to execute a chain of algorithms to a given image input. More...

#include <ImageClusterManager.h>

Inheritance diagram for larcv::ImageClusterManager:
larcv::laropencv_base

Public Member Functions

 ImageClusterManager (const std::string name="ImageClusterManager")
 Default constructor. More...
 
 ~ImageClusterManager ()
 Default destructor. More...
 
const std::string & Name () const
 Name accessor, used to identify a block of configuration parameters via fhicl. More...
 
void Reset ()
 Clears attributes except for _name. More...
 
ImageClusterBaseGetAlg (const AlgorithmID_t id) const
 Algorithm adder: returns a unique identifier with which you can retrieve back. More...
 
ImageClusterBaseGetAlg (const std::string name) const
 Algorithm getter via unique identifier (string name) More...
 
AlgorithmID_t GetAlgID (const std::string name) const
 Algorithm ID getter via unique identifier (string name) More...
 
void Configure (const ::fcllite::PSet &main_cfg)
 Read-in configuration object & enforce configurations to algorithms. More...
 
void Process (const ::cv::Mat &img, const larcv::ImageMeta &meta)
 Execute algorithms to construct clusters + corresponding meta data. More...
 
const ImageMetaMetaData (const AlgorithmID_t alg_id) const
 Accessor to a specific meta data constructed by an algorithm (via algorithm id) More...
 
const Contour_tCluster (const ClusterID_t cluster_id, const AlgorithmID_t alg_id=kINVALID_ALGO_ID) const
 Accessor to a specific cluster constructed by an algorithm (via algorithm + cluster id) More...
 
const ContourArray_tClusters (const AlgorithmID_t alg_id=kINVALID_ALGO_ID) const
 Accessor to a set of clusters constructed by an algorithm (via algorithm id) More...
 
ClusterID_t ClusterID (const double x, const double y, AlgorithmID_t alg_id=kINVALID_ALGO_ID) const
 For a specified algorithm, find a cluster that contains coordinate (x,y). By default "last algorithm" is used. More...
 
void Report () const
 Report process summary. More...
 
const larcv::loggerlogger () const
 Logger getter. More...
 
void set_verbosity (::larcv::msg::Level_t level)
 Verbosity level. More...
 

Private Attributes

std::string _name
 Name identifier: used to fetch a block of configuration parameters. More...
 
bool _configured
 Boolean flag to enforce Configure method to be called before Process. More...
 
std::vector
< larcv::ImageClusterBase * > 
_alg_v
 Array of algorithms to be executed. More...
 
std::map< std::string,
larcv::AlgorithmID_t
_alg_m
 Map of algorithm instance name to ID. More...
 
std::vector
< larcv::ContourArray_t
_clusters_v
 Array of resulting clusters per algorithms. More...
 
std::vector< larcv::ImageMeta_meta_v
 Array of meta data: one per set of clusters (updated by each algorithm) More...
 
larcv::ImageMeta _orig_meta
 Original meta data. More...
 
bool _profile
 Boolean flag to measure process time + report. More...
 
Watch _watch
 Stopwatch. More...
 
size_t _process_count
 Process counter (cumulative) More...
 
double _process_time
 Process time (cumulative) More...
 

Detailed Description

Class to execute a chain of algorithms to a given image input.

larcv::ImageClusterManager executes a chain of algorithms (ImageClusterBase inherit class instances)
to a provided cv::Mat image object. Each algorithm receives an image, clusters from a previous algorithm
(for the 1st algorithm this is empty), and a meta data that needs to be modified by each algorithm to interpret
the returning clusters' coordinate. After execution of algorithms it stores resulting clusters (many per algorithm)
and a meta data (one per algorithm) that are accessible via unique id (larcv::AlgorithmID_t and larcv::ClusterID_t).
At the execution of algorithms, a user must provide both image (cv::Mat) and image meta data (larcv::ImageMeta).
Each cluster is represented as a 2D contour (larcv::Contour_t), and the class provides a utility method to identify
a cluster that contains a specific 2D point (x,y) in original coordinate system. This is possible via provided
meta data for an original image that contains image origin (left bottom corner) in the original coordinate, the width
and height of an image, etc. + updated meta data from each algorithm execution.

Definition at line 34 of file ImageClusterManager.h.

Constructor & Destructor Documentation

larcv::ImageClusterManager::ImageClusterManager ( const std::string  name = "ImageClusterManager")

Default constructor.

Definition at line 10 of file ImageClusterManager.cxx.

References LARCV_DEBUG, and Reset().

11  : laropencv_base(name)
12  , _name(name)
13  , _configured(false)
14  , _profile(true)
15  {
16  LARCV_DEBUG((*this)) << "start" << std::endl;
17  Reset();
18  LARCV_DEBUG((*this)) << "end" << std::endl;
19  }
larcv::ImageClusterManager::~ImageClusterManager ( )
inline

Default destructor.

Definition at line 42 of file ImageClusterManager.h.

42 {}

Member Function Documentation

const Contour_t & larcv::ImageClusterManager::Cluster ( const ClusterID_t  cluster_id,
const AlgorithmID_t  alg_id = kINVALID_ALGO_ID 
) const

Accessor to a specific cluster constructed by an algorithm (via algorithm + cluster id)

Definition at line 174 of file ImageClusterManager.cxx.

References Clusters().

175  {
176  auto const& clusters = Clusters(alg_id);
177  if(cluster_id >= clusters.size()) throw larbys("Invalid cluster ID requested");
178  return clusters[cluster_id];
179  }
ClusterID_t larcv::ImageClusterManager::ClusterID ( const double  x,
const double  y,
AlgorithmID_t  alg_id = kINVALID_ALGO_ID 
) const

For a specified algorithm, find a cluster that contains coordinate (x,y). By default "last algorithm" is used.

Definition at line 188 of file ImageClusterManager.cxx.

References _clusters_v, _meta_v, larcv::kINVALID_ALGO_ID, and larcv::kINVALID_CLUSTER_ID.

189  {
190 
192 
193  if(_clusters_v.empty()) return result;
194 
195  if(alg_id == kINVALID_ALGO_ID) alg_id = _clusters_v.size() - 1;
196 
197  auto const& clusters = _clusters_v[alg_id];
198 
199  auto const& meta = _meta_v[alg_id];
200 
201  auto const& origin = meta.origin();
202 
203  if(x < origin.x || x > (origin.x + meta.width())) return result;
204 
205  if(y < origin.y || x > (origin.y + meta.height())) return result;
206 
207  //std::cout<<"Inspecting a point "<<x<<" : "<<y<<" ... ";
208 
209  auto pt = ::cv::Point2d((x-origin.x)/meta.pixel_width(),(y-origin.y)/meta.pixel_height());
210 
211  //std::cout<<pt.x<<" : "<<pt.y<<std::endl;
212 
213  for(size_t id=0; id<clusters.size(); ++id) {
214 
215  auto const& c = clusters[id];
216 
217  double inside = ::cv::pointPolygonTest(c,pt,false);
218 
219  if(inside < 0) continue;
220 
221  result = id;
222 
223  break;
224  }
225 
226  return result;
227  }
const ContourArray_t & larcv::ImageClusterManager::Clusters ( const AlgorithmID_t  alg_id = kINVALID_ALGO_ID) const

Accessor to a set of clusters constructed by an algorithm (via algorithm id)

Definition at line 181 of file ImageClusterManager.cxx.

References _clusters_v, and larcv::kINVALID_ALGO_ID.

Referenced by Cluster().

182  {
183  if(alg_id < _clusters_v.size()) return _clusters_v[alg_id];
184  if(alg_id == kINVALID_ALGO_ID && _clusters_v.size()) return _clusters_v.back();
185  throw larbys("Execution of an algorithm not yet done!");
186  }
void larcv::ImageClusterManager::Configure ( const ::fcllite::PSet &  main_cfg)

Read-in configuration object & enforce configurations to algorithms.

Definition at line 60 of file ImageClusterManager.cxx.

References _alg_m, _alg_v, _configured, _profile, larcv::ImageClusterFactory::get(), LARCV_CRITICAL, LARCV_DEBUG, larcv::laropencv_base::logger(), and larcv::laropencv_base::set_verbosity().

61  {
62  LARCV_DEBUG((*this)) << "start" << std::endl;
63  _profile = main_cfg.get<bool>("Profile");
64 
65  this->set_verbosity((msg::Level_t)(main_cfg.get<unsigned short>("Verbosity",(unsigned short)(this->logger().level()))));
66 
67  std::vector<std::string> instance_type_v = main_cfg.get<std::vector<std::string> >("AlgoType");
68  std::vector<std::string> instance_name_v = main_cfg.get<std::vector<std::string> >("AlgoName");
69 
70  if(instance_type_v.size() != instance_name_v.size()) {
71  LARCV_CRITICAL((*this)) << "AlgoType and AlgoName config parameters have different length! "
72  << "(" << instance_type_v.size() << " vs. " << instance_name_v.size() << ")" << std::endl;
73  throw larbys();
74  }
75 
76  _alg_v.clear();
77  _alg_m.clear();
78  for(size_t i=0; i<instance_type_v.size(); ++i) {
79  auto const& name = instance_name_v[i];
80  auto const& type = instance_type_v[i];
81  if(_alg_m.find(name) != _alg_m.end()) {
82  LARCV_CRITICAL((*this)) << "Duplicate algorithm name found!" << std::endl;
83  throw larbys("Duplicate algorithm name found!");
84  }
85 
86  _alg_m[name] = _alg_v.size();
87  _alg_v.push_back(ImageClusterFactory::get().create(type,name));
88  }
89 
90  for(auto& ptr : _alg_v) {
91  ptr->Profile(_profile);
92  ptr->set_verbosity(this->logger().level());
93  ptr->Configure(main_cfg.get_pset(ptr->Name()));
94  }
95  _configured=true;
96  LARCV_DEBUG((*this)) << "end" << std::endl;
97  }
ImageClusterBase * larcv::ImageClusterManager::GetAlg ( const AlgorithmID_t  id) const

Algorithm adder: returns a unique identifier with which you can retrieve back.

Algorithm getter via unique identifier (AlgorithmID_t)

Definition at line 32 of file ImageClusterManager.cxx.

References _alg_v.

Referenced by GetAlg().

33  {
34  if(id >= _alg_v.size()) throw larbys("Invalid algorithm ID requested...");
35  return _alg_v[id];
36  }
ImageClusterBase * larcv::ImageClusterManager::GetAlg ( const std::string  name) const

Algorithm getter via unique identifier (string name)

Definition at line 38 of file ImageClusterManager.cxx.

References GetAlg(), and GetAlgID().

39  {
40  return GetAlg(GetAlgID(name));
41  }
AlgorithmID_t larcv::ImageClusterManager::GetAlgID ( const std::string  name) const

Algorithm ID getter via unique identifier (string name)

Definition at line 43 of file ImageClusterManager.cxx.

References _alg_m, and larcv::kINVALID_ALGO_ID.

Referenced by GetAlg().

44  {
45  auto iter = _alg_m.find(name);
46  if(iter == _alg_m.end()) return kINVALID_ALGO_ID;
47  return (*iter).second;
48  }
const larcv::logger& larcv::laropencv_base::logger ( ) const
inlineinherited

Logger getter.

Definition at line 43 of file laropencv_base.h.

References larcv::laropencv_base::_logger.

Referenced by Configure().

44  { return *_logger; }
const ImageMeta & larcv::ImageClusterManager::MetaData ( const AlgorithmID_t  alg_id) const

Accessor to a specific meta data constructed by an algorithm (via algorithm id)

Definition at line 167 of file ImageClusterManager.cxx.

References _meta_v.

168  {
169  if(alg_id < _meta_v.size()) return _meta_v[alg_id];
170  if(alg_id >= _meta_v.size()) throw larbys("Invalid algorithm ID requested");
171  throw larbys("Execution of an algorithm not yet done!");
172  }
const std::string& larcv::ImageClusterManager::Name ( ) const
inline

Name accessor, used to identify a block of configuration parameters via fhicl.

Definition at line 45 of file ImageClusterManager.h.

References _name.

Referenced by Report().

45 { return _name; }
void larcv::ImageClusterManager::Process ( const ::cv::Mat &  img,
const larcv::ImageMeta meta 
)

Execute algorithms to construct clusters + corresponding meta data.

Definition at line 112 of file ImageClusterManager.cxx.

References _alg_v, _clusters_v, _configured, _meta_v, _orig_meta, _process_count, _process_time, _watch, LARCV_DEBUG, larcv::Watch::Start(), and larcv::Watch::WallTime().

113  {
114  LARCV_DEBUG((*this)) << "start" << std::endl;
115 
116  if(!_configured) throw larbys("Must Configure() before Process()!");
117 
118  if(meta.num_pixel_row()!=img.rows)
119  throw larbys("Provided metadata has incorrect # horizontal pixels w.r.t. image!");
120 
121  if(meta.num_pixel_column()!=img.cols)
122  throw larbys("Provided metadata has incorrect # vertical pixels w.r.t. image!");
123 
124  _watch.Start();
125 
126  _orig_meta = meta;
127  _meta_v.clear();
128  _clusters_v.clear();
129  _clusters_v.reserve(_alg_v.size());
130  _meta_v.reserve(_alg_v.size());
131 
132  for(auto& alg_ptr : _alg_v) {
133 
134  if(!alg_ptr) throw larbys("Invalid algorithm pointer!");
135 
136  if(alg_ptr == _alg_v.front()) {
137 
138  ContourArray_t clusters;
139  _meta_v.push_back(_orig_meta);
140  _clusters_v.emplace_back(alg_ptr->Process(clusters,img,_meta_v.back()));
141 
142  }else{
143 
144  auto const& clusters = _clusters_v.back();
145  _meta_v.push_back(_orig_meta);
146  _clusters_v.emplace_back(alg_ptr->Process(clusters,img,_meta_v.back()));
147 
148  }
149  // Sanity check on meta data
150  auto const& result_meta = _meta_v.back();
151  if(result_meta.height() > _orig_meta.height()) {
152  std::cerr << "Return meta data by " << alg_ptr->Name() << " exceeds original image height!" << std::endl;
153  throw larbys();
154  }
155  if(result_meta.width() > _orig_meta.width()) {
156  std::cerr << "Return meta data by " << alg_ptr->Name() << " exceeds original image width!" << std::endl;
157  throw larbys();
158  }
159 
160  }
161 
163  ++_process_count;
164  LARCV_DEBUG((*this)) << "end" << std::endl;
165  }
void larcv::ImageClusterManager::Report ( ) const

Report process summary.

Definition at line 99 of file ImageClusterManager.cxx.

References _alg_v, _process_count, _process_time, and Name().

100  {
101 
102  std::cout << " ================== " << Name() << " Profile Report ==================" << std::endl
103  << " # Process call = " << _process_count << " ... Total time = " << _process_time << " [s]" << std::endl;
104  for(auto const& ptr : _alg_v) {
105  std::cout << Form(" \033[93m%-20s\033[00m ... # call %-5zu ... total time %g [s] ... average time %g [s/process]",
106  ptr->Name().c_str(), ptr->ProcessCount(), ptr->ProcessTime(), ptr->ProcessTime() / (double)(ptr->ProcessCount()))
107  << std::endl;
108  }
109 
110  }
void larcv::ImageClusterManager::Reset ( )

Clears attributes except for _name.

Definition at line 21 of file ImageClusterManager.cxx.

References _alg_v, _clusters_v, _configured, _process_count, _process_time, and LARCV_DEBUG.

Referenced by ImageClusterManager().

22  {
23  LARCV_DEBUG((*this)) << "start" << std::endl;
24  _configured = false;
25  _alg_v.clear();
26  _clusters_v.clear();
28  _process_time=0;
29  LARCV_DEBUG((*this)) << "end" << std::endl;
30  }
void larcv::laropencv_base::set_verbosity ( ::larcv::msg::Level_t  level)
inlineinherited

Verbosity level.

Definition at line 47 of file laropencv_base.h.

References larcv::laropencv_base::_logger.

Referenced by larcv::ImageClusterBase::Configure(), and Configure().

48  { _logger->set(level); }

Member Data Documentation

std::map<std::string,larcv::AlgorithmID_t> larcv::ImageClusterManager::_alg_m
private

Map of algorithm instance name to ID.

Definition at line 79 of file ImageClusterManager.h.

Referenced by Configure(), and GetAlgID().

std::vector<larcv::ImageClusterBase*> larcv::ImageClusterManager::_alg_v
private

Array of algorithms to be executed.

Definition at line 77 of file ImageClusterManager.h.

Referenced by Configure(), GetAlg(), Process(), Report(), and Reset().

std::vector<larcv::ContourArray_t> larcv::ImageClusterManager::_clusters_v
private

Array of resulting clusters per algorithms.

Definition at line 81 of file ImageClusterManager.h.

Referenced by ClusterID(), Clusters(), Process(), and Reset().

bool larcv::ImageClusterManager::_configured
private

Boolean flag to enforce Configure method to be called before Process.

Definition at line 75 of file ImageClusterManager.h.

Referenced by Configure(), Process(), and Reset().

std::vector<larcv::ImageMeta> larcv::ImageClusterManager::_meta_v
private

Array of meta data: one per set of clusters (updated by each algorithm)

Definition at line 83 of file ImageClusterManager.h.

Referenced by ClusterID(), MetaData(), and Process().

std::string larcv::ImageClusterManager::_name
private

Name identifier: used to fetch a block of configuration parameters.

Definition at line 73 of file ImageClusterManager.h.

Referenced by Name().

larcv::ImageMeta larcv::ImageClusterManager::_orig_meta
private

Original meta data.

Definition at line 85 of file ImageClusterManager.h.

Referenced by Process().

size_t larcv::ImageClusterManager::_process_count
private

Process counter (cumulative)

Definition at line 91 of file ImageClusterManager.h.

Referenced by Process(), Report(), and Reset().

double larcv::ImageClusterManager::_process_time
private

Process time (cumulative)

Definition at line 93 of file ImageClusterManager.h.

Referenced by Process(), Report(), and Reset().

bool larcv::ImageClusterManager::_profile
private

Boolean flag to measure process time + report.

Definition at line 87 of file ImageClusterManager.h.

Referenced by Configure().

Watch larcv::ImageClusterManager::_watch
private

Stopwatch.

Definition at line 89 of file ImageClusterManager.h.

Referenced by Process().


The documentation for this class was generated from the following files: