Skip to content
Back to formatted view

Raw Message

Message-ID: <4851008B.8020803@uni-muenster.de>
Date: 2008-06-12T10:55:07Z
From: Edzer Pebesma
Subject: clustering multi band images
In-Reply-To: <Pine.LNX.4.64.0806120902271.25820@reclus.nhh.no>

Roger Bivand wrote:
> On Thu, 12 Jun 2008, Laura Poggio wrote:
>
>> Dear list,
>> I am trying to do some clustering on images. And I have two main 
>> problems:
>>
>> 1) Clustering multiband images.
>> I managed to be successful with a single band image, but when trying to
>> apply to a 3 band I get the following warning message:
>> In as.matrix.SpatialGridDataFrame(x) :
>>  as.matrix.SpatialPixelsDataFrame uses first column;
>> pass subset or [] for other columns
>>
>>
>> 2) saving clustering results as grid or image.
>> I get a vector of clusters, but without both coordinates. How it is 
>> possible
>> to transform it in a grid?
>>
>> Here the code I use to read the image itself and to do the clustering:
>>
>> library(rgdal)
>> fld <- system.file("E:/data/IMG/fr/", package="rgdal")
>> img <- readGDAL("123_rawR.tif")
>>
>> kl <- kmeans(img, 5)
>
> img is a SpatialGridDataFrame. kmeans() wants a matrix or data frame, 
> so say:
>
> kl <- kmeans(as(img, "data.frame"), 5)
this also passes the coordinates to the clustering routine; I'm not sure 
but I think that was not the initial idea.
In case img is a 3-band image, use

kl <- kmeans(as(img, "data.frame")[1:3], 5)
--
Edzer