|
| 1 | +#ifndef IMAGE_FILTERING__LIB__FILTERS__GRAY_WORLD_WHITE_BALANCING_HPP_ |
| 2 | +#define IMAGE_FILTERING__LIB__FILTERS__GRAY_WORLD_WHITE_BALANCING_HPP_ |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <opencv2/imgproc.hpp> |
| 6 | +#include <vector> |
| 7 | +#include "abstract_filter_class.hpp" |
| 8 | +///////////////////////////// |
| 9 | +// Gray World White Balance |
| 10 | +///////////////////////////// |
| 11 | +// Assumes the average colour of the scene is gray and scales each channel to |
| 12 | +// enforce it. Tends to remove the global colour cast (e.g. the blue/green tint |
| 13 | +// of underwater imagery) more aggressively than SimpleWB. |
| 14 | +// |
| 15 | +// This is a robust variant of the classic Gray World algorithm, tailored for |
| 16 | +// the extreme, near-uniform colour casts seen in underwater footage where the |
| 17 | +// stock OpenCV GrayworldWB is unstable: |
| 18 | +// * Channel gains are computed over the WHOLE frame (no saturation cutoff), |
| 19 | +// so the result does not flicker as scene brightness jitters frame-to-frame |
| 20 | +// across a threshold. |
| 21 | +// * Gains are clamped to [1/max_gain, max_gain] so a degenerate frame cannot |
| 22 | +// collapse the image to black (or blow it out to white). |
| 23 | +// * Overall luminance is preserved, so correcting the colour cast does not |
| 24 | +// darken the image. |
| 25 | +namespace vortex::image_filtering { |
| 26 | +struct GrayWorldWhiteBalanceParams { |
| 27 | + // Upper bound on any per-channel gain (and lower bound 1/max_gain). Caps |
| 28 | + // how aggressively a weak channel is amplified; the main guard against the |
| 29 | + // black/white flicker on degenerate frames. Typical range 2-6. |
| 30 | + double max_gain; |
| 31 | +}; |
| 32 | + |
| 33 | +class GrayWorldWhiteBalance : public Filter { |
| 34 | + public: |
| 35 | + explicit GrayWorldWhiteBalance(GrayWorldWhiteBalanceParams params) |
| 36 | + : filter_params_(params) {} |
| 37 | + void apply_filter(const cv::Mat& original, |
| 38 | + cv::Mat& filtered) const override; |
| 39 | + |
| 40 | + private: |
| 41 | + GrayWorldWhiteBalanceParams filter_params_; |
| 42 | +}; |
| 43 | + |
| 44 | +inline void GrayWorldWhiteBalance::apply_filter(const cv::Mat& original, |
| 45 | + cv::Mat& filtered) const { |
| 46 | + if (original.channels() != 3) { |
| 47 | + original.copyTo(filtered); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + cv::Mat img; |
| 52 | + original.convertTo(img, CV_32F); |
| 53 | + |
| 54 | + // Per-channel means over the whole frame (stable frame-to-frame). |
| 55 | + const cv::Scalar means = cv::mean(img); |
| 56 | + const double mean_gray = (means[0] + means[1] + means[2]) / 3.0; |
| 57 | + |
| 58 | + const double eps = 1e-6; |
| 59 | + const double max_gain = std::max(this->filter_params_.max_gain, 1.0); |
| 60 | + const double min_gain = 1.0 / max_gain; |
| 61 | + |
| 62 | + std::vector<cv::Mat> channels; |
| 63 | + cv::split(img, channels); |
| 64 | + |
| 65 | + for (int c = 0; c < 3; ++c) { |
| 66 | + double gain = mean_gray / std::max(means[c], eps); |
| 67 | + gain = std::clamp(gain, min_gain, max_gain); |
| 68 | + channels[c] *= gain; |
| 69 | + } |
| 70 | + |
| 71 | + cv::merge(channels, img); |
| 72 | + |
| 73 | + // Preserve overall luminance: rescale so the post-correction mean matches |
| 74 | + // the input mean, so fixing the colour cast doesn't darken the image. |
| 75 | + const cv::Scalar new_means = cv::mean(img); |
| 76 | + const double new_mean_gray = |
| 77 | + (new_means[0] + new_means[1] + new_means[2]) / 3.0; |
| 78 | + if (new_mean_gray > eps) { |
| 79 | + img *= mean_gray / new_mean_gray; |
| 80 | + } |
| 81 | + |
| 82 | + img.convertTo(filtered, original.type()); |
| 83 | +} |
| 84 | +} // namespace vortex::image_filtering |
| 85 | +#endif // IMAGE_FILTERING__LIB__FILTERS__GRAY_WORLD_WHITE_BALANCING_HPP_ |
0 commit comments