Showing results for 
Search instead for 
Do you mean 

Collapse Clump/Sieve Original Value back to DN value

by Technical Evangelist on ‎12-16-2019 01:04 PM - edited on ‎02-21-2020 07:14 AM by Community Manager (252 Views)

Download models and sample data


sieved.img showing "holes" as green background and thousands of clumped DN values

I found myself in the embarrassing situation the other day that I had processed a land cover classification that I had produced through Clump and Sieve to remove small clumps of pixels from the image, but had lost the original image. This left me with a sieved image with thousands of clump DN values and a limited set of attribute fields including Original_Value (the class DN values from the original land cover classification), Class_Names and Color. I wanted to collapse the thousands of clump DN values back to the original landcover classification DN values - I had already sieved out the small clumps so I no longer needed the clump values. This is relatively easy to do by simply applying a Lookup of the clump DN values through the Original_Values field, to output a new "collapsed" raster with a limited number of DN values (0 to 16 in my example). But there was no easy way to re-associate all the other attribute information (such as the all-important Class_Names) since I had lost the original land cover classification file. So I could not simply copy/paste the original attribute fields across to my newly created collapsed image.

But all the information was present in the attribute fields of the Sieved image. I just needed a way to map that data to the collapsed image DN values. 

Fortunately Spatial Modeler now includes a wealth of Feature-based operators developed by the GeoMedia teams. Features, it should be remembered, are simply databases of attribute information where one or more of the attribute fields may, or may not, be geometry types. Similarly, thematic raster attribute fields are just tables of attribute information. So in theory I can use the Features operators to manipulate raster attributes and combine the two.

The following model (clump_2_class_v16_6_0.gmdx) shows a basic example of plucking the Class_Names attribute values out of the sieved image and mapping them into the column attributes of the collapsed image. A later example (clump_2_class_all_v16_6_0.gmdx) shows how to transfer more attributes, such as the class colors.


How does it do that?

The input image, in this example sieved.img, is read into the Spatial Model via the Raster Input operator. As can be seen from the screenshot above, sieved.img has over 15,680 DN values representing the unique clumps (actually a total of 33,016), and has attached attribute fields called Color (depicting the original Class color), Original_Value (a standard attribute created by the Clump operator depicting the original class values of the landcover classification fed to Clump and Sieve) and Class_Names (the original landcover class names).

The uppermost output port of the Raster Input sends the pixel data downstream in the model. The lower output port reads all the attribute information from the input image.

That attribute information is passed to a Dictionary Item operator which is used to single out just two specific fields. The first is the Original_Value field and the second is the Class_Names. It is important to isolate the Original_Value field since this provides the information that will be used to "relate" the other attribute fields to the DN values of the thematic image output by the model. 

The two fields are output from Dictionary Item as two streams of IMAGINE.Table data. The Original_Value table is first used in the Lookup operator to convert the raster stream from clumped DN values back to the original landcover DN values (effectively "collapsing" the large number of clumps back to a limited number of landcover classes).

The Original_Value table, along with the Class_Names table are fed to a Create Columns(s) operator which turns the Tables back into raster-style AttributeTable (but not associated with a raster). 

The AttributeTable is fed to a Features Input operator to convert the raster-style AttributeTable to Features, consisting of two attribute fields (a string and an integer) with 33,016 entries and no geometry attributes.

Next we want to consolidate those 33,016 features down into just the unique occurrences of Original_Value so that they match the DN values of the collapsed final output raster DNs. The first step of this is performed by the Merge Features operator. This operator merges features based on attribute matches and/or spatial relationships. In this instance there are no geometry fields, so only matching attributes are used. The MatchAttributesName port is set to Original_Value to merge all rows that have the same Original_Value value.

We want to summarise attributes into those new features, so there are two SummaryAttributen input ports. These use Functional Attributes to get the First value of Original_Value and the first occurrence of Class_Names per unique occurrence of Original_Value. 

The Merge Features operator thereby outputs a new set of Features with again two attribute fields, but only 17 features (because there were only 17 unique occurrences of Original_Value).

At this stage we have the information we need, but not organized in the required order. If you look at the attribute tables for sieved.img the first row has an Original_Value of 0 (Background), but the second clump is Original_Value 7 (Class 7), the third is Original_Value 8, etc. So the output from Merge Features is in that order: 0, 7, 8, 9, 11, etc. Whereas the DN values of a thematic raster (such as that coming out of Lookup) are always in ascending order: 0, 1, 2, 3, 4, etc.

So we need to re-order Original_Value attributes to be ascending and maintain the relationship to the corresponding Class_Names.

So the results of Merge Features is sent to two Select Attribute operators which convert the selected attribute field back to Tables. The Table containing the Original_Value is sorted in Ascending order by the Sort Items operator. The Index from the re-ordering of Original_Value is used by Arrange Items to re-order the Class_Names Table in the same order. Thereby the (unique) rows of Class_Names are in the same order as the DN values and can be converted to raster-style AttributeTable using the Create Column(s) operator and attached to the pixels from Lookup using the Attach Attributes operator. 

These pixels, with associated attributes can be sent to a Preview or to a Raster Output. The Raster Output of our example data would look similar to this


"Collapsed" Landcover classification class values with attributes

Note that the images look visually identical, but they aren't if you look at the attributes. We now only have 17 classes because we have collapsed the Clump/Sieve results back to the original class values and we have re-attached the Class_Names attribute.

Actually, the above screenshot isn't the result of that model. It is a result of this one which has used the same techniques but also transferred Color and Opacity attributes across:



This example was very simple, but provides an idea of how Features operators can be used to help manipulate and analyse raster data. 

The model can be easily modified to cater for different attribute names, but does rely on the presence of the Original_Value field produced by Clump.

Example data:

sieved.img – A thematic image which was derived by running a Clump and Sieve operation on a landcover classification dataset. Sieving removed clumps smaller than a specified area tolerance. The ORiginal Value field produced by Clump, as well as other input attributes (primarily Class_Names), were passed through to sieved.img.