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

User custom analysis class made by SHELL_USER_NAME. More...

#include <HitImageMaker.h>

Inheritance diagram for larlite::HitImageMaker:

Public Member Functions

 HitImageMaker ()
 Default constructor. More...
 
virtual ~HitImageMaker ()
 Default destructor. More...
 
virtual bool initialize ()
 IMPLEMENT in HitImageMaker.cc! Initialization method to be called before the analysis event loop. More...
 
virtual bool analyze (storage_manager *storage)
 IMPLEMENT in HitImageMaker.cc! Analyze a data event-by-event. More...
 
virtual bool finalize ()
 IMPLEMENT in HitImageMaker.cc! Finalize method to be called after all events processed. More...
 
PyObjectGetImage (const size_t plane)
 
PyObjectGetCanny (const size_t plane)
 
PyObjectGetContour (const size_t plane, const size_t contour_index)
 
size_t NumContours (const size_t plane) const
 

Protected Member Functions

void init ()
 

Protected Attributes

std::vector<::cv::Mat > _mat_v
 
std::vector<::cv::Mat > _canny_v
 
std::vector< std::vector
< larcv::Point2DArray > > 
_contour_v
 

Detailed Description

User custom analysis class made by SHELL_USER_NAME.

Definition at line 35 of file HitImageMaker.h.

Constructor & Destructor Documentation

larlite::HitImageMaker::HitImageMaker ( )

Default constructor.

Definition at line 9 of file HitImageMaker.cxx.

References init().

10  { _name="HitImageMaker"; _fout=0; init(); }
virtual larlite::HitImageMaker::~HitImageMaker ( )
inlinevirtual

Default destructor.

Definition at line 43 of file HitImageMaker.h.

43 {}

Member Function Documentation

bool larlite::HitImageMaker::analyze ( storage_manager *  storage)
virtual

IMPLEMENT in HitImageMaker.cc! Analyze a data event-by-event.

Canny detector

Definition at line 19 of file HitImageMaker.cxx.

References _canny_v, _contour_v, and _mat_v.

19  {
20 
21  auto const ev_hit = storage->get_data<event_hit>("gaushit");
22 
23  _mat_v.clear();
24  _canny_v.clear();
25  _contour_v.clear();
26 
27  std::vector<int> x_min_v;
28  std::vector<int> x_max_v;
29  std::vector<int> y_min_v;
30  std::vector<int> y_max_v;
31  std::vector<float> q_max_v;
32  for(auto const& h : *ev_hit) {
33 
34  if(h.Integral()<5.) continue;
35  size_t plane = h.WireID().Plane;
36  if(plane >= x_min_v.size()) {
37 
38  x_min_v.resize(plane+1,4000);
39  x_max_v.resize(plane+1,0);
40  y_min_v.resize(plane+1,9600);
41  y_max_v.resize(plane+1,0);
42  q_max_v.resize(plane+1,0);
43  }
44 
45  //std::cout<<plane<< " : " <<h.Integral()<<std::endl;
46 
47  int wire = h.WireID().Wire;
48  int time = (int)(h.PeakTime());
49  float q = h.Integral();
50  if( x_min_v[plane] > wire ) x_min_v[plane] = wire;
51  if( x_max_v[plane] < wire ) x_max_v[plane] = wire;
52  if( y_min_v[plane] > time ) y_min_v[plane] = time;
53  if( y_max_v[plane] < time ) y_max_v[plane] = time;
54  if( q_max_v[plane] < q ) q_max_v[plane] = q;
55  }
56 
57  for(size_t plane=0; plane<x_min_v.size(); ++plane) {
58  std::cout<<"Plane "<<plane<<" : "
59  <<x_min_v[plane]<<" => "<<x_max_v[plane]<< " : "
60  <<y_min_v[plane]<<" => "<<y_max_v[plane]<< std::endl;
61 
62  ::cv::Mat mat(x_max_v[plane] - x_min_v[plane] + 1,
63  y_max_v[plane] - y_min_v[plane] + 1,
64  CV_8UC1, cvScalar(0.));
65 
66  ::cv::Mat canny;
67  canny.create(mat.size(),mat.type());
68 
69  _mat_v.emplace_back(mat);
70  _canny_v.emplace_back(canny);
71  }
72  _contour_v.resize(x_min_v.size());
73 
74  for(auto const& h : *ev_hit) {
75 
76  if(h.Integral()<5.) continue;
77  int wire = h.WireID().Wire;
78  int time = (int)(h.PeakTime());
79  size_t plane = h.WireID().Plane;
80  int charge = ((256. * h.Integral() / q_max_v[plane]));
81  wire -= x_min_v[plane];
82  time -= y_min_v[plane];
83  auto& mat = _mat_v[plane];
84 
85  charge += mat.at<unsigned char>(wire,time);
86 
87  if(charge>256) charge = 256;
88  mat.at<unsigned char>(wire,time) = (unsigned char)(charge);
89 
90  }
91 
92  for(size_t plane=0; plane<_mat_v.size(); ++plane) {
93 
94  ::cv::blur( _mat_v[plane],
95  _mat_v[plane],
96  ::cv::Size(3,3) );
97 
99  //Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
100 
101  ::cv::Canny(_mat_v[plane],_canny_v[plane],10,100,3);
102 
103  std::vector<std::vector<cv::Point> > cv_contour_v;
104  std::vector<cv::Vec4i> cv_hierarchy_v;
105  //::cv::findContours(_canny_v[plane],cv_contour_v,cv_hierarchy_v,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
106  ::cv::findContours(_canny_v[plane],cv_contour_v,cv_hierarchy_v,
107  CV_RETR_EXTERNAL,
108  CV_CHAIN_APPROX_SIMPLE);
109 
110  std::cout<<"Found "<<cv_contour_v.size()<<" contours..."<<std::endl;
111 
112  auto& plane_contour_v = _contour_v[plane];
113  plane_contour_v.resize( cv_contour_v.size() );
114 
115  for(size_t c_index=0; c_index<cv_contour_v.size(); ++c_index) {
116 
117  auto& cv_contour = cv_contour_v[c_index];
118  auto& out_contour = plane_contour_v[c_index];
119 
120  out_contour.resize(cv_contour.size());
121  for(size_t p_index=0; p_index<cv_contour.size(); ++p_index)
122  out_contour.set(p_index, cv_contour[p_index].x, cv_contour[p_index].y);
123 
124  }
125  }
126 
127  //::cv::namedWindow( "Display window", ::cv::WINDOW_AUTOSIZE );// Create a window for display.
128  //::cv::imshow( "Display window", _mat_v.back() ); // Show our image inside it.
129 
130  return true;
131  }
bool larlite::HitImageMaker::finalize ( )
virtual

