OSVR-Core
EdgeHoleBasedLedExtractor.h
Go to the documentation of this file.
1 
11 // Copyright 2016 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_EdgeHoleBasedLedExtractor_h_GUID_225A962F_C636_4CA5_2D16_964D63A0571A
26 #define INCLUDED_EdgeHoleBasedLedExtractor_h_GUID_225A962F_C636_4CA5_2D16_964D63A0571A
27 
30 #undef OSVR_USE_REALTIME_LAPLACIAN
31 
32 // Internal Includes
33 #include <BlobExtractor.h>
34 #include <BlobParams.h>
35 #include <LedMeasurement.h>
37 
38 // Library/third-party includes
39 #include <opencv2/core/core.hpp>
40 #include <opencv2/imgproc/imgproc.hpp>
41 
42 // Standard includes
43 #include <cstdint>
44 #include <memory>
45 #include <tuple>
46 #include <vector>
47 
48 #if defined(OSVR_OPENCV_3PLUS) && defined(OSVR_USE_REALTIME_LAPLACIAN)
49 #undef OSVR_USE_REALTIME_LAPLACIAN
51 #endif
52 
56 #undef OSVR_PERMIT_OPENCL
57 
58 #if defined(OSVR_OPENCV_3PLUS) && defined(OSVR_PERMIT_OPENCL)
59 #define OSVR_EDGEHOLE_UMAT 1
60 #else
61 #define OSVR_EDGEHOLE_UMAT 0
62 #endif
63 
64 namespace osvr {
65 namespace vbtracker {
67  class RealtimeLaplacian;
68 
69  enum class RejectReason { Area, CenterPointValue, Circularity, Convexity };
71  public:
72 #if OSVR_EDGEHOLE_UMAT
73  using MatType = cv::UMat;
74  using ExternalMatGetterReturn = cv::Mat;
75 #else
76  using MatType = cv::Mat;
77  using ExternalMatGetterReturn = cv::Mat const &;
78 #endif
79 
81  EdgeHoleParams const &extractorParams = EdgeHoleParams());
82  LedMeasurementVec const &operator()(cv::Mat const &gray,
83  BlobParams const &p,
84  bool verboseBlobOutput = false);
86 
87  using ContourId = std::size_t;
88  // Contour ID and center.
89  using RejectType = std::tuple<ContourId, RejectReason, cv::Point2d>;
90  using RejectList = std::vector<RejectType>;
91 
92  void reset();
93 
94  ExternalMatGetterReturn getInputGrayImage() const {
95  return externalMatGetter(gray_);
96  }
97  ExternalMatGetterReturn getEdgeDetectedImage() const {
98  return externalMatGetter(edge_);
99  }
100  ExternalMatGetterReturn getEdgeDetectedBinarizedImage() const {
101  return externalMatGetter(edgeBinary_);
102  }
103  ContourList const &getContours() const { return contours_; }
104  LedMeasurementVec const &getMeasurements() const {
105  return measurements_;
106  }
107  RejectList const &getRejectList() const { return rejectList_; }
108 
109  private:
110 #if OSVR_EDGEHOLE_UMAT
111  static ExternalMatGetterReturn externalMatGetter(MatType const &input) {
112  return input.getMat(cv::ACCESS_READ);
113  }
114 #else
115  static ExternalMatGetterReturn externalMatGetter(MatType const &input) {
116  return input;
117  }
118 #endif
119  void checkBlob(ContourType &&contour, BlobParams const &p);
120  void addToRejectList(ContourId id, RejectReason reason,
121  BlobData const &data) {
122  rejectList_.emplace_back(id, reason, data.center);
123  }
124 
126  const EdgeHoleParams extParams_;
127 
128  std::uint8_t minBeaconCenterVal_ = 127;
129 
132  MatType gray_;
133  MatType edge_;
134  MatType edgeBinary_;
136 
141  MatType blurred_;
143  MatType edgeTemp_;
145  MatType binTemp_;
147 
150  std::vector<ContourType> contoursTempStorage_;
151  std::vector<cv::Vec4i> hierarchyTempStorage_;
153 
156  cv::Mat compressionArtifactRemovalKernel_;
157 #ifdef OSVR_OPENCV_2
158  cv::Ptr<cv::FilterEngine> compressionArtifactRemoval_;
159 #endif
160 
161 #ifdef OSVR_USE_REALTIME_LAPLACIAN
162  std::unique_ptr<RealtimeLaplacian> laplacianImpl_;
163 #endif
164 
165  ContourList contours_;
166  LedMeasurementVec measurements_;
167  RejectList rejectList_;
168  bool verbose_ = false;
169 
170  ContourId contourId_ = 0;
171  };
172 } // namespace vbtracker
173 } // namespace osvr
174 #endif // INCLUDED_EdgeHoleBasedLedExtractor_h_GUID_225A962F_C636_4CA5_2D16_964D63A0571A
Definition: EdgeHoleBasedLedExtractor.h:70
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: BlobParams.h:93
Data structure with a lot of info about a contour.
Definition: BlobExtractor.h:45
Blob detection configuration parameters.
Definition: BlobParams.h:40
Header.
Header defining a few simple preprocessor macros for dealing with OpenCV 2 and 3 compatibility.