[GRASS-user] How to obtain selected adjacent pixel values?

Hello all, this is my first time using a subscriber list and I have some doubts about using it and about grass gis.

I have an AML script (old arcinfo srcipt) that uses focal operations that I’ve sorted out well using r.neighbors but I have a problem in certain part of the script to follow in grass GIS. In some part of the script the author declares a variable as a grid called “cbd” and codes as follows:

%cb%d = int(abs((%cb% - %cb%(1,0)) + (%cb% - %cb%(0,1)) + (%cb% - %cb%(0,-1)) + (%cb% - %cb%(-1,0))))

From where he obtains a grid filled with integer positive values from 0 to 4. All these values come from a binary grid (0 or 1 values) declared as “cb” where I believe (%cb% - %cb%(1,0)) means something like “central pixel value minus pixel value at west from central pixel value” and (%cb% - %cb%(0,1)) means something like “central pixel value minus pixel value at north from central pixel value”, and so on…

How could I obtain the differences between central pixel values and cardinal directions pixel values related with a central pixel?

Or in a different manner, how to do this in grass gis?

RASTER_OUT= round(abs((RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_UP) + (RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_DOWN) + (RASTER_IN - RASTER_IN_PUSHED_ONE_COLUMN_LEFT) + (RASTER_IN - RASTER_IN_PUSHED_ONE_COLUMN_RIGHT))

Thanks in advance.


Saludos,

Yasser

Yasser Said Lopez de Olmos Reyes wrote:

How could I obtain the differences between central pixel values and
cardinal directions pixel values related with a central pixel?

Or in a different manner, how to do this in grass gis?

RASTER_OUT= round(abs((RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_UP) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_DOWN) + (RASTER_IN -
RASTER_IN_PUSHED_ONE_COLUMN_LEFT) + (RASTER_IN -
RASTER_IN_PUSHED_ONE_COLUMN_RIGHT))

This boils down to calculating

(RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_UP) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_DOWN) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_COLUMN_LEFT) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_COLUMN_RIGHT)

Please check the manual of r.mapcalc. There is

--%<---
THE NEIGHBORHOOD MODIFIER

Maps and images are data base files stored in raster format, i.e., two-
dimensional matrices of integer values. In r.mapcalc, maps may be followed by
a neighborhood modifier that specifies a relative offset from the current cell
being evaluated. The format is map[r,c], where r is the row offset and c is
the column offset. For example, map[1,2] refers to the cell one row below and
two columns to the right of the current cell, map[-2,-1] refers to the cell
two rows above and one column to the left of the current cell, and map[0,1]
refers to the cell one column to the right of the current cell. This syntax
permits the development of neighborhood-type filters within a single map or
across multiple maps.
--->%--

which I think can be used to obtain what is asked.

So, for each of the off-current pixel, the modifiers would be like

RASTER_IN_PUSHED_ONE_ROW_UP: map[-1,0]
RASTER_IN_PUSHED_ONE_ROW_DOWN: map[1,0]
RASTER_IN_PUSHED_ONE_COLUMN_LEFT: map[0,-1]
RASTER_IN_PUSHED_ONE_COLUMN_RIGHT: map[0,1]

I am not exactly sure about the implementation though.

Nikos

(cc-ing @MarkusMetz)

Yasser Said Lopez de Olmos Reyes wrote:

> How could I obtain the differences between central pixel values and
> cardinal directions pixel values related with a central pixel?
> Or in a different manner, how to do this in grass gis?

> RASTER_OUT= round(abs((RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_UP) +
> (RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_DOWN) + (RASTER_IN -
> RASTER_IN_PUSHED_ONE_COLUMN_LEFT) + (RASTER_IN -
> RASTER_IN_PUSHED_ONE_COLUMN_RIGHT))

Nikos Alexandris wrote:

This boils down to calculating
(RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_UP) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_DOWN) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_COLUMN_LEFT) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_COLUMN_RIGHT)

