LArOpenCV  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
Utilities.cxx
Go to the documentation of this file.
1 #ifndef __IMAGECLUSTER_UTILITIES_CXX__
2 #define __IMAGECLUSTER_UTILITIES_CXX__
3 
4 #include <limits.h>
5 #include <opencv2/imgproc/imgproc.hpp>
6 #include "Core/larbys.h"
7 #include "Utilities.h"
8 namespace larcv {
9 
10  ::cv::Rect BoundingBox(const larcv::Contour_t& cluster)
11  {
12  if(cluster.size()<2)
13  throw larbys("Cannot create SubMatrix for a contour of size < 2!");
14 
15  int x_min = INT_MAX;
16  int y_min = INT_MAX;
17  int x_max = 0;
18  int y_max = 0;
19 
20  for(auto const& pt : cluster) {
21 
22  if(pt.x < x_min) x_min = pt.x;
23  if(pt.x > x_max) x_max = pt.x;
24  if(pt.y < y_min) y_min = pt.y;
25  if(pt.y > y_max) y_max = pt.y;
26 
27  }
28 
29  return ::cv::Rect( x_min, y_min,
30  x_max - x_min + 1,
31  y_max - y_min + 1);
32  }
33 
34  ::cv::Mat CreateSubMatRef(const larcv::Contour_t& cluster, cv::Mat& img)
35  {
36  if(cluster.size()<2)
37  throw larbys("Cannot create SubMatrix for a contour of size < 2!");
38 
39  auto bbox = BoundingBox(cluster);
40 
41  ::cv::Mat result;
42  img(bbox).copyTo(result);
43 
44  return result;
45  }
46 
47  ::cv::Mat CreateSubMatCopy(const larcv::Contour_t& cluster, const cv::Mat& img)
48  {
49  if(cluster.size()<2)
50  throw larbys("Cannot create SubMatrix for a contour of size < 2!");
51 
52  auto bbox = BoundingBox(cluster);
53 
54  ::cv::Mat result = ::cv::Mat::zeros(img.size(),img.type());
55 
56  for(int x=0; x<img.rows; ++x) {
57 
58  for(int y=0; y<img.cols; ++y) {
59 
60  int submat_x = x - bbox.x;
61  int submat_y = y - bbox.y;
62 
63  double inside = ::cv::pointPolygonTest(cluster,::cv::Point2f(x,y),false);
64 
65  if(inside<0) continue;
66 
67  result.at<float>(submat_x,submat_y,0) = img.at<float>(x,y,0);
68  }
69 
70  }
71 
72  return result;
73  }
74 
75 }
76 
77 #endif