[R-sig-Geo] Fwd: clump of binary pixels on raster package

van Etten, Jacob (IRRI) J.vanEtten at cgiar.org
Tue May 12 07:17:46 CEST 2009


Now I realize that the function I sent earlier drops single-cell
patches, which might be a problem.

This version of the function takes them into account: 

clumps <- function(MM)
{
	val <- which(values(MM)==1)
	adj <- adjacency(MM,val,val,directions=8)
	adjv <- as.vector(t(adj))
	g <- graph(adjv, directed=FALSE)
	cl <- clusters(g)
	memb <- cl$membership[val+1]
	MC <- raster(MM)
	MC[val] <- memb
	return(MC)
}

Roughly same speed.

-----Original Message-----
From: r-sig-geo-bounces at stat.math.ethz.ch
[mailto:r-sig-geo-bounces at stat.math.ethz.ch] On Behalf Of van Etten,
Jacob (IRRI)
Sent: Tuesday, May 12, 2009 11:46 AM
To: milton ruser
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] Fwd: clump of binary pixels on raster package

Clumps (patches) from binary pixels

 

This is a solution using raster and igraph. 

 

(It seems that a raster only solution is underway.) 

 

=========================

 

install.packages("raster",repos="r-forge.r-project.org")

 

require(raster)

require(igraph)

 

#New function to make clumps, MM is a Raster object

 

clumps <- function(MM)

{

            adj <-
adjacency(MM,which(values(MM)==1),which(values(MM)==1),directions=8)

            adjv <- as.vector(t(adj))

            g <- graph(adjv, directed=FALSE)

            cl <- clusters(g)

            memb <- cbind(0:(length(cl$membership)-1),cl$membership)

            memb <- memb[memb[,2] %in% (which(cl$csize>1)-1),]

            MC <- raster(MM)

            MC[memb[,1]] <- memb[,2]

            return(MC)

}

 

#Your example

 

MyMatrix<-matrix(rep(0,100), ncol=10)

MyMatrix[2:4,3:6]<-1

MyMatrix[7:8,1:3]<-1

MyMatrix[8,7:8]<-1

MyMatrix[8,7:8]<-1

MyMatrix[6:7,8:9]<-1

 

# Make a raster from the matrix

MM <- raster(MyMatrix)

 

# Make a raster with clumps

MC <- clumps(MM)

 

#Make a matrix from it again

MyClusters <- matrix(values(MC),nrow=10,ncol=10)

image(MyClusters)

 

#Time evaluation with 100 500x500 rasters with random values (0,1)

eval <- function()

{

for(i in 1:100)

{

r1 <- raster(proj="",nrows=500,ncols=500)

r1[] <- sample(c(0,1), ncell(r1), replace=T)

clumps(r1)

}

}

 

system.time(eval())

 

====================================

 

My computer did this:

 

   user  system elapsed 

117.42        9.89  127.66 

 

This does not seem to run into problems. I have not compared with the
alternatives, but would be interested to know.

 

Jacob van Etten

________________________________

From: milton ruser [mailto:milton.ruser at gmail.com] 
Sent: Tuesday, May 12, 2009 2:04 AM
To: van Etten, Jacob (IRRI)
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] Fwd: clump of binary pixels on raster package



Dear Dylan and Jacob,



In fact I not wrote to GRASS list, because it is working grass, and as
my images are about 512x512, grass can deal with the r.clump without
problem. But it takes about 1min for each image (i have other steps not
mentioned that sum this 1min), and as I have about 30,000 images, it is
so many time.



Labcon/adehabitat is fast then r.clump/grass, but some times when images
have so many clumps, labcon() not finish the task, and not go to next
image (it appears like a spiral processing, without end). So labcon, for
this job, is not a good solution for me.



I was thinking about raster package, but the discussed issued on the
list not give - apparently - a solution like that one I need (see the
sample code).



By the way, I am running Vista on a dell 6GB ram machine.



Any other suggestion are welcome.



milton

brazil=toronto

On Sun, May 10, 2009 at 7:56 PM, van Etten, Jacob (IRRI)
<J.vanEtten at cgiar.org> wrote:

This seems to be a connected components problem, discussed recently on
this list.

Jacob.


-----Original Message-----
From: r-sig-geo-bounces at stat.math.ethz.ch
[mailto:r-sig-geo-bounces at stat.math.ethz.ch] On Behalf Of milton ruser
Sent: Monday, May 11, 2009 5:30 AM
To: r-sig-geo at stat.math.ethz.ch
Subject: [R-sig-Geo] Fwd: clump of binary pixels on raster package

Dear all,

sorry for this double-posting, I know it is not a good idea,
but may be this list is more appropriated for this issue.

Thanks a lot

milton

---------- Forwarded message ----------
From: milton ruser <milton.ruser at gmail.com>
Date: Sat, May 9, 2009 at 5:59 PM
Subject: clump of binary pixels on raster
To: "r-help at r-project.org" <r-help at r-project.org>


Dear all,

I have a set od 30,000 binary landscapes, which represent habitat and
non-habitat cover.
I need to generate images that identify those neighbour (rule 8) pixels
as
one patch ID,
and a different patch ID for each clump of pixels. I coded it using
labcon(adehabitat),
but as some of my landscapes have so many patches, labcon not finish and
entry in
a eternal looping. By other side, I coded another solution using R &
grass
(r.clump),
but the solution is so slow, and as I need to run it a lot of time, I
will
need about 3 weeks
to finish... I was thinking if raster package could do the job fastly
than
R-grass.
Below you can find a simulation of what I need. On the second image,
each
color
have different values.

MyMatrix<-matrix(rep(0,100), ncol=10)
MyMatrix[2:4,3:6]<-1
MyMatrix[7:8,1:3]<-1
MyMatrix[8,7:8]<-1
MyMatrix[8,7:8]<-1
MyMatrix[6:7,8:9]<-1
x11(800,400)
par(mfrow=c(1,2))
image(MyMatrix)

MyClusters<-matrix(rep(0,100), ncol=10)
MyClusters[2:4,3:6]<-1
MyClusters[7:8,1:3]<-2
MyClusters[8,7:8]<-3
MyClusters[8,7:8]<-4
MyClusters[6:7,8:9]<-4
image(MyClusters, col=c("transparent", 1,3,4,5))

Regards a lot,

milton
brazil=toronto.

       [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo




	[[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list