4
$\begingroup$

When applying Gaussian filters close to the borders of an image, algorithms usually zero-pad or replicate/mirror/wrap the borders. This is not good enough for my case, so I wonder if there is something better out there.

As the idea of smoothing is to absorb the information of surrounding points, I wonder if it would make sense to use a different kernel in the border cases. This kernel would only take values from available surrounding points, e.g. if my kernel is something like:

\begin{bmatrix} 0.0113 & 0.0838 & 0.0113 \\ 0.0838 & 0.6193 & 0.0838 \\ 0.0113 & 0.0838 & 0.0113 \end{bmatrix}

To calculate a pixel on the top margin (but not close to the corner), I would use

\begin{bmatrix} 0.0938 & 0.6933 & 0.0938 \\ 0.0126 & 0.0938 & 0.0126 \end{bmatrix}

The latter matrix was calculated by cropping out the part of the kernel that would be out of the image (the top row) and renormalizing. Is there something wrong with this approach, or an alternative?

Here's some code to explain my idea better:

kernel = [0.0113 0.0838 0.0113; 0.0838 0.6193 0.0838; 0.0113 0.0838 0.0113]
kernel2 = [0.0938 0.6933 0.0938; 0.0126 0.0938 0.0126];
M = [1 2 3; 4 5 6]; % my image

% application of kernel to point 2 in M would provide the value:
res1 = sum(sum( kernel(2:3,:).*M )); % 0 padding, equals 2.1058
res2 = sum(sum( kernel.*[1 2 3; M] )); % row replication, equals 2.3186

% application of kernel2 to the same point:
res3 = sum(sum( kernel2 .* M )); % equals 2.3568

Note that in using kernel2 I don't extrapolate a row of [0 0 0] or [1 2 3], which would underestimate the point.

$\endgroup$
  • $\begingroup$ You need to define if the filter is of the "scattering" or the "gathering" variety. $\endgroup$ – Olli Niemitalo Sep 17 '15 at 18:06
  • $\begingroup$ Forgive my ignorance, I don't know what is that scatter / gather. I'm just applying a gaussian kernel using a convolution. That sounds like "gather"? $\endgroup$ – serigado Sep 17 '15 at 23:23
  • $\begingroup$ In the scattering variety you start with a zero-filled output image, and then for every input image pixel you add to the output image the "impulse response matrix" scaled by and translated to the coordinates of the input image pixel. In the gathering variety you calculate each output pixel by multiplying the input image by the translated "filter coefficient matrix" and taking the sum over the resulting values. Normally scattering and gathering produce the same result, but not when the filter kernel depends on the position. Scattering preserves average image intensity but gathering might not. $\endgroup$ – Olli Niemitalo Sep 18 '15 at 6:22
2
$\begingroup$

Pixels outside the image borders must be extrapolated.
Now, you need to chose the model of your extrapolation.

For instance, if you're working within the Discrete Fourier model a periodic extrapolation is the native choice.
Usually the most "Eye Friendly" solution is replication based on "Nearest Neighbor".

Your choice is as valid as any choice, you just need to figure which model it satisfies and if that model suits you.

Yet it has 1 noticeable caveat, it makes your kernel Spatially Variant, which means, without a caring treatment it will hurt performance.
The right thing to do is isolate the pixels which should be carefully treated because of the boundary condition and work very efficiently on the rest.

Tim Holy talks about it in the video Image Representation and Analysis (Go to 42:30).

$\endgroup$
  • $\begingroup$ What I'm saying is that I won't do any extrapolation, instead I adapt my kernel according to the position, in order to account for "visible" pixels only. For all pixels at a distance bigger than (N-1)/2 of the limit, I can use a normal convolution, so performance in not much affected. My case is for a gaussian-like filter, it just happens that the border points are important, so I can't simply add or extrapolate something that will affect the border pixels. $\endgroup$ – serigado Sep 17 '15 at 23:04
  • $\begingroup$ You're wrong about that. You can always find a non linear operation which involves extrapolation to describe what you do. For instance, what you do is equivalent of extrapolation with zeros and scaling (Adaptive) of the Kernel. Regarding performance, you'd be much slower than a padding and then only "Valid Convolution". $\endgroup$ – Royi Sep 18 '15 at 8:05
  • $\begingroup$ By the way, would you assist us on opening a community for Image Processing: area51.stackexchange.com/proposals/86832. Please follow, add your question and up vote the other proposed questions. Thank You. $\endgroup$ – Royi Sep 18 '15 at 8:08
1
$\begingroup$

I don't like very much the idea of cropping the kernel and renormalizing it, as it will artificially emphasize pixels that needn't be (the weights increase globally as the ratio of the normalization constants).

I'd prefer to extrapolate the values with a rule that has "physical soundness" and apply the full kernel. If the extrapolation rule is linear, you can indeed recompute a kernel that incorporates the interpolation rule.

To give a trivial example, assume the binomial kernel $\frac14\frac24\frac14$ over pixels a b c.

If the pixel a is missing,

  • cropping and renormalizing gives $\frac23\frac13$;

  • simple extrapolation by pixel replication (the missing pixel is assumed b) is equivalent to $\frac34\frac14$;

  • mirroring (c assumed) gives $\frac24\frac24$;

  • linear extrapolation (2b-c assumed) gives $\frac44\frac04$.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.