Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Blurring with mathematica

11 views
Skip to first unread message

V.Mar...@damtp.cam.ac.uk

unread,
Feb 27, 2005, 1:49:22 AM2/27/05
to
Hello everyone.

I have an image which is stored as an 128 times 128
matrix and I want to add gaussian blurring. Does anyone
know how can this be done with mathematica?

Best, Vangelis Marinakis.

Jens-Peer Kuska

unread,
Feb 28, 2005, 3:33:00 AM2/28/05
to
Hi,

ListConvolve[] with a filter you like ?
gg = Graphics[
Text["Filter", {0, 0},
TextStyle -> {FontFamily -> "Helvetica", FontSize -> 48}]];

ras = ImportString[ExportString[gg, "PGM", ImageSize -> {256, 256}], "PGM"];

Show[ras /. Raster[bm_, args___] :>
Raster[ListConvolve[
{{1, 2, 1},
{2, 4, 2},
{1, 2, 1}}/16, bm, {1, 1}], args]]

Regards

Jens


<V.Mar...@damtp.cam.ac.uk> schrieb im Newsbeitrag
news:cvrqhi$p5c$1...@smc.vnet.net...

Steve Luttrell

unread,
Feb 28, 2005, 3:40:37 AM2/28/05
to
Generate an image.

image = Table[Random[], {128}, {128}];
Show[Graphics[Raster[image]], AspectRatio -> 1];

Generate a Gaussian blurring kernel.

halfwindow = 5;
sdev = 2;
kernel = Table[Exp[-((i^2 + j^2)/(2*sdev^2))],
{i, -halfwindow, halfwindow}, {j, -halfwindow, halfwindow}];
Show[Graphics[Raster[kernel]], AspectRatio -> 1];

Blur the image.

image2 = ListCorrelate[kernel, image];
image2 /= Max[image2];
Show[Graphics[Raster[image2]], AspectRatio -> 1];

If you want to blur with circular wraparound then use

ListCorrelate[kernel, image,
{{halfwindow + 1, halfwindow + 1},
{halfwindow + 1, halfwindow + 1}}];
Steve Luttrell

<V.Mar...@damtp.cam.ac.uk> wrote in message
news:cvrqhi$p5c$1...@smc.vnet.net...

DrBob

unread,
Mar 1, 2005, 2:15:35 AM3/1/05
to
That code doesn't work in Mathematica 5.1 (at my machine).

ListConvolve throws the kldims exception and Raster throws a matrix exception.

Is this because I don't have the Image Processing package, perhaps? If that's it, I'd think ExportString would complain about the "PGM" format.

Bobby

On Mon, 28 Feb 2005 03:27:00 -0500 (EST), Jens-Peer Kuska <ku...@informatik.uni-leipzig.de> wrote:

> Hi,
>
> ListConvolve[] with a filter you like ?
> gg = Graphics[
> Text["Filter", {0, 0},
> TextStyle -> {FontFamily -> "Helvetica", FontSize -> 48}]];
>
> ras = ImportString[ExportString[gg, "PGM", ImageSize -> {256, 256}], "PGM"];
>
> Show[ras /. Raster[bm_, args___] :>
> Raster[ListConvolve[
> {{1, 2, 1},
> {2, 4, 2},
> {1, 2, 1}}/16, bm, {1, 1}], args]]
>
> Regards
>
> Jens
>
>
>
>
> <V.Mar...@damtp.cam.ac.uk> schrieb im Newsbeitrag
> news:cvrqhi$p5c$1...@smc.vnet.net...

--
Dr...@bigfoot.com
www.eclecticdreams.net

V.Mar...@damtp.cam.ac.uk

unread,
Mar 2, 2005, 10:33:23 PM3/2/05
to
Dear all,

I have some questions regarding the following email:
1) Shouldn't it be the factor (1/Sqrt[2 Pi sdev^2]) in the kernel?
2) What should I change in order to normalise the gaussian to an interval
of 1;

Best, Vangelis Marinakis.


--------------------------------------------------------------------

Generate an image.

image = Table[Random[], {128}, {128}];
Show[Graphics[Raster[image]], AspectRatio -> 1];

Generate a Gaussian blurring kernel.

halfwindow = 5;
sdev = 2;
kernel = Table[Exp[-((i^2 + j^2)/(2*sdev^2))],
{i, -halfwindow, halfwindow}, {j, -halfwindow, halfwindow}];
Show[Graphics[Raster[kernel]], AspectRatio -> 1];

Blur the image.

image2 = ListCorrelate[kernel, image];
image2 /= Max[image2];
Show[Graphics[Raster[image2]], AspectRatio -> 1];

If you want to blur with circular wraparound then use

ListCorrelate[kernel, image,
{{halfwindow + 1, halfwindow + 1},
{halfwindow + 1, halfwindow + 1}}];
Steve Luttrell

<V.Marinakis@[EMAIL PROTECTED]
> wrote in message
news:cvrqhi$p5c$1@[EMAIL PROTECTED]

Steve Luttrell

unread,
Mar 4, 2005, 5:34:19 AM3/4/05
to
Your questions show that you are not having problems with Mathematica as
such, but with basic mathematics itself. Maybe it would be a good idea to do
some background reading.

Here are the answers to your questions anyway:

1) If you want to normalise the blurring kernel then use

(1/(2*Pi*sdev^2))*Exp[-((i^2 + j^2)/(2*sdev^2))]

There is a factor of 1/Sqrt[2 Pi sdev^2] for each of the 2 dimensions.

You can verify this is correct by integrating over the continuous-valued
versions of i and j

Integrate[(1/(2*Pi*sdev^2))*Exp[-((i^2 + j^2)/(2*sdev^2))],
{i, -Infinity, Infinity}, {j, -Infinity, Infinity},
Assumptions -> Re[sdev^2] > 0]

to obtain 1.

However, the computations you want to do are over a discrete 2-dimensional
grid, so if you want to normalise then you should make the appropriate
changes (i.e use sums rather than integrals). Also have a look at my
response to question (2) for the general approach to computing a
normalisation factor.

2) The precise meaning of your question is not clear. However, it appears
that you want to normalise a kernel so that its integral (or sum) over some
domain is unity. The normalisation factor to put in the denominator is thus
the integral (or sum) of the kernel that you started with, and which thus
acts to cancel whatever (wrong) norm you had in the first place.

Steve Luttrell

<V.Mar...@damtp.cam.ac.uk> wrote in message
news:d060i3$ko6$1...@smc.vnet.net...

0 new messages