[R-sig-Geo] extract by attributes from raster-image to polygons

Rafael Wüest rafael.wueest at wsl.ch
Fri Jun 1 09:39:45 CEST 2012


Hi Benny

I once had the same problem and couldn't find a solution, so I coded a workaround. Adjusted to your example, this will look like:

library(rgeos)
library(raster)
library(maptools)
im=brick("P1020393.JPG")
extract=(im[[1]]>50)&(im[[2]]>180)&(im[[3]]>220)
poly_dissolve=rasterToPolygons(extract,fun=function(x){x==1},dissolve=T)

len<-length(poly_dissolve at polygons[[1]]@Polygons)

pol<-poly_dissolve
pol at plotOrder<-1:len
pol at data<-data.frame(1:len)

for(i in 1:len){
   pol at polygons[[i]]<-Polygons(list(poly_dissolve at polygons[[1]]@Polygons[[i]]),i)
}

plot(pol,col=rainbow(len))

May not be the most elegant way, but it solved my problem.

HTH,
Rafi


On 25.05.2012, at 19:13, BennyB wrote:

> Dear Geo List, 
> 
> Before I ask my question let me apologize for my lack of knowledge about
> spatial information processing in R, being merely a field biologist.. 
> 
> What I am trying to achieve is the quantification of a bunch of marks
> (rodent scent marks) in a picture (jpeg - 
> http://r-sig-geo.2731867.n2.nabble.com/file/n7576826/P1020393.JPG
> P1020393.JPG ). 
> This means I need to know how many marks there are, and what each mark's
> area is. 
> 
> I manage to import the image and to extract only the marks using an "extract
> by attributes" of the three RGB band values of the image. 
> This gives me a raster layer with values 1 for the marks I want, and values
> 0 for everything else. 
> I then use the rasterToPolygons function, which can convert the raster file
> to a Spatial Polygon Dataframe. 
> 
> My problem now is the following: 
> The rasterToPolygons function gives me either one polygon for each pixel
> value in the raster image (dissolve=FALSE), or one multipolygon that
> contains all the marks (dissolve=TRUE). 
> I then do not manage to either aggregate the many polygons into one polygon
> for each mark, or to separate the multipolygon into many polygons that each
> represent a mark. 
> For the former I have tried using the unionSpatialPolygons function, but
> this again gives me only one big multipolygon instead of one polygon for
> each mark. 
> 
> I have been looking like crazy on the internet and in manuals, but being
> having a big lack of knowledge of the precise terminology of GIS processes,
> this has been proving fruitless. 
> 
> Probably it is something very trivial I am missing, so if anybody can point
> me in the right direction I would be extremely grateful! 
> 
> Many thanks, 
> 
> Benny 
> 
> Below you can see the script I have been using, and attached you can find
> the original jpg image of the marks. 
> ---------------- 
> 
> library(raster);library(maptools) 
> im=brick("P1020393.JPG") 
> extract=(im[[1]]>50)&(im[[2]]>180)&(im[[3]]>220) 
> 
> # using the dissolve=T option 
> poly_dissolve=rasterToPolygons(extract,fun=function(x){x==1},dissolve=T) 
> # this gives me one big multipolygon and not one polygon for each mark 
> 
> # using the dissolve=F option 
> poly_no_dissolve=rasterToPolygons(extract,fun=function(x){x==1},dissolve=F) 
> # this gives me one polygon for each pixel having value=1 
> # I then try to create aggregate those polygons, which again gives me only
> one multipolygon 
> uni_poly=unionSpatialPolygons(poly_no_dissolve,cut(pol_no_dissolve[,1],
> quantile(pol_no_dissolve[,1]), include.lowest=TRUE))) 
> 
> -----------------
> http://r-sig-geo.2731867.n2.nabble.com/file/n7576826/P1020393.JPG
> P1020393.JPG 
> 
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/extract-by-attributes-from-raster-image-to-polygons-tp7576826.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



--
Rafael Wüest
Swiss Federal Research Institute WSL
Zürcherstrasse 111
8903 Birmensdorf
Switzerland

+41 44 7392126
rafael.wueest at wsl.ch
http://www.wsl.ch/info/mitarbeitende/wueest



More information about the R-sig-Geo mailing list