Skip to content

Commit 8df62be

Browse files
authored
Merge pull request #15 from rursprung/minor-cosmetic-changes
minor cosmetic changes
2 parents 391812b + 4f540a5 commit 8df62be

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

include/banana-lib/lib.hpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ namespace banana {
8888
/// Color used to annotate the contours on the analyzed image.
8989
cv::Scalar const contour_annotation_color_{0, 255, 0};
9090

91+
/// Maximum score of `cv::matchShapes` which we still accept as a banana.
92+
float match_max_score_ = 0.8f;
93+
9194
/// Reference contour for the banana, used in filtering.
9295
Contour reference_contour_;
9396

@@ -100,12 +103,12 @@ namespace banana {
100103
auto ColorFilter(cv::Mat const& image) const -> cv::Mat;
101104

102105
/**
103-
* Compares the input with a banana contour and only passes on those that are similar to it
104-
* @param contours
105-
* @return banana contour
106+
* Checks whether the passed contour is - with a good likelihood - a banana.
107+
* @param contour the contour which may or may not be a banana
108+
* @return whether it is a banana
106109
*/
107110
[[nodiscard]]
108-
auto FilterContours(Contours const& contours) const -> Contours;
111+
auto IsBananaContour(Contour const& contour) const -> bool;
109112

110113
/**
111114
* Identify all bananas present in an image and return their contours.

src/lib.cpp

+8-12
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,8 @@ namespace banana {
8282
return mask;
8383
}
8484

85-
auto Analyzer::FilterContours(Contours const& contours) const -> Contours {
86-
Contours filtered_contours;
87-
auto const match_max_score_ = 0.8;
88-
for (auto& contour : contours) {
89-
auto const match_score = cv::matchShapes(contour, this->reference_contour_, cv::CONTOURS_MATCH_I1, 0.0);
90-
if (match_score < match_max_score_) {
91-
filtered_contours.push_back(contour);
92-
}
93-
}
94-
return filtered_contours;
85+
auto Analyzer::IsBananaContour(Contour const& contour) const -> bool {
86+
return cv::matchShapes(contour, this->reference_contour_, cv::CONTOURS_MATCH_I1, 0.0) > match_max_score_;
9587
}
9688

9789
auto Analyzer::FindBananaContours(cv::Mat const& image) const -> Contours {
@@ -104,13 +96,17 @@ namespace banana {
10496
SHOW_DEBUG_IMAGE(filtered_image, "morph");
10597

10698
// Smooth the image
107-
cv::medianBlur(filtered_image, filtered_image, 37); // 41
99+
cv::medianBlur(filtered_image, filtered_image, 37); // TODO: test again with 41
108100
SHOW_DEBUG_IMAGE(filtered_image, "blur");
109101

110102
Contours contours;
111103
cv::findContours(filtered_image, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
112104

113-
return FilterContours(contours);
105+
std::erase_if(contours, [this](auto const& contour) -> auto {
106+
return this->IsBananaContour(contour);
107+
});
108+
109+
return contours;
114110
}
115111

116112
auto Analyzer::AnalyzeBanana(const cv::Mat &image, const Contour &banana_contour) const -> std::expected<AnalysisResult, AnalysisError> {

test/banana-lib-test.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
#include "banana-lib/lib.hpp"
77

8-
/**
9-
* Assert that two matrices are identical (same values for all pixels).
10-
*/
8+
/// Assert that two matrices are identical (same values for all pixels).
119
#define ASSERT_SAME_MAT(a,b) ASSERT_EQ(cv::Scalar(), cv::sum(a != b))
1210

1311
TEST(GeneralBananaTestSuite, FailOnNonExistingImage) {
@@ -23,7 +21,7 @@ TEST(BananaContourFinderTestSuite, AnalyzeEmptyPicture) {
2321
auto const result = analyzer.AnalyzeImage(image);
2422
ASSERT_TRUE(result.has_value());
2523
auto const& results = result.value();
26-
ASSERT_TRUE(results.empty());
24+
ASSERT_EQ(0, results.size());
2725
}
2826

2927
TEST(BananaContourFinderTestSuite, AnalyzeAndAnnotateEmptyPicture) {
@@ -32,7 +30,7 @@ TEST(BananaContourFinderTestSuite, AnalyzeAndAnnotateEmptyPicture) {
3230
auto const result = analyzer.AnalyzeAndAnnotateImage(image);
3331
ASSERT_TRUE(result.has_value());
3432
auto const& analysis_result = result.value();
35-
ASSERT_TRUE(analysis_result.banana.empty());
33+
ASSERT_EQ(0, analysis_result.banana.size());
3634
ASSERT_SAME_MAT(analysis_result.annotated_image, image);
3735
}
3836

0 commit comments

Comments
 (0)