OSVR-Core
BlobExtractor.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_BlobExtractor_h_GUID_E11BAD60_5C84_4581_C859_6E1FEF0838F5
26 #define INCLUDED_BlobExtractor_h_GUID_E11BAD60_5C84_4581_C859_6E1FEF0838F5
27 
28 // Internal Includes
29 #include <BlobParams.h>
30 #include <LedMeasurement.h>
31 
32 // Library/third-party includes
33 #include <opencv2/core/core.hpp>
34 
35 // Standard includes
36 #include <algorithm> // for std::max
37 #include <vector>
38 
39 namespace osvr {
40 namespace vbtracker {
41  using ContourType = std::vector<cv::Point2i>;
42  using ContourList = std::vector<ContourType>;
43 
45  struct BlobData {
46  cv::Point2d center;
47  double area;
48  double circularity;
49  double diameter;
50  cv::Rect bounds;
51  ContourType contour;
52  };
53 
55  struct BlobBasics {
56  cv::Point2d center;
57  double area;
58  cv::Rect bounds;
59  };
60 
61  BlobData getBlobDataFromContour(ContourType const &contour);
62 
63  BlobBasics getContourBasicDetails(ContourType const &contour);
64 
67  double getConvexity(ContourType const &contour, const double area);
68 
69  struct ImageRangeInfo {
70  explicit ImageRangeInfo(cv::InputArray img) {
71  cv::minMaxIdx(img, &minVal, &maxVal);
72  }
73  double minVal;
74  double maxVal;
75  double lerp(double alpha) const {
76  return minVal + (maxVal - minVal) * alpha;
77  }
78  };
79 
81 
82  ImageThresholdInfo(ImageRangeInfo const &rangeInfo, BlobParams const &p)
85  : minThreshold(std::max(rangeInfo.lerp(p.minThresholdAlpha),
86  p.absoluteMinThreshold)),
87  maxThreshold(std::max(rangeInfo.lerp(0.8),
88  p.absoluteMinThreshold)),
89  thresholdStep((maxThreshold - minThreshold) / p.thresholdSteps) {}
90 
91  ImageThresholdInfo(cv::Mat const &img, BlobParams const &p)
93 
94  double minThreshold = 0;
95  double maxThreshold = 255;
96  double thresholdStep = 255;
97  };
98 
100  public:
101  LedMeasurementVec
102  operator()(cv::Mat const &gray,
103  cv::SimpleBlobDetector::Params const &params);
104  ContourList binarizeAndGetSolidComponents(int thresh);
105 
106  private:
107  void makeFloodFillMask(cv::Mat const &gray);
108  void augmentPoint(cv::Point peakCenter, const int loDiff = 2,
109  const int upDiff = 2);
110  cv::Mat grayImage_;
111  cv::Mat floodFillMask_;
112  cv::Rect origBoundsInFloodFill_;
113  };
114 
115 } // namespace vbtracker
116 } // namespace osvr
117 
118 #endif // INCLUDED_BlobExtractor_h_GUID_E11BAD60_5C84_4581_C859_6E1FEF0838F5
Data structure with just a few bare facts about a contour.
Definition: BlobExtractor.h:55
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: TypeSafeIdHash.h:44
Definition: BlobExtractor.h:99
BlobData getBlobDataFromContour(ContourType const &contour)
Definition: BlobExtractor.cpp:73
Definition: BlobExtractor.h:69
Definition: BlobExtractor.h:80
ImageThresholdInfo(ImageRangeInfo const &rangeInfo, BlobParams const &p)
lerp between min and max, but don&#39;t let really dim frames confuse us.
Definition: BlobExtractor.h:82
Data structure with a lot of info about a contour.
Definition: BlobExtractor.h:45
Blob detection configuration parameters.
Definition: BlobParams.h:40
double getConvexity(ContourType const &contour, const double area)
Returns a value in [0, 1] that is the ratio of the contour&#39;s area as provided to the area of the cont...
Definition: BlobExtractor.cpp:90
Header.