25 #ifndef INCLUDED_cvUtils_h_GUID_65B004C2_722B_4BBF_4EC7_05B2AD861254 26 #define INCLUDED_cvUtils_h_GUID_65B004C2_722B_4BBF_4EC7_05B2AD861254 32 #include <opencv2/core/core.hpp> 33 #include <opencv2/imgproc/imgproc.hpp> 43 inline cv::Point2f castPointToFloat(cv::Point2d
const &p) {
44 return cv::Point2f(static_cast<float>(p.x), static_cast<float>(p.y));
47 inline void drawSubpixelPoint(cv::Mat image, cv::Point2d point,
50 std::uint8_t fractionalBits = 3) {
51 const auto SHIFT = std::pow(2, fractionalBits);
52 auto intPoint = cv::Point(static_cast<int>(point.x * SHIFT),
53 static_cast<int>(point.y * SHIFT));
54 cv::circle(image, intPoint, static_cast<int>(radius * SHIFT), color, -1,
77 cv::Mat
const &baseImage, std::vector<ContourType>
const &contours,
78 F &&colorFunc,
bool fillContours =
false,
double centerDotRadius = 1.2,
79 bool colorCenterDot =
true) {
80 cv::Mat highlightedContours;
81 if (baseImage.channels() > 1) {
82 highlightedContours = baseImage.clone();
84 cv::cvtColor(baseImage, highlightedContours, cv::COLOR_GRAY2BGR);
87 const std::size_t n = contours.size();
88 for (std::size_t i = 0; i < n; ++i) {
89 cv::Scalar color = std::forward<F>(colorFunc)(contours[i], i);
90 cv::drawContours(highlightedContours, contours, i, color,
91 fillContours ? -1 : 1);
95 auto data = getContourBasicDetails(contours[i]);
96 drawSubpixelPoint(highlightedContours, data.center,
97 colorCenterDot ? color : black,
101 return highlightedContours;
121 std::vector<ContourType>
const &contours,
122 bool deterministic =
true,
123 bool fillContours =
false,
124 double centerDotRadius = 1.2,
125 bool colorCenterDot =
true) {
128 std::mt19937 mt(31415);
129 if (!deterministic) {
132 std::random_device rd;
138 auto colorDist = [](std::mt19937 &mt) {
return mt() & 0xff; };
141 [&](ContourType
const &, std::size_t) {
142 return cv::Scalar(colorDist(mt), colorDist(mt), colorDist(mt));
144 fillContours, centerDotRadius, colorCenterDot);
164 cv::Mat
const &baseImage, std::vector<ContourType>
const &contours,
166 double centerDotRadius = 1.2,
bool colorCenterDot =
true) {
169 [&color](ContourType
const &, std::size_t) {
return color; },
170 fillContours, centerDotRadius, colorCenterDot);
175 HIERARCHY_NEXT_SIBLING_CONTOUR = 0,
176 HIERARCHY_PREV_SIBLING_CONTOUR = 1,
177 HIERARCHY_FIRST_CHILD_CONTOUR = 2,
178 HIERARCHY_PARENT_CONTOUR = 3,
181 template <
typename F>
182 std::vector<ContourType>
183 getOutsidesOfConnectedComponents(cv::Mat input, F &&additionalPredicate) {
184 std::vector<ContourType> contours;
185 std::vector<cv::Vec4i> hierarchy;
186 cv::findContours(input, contours, hierarchy, cv::RETR_CCOMP,
187 cv::CHAIN_APPROX_NONE);
190 int n =
static_cast<int>(contours.size());
191 std::vector<ContourType> ret;
192 for (std::size_t i = 0; i < n; ++i) {
195 if (hierarchy[i][HIERARCHY_PARENT_CONTOUR] < 0 &&
196 std::forward<F>(additionalPredicate)(contours[i])) {
197 ret.emplace_back(std::move(contours[i]));
207 template <
typename F>
209 cv::InputOutputArray input,
210 std::vector<ContourType> &contoursTempStorage,
211 std::vector<cv::Vec4i> &hierarchyTempStorage, F &&continuation) {
212 cv::findContours(input, contoursTempStorage, hierarchyTempStorage,
213 cv::RETR_CCOMP, cv::CHAIN_APPROX_NONE);
216 int n =
static_cast<int>(contoursTempStorage.size());
218 for (
int outsides = 0; outsides >= 0 && outsides < n;
219 outsides = hierarchyTempStorage[outsides]
220 [HIERARCHY_NEXT_SIBLING_CONTOUR]) {
221 for (
int idx = hierarchyTempStorage[outsides]
222 [HIERARCHY_FIRST_CHILD_CONTOUR];
225 hierarchyTempStorage[idx][HIERARCHY_NEXT_SIBLING_CONTOUR])
227 std::forward<F>(continuation)(
228 std::move(contoursTempStorage[idx]));
232 template <
typename F>
235 std::vector<ContourType> contours;
236 std::vector<cv::Vec4i> hierarchy;
238 std::forward<F>(continuation));
243 #endif // INCLUDED_cvUtils_h_GUID_65B004C2_722B_4BBF_4EC7_05B2AD861254 cv::Mat drawColoredContours(cv::Mat const &baseImage, std::vector< ContourType > const &contours, bool deterministic=true, bool fillContours=false, double centerDotRadius=1.2, bool colorCenterDot=true)
Draw contours on a copy of the base image (converted to BGR, if required), with each contour a unique...
Definition: cvUtils.h:120
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
void consumeHolesOfConnectedComponents(cv::InputOutputArray input, std::vector< ContourType > &contoursTempStorage, std::vector< cv::Vec4i > &hierarchyTempStorage, F &&continuation)
Calls a continuation on every hole of a connected component.
Definition: cvUtils.h:208
cv::Mat drawFunctorColoredContours(cv::Mat const &baseImage, std::vector< ContourType > const &contours, F &&colorFunc, bool fillContours=false, double centerDotRadius=1.2, bool colorCenterDot=true)
Draw contours on a copy of the base image (converted to BGR, if required), with each contour a potent...
Definition: cvUtils.h:76
cv::Mat drawSingleColoredContours(cv::Mat const &baseImage, std::vector< ContourType > const &contours, cv::Scalar color, bool fillContours=false, double centerDotRadius=1.2, bool colorCenterDot=true)
Draw contours on a copy of the base image (converted to BGR, if required), with all contours the same...
Definition: cvUtils.h:163
double Scalar
Common scalar type.
Definition: FlexibleKalmanBase.h:48