[GRASS-user] circular neighborhoods?

Hello all.

I was thinking about the circular neighborhoods that r.neighbors use.
How exactly are they shaped? I mean, in a 3x3 window, does the
circular one looks like a cross? and then it start to look more like a
circle as the size increases?

cheers

Carlos

--
+-----------------------------------------------------------+
Carlos Henrique Grohmann - Guano
Geologist M.Sc - Doctorate Student at IGc-USP - Brazil
Linux User #89721 - carlos dot grohmann at gmail dot com
+-----------------------------------------------------------+
_________________
"Good morning, doctors. I have taken the liberty of removing Windows
95 from my hard drive."
--The winning entry in a "What were HAL's first words" contest judged
by 2001: A SPACE ODYSSEY creator Arthur C. Clarke

Can't stop the signal.

I was thinking about the circular neighborhoods that r.neighbors use.
How exactly are they shaped? I mean, in a 3x3 window, does the
circular one looks like a cross? and then it start to look more like a
circle as the size increases?

As far as I understand it,

A 3x3 window would look like this:

o-o-o
o-X-o
o-o-o

Where the 'X' is the current cell being processed. Similarly, a 5X5 window
would look like this:

o-o-o-o-o
o-o-o-o-o
o-o-X-o-o
o-o-o-o-o
o-o-o-o-o

r.neighbors will perform calculations on all the cells I've marked 'o' and and
assign the output value to 'X', according to whatever method has been chosen
(i.e., average, median, min, max, etc.).

~ Eric.

Yes, Eric, that is what it does for a _square_ neighborhood. I am a
little confuse about the actual shape of the _circular_ neighborhood.

using your examples:

will a 3x3 circular neighborhood look like this:?

---o---
o-X-o
---o---

and a 5x5?

---o-o-o---
o-o-o-o-o
o-o-X-o-o
o-o-o-o-o
---o-o-o---

maybe now my question is more clear...

thanks

Carlos

On Thu, Feb 21, 2008 at 3:07 PM, Patton, Eric <epatton@nrcan.gc.ca> wrote:

>I was thinking about the circular neighborhoods that r.neighbors use.
>How exactly are they shaped? I mean, in a 3x3 window, does the
>circular one looks like a cross? and then it start to look more like a
>circle as the size increases?

As far as I understand it,

A 3x3 window would look like this:

o-o-o
o-X-o
o-o-o

Where the 'X' is the current cell being processed. Similarly, a 5X5 window
would look like this:

o-o-o-o-o
o-o-o-o-o
o-o-X-o-o
o-o-o-o-o
o-o-o-o-o

r.neighbors will perform calculations on all the cells I've marked 'o' and and
assign the output value to 'X', according to whatever method has been chosen
(i.e., average, median, min, max, etc.).

~ Eric.

--
+-----------------------------------------------------------+
Carlos Henrique Grohmann - Guano
Geologist M.Sc - Doctorate Student at IGc-USP - Brazil
Linux User #89721 - carlos dot grohmann at gmail dot com
+-----------------------------------------------------------+
_________________
"Good morning, doctors. I have taken the liberty of removing Windows
95 from my hard drive."
--The winning entry in a "What were HAL's first words" contest judged
by 2001: A SPACE ODYSSEY creator Arthur C. Clarke

Can't stop the signal.

On Thu, Feb 21, 2008 at 03:11:43PM -0300, we recorded a bogon-computron collision of the <carlos.grohmann@gmail.com> flavor, containing:

Yes, Eric, that is what it does for a _square_ neighborhood. I am a
little confuse about the actual shape of the _circular_ neighborhood.

using your examples:

will a 3x3 circular neighborhood look like this:?

---o---
o-X-o
---o---

and a 5x5?

---o-o-o---
o-o-o-o-o
o-o-X-o-o
o-o-o-o-o
---o-o-o---

maybe now my question is more clear...

Piping in here after a quick glance at the source code...

When the "-c" option to r.neighbors is selected, a neighborhood mask of booleans
is created by this loop (in pseudocode):

  neighborhoodDistance=neighborhoodSize/2 (in integer math)
  for (i=0; i<neigborhoodSize; i++)
    for (j=0; j<neighborhoodSize; j++)
       mask[i][k]= ( (i-neighborhoodDistance)^2 + (j-neighborhoodDistance)^2 <= (neighborhoodDistance)^2

So in a 3x3 example, neighborhoodSize=3, neighborhoodDistance=1, and your
first diagram has "o"s where the mask is true. The corners of the square
wouldn't be in the mask (as in Eric's 3x3), because those points would have
the left hand side of the inequality equal to 2, making the mask false.

I haven't actually *tried* the code, but that is what the block of code in
gather.c for circular neighborhoods says it should be doing.

