# How to group individual groups of pixels into a single class

I have a raster image with certain pixel groups of equal area. Each group represents a single house. I want all the pixels near a group of houses to have the same pixel value, i.e., I want to name that area "residential area". How can I do this? I want something like this done: this image should change to: • The operation used will depend on what you define as a "neighborhood" for your residential area. Have you looked at focal statistics? Jun 18 '14 at 17:48
• @Keviv is there a way you could post the above raster (the 1st one)? If the focal statistics doesn't work, I think there's another way to do this... Jun 18 '14 at 17:56

Computing the density of houses and thresholding that will give you flexibility to achieve a reproducible solution to your liking.

The flexibility comes about through varying two parameters: the extent ("radius" or "width") of the density kernel and the threshold value.

The density can be computed through convolution with a Gaussian kernel (a "Gaussian blur" in image-processing parlance) or even through repeated focal means (using circular neighborhoods). The thresholding is a simple comparison operation ("less than" or "greater than") to select regions of highest density. The numeric headings in the figure are the width of the Gaussian kernel in pixels. (The image itself is approximately 750 by 500 pixels.) The dark areas are the regions selected via a suitable threshold. In this case it appears that widths between 20 and 50 or so will give a result conforming to that sketched in the question. Larger widths can cause the regions to merge while smaller widths may keep them separated.

A nice feature of this approach is its interpretability: the regions can be characterized as those areas where the housing density exceeds so-and-so many houses per unit area (square kilometer or square mile, for instance). That makes it more than a mere cartographic tool and enables its use within quantitative analyses.

The figures were produced with Mathematica 9, but the process in a raster GIS such as Spatial Analyst or GRASS will be similar. It can even (easily) be done with most image processing software.

``````(* Recreate the original housing indicator grid *)
i = Import["http://i.stack.imgur.com/xWbby.png"]; (* Read the image *)
i2 = ImageTake[i, {80, 570}, {150, 900}];         (* Trim away the border *)
{r, g, b} = ColorSeparate[i2];                    (* Split into color channels *)
bb = Binarize[b];                                 (* Create a housing indicator grid *)

(* Create the five examples*)
TableForm[{Table[ImageCompose[i2,                 (* Overlay two images *)
{Binarize[GaussianFilter[bb, r[]], 1 - 2^(-r[])], 1/8}], (* Density and thresholding *)
{r, {{5, 6.5}, {10, 7}, {20, 7.5}, {40, 8}, {80, 8.5}}}]},
TableHeadings -> {{}, {5, 10, 20, 40, 80}},
TableAlignments -> Center]
``````

Using focal statistics will give you the desired results.

Lets say the house pixels have a value of 1 and others have a value of 0. Use the focal statistic with the MAXIMUM statistic type with a neighborhood of your liking. The resulting raster will be a residential area of your neighborhood.

• This is not a hard-and-fast solution. Focal statistics relay on a neighborhood and as such will expand the outer boundary in potential undesirable ways. Jun 18 '14 at 20:05
• Applying focal stats is not a bad idea, but using the maximum is too crude: it amounts to an expansion (see the answer by @Dan Patterson) without the subsequent shrinkage to control the boundary. Jun 18 '14 at 20:29

I would imagine that the most efficient way would be to 1) convert your "house" values to a point feature class, 2) create a grouping attribute using a minimum distance criteria with the near tool, 3) loop through each group to generate minimum convex polygons using the Minimum Bounding Geometry tool. You can then convert the combined minimum convex polygons back to a raster.

• The sketch in the suggestion indicates that something more flexible than a convex hull is needed. Alpha hulls will do the trick. The conversion is not a bad idea when a small number of houses (relative to the total number of cells) is involved. However, the two data format conversions, applying the (relatively inefficient) `Near` tool, and the need for a loop suggest this solution incorporates some computational and labor inefficiencies. Jun 18 '14 at 20:31
• This is not a bad idea, but will this method not be too precise, and eliminate the area around the group of houses? Jun 18 '14 at 20:34
• Agreed, however this is a limitation of the convex geometry algorithm employed by ESRI. Take a look at the Pateiro-López & Rodríguez-Casal (2009) Alpha Convex Hull method in this thread (gis.stackexchange.com/questions/1200/…). You can adjust the alpha parameter to better fit your data. Jun 18 '14 at 20:48

I recently had a similar problem where I needed to recognize clusters of items. I ended up using a hierarchical clustering algorithm provided by the clusterfck library. Demo here.

To apply this to your problem, you'd first traverse the image and make a list of points fitting your criteria.

For drawing the boundaries, you could use any of several techniques: you could compute a convex hull or perhaps use Voronoi cells.

I'm not sure if these algorithms are implemented in the language you're using, but it's not too difficult to do.