IMPLEMENT in HitImageMaker.cc! Finalize method to be called after all events processed.

Definition at line 133 of file HitImageMaker.cxx.

133  {
134 
135  return true;
136  }
PyObject * larlite::HitImageMaker::GetCanny ( const size_t  plane)

Definition at line 150 of file HitImageMaker.cxx.

References _canny_v, _mat_v, larcaffe::msg::kERROR, and larcv::convert::NDArrayConverter::toNDArray().

151  {
152  if(plane >= _mat_v.size()) {
153  print(msg::kERROR,__FUNCTION__,"Invalid plane ID requested...");
154  throw std::exception();
155  }
156 
158  return converter.toNDArray(_canny_v[plane]);
159 
160  }
PyObject * larlite::HitImageMaker::GetContour ( const size_t  plane,
const size_t  contour_index 
)

Definition at line 171 of file HitImageMaker.cxx.

References _contour_v, _mat_v, and larcaffe::msg::kERROR.

172  {
173  if(plane >= _mat_v.size()) {
174  print(msg::kERROR,__FUNCTION__,"Invalid plane ID requested...");
175  throw std::exception();
176  }
177 
178  if(contour_index > _contour_v[plane].size()){
179  print(msg::kERROR,__FUNCTION__,"Invalid contour ID requested...");
180  throw std::exception();
181  }
182 
183  int* dim_data = new int[2];
184  dim_data[0] = 2;
185  dim_data[1] = (int)(_contour_v[plane][contour_index].size());
186 
187  return PyArray_FromDimsAndData( 2, dim_data, NPY_DOUBLE, (char*) &(_contour_v[plane][contour_index].raw_data()[0]) );
188 
189  }
PyObject * larlite::HitImageMaker::GetImage ( const size_t  plane)

Definition at line 138 of file HitImageMaker.cxx.

References _mat_v, larcaffe::msg::kERROR, and larcv::convert::NDArrayConverter::toNDArray().

139  {
140  if(plane >= _mat_v.size()) {
141  print(msg::kERROR,__FUNCTION__,"Invalid plane ID requested...");
142  throw std::exception();
143  }
144 
146  return converter.toNDArray(_mat_v[plane]);
147 
148  }
void larlite::HitImageMaker::init ( )
protected

Definition at line 12 of file HitImageMaker.cxx.

Referenced by HitImageMaker().

13  { import_array(); }
bool larlite::HitImageMaker::initialize ( )
virtual

IMPLEMENT in HitImageMaker.cc! Initialization method to be called before the analysis event loop.

Definition at line 15 of file HitImageMaker.cxx.

15  {
16  return true;
17  }
size_t larlite::HitImageMaker::NumContours ( const size_t  plane) const

Definition at line 162 of file HitImageMaker.cxx.

References _contour_v, _mat_v, and larcaffe::msg::kERROR.

163  {
164  if(plane >= _mat_v.size()) {
165  print(msg::kERROR,__FUNCTION__,"Invalid plane ID requested...");
166  throw std::exception();
167  }
168  return _contour_v[plane].size();
169  }

Member Data Documentation

std::vector<::cv::Mat> larlite::HitImageMaker::_canny_v
protected

Definition at line 69 of file HitImageMaker.h.

Referenced by analyze(), and GetCanny().

std::vector< std::vector<larcv::Point2DArray> > larlite::HitImageMaker::_contour_v
protected

Definition at line 71 of file HitImageMaker.h.

Referenced by analyze(), GetContour(), and NumContours().

std::vector<::cv::Mat> larlite::HitImageMaker::_mat_v
protected

Definition at line 68 of file HitImageMaker.h.

Referenced by analyze(), GetCanny(), GetContour(), GetImage(), and NumContours().


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