Andrian,

The problem I have is that the window is shrinking when doing dilation ! If
it had to move, I think it should be expanding at least.

Example :

library(spatstat)
data(letterR)
rwin <- as.owin(as.im(letterR))
rero<-erode.owin(rwin,0.3,FALSE)
redil<-dilate.owin(rero,0.3)
par(mfrow=c(1,3))
plot(rwin);plot(rero);plot(redil)
rwin$xrange;rero$xrange;redil$xrange
rwin$yrange;rero$yrange;redil$yrange
# notice that redil (the product of dilatation)
# is smaller than the other windows

So I have a suggestion, maybe I'm wrong, but tried to modify erode.owin.

# I had to create a function so I could compare two windows (I haven't
# found one).
compare.owin<-function(s,b){
    all(c(
        s$xrange[1]>b$xrange[1],
        s$xrange[2]<b$xrange[2],
        s$yrange[1]>b$yrange[1],
        s$yrange[2]<b$yrange[2]
        ))
}

# Here is my proposal. The only thing is that it is not working when the
# dilation exceeds the window, and that you specify
# exand.frame= FALSE.
# Maybe you could help on this, I suspect (obviously) intersect.owin(),
# but, I don't know why it's not working.

my_dilate.owin <- function (w, r, expand.frame = TRUE, ...)
{
    verifyclass(w, "owin")
    if (!is.numeric(r) || length(r) != 1)
        stop("r must be a single number")
    if (r < 0)
        stop("r must be nonnegative")
    if (r == 0)
        return(w)
    if (w$type == "mask" && !any(w$m))
        return(w)
    epsilon <- sqrt(w$xstep^2 + w$ystep^2)
    r <- max(r, epsilon)
    rect <- as.rectangle(w)
    bb <- bounding.box(w)
    newbox <- owin(bb$xrange + c(-r, r), bb$yrange + c(-r, r))

    #check if newbox is bigger than window extent
    toobig <- compare.owin(s=rect,b=newbox)
    # if too big, you need to expand the window
    if (toobig) {
        w <- rebound.owin(w, newbox)
    }
    distant <- distmap(w, ...)
    dil <- levelset(distant, r, "<=")
    if (!expand.frame){
        dil<-intersect.owin(dil, as.mask(rect))
    }
    return(dil)
}

# to view all this :
my_redil <- my_dilate.owin(rero, 0.3, FALSE)
my_redil_1_true <- my_dilate.owin(rero, 1, TRUE)
my_redil_1_false <- my_dilate.owin(rero, 1, FALSE)

par(mfrow=c(3,3))
plot(rwin);plot(rero);plot(redil)
plot(my_redil);plot(my_redil_1_true);plot(my_redil_1_false)

# check range :
rwin$xrange;my_redil$xrange;my_redil_1_true$xrange;my_redil_1_false$xrange
rwin$yrange;my_redil$yrange;my_redil_1_true$yrange;my_redil_1_false$yrange

Thank you,
Etienne

On Fri, Jun 5, 2009 at 6:19 AM, <Adrian.Baddeley@csiro.au> wrote:

>  Etienne Bellemare etiennebr@gmail.com
> writes:
>
> > When using erode.owin from spatstat package, you can specify to keep the
>
> > window extent (shrink.frame=FALSE). But, with dilate.owin, you cannot
> use
>
> > shrink.frame. What can I use to keep the same extent ?
>
> This question refers to the 'frame' or 'bounding rectangle' of a window
> (object of class "owin" in spatstat). You can find the bounding rectangle of
> a window using as.rectangle() or by extracting the $xrange and $yrange
> entries of the window.
>
> In principle you can't keep the same bounding rectangle when you dilate a
> window, because the dilation could be larger than this rectangle.
>
> However, you can manipulate the bounding rectangle of a window using
>
>             w <- rebound.owin(w, rect)
>
> where 'rect' is a rectangular window e.g. rect=owin(xrange, yrange).
>
> This will generate an error if 'rect' does not contain 'w'.
>
>
>
> Adrian Baddeley
>

	[[alternative HTML version deleted]]

