[R] floodfill with matrix data

Thomas Petzoldt thpe at hhbio.wasser.tu-dresden.de
Mon Aug 25 15:14:56 CEST 2003


Dear R users,

I'm trying to do some sort of "floodfill" or "seedfill" with data stored 
within a matrix in R (usually floating numbers), where a marker value is 
given to specify the limits of an area to be filled. A reduced example 
may demonstrate this below. Although I wrote a simple C function for 
this, it would be very helpful to find a more professional solution.

Any suggestions or code snippets are greatly appreciated.

Thomas Petzoldt


--------------------------------------------------------------------------

The problem:

## (1) Given the following matrix

z <- matrix(c(0,0,0,0,0,0,
               0,1,1,1,1,0,
               0,1,0,0,1,0,
               0,1,1,1,1,0,
               0,1,0,0,1,0,
               0,1,0,1,1,0,
               0,0,0,0,0,0), nrow=6, ncol=7)
image(z)

## (2) _a_procedure_is_wanted to
# "floodfill" all zeroes with 2 "outside 1"
# to give

## (3) the following result:

z <- matrix(c(2,2,2,2,2,2,
               2,1,1,1,1,2,
               2,1,0,0,1,2,
               2,1,1,1,1,2,
               2,1,2,2,1,2,
               2,1,2,1,1,2,
               2,2,2,2,2,2), nrow=6, ncol=7)

image(z)




More information about the R-help mailing list