> >I was thinking about the circular neighborhoods that r.neighbors use.
> >How exactly are they shaped? I mean, in a 3x3 window, does the
> >circular one looks like a cross? and then it start to look more like a
> >circle as the size increases?
>
> As far as I understand it,
>
> A 3x3 window would look like this:
>
> o-o-o
> o-X-o
> o-o-o
>
> Where the 'X' is the current cell being processed. Similarly, a 5X5 window
> would look like this:
>
> o-o-o-o-o
> o-o-o-o-o
> o-o-X-o-o
> o-o-o-o-o
> o-o-o-o-o
>
> r.neighbors will perform calculations on all the cells I've marked 'o' and and
> assign the output value to 'X', according to whatever method has been chosen
> (i.e., average, median, min, max, etc.).
>
> ~ Eric.
>

--
+-----------------------------------------------------------+
Carlos Henrique Grohmann - Guano
Geologist M.Sc - Doctorate Student at IGc-USP - Brazil
Linux User #89721 - carlos dot grohmann at gmail dot com
+-----------------------------------------------------------+
_________________
"Good morning, doctors. I have taken the liberty of removing Windows
95 from my hard drive."
--The winning entry in a "What were HAL's first words" contest judged
by 2001: A SPACE ODYSSEY creator Arthur C. Clarke

Can't stop the signal.
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
Tom Russo KM5VY SAR502 DM64ux http://www.swcp.com/~russo/
Tijeras, NM QRPL#1592 K2#398 SOC#236 AHTB#1 http://kevan.org/brain.cgi?DDTNM
"And, isn't sanity really just a one-trick pony anyway? I mean all you get is
one trick, rational thinking, but when you're good and crazy, oooh, oooh,
oooh, the sky is the limit!" --- The Tick

So in a 3x3 example, neighborhoodSize=3, neighborhoodDistance=1, and your
first diagram has "o"s where the mask is true. The corners of the square
wouldn't be in the mask (as in Eric's 3x3), because those points would have
the left hand side of the inequality equal to 2, making the mask false.

I haven't actually *tried* the code, but that is what the block of code in
gather.c for circular neighborhoods says it should be doing.

Thanks for the info - I wasn't even aware that r.neighbors could do that, as
I've always used the square windows for my work.

If I get a chance to test it out, I'll report back on what happening.

~ Eric.

Thanks for the quick answer Tom.

Carlos

On Thu, Feb 21, 2008 at 3:52 PM, Tom Russo <russo@bogodyn.org> wrote:

On Thu, Feb 21, 2008 at 03:11:43PM -0300, we recorded a bogon-computron collision of the <carlos.grohmann@gmail.com> flavor, containing:

> Yes, Eric, that is what it does for a _square_ neighborhood. I am a
> little confuse about the actual shape of the _circular_ neighborhood.
>
> using your examples:
>
> will a 3x3 circular neighborhood look like this:?
>
> ---o---
> o-X-o
> ---o---
>
> and a 5x5?
>
> ---o-o-o---
> o-o-o-o-o
> o-o-X-o-o
> o-o-o-o-o
> ---o-o-o---
>
>
> maybe now my question is more clear...

Piping in here after a quick glance at the source code...

When the "-c" option to r.neighbors is selected, a neighborhood mask of booleans
is created by this loop (in pseudocode):

  neighborhoodDistance=neighborhoodSize/2 (in integer math)
  for (i=0; i<neigborhoodSize; i++)
    for (j=0; j<neighborhoodSize; j++)
       mask[i][k]= ( (i-neighborhoodDistance)^2 + (j-neighborhoodDistance)^2 <= (neighborhoodDistance)^2

So in a 3x3 example, neighborhoodSize=3, neighborhoodDistance=1, and your
first diagram has "o"s where the mask is true. The corners of the square
wouldn't be in the mask (as in Eric's 3x3), because those points would have
the left hand side of the inequality equal to 2, making the mask false.

I haven't actually *tried* the code, but that is what the block of code in
gather.c for circular neighborhoods says it should be doing.

> > >I was thinking about the circular neighborhoods that r.neighbors use.
> > >How exactly are they shaped? I mean, in a 3x3 window, does the
> > >circular one looks like a cross? and then it start to look more like a
> > >circle as the size increases?
> >
> > As far as I understand it,
> >
> > A 3x3 window would look like this:
> >
> > o-o-o
> > o-X-o
> > o-o-o
> >
> > Where the 'X' is the current cell being processed. Similarly, a 5X5 window
> > would look like this:
> >
> > o-o-o-o-o
> > o-o-o-o-o
> > o-o-X-o-o
> > o-o-o-o-o
> > o-o-o-o-o
> >
> > r.neighbors will perform calculations on all the cells I've marked 'o' and and
> > assign the output value to 'X', according to whatever method has been chosen
> > (i.e., average, median, min, max, etc.).
> >
> > ~ Eric.
> >
>
>
>
> --
> +-----------------------------------------------------------+
> Carlos Henrique Grohmann - Guano
> Geologist M.Sc - Doctorate Student at IGc-USP - Brazil
> Linux User #89721 - carlos dot grohmann at gmail dot com
> +-----------------------------------------------------------+
> _________________
> "Good morning, doctors. I have taken the liberty of removing Windows
> 95 from my hard drive."
> --The winning entry in a "What were HAL's first words" contest judged
> by 2001: A SPACE ODYSSEY creator Arthur C. Clarke
>
> Can't stop the signal.
> _______________________________________________
> grass-user mailing list
> grass-user@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user

