OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
GenerateBlobDebugImage.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_GenerateBlobDebugImage_h_GUID_558B5666_8637_48B9_A215_E7A1B798DDD8
26 #define INCLUDED_GenerateBlobDebugImage_h_GUID_558B5666_8637_48B9_A215_E7A1B798DDD8
27 
28 // Internal Includes
30 #include <cvUtils.h>
31 
32 // Library/third-party includes
33 #include <opencv2/core/core.hpp>
34 #include <opencv2/imgproc/imgproc.hpp>
35 
36 // Standard includes
37 #include <random>
38 #include <sstream>
39 #include <string>
40 
41 namespace osvr {
42 namespace vbtracker {
43  inline cv::Mat
44  generateBlobDebugImage(cv::Mat const &inputImage,
45  EdgeHoleBasedLedExtractor const &extractor) {
46 
49  cv::Mat highlightedContours =
50  drawColoredContours(inputImage, extractor.getContours());
51 
52  // Figure out where to put the text - further along the ray
53  // that passes from the image center through the reject center, to try
54  // to get it "away from the crowd"
55  {
56  static const auto FONT_FACE = cv::FONT_HERSHEY_PLAIN;
57  static const auto FONT_SCALE = 0.8;
58  static const int FONT_THICKNESS = 1;
59 
60  auto imageSize = highlightedContours.size();
61  auto imageCenter =
62  cv::Point2d(imageSize.width / 2, imageSize.height / 2);
63  std::random_device rd;
64  std::mt19937 mt(rd());
65  std::uniform_real_distribution<double> vectorScaleDistribution(1.1,
66  1.3);
67  auto computeTextLocation = [&](cv::Point2d rejectCenter,
68  std::string const &text) {
69  auto centerToRejectVec = rejectCenter - imageCenter;
70  auto textPoint =
71  cv::Point(centerToRejectVec * vectorScaleDistribution(mt) +
72  imageCenter);
73 
76  auto textSize = cv::getTextSize(text, FONT_FACE, FONT_SCALE,
77  FONT_THICKNESS, nullptr);
78 
79  auto textOffset =
80  cv::Point(textSize.width / 2, textSize.height / 2);
81  return textPoint - textOffset;
82  };
83 
85  for (auto &reject : extractor.getRejectList()) {
86  EdgeHoleBasedLedExtractor::ContourId contourId = 0;
87  RejectReason reason;
88  cv::Point2d center;
89  std::tie(contourId, reason, center) = reject;
90 
91  if (osvr::vbtracker::RejectReason::Area == reason ||
92  RejectReason::CenterPointValue == reason) {
93  // Skip drawing these, they clutter the display
94  continue;
95  }
96  drawSubpixelPoint(highlightedContours, center,
97  cv::Scalar(0, 0, 0), 1.2);
98  std::ostringstream os;
99  // os << "[";
100  os << contourId;
101  switch (reason) {
102  case RejectReason::Area:
103  os << ":AREA";
104  break;
105  case RejectReason::CenterPointValue:
106  os << ":VAL";
107  break;
108  case RejectReason::Circularity:
109  os << ":CIRC";
110  break;
111  case RejectReason::Convexity:
112  os << ":CONV";
113  break;
114  default:
115  break;
116  }
117  // os << "]";
118  auto text = os.str();
119  cv::putText(highlightedContours, text,
120  computeTextLocation(center, text), FONT_FACE,
121  FONT_SCALE, cv::Scalar(0, 0, 255), FONT_THICKNESS);
122  }
123  }
124 
125  return highlightedContours;
126  }
127 } // namespace vbtracker
128 } // namespace osvr
129 #endif // INCLUDED_GenerateBlobDebugImage_h_GUID_558B5666_8637_48B9_A215_E7A1B798DDD8
Header.
double Scalar
Common scalar type.