hallo,
thank you very much for your hints! No I try to rewrite it some better way.
On Thu, Apr 21, 2005 at 07:06:39PM +0100, Glynn Clements wrote:
Jachym Cepicky wrote:
> I have tryed to program the Gaussian filter for raster maps. It
> works only with CELL maps for now, but I hope to extend it for DCELL
> and FCELL maps too.
The simplest solution is to use DCELL throughout. All CELL and FCELL
values can be represented as DCELL without loss of range or precision.
OH! I didn't know this! I have to read it in the Programers manual - I
thought, DCELL is just 'some' another type of map
> Because this is my first raster module for GRASS and also my first bigger
> C-program, I would be very thankfull, if someone could have a look at the
> code at to tell me, what I could do better/safer.
Use a scrolling window; don't read each row three times.
At the moment, you're reading rows 0/1/2, then 1/2/3, then 2/3/4 and
so on. Note that you're reading each row (except the first and last
two) three times. Also note that you already have two out of the three
rows in memory from the previous pass (again, except for the first and
last two passes).
See r.neighbors or r.grow2.
I looked there, but I did not see it. Thanks again...
Actually, is there any reason why this filter can't be added to
r.neighbors as another method?
Well, I wanted somehow to start to write my own modules. Gaussian is not much complicated
filter, but mean of least variance, bilateral filter and others will be a bit
complicated (well - I thing so). So I wanted to start somehow, so I know, how to
write a module from the beginning. I learned very much on this.
> I would like to write some other filters for raster maps too, the next stage
> should be Mean of least variance filter. But before I start, I would like to be
> shure, that I don't do something really stupid in the program.
>
> I could not to solve, how to work on the boundaries of the raster. I had to
> skip these cells, like e.g. r.mapcalc does -- so NULL values appear there.
> How to do it some better way?
In general, you can't. Convolution-style filters always result in the
map being "shrunk".
But one could count the value of the central cell only from the values, which
are laying in the raster
+-----------+
| 1| 2| 3| 4| So for cell number 1, one would take the cells 1,2,5 and 6
+-----------+ in the count. For number 2, One would take 1,2,3,5,6,7
| 5| 6| 7| 8| first for cell number 6, one would take all 9 values around.
+-----------+
| 9|10|11|12| But it would be too complicated tough :-/
+-----------+
> I'm also not shure, if I do the allocation of neigborhood rows the right way.
>
> I hope, I did not forget to free all the buffers at the end.
It doesn't matter about freeing buffers; all memory will be reclaimed
by the OS when the program terminates.
Oh, I did! I did it by realy BAD way: For each CELL I allocated the 3 rows
again!! And than I forget to free it! But it is all right now.
--
Glynn Clements <glynn@gclements.plus.com>
OK, I will work with DCELL type, than I implement the row swiching from r.grow2
tmp = in_rows[0];
for (i = 0; i < size * 2; i++)
in_rows[i] = in_rows[i + 1];
in_rows[size * 2] = tmp;
This is a good trick and I post the result again, so you and others could have
a look at it.
Now it works nearly 2x faster, than r.mapcalc filter (from GRASS add-ons). With
these modifications, it will be even better!
Thank you again
Jachym
--
Jachym Cepicky
e-mail: jachym.cepicky@centrum.cz
URL: http://les-ejk.cz
GPG: http://www.fle.czu.cz/~jachym/gnupg_public_key/