I've used unionSpatialPolygons to dissolve polygons and get a grid as a spatialpolygon.

A simple example:
grelha<-readGDAL(paste("C:/...","teste.bmp",sep=""))
grd <- slot(grelha, "grid")
pol <- as.SpatialPolygons.GridTopology(grd)
pol <- unionSpatialPolygons(pol, SGDF2PCT(grelha)$idx) 
   
When the original image have more than one region or 'islands' of a single color (imagine a small logo), the unionSpatialPolygons() will dissolve polygons based on the ID's provided, in such a way that coordinates(pol) will return N lines, as much as different ID's or colors retrieved with SGDF2PCT(grelha)$idx.

It means that one cannot get centroids of each individual regions. Beside this, if the original image have this pattern, each number is a pixel of a single color: 

00100
02000
00020
10000

coordinates() will give only 3 centroids and only for three of the polygons (not even a common centroid for 1 and 2 ID's).

I'd like to split the spatialpolygon object obtained with unionSpatialPolygons(pol, SGDF2PCT(grelha)$idx) but retaining the ID's in order to get individual centroids for each region, in this simple case, two centroids for 1, two for 2 and two for 0.

Any idea will be welcome



	[[alternative HTML version deleted]]

