LArOpenCV  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
HitImageMaker.cxx
Go to the documentation of this file.
1 #ifndef LARLITE_HITIMAGEMAKER_CXX
2 #define LARLITE_HITIMAGEMAKER_CXX
3 
4 #include "HitImageMaker.h"
6 #include "DataFormat/hit.h"
7 namespace larlite {
8 
10  { _name="HitImageMaker"; _fout=0; init(); }
11 
13  { import_array(); }
14 
16  return true;
17  }
18 
19  bool HitImageMaker::analyze(storage_manager* storage) {
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  }
132 
134 
135  return true;
136  }
137 
138  PyObject* HitImageMaker::GetImage(const size_t plane)
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  }
149 
150  PyObject* HitImageMaker::GetCanny(const size_t plane)
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  }
161 
162  size_t HitImageMaker::NumContours(const size_t plane) const
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  }
170 
171  PyObject* HitImageMaker::GetContour(const size_t plane, const size_t contour_index)
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  }
190 
191 }
192 #endif