Please check the manual of r.mapcalc. There is

--%<---
THE NEIGHBORHOOD MODIFIER

..

--->%--

which I think can be used to obtain what is asked.
So, for each of the off-current pixel, the modifiers would be like

RASTER_IN_PUSHED_ONE_ROW_UP: map[-1,0]
RASTER_IN_PUSHED_ONE_ROW_DOWN: map[1,0]
RASTER_IN_PUSHED_ONE_COLUMN_LEFT: map[0,-1]
RASTER_IN_PUSHED_ONE_COLUMN_RIGHT: map[0,1]

I am not exactly sure about the implementation though.

@Yasser, if you implement this and think it is something that can be justified
as generically useful, why not ask @MarkusMetz if it is easy to include in
"r.neighbors" as yet another method?

Best, Nikos

On Sun, Jun 30, 2013 at 3:31 PM, Nikos Alexandris
<nik@nikosalexandris.net> wrote:

(cc-ing @MarkusMetz)

Yasser Said Lopez de Olmos Reyes wrote:

> How could I obtain the differences between central pixel values and
> cardinal directions pixel values related with a central pixel?
> Or in a different manner, how to do this in grass gis?

> RASTER_OUT= round(abs((RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_UP) +
> (RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_DOWN) + (RASTER_IN -
> RASTER_IN_PUSHED_ONE_COLUMN_LEFT) + (RASTER_IN -
> RASTER_IN_PUSHED_ONE_COLUMN_RIGHT))

Nikos Alexandris wrote:

This boils down to calculating
(RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_UP) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_DOWN) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_COLUMN_LEFT) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_COLUMN_RIGHT)

Please check the manual of r.mapcalc. There is

--%<---
THE NEIGHBORHOOD MODIFIER

..

--->%--

which I think can be used to obtain what is asked.
So, for each of the off-current pixel, the modifiers would be like

RASTER_IN_PUSHED_ONE_ROW_UP: map[-1,0]
RASTER_IN_PUSHED_ONE_ROW_DOWN: map[1,0]
RASTER_IN_PUSHED_ONE_COLUMN_LEFT: map[0,-1]
RASTER_IN_PUSHED_ONE_COLUMN_RIGHT: map[0,1]

I am not exactly sure about the implementation though.

r.mapcalc "RASTER_OUT=round(abs((RASTER_IN - RASTER_IN[0,-1]) + \
                                                       (RASTER_IN -
RASTER_IN[0,1]) + \
                                                       (RASTER_IN -
RASTER_IN[-1,0]) + \
                                                       (RASTER_IN -
RASTER_IN[1,0])))"

Note that this is the same like round(abs(4 * RASTER_IN -
(RASTER_IN[0,-1] + RASTER_IN[0,1] + RASTER_IN[-1,0] +
RASTER_IN[1,0])))

If you are interested in the sum of the absolute differences, you will
need to use abs() for each difference.

@Yasser, if you implement this and think it is something that can be justified
as generically useful, why not ask @MarkusMetz if it is easy to include in
"r.neighbors" as yet another method?

# prepare a weights file with the contents
0 1 0
1 0 1
0 1 0

r.neighbors in=raster_in out=raster_cardinalsum weight=weights method=sum

r.mapcalc "RASTER_OUT = round(abs(4 * RASTER_IN - raster_cardinalsum))"

HTH,

Markus M

Sorry, I just need to tell you I’m gonna work on this next week, but I really don’t want to discourage you waiting with no news.

Thank you all.

···

2013/6/30 Markus Metz <markus.metz.giswork@gmail.com>

On Sun, Jun 30, 2013 at 3:31 PM, Nikos Alexandris
<nik@nikosalexandris.net> wrote:

(cc-ing @MarkusMetz)

Yasser Said Lopez de Olmos Reyes wrote:

How could I obtain the differences between central pixel values and
cardinal directions pixel values related with a central pixel?
Or in a different manner, how to do this in grass gis?

