Skip to content

Commit 55e2378

Browse files
authored
Merge pull request #22 from Nic822/improve-shape-detection
additionally filter contours by their size
2 parents f85c8b7 + b0f4b62 commit 55e2378

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

apps/livecam/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int main(int const argc, char const * const argv[]) {
3939

4040
banana::Analyzer const analyzer{{
4141
.verbose_annotations = true,
42-
.pixels_per_meter = 12370, // measured 10cm = 1237 on "reference-measurement.jpg"
42+
.pixels_per_meter = 2000, // measured 29cm = 580px
4343
}};
4444
try {
4545
auto cap = GetVideoCaptureFromArgs(argc, argv);

include/banana-lib/lib.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ namespace banana {
118118
/// Maximum score of `cv::matchShapes` which we still accept as a banana.
119119
float const match_max_score{0.6f};
120120

121+
/// Minimum area value of a banana (in px^2).
122+
float const min_area{1e5f};
123+
124+
/// Maximum area value of a banana (in px^2).
125+
float const max_area{1e7f};
126+
121127
/// How long (in pixels) is a meter? This is the extrinsic calibration needed to calculate sizes.
122128
double const pixels_per_meter;
123129

resources/test-images/banana-30.jpg

-11.9 MB
Binary file not shown.

src/lib.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,19 @@ namespace banana {
106106
}
107107

108108
auto Analyzer::IsBananaContour(Contour const& contour) const -> bool {
109-
return cv::matchShapes(contour, this->reference_contour_, cv::CONTOURS_MATCH_I1, 0.0) > this->settings_.match_max_score;
109+
if (cv::matchShapes(contour, this->reference_contour_, cv::CONTOURS_MATCH_I1, 0.0) > this->settings_.match_max_score) {
110+
return false;
111+
}
112+
auto const area = cv::contourArea(contour);
113+
return settings_.min_area < area && area < settings_.max_area;
110114
}
111115

112116
auto Analyzer::FindBananaContours(cv::Mat const& image) const -> Contours {
113117
auto filtered_image = ColorFilter(image, settings_.filter_lower_threshold_color, settings_.filter_upper_threshold_color);
114118
SHOW_DEBUG_IMAGE(filtered_image, "color filtered image");
115119

116120
// Removing noise
117-
auto const kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
121+
auto const kernel = cv::getStructuringElement(cv::MORPH_RECT, {5, 5});
118122
cv::morphologyEx(filtered_image, filtered_image, cv::MORPH_OPEN, kernel);
119123
SHOW_DEBUG_IMAGE(filtered_image, "morph");
120124

@@ -126,7 +130,7 @@ namespace banana {
126130
cv::findContours(filtered_image, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
127131

128132
std::erase_if(contours, [this](auto const& contour) -> auto {
129-
return this->IsBananaContour(contour);
133+
return !this->IsBananaContour(contour);
130134
});
131135

132136
return contours;
@@ -304,7 +308,7 @@ namespace banana {
304308
// rotate the center line back so that it fits on the image
305309
auto const rotated_center_line = this->RotateContour(center_line_points2i, result.estimated_center, -result.rotation_angle);
306310

307-
cv::polylines(draw_target, rotated_center_line, false, this->settings_.helper_annotation_color, 10);
311+
cv::polylines(draw_target, rotated_center_line, false, this->settings_.helper_annotation_color, 3);
308312
}
309313

310314
void Analyzer::PlotPCAResult(cv::Mat& draw_target, AnalysisResult const& result) const {
@@ -321,7 +325,7 @@ namespace banana {
321325
auto annotated_image = cv::Mat{image};
322326

323327
for (auto const& [n, result] : std::ranges::enumerate_view(analysis_result)) {
324-
cv::drawContours(annotated_image, std::vector{{result.contour}}, -1, this->settings_.contour_annotation_color, 10);
328+
cv::drawContours(annotated_image, std::vector{{result.contour}}, -1, this->settings_.contour_annotation_color, 3);
325329

326330
if (this->settings_.verbose_annotations) {
327331
cv::putText(annotated_image, std::to_string(n), result.estimated_center + cv::Point{35, -35}, cv::FONT_HERSHEY_COMPLEX_SMALL, 2, this->settings_.helper_annotation_color);

0 commit comments

Comments
 (0)