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

BennyB benny.borremans at ua.ac.be
Fri May 25 19:13:53 CEST 2012


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.



More information about the R-sig-Geo mailing list