RASTER_OUT= round(abs((RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_UP) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_DOWN) + (RASTER_IN -
RASTER_IN_PUSHED_ONE_COLUMN_LEFT) + (RASTER_IN -
RASTER_IN_PUSHED_ONE_COLUMN_RIGHT))

Nikos Alexandris wrote:

This boils down to calculating
(RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_UP) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_ROW_DOWN) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_COLUMN_LEFT) +
(RASTER_IN - RASTER_IN_PUSHED_ONE_COLUMN_RIGHT)

Please check the manual of r.mapcalc. There is

–%<—
THE NEIGHBORHOOD MODIFIER

—>%–

which I think can be used to obtain what is asked.
So, for each of the off-current pixel, the modifiers would be like

RASTER_IN_PUSHED_ONE_ROW_UP: map[-1,0]
RASTER_IN_PUSHED_ONE_ROW_DOWN: map[1,0]
RASTER_IN_PUSHED_ONE_COLUMN_LEFT: map[0,-1]
RASTER_IN_PUSHED_ONE_COLUMN_RIGHT: map[0,1]

I am not exactly sure about the implementation though.

r.mapcalc “RASTER_OUT=round(abs((RASTER_IN - RASTER_IN[0,-1]) +
(RASTER_IN -
RASTER_IN[0,1]) +
(RASTER_IN -
RASTER_IN[-1,0]) +
(RASTER_IN -
RASTER_IN[1,0])))”

Note that this is the same like round(abs(4 * RASTER_IN -
(RASTER_IN[0,-1] + RASTER_IN[0,1] + RASTER_IN[-1,0] +
RASTER_IN[1,0])))

If you are interested in the sum of the absolute differences, you will
need to use abs() for each difference.

@Yasser, if you implement this and think it is something that can be justified
as generically useful, why not ask @MarkusMetz if it is easy to include in
“r.neighbors” as yet another method?

prepare a weights file with the contents

0 1 0
1 0 1
0 1 0

r.neighbors in=raster_in out=raster_cardinalsum weight=weights method=sum

r.mapcalc “RASTER_OUT = round(abs(4 * RASTER_IN - raster_cardinalsum))”

HTH,

Markus M


Saludos,

Yasser

[..]

Markus Metz wrote:

# prepare a weights file with the contents
0 1 0
1 0 1
0 1 0

r.neighbors in=raster_in out=raster_cardinalsum weight=weights method=sum

r.mapcalc "RASTER_OUT = round(abs(4 * RASTER_IN - raster_cardinalsum))"

...easy, it is, to build a filter in GRASS :smiley:

Thanks Markus,

N

[..]

Nikos A:

>> Please check the manual of r.mapcalc. There is "THE NEIGHBORHOOD
>> MODIFIER" which I think can be used to obtain what is asked.
>> So, for each of the off-current pixel, the modifiers would be like
>> RASTER_IN_PUSHED_ONE_ROW_UP: map[-1,0]
>> RASTER_IN_PUSHED_ONE_ROW_DOWN: map[1,0]
>> RASTER_IN_PUSHED_ONE_COLUMN_LEFT: map[0,-1]
>> RASTER_IN_PUSHED_ONE_COLUMN_RIGHT: map[0,1]

Markus Metz wrote:

r.mapcalc \
"RASTER_OUT=round(abs((RASTER_IN - RASTER_IN[0,-1]) + \
(RASTER_IN - RASTER_IN[0,1]) + \
(RASTER_IN - RASTER_IN[-1,0]) + \
(RASTER_IN - RASTER_IN[1,0])))"

Note that this is the same like
round(abs(4 * RASTER_IN - \
(RASTER_IN[0,-1] + \
RASTER_IN[0,1] + \
RASTER_IN[-1,0] + \
RASTER_IN[1,0])))

[..]

Maybe we can add this example in the manual?

Nikos