|
| 1 | +#ifndef IMAGE_FILTERING__LIB__FILTERS__TEMPORAL_NOISE_HPP_ |
| 2 | +#define IMAGE_FILTERING__LIB__FILTERS__TEMPORAL_NOISE_HPP_ |
| 3 | + |
| 4 | +#include "abstract_filter_class.hpp" |
| 5 | +#include "image_filtering/lib/utilities.hpp" |
| 6 | + |
| 7 | +///////////////////////////// |
| 8 | +// Sonar noise |
| 9 | +///////////////////////////// |
| 10 | +namespace vortex::image_filtering { |
| 11 | +struct TemporalNoiseParams { |
| 12 | + int median_kernel_size; |
| 13 | + double power_law_weight; |
| 14 | + |
| 15 | + int canny_low; |
| 16 | + int canny_high; |
| 17 | + int edge_protection_radius; |
| 18 | + |
| 19 | + int erotion_size; |
| 20 | + int dilation_size; |
| 21 | +}; |
| 22 | + |
| 23 | +class TemporalNoise : public Filter { |
| 24 | + public: |
| 25 | + explicit TemporalNoise(TemporalNoiseParams params) |
| 26 | + : filter_params_(params) {} |
| 27 | + void apply_filter(const cv::Mat& original, |
| 28 | + cv::Mat& filtered) const override; |
| 29 | + |
| 30 | + private: |
| 31 | + TemporalNoiseParams filter_params_; |
| 32 | + mutable cv::Mat previous_; |
| 33 | + mutable bool has_prev_{false}; |
| 34 | +}; |
| 35 | + |
| 36 | +inline void TemporalNoise::apply_filter(const cv::Mat& original, |
| 37 | + cv::Mat& filtered) const { |
| 38 | + const double power_law_weight = filter_params_.power_law_weight; |
| 39 | + const int erosion_size = filter_params_.erotion_size; |
| 40 | + const int dilation_size = filter_params_.dilation_size; |
| 41 | + const int protect_radius = filter_params_.edge_protection_radius; |
| 42 | + const int canny_low = filter_params_.canny_low; |
| 43 | + const int canny_high = filter_params_.canny_high; |
| 44 | + const int median_kernel_size = filter_params_.median_kernel_size; |
| 45 | + |
| 46 | + cv::Mat temp; |
| 47 | + original.copyTo(temp); |
| 48 | + |
| 49 | + apply_median(temp, temp, median_kernel_size); |
| 50 | + |
| 51 | + apply_auto_gamma(temp, power_law_weight); |
| 52 | + |
| 53 | + if (!has_prev_) { |
| 54 | + temp.copyTo(previous_); |
| 55 | + temp.copyTo(filtered); |
| 56 | + has_prev_ = true; |
| 57 | + } else { |
| 58 | + cv::addWeighted(temp, 0.5, previous_, 0.5, 0.0, filtered); |
| 59 | + temp.copyTo(previous_); |
| 60 | + } |
| 61 | + |
| 62 | + cv::Mat edges; |
| 63 | + cv::Canny(filtered, edges, canny_low, canny_high); |
| 64 | + |
| 65 | + // Thicken mask a bit so we protect the whole line, not just 1px edge. |
| 66 | + if (protect_radius > 0) { |
| 67 | + cv::Mat k = cv::getStructuringElement( |
| 68 | + cv::MORPH_ELLIPSE, |
| 69 | + cv::Size(2 * protect_radius + 1, 2 * protect_radius + 1)); |
| 70 | + cv::dilate(edges, edges, k); |
| 71 | + } |
| 72 | + |
| 73 | + // Invert mask: where NOT edges => safe to morph aggressively. |
| 74 | + cv::Mat not_edges; |
| 75 | + cv::bitwise_not(edges, not_edges); |
| 76 | + |
| 77 | + // Morphing only outside the protected areas |
| 78 | + cv::Mat morphed = filtered.clone(); |
| 79 | + |
| 80 | + apply_erosion(morphed, morphed, erosion_size); |
| 81 | + apply_dilation(morphed, morphed, dilation_size); |
| 82 | + |
| 83 | + morphed.copyTo(filtered, not_edges); |
| 84 | +} |
| 85 | +} // namespace vortex::image_filtering |
| 86 | +#endif // IMAGE_FILTERING__LIB__FILTERS__TEMPORAL_NOISE_HPP_ |
0 commit comments