--
Tom Russo KM5VY SAR502 DM64ux http://www.swcp.com/~russo/
Tijeras, NM QRPL#1592 K2#398 SOC#236 AHTB#1 http://kevan.org/brain.cgi?DDTNM
"And, isn't sanity really just a one-trick pony anyway? I mean all you get is
  one trick, rational thinking, but when you're good and crazy, oooh, oooh,
  oooh, the sky is the limit!" --- The Tick

--
+-----------------------------------------------------------+
Carlos Henrique Grohmann - Guano
Geologist M.Sc - Doctorate Student at IGc-USP - Brazil
Linux User #89721 - carlos dot grohmann at gmail dot com
+-----------------------------------------------------------+
_________________
"Good morning, doctors. I have taken the liberty of removing Windows
95 from my hard drive."
--The winning entry in a "What were HAL's first words" contest judged
by 2001: A SPACE ODYSSEY creator Arthur C. Clarke

Can't stop the signal.

Tom Russo wrote:

When the "-c" option to r.neighbors is selected, a neighborhood mask of booleans
is created by this loop (in pseudocode):

  neighborhoodDistance=neighborhoodSize/2 (in integer math)
  for (i=0; i<neigborhoodSize; i++)
    for (j=0; j<neighborhoodSize; j++)
       mask[i][k]= ( (i-neighborhoodDistance)^2 + (j-neighborhoodDistance)^2 <= (neighborhoodDistance)^2

So in a 3x3 example, neighborhoodSize=3, neighborhoodDistance=1, and your
first diagram has "o"s where the mask is true. The corners of the square
wouldn't be in the mask (as in Eric's 3x3), because those points would have
the left hand side of the inequality equal to 2, making the mask false.

I haven't actually *tried* the code, but that is what the block of code in
gather.c for circular neighborhoods says it should be doing.

FWIW, here are the exact masks for the first few sizes:

3x3 . X .
  X X X
  . X .

5x5 . . X . .
  . X X X .
  X X X X X
  . X X X .
  . . X . .

7x7 . . . X . . .
  . X X X X X .
  . X X X X X .
  X X X X X X X
  . X X X X X .
  . X X X X X .
  . . . X . . .

9x9 . . . . X . . . .
  . . X X X X X . .
  . X X X X X X X .
  . X X X X X X X .
  X X X X X X X X X
  . X X X X X X X .
  . X X X X X X X .
  . . X X X X X . .
  . . . . X . . . .

11x11 . . . . . X . . . . .
  . . X X X X X X X . .
  . X X X X X X X X X .
  . X X X X X X X X X .
  . X X X X X X X X X .
  X X X X X X X X X X X
  . X X X X X X X X X .
  . X X X X X X X X X .
  . X X X X X X X X X .
  . . X X X X X X X . .
  . . . . . X . . . . .

13x13 . . . . . . X . . . . . .
  . . . X X X X X X X . . .
  . . X X X X X X X X X . .
  . X X X X X X X X X X X .
  . X X X X X X X X X X X .
  . X X X X X X X X X X X .
  X X X X X X X X X X X X X
  . X X X X X X X X X X X .
  . X X X X X X X X X X X .
  . X X X X X X X X X X X .
  . . X X X X X X X X X . .
  . . . X X X X X X X . . .
  . . . . . . X . . . . . .

The "pips" on the four edges arise from the fact that the distance
from the centres of those cells to the centre of the centre cell is
exactly equal to the radius. If the radius wasn't truncated to an
integer (e.g. 5x5 => radius = 2.5 instead of 2), the masks would look
slightly more circular.

If you want some other shape, you can achieve the desired result by
using the weight= option to specify a weights file where all values
are equal (for method=sum, the sum of the weights should be 1).

Actually, the values don't have to be equal. You might want to make
the weights at the edge of the neighbourhood vary according to the
proportion of the cell that lies inside the circle, effectively
anti-aliasing the mask.

For aggregates where a weighted calculation isn't meaningful
(specifically: minimum, maximum, diversity and interspersion), the
weights are used to create a binary mask, where zero causes the cell
to be ignored and any non-zero value causes the cell to be used.

--
Glynn Clements <glynn@gclements.plus.com>

I've added Glynn's diagrams of the circular neighborhoods used with the -c flag to the manpage in trunk and the 6.3 release branch. I also included some of his comments on the effect of using various weight files to modify the neighborhood window shape.

(Aside: the shape can only be modified by using the -c flag *and* using a weights file?)

~ Eric

Patton, Eric wrote:

(Aside: the shape can only be modified by using the -c flag *and* using
a weights file?)

No, the two are mutually exclusive.

If you use neither weights= nor -c, you get a square neighbourhood. If
you use -c, you get a circular neighbourhood. If you use weights=, the
values in the weights file determine the shape of the neighbourhood.
If you use both -c and weights=, you get an error.

--
Glynn Clements <glynn@gclements.plus.com>