|
| 1 | +# ruff: noqa: E741 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +## @package guided_filter.core.filters |
| 4 | +# |
| 5 | +# Implementation of guided filter. |
| 6 | +# * GuidedFilter: Original guided filter. |
| 7 | +# * FastGuidedFilter: Fast version of the guided filter. |
| 8 | +# @author tody |
| 9 | +# @date 2015/08/26 |
| 10 | + |
| 11 | + |
| 12 | +import cv2 |
| 13 | +import numpy as np |
| 14 | + |
| 15 | + |
| 16 | +## Convert image into float32 type. |
| 17 | +def to32F(img): |
| 18 | + if img.dtype == np.float32: |
| 19 | + return img |
| 20 | + return (1.0 / 255.0) * np.float32(img) |
| 21 | + |
| 22 | + |
| 23 | +## Convert image into uint8 type. |
| 24 | +def to8U(img): |
| 25 | + if img.dtype == np.uint8: |
| 26 | + return img |
| 27 | + return np.clip(np.uint8(255.0 * img), 0, 255) |
| 28 | + |
| 29 | + |
| 30 | +## Return if the input image is gray or not. |
| 31 | +def _isGray(I): |
| 32 | + return len(I.shape) == 2 |
| 33 | + |
| 34 | + |
| 35 | +## Return down sampled image. |
| 36 | +# @param scale (w/s, h/s) image will be created. |
| 37 | +# @param shape I.shape[:2]=(h, w). numpy friendly size parameter. |
| 38 | +def _downSample(I, scale=4, shape=None): |
| 39 | + if shape is not None: |
| 40 | + h, w = shape |
| 41 | + return cv2.resize(I, (w, h), interpolation=cv2.INTER_NEAREST) |
| 42 | + |
| 43 | + h, w = I.shape[:2] |
| 44 | + return cv2.resize(I, (int(w / scale), int(h / scale)), interpolation=cv2.INTER_NEAREST) |
| 45 | + |
| 46 | + |
| 47 | +## Return up sampled image. |
| 48 | +# @param scale (w*s, h*s) image will be created. |
| 49 | +# @param shape I.shape[:2]=(h, w). numpy friendly size parameter. |
| 50 | +def _upSample(I, scale=2, shape=None): |
| 51 | + if shape is not None: |
| 52 | + h, w = shape |
| 53 | + return cv2.resize(I, (w, h), interpolation=cv2.INTER_LINEAR) |
| 54 | + |
| 55 | + h, w = I.shape[:2] |
| 56 | + return cv2.resize(I, (int(w * scale), int(h * scale)), interpolation=cv2.INTER_LINEAR) |
| 57 | + |
| 58 | + |
| 59 | +## Fast guide filter. |
| 60 | +class FastGuidedFilter: |
| 61 | + ## Constructor. |
| 62 | + # @param I Input guidance image. Color or gray. |
| 63 | + # @param radius Radius of Guided Filter. |
| 64 | + # @param epsilon Regularization term of Guided Filter. |
| 65 | + # @param scale Down sampled scale. |
| 66 | + def __init__(self, I, radius=5, epsilon=0.4, scale=4): |
| 67 | + I_32F = to32F(I) |
| 68 | + self._I = I_32F |
| 69 | + h, w = I.shape[:2] |
| 70 | + |
| 71 | + I_sub = _downSample(I_32F, scale) |
| 72 | + |
| 73 | + self._I_sub = I_sub |
| 74 | + radius = int(radius / scale) |
| 75 | + |
| 76 | + if _isGray(I): |
| 77 | + self._guided_filter = GuidedFilterGray(I_sub, radius, epsilon) |
| 78 | + else: |
| 79 | + self._guided_filter = GuidedFilterColor(I_sub, radius, epsilon) |
| 80 | + |
| 81 | + ## Apply filter for the input image. |
| 82 | + # @param p Input image for the filtering. |
| 83 | + def filter(self, p): |
| 84 | + p_32F = to32F(p) |
| 85 | + shape_original = p.shape[:2] |
| 86 | + |
| 87 | + p_sub = _downSample(p_32F, shape=self._I_sub.shape[:2]) |
| 88 | + |
| 89 | + if _isGray(p_sub): |
| 90 | + return self._filterGray(p_sub, shape_original) |
| 91 | + |
| 92 | + cs = p.shape[2] |
| 93 | + q = np.array(p_32F) |
| 94 | + |
| 95 | + for ci in range(cs): |
| 96 | + q[:, :, ci] = self._filterGray(p_sub[:, :, ci], shape_original) |
| 97 | + return to8U(q) |
| 98 | + |
| 99 | + def _filterGray(self, p_sub, shape_original): |
| 100 | + ab_sub = self._guided_filter._computeCoefficients(p_sub) |
| 101 | + ab = [_upSample(abi, shape=shape_original) for abi in ab_sub] |
| 102 | + return self._guided_filter._computeOutput(ab, self._I) |
| 103 | + |
| 104 | + |
| 105 | +## Guide filter. |
| 106 | +class GuidedFilter: |
| 107 | + ## Constructor. |
| 108 | + # @param I Input guidance image. Color or gray. |
| 109 | + # @param radius Radius of Guided Filter. |
| 110 | + # @param epsilon Regularization term of Guided Filter. |
| 111 | + def __init__(self, I, radius=5, epsilon=0.4): |
| 112 | + I_32F = to32F(I) |
| 113 | + |
| 114 | + if _isGray(I): |
| 115 | + self._guided_filter = GuidedFilterGray(I_32F, radius, epsilon) |
| 116 | + else: |
| 117 | + self._guided_filter = GuidedFilterColor(I_32F, radius, epsilon) |
| 118 | + |
| 119 | + ## Apply filter for the input image. |
| 120 | + # @param p Input image for the filtering. |
| 121 | + def filter(self, p): |
| 122 | + return to8U(self._guided_filter.filter(p)) |
| 123 | + |
| 124 | + |
| 125 | +## Common parts of guided filter. |
| 126 | +# |
| 127 | +# This class is used by guided_filter class. GuidedFilterGray and GuidedFilterColor. |
| 128 | +# Based on guided_filter._computeCoefficients, guided_filter._computeOutput, |
| 129 | +# GuidedFilterCommon.filter computes filtered image for color and gray. |
| 130 | +class GuidedFilterCommon: |
| 131 | + def __init__(self, guided_filter): |
| 132 | + self._guided_filter = guided_filter |
| 133 | + |
| 134 | + ## Apply filter for the input image. |
| 135 | + # @param p Input image for the filtering. |
| 136 | + def filter(self, p): |
| 137 | + p_32F = to32F(p) |
| 138 | + if _isGray(p_32F): |
| 139 | + return self._filterGray(p_32F) |
| 140 | + |
| 141 | + cs = p.shape[2] |
| 142 | + q = np.array(p_32F) |
| 143 | + |
| 144 | + for ci in range(cs): |
| 145 | + q[:, :, ci] = self._filterGray(p_32F[:, :, ci]) |
| 146 | + return q |
| 147 | + |
| 148 | + def _filterGray(self, p): |
| 149 | + ab = self._guided_filter._computeCoefficients(p) |
| 150 | + return self._guided_filter._computeOutput(ab, self._guided_filter._I) |
| 151 | + |
| 152 | + |
| 153 | +## Guided filter for gray guidance image. |
| 154 | +class GuidedFilterGray: |
| 155 | + # @param I Input gray guidance image. |
| 156 | + # @param radius Radius of Guided Filter. |
| 157 | + # @param epsilon Regularization term of Guided Filter. |
| 158 | + def __init__(self, I, radius=5, epsilon=0.4): |
| 159 | + self._radius = 2 * radius + 1 |
| 160 | + self._epsilon = epsilon |
| 161 | + self._I = to32F(I) |
| 162 | + self._initFilter() |
| 163 | + self._filter_common = GuidedFilterCommon(self) |
| 164 | + |
| 165 | + ## Apply filter for the input image. |
| 166 | + # @param p Input image for the filtering. |
| 167 | + def filter(self, p): |
| 168 | + return self._filter_common.filter(p) |
| 169 | + |
| 170 | + def _initFilter(self): |
| 171 | + I = self._I |
| 172 | + r = self._radius |
| 173 | + self._I_mean = cv2.blur(I, (r, r)) |
| 174 | + I_mean_sq = cv2.blur(I**2, (r, r)) |
| 175 | + self._I_var = I_mean_sq - self._I_mean**2 |
| 176 | + |
| 177 | + def _computeCoefficients(self, p): |
| 178 | + r = self._radius |
| 179 | + p_mean = cv2.blur(p, (r, r)) |
| 180 | + p_cov = p_mean - self._I_mean * p_mean |
| 181 | + a = p_cov / (self._I_var + self._epsilon) |
| 182 | + b = p_mean - a * self._I_mean |
| 183 | + a_mean = cv2.blur(a, (r, r)) |
| 184 | + b_mean = cv2.blur(b, (r, r)) |
| 185 | + return a_mean, b_mean |
| 186 | + |
| 187 | + def _computeOutput(self, ab, I): |
| 188 | + a_mean, b_mean = ab |
| 189 | + return a_mean * I + b_mean |
| 190 | + |
| 191 | + |
| 192 | +## Guided filter for color guidance image. |
| 193 | +class GuidedFilterColor: |
| 194 | + # @param I Input color guidance image. |
| 195 | + # @param radius Radius of Guided Filter. |
| 196 | + # @param epsilon Regularization term of Guided Filter. |
| 197 | + def __init__(self, I, radius=5, epsilon=0.2): |
| 198 | + self._radius = 2 * radius + 1 |
| 199 | + self._epsilon = epsilon |
| 200 | + self._I = to32F(I) |
| 201 | + self._initFilter() |
| 202 | + self._filter_common = GuidedFilterCommon(self) |
| 203 | + |
| 204 | + ## Apply filter for the input image. |
| 205 | + # @param p Input image for the filtering. |
| 206 | + def filter(self, p): |
| 207 | + return self._filter_common.filter(p) |
| 208 | + |
| 209 | + def _initFilter(self): |
| 210 | + I = self._I |
| 211 | + r = self._radius |
| 212 | + eps = self._epsilon |
| 213 | + |
| 214 | + Ir, Ig, Ib = I[:, :, 0], I[:, :, 1], I[:, :, 2] |
| 215 | + |
| 216 | + self._Ir_mean = cv2.blur(Ir, (r, r)) |
| 217 | + self._Ig_mean = cv2.blur(Ig, (r, r)) |
| 218 | + self._Ib_mean = cv2.blur(Ib, (r, r)) |
| 219 | + |
| 220 | + Irr_var = cv2.blur(Ir**2, (r, r)) - self._Ir_mean**2 + eps |
| 221 | + Irg_var = cv2.blur(Ir * Ig, (r, r)) - self._Ir_mean * self._Ig_mean |
| 222 | + Irb_var = cv2.blur(Ir * Ib, (r, r)) - self._Ir_mean * self._Ib_mean |
| 223 | + Igg_var = cv2.blur(Ig * Ig, (r, r)) - self._Ig_mean * self._Ig_mean + eps |
| 224 | + Igb_var = cv2.blur(Ig * Ib, (r, r)) - self._Ig_mean * self._Ib_mean |
| 225 | + Ibb_var = cv2.blur(Ib * Ib, (r, r)) - self._Ib_mean * self._Ib_mean + eps |
| 226 | + |
| 227 | + Irr_inv = Igg_var * Ibb_var - Igb_var * Igb_var |
| 228 | + Irg_inv = Igb_var * Irb_var - Irg_var * Ibb_var |
| 229 | + Irb_inv = Irg_var * Igb_var - Igg_var * Irb_var |
| 230 | + Igg_inv = Irr_var * Ibb_var - Irb_var * Irb_var |
| 231 | + Igb_inv = Irb_var * Irg_var - Irr_var * Igb_var |
| 232 | + Ibb_inv = Irr_var * Igg_var - Irg_var * Irg_var |
| 233 | + |
| 234 | + I_cov = Irr_inv * Irr_var + Irg_inv * Irg_var + Irb_inv * Irb_var |
| 235 | + Irr_inv /= I_cov |
| 236 | + Irg_inv /= I_cov |
| 237 | + Irb_inv /= I_cov |
| 238 | + Igg_inv /= I_cov |
| 239 | + Igb_inv /= I_cov |
| 240 | + Ibb_inv /= I_cov |
| 241 | + |
| 242 | + self._Irr_inv = Irr_inv |
| 243 | + self._Irg_inv = Irg_inv |
| 244 | + self._Irb_inv = Irb_inv |
| 245 | + self._Igg_inv = Igg_inv |
| 246 | + self._Igb_inv = Igb_inv |
| 247 | + self._Ibb_inv = Ibb_inv |
| 248 | + |
| 249 | + def _computeCoefficients(self, p): |
| 250 | + r = self._radius |
| 251 | + I = self._I |
| 252 | + Ir, Ig, Ib = I[:, :, 0], I[:, :, 1], I[:, :, 2] |
| 253 | + |
| 254 | + p_mean = cv2.blur(p, (r, r)) |
| 255 | + |
| 256 | + Ipr_mean = cv2.blur(Ir * p, (r, r)) |
| 257 | + Ipg_mean = cv2.blur(Ig * p, (r, r)) |
| 258 | + Ipb_mean = cv2.blur(Ib * p, (r, r)) |
| 259 | + |
| 260 | + Ipr_cov = Ipr_mean - self._Ir_mean * p_mean |
| 261 | + Ipg_cov = Ipg_mean - self._Ig_mean * p_mean |
| 262 | + Ipb_cov = Ipb_mean - self._Ib_mean * p_mean |
| 263 | + |
| 264 | + ar = self._Irr_inv * Ipr_cov + self._Irg_inv * Ipg_cov + self._Irb_inv * Ipb_cov |
| 265 | + ag = self._Irg_inv * Ipr_cov + self._Igg_inv * Ipg_cov + self._Igb_inv * Ipb_cov |
| 266 | + ab = self._Irb_inv * Ipr_cov + self._Igb_inv * Ipg_cov + self._Ibb_inv * Ipb_cov |
| 267 | + b = p_mean - ar * self._Ir_mean - ag * self._Ig_mean - ab * self._Ib_mean |
| 268 | + |
| 269 | + ar_mean = cv2.blur(ar, (r, r)) |
| 270 | + ag_mean = cv2.blur(ag, (r, r)) |
| 271 | + ab_mean = cv2.blur(ab, (r, r)) |
| 272 | + b_mean = cv2.blur(b, (r, r)) |
| 273 | + |
| 274 | + return ar_mean, ag_mean, ab_mean, b_mean |
| 275 | + |
| 276 | + def _computeOutput(self, ab, I): |
| 277 | + ar_mean, ag_mean, ab_mean, b_mean = ab |
| 278 | + |
| 279 | + Ir, Ig, Ib = I[:, :, 0], I[:, :, 1], I[:, :, 2] |
| 280 | + |
| 281 | + q = ar_mean * Ir + ag_mean * Ig + ab_mean * Ib + b_mean |
| 282 | + |
| 283 | + return q |
0 commit comments