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.
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...
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...
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...
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]
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...