# [SciPy-User] Gaussian filter on an angle

Zachary Pincus zachary.pincus@yale....
Fri Jun 4 09:36:36 CDT 2010

```On Jun 4, 2010, at 6:38 AM, Anne Archibald wrote:

> On 4 June 2010 06:00, Matthieu Brucher <matthieu.brucher@gmail.com>
> wrote:
>> Hi,
>>
>> I'm trying to blur an angle field, but it's not easy ;)
>> Applying gaussian_filter (from ndimage) on the sinus and the cos is
>> not enough to have a smooth angle field, and of course applying
>> gaussian_filter directly on the angle field does not yeild
>> satisfactiry results.
>> Does anyone know of a function (even if it not in Python yet) that
>> could gaussian filter an angle field? Something like a Riemanian
>> filter (instead of an Euclidian one)...
>
> This isn't my field, but I suspect you will have problems with this.
> In particular, there is a *topological* obstacle to blurring angle
> fields. In the blurred field, you want each angle to be close to that
> of nearby pixels. But imagine following the angle around the image in
> a circle: the angle changes by one full turn as you go around this
> loop. Any smoothing mechanism must either introduce a discontinuity in
> this loop or retain one full turn around the loop.

Anne's quite right -- I've banged my head on things like this before
too. I have a different idea about how to get around these issues, in
a killing-a-gnat-with-a-bazooka kind of way, though: you might be able
to pose this question as one of smoothing via curve-fitting instead of
via filtering?

E.g. fit a bivariate spline or some other polynomial surface to your
angles in such a was as to minimize not the squared residuals
directly, but the square of the minimum angle between the fit surface
at that point and the data (going clockwise or counterclockwise,
whichever is smaller... I forget exactly but there's a closed-form way
to calculate that). This way you get a smooth underlying fit in a way
that is (I think?) immune to discontinuities in the angle data.

Problem is either this will be very slow (fitting a many-parameter
surface) or probably over-smooth (fitting a low-parameter surface).
There are probably some multi-resolution methods you could use. I
think the nonlinear least squares optimizers in scipy would be the way
to go here?

Zach

> The former is
> unlikely to be desirable, and the latter is asking rather a lot of a
> smoothing method, and in any case still results in rapidly-changing
> angles around small loops. You could look into "phase unwrapping",
> techniques to reconstruct a function from its values modulo 2 pi;
> obviously once you had an unwrapped function blurring would work
> normally. In this setting unwrapping simply fails when there are
> topological obstacles. The alternative I would suggest is what you
> already tried, converting your angles to a vector field and smoothing
> that. You'll still get defects where the angles change rapidly, but I
> don't think that can be avoided, and the length of the resulting
> vectors will tell you something about the degree of defectiveness.
>
> The key to making any of this work is having original angles that are
> not too noisy. If you're extracting the angles from some underlying
> data, say by calculating an average direction over squares of an
> image, I recommend using enough averaging to get the noise on the
> angle quite small, so that defects will be rare. You may find yourself
> needing to resolve defects manually if you can't just live with them.
>
>
> Anne
>
> P.S. This sort of topological obstruction is the origin for
> hypothetical "cosmic strings" as well as some of the neat dynamics of
> vortices in inviscid fluids and magnetic fields in type II
> superconductors. -A
>
>> Matthieu
>> --
>> Information System Engineer, Ph.D.
>> Blog: http://matt.eifelle.com
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

```