Skip to content

Commit db5fcca

Browse files
committed
Making odd numbers required for some filters
1 parent 7e3c1e8 commit db5fcca

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

image-filtering/config/image_filtering_params.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
overlap:
3434
percentage_threshold: 20.0 # Percentage (0-100) to cap the pixel intensity difference
3535
median_binary: # finds the median of each n x n square around each pixel
36-
kernel_size: 3 # must be odd >= 1
36+
kernel_size: 3 # must be odd > 1
3737
threshold: 100 # [0, 255]
3838
invert: false
3939
binary:

image-filtering/include/image_filtering/lib/filters/median_binary.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class MedianBinary : public Filter {
2525

2626
inline void MedianBinary::apply_filter(const cv::Mat& original,
2727
cv::Mat& filtered) const {
28-
apply_median(original, filtered, this->filter_params_.kernel_size);
28+
29+
odd_int kernel_size = odd_int(this->filter_params_.kernel_size);
30+
apply_median(original, filtered, int(kernel_size));
2931
apply_fixed_threshold(filtered, filtered, this->filter_params_.threshold,
3032
this->filter_params_.invert);
3133
}

image-filtering/include/image_filtering/lib/utilities.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,23 @@ void apply_fixed_threshold(const cv::Mat& img,
4848
cv::Mat& filtered,
4949
int thresh,
5050
bool invert = false);
51-
} // namespace vortex::image_filtering
51+
52+
// For values that is required to be odd and biger than one
53+
struct odd_int {
54+
int value;
55+
56+
explicit odd_int(int v) : value(v) {
57+
if (v <= 1 || (v % 2) == 0) {
58+
throw std::invalid_argument("odd_int must be odd and > 1");
59+
}
60+
}
61+
62+
// Allow passing to APIs that expect int without changing call sites
63+
operator int() const noexcept { return value; }
64+
65+
66+
int get() const noexcept { return value; }
67+
};
68+
} // namespace vortex::image_filtering_
69+
5270
#endif // IMAGE_FILTERING__LIB__UTILITIES_HPP_

image-filtering/src/ros/image_filtering_ros.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,12 @@ void ImageFilteringNode::image_callback(
230230
try {
231231
filter_ptr_->apply_filter(input_image, filtered_image);
232232
} catch (const cv::Exception& e) {
233-
spdlog::error(fmt::format(fmt::fg(fmt::rgb(31, 161, 221)),
233+
spdlog::error(fmt::format(fmt::fg(fmt::rgb(255, 0, 0)),
234234
"OpenCV error while applying filter: {}",
235235
e.what()));
236236
filtered_image = input_image.clone(); // fallback to no filter
237237
} catch (const std::exception& e) {
238-
spdlog::error(fmt::format(fmt::fg(fmt::rgb(31, 161, 221)),
238+
spdlog::error(fmt::format(fmt::fg(fmt::rgb(255, 0, 0)),
239239
"Error while applying filter: {}", e.what()));
240240
filtered_image = input_image.clone();
241241
}

0 commit comments

Comments
 (0)