Showing results for 
Search instead for 
Do you mean 

Filter an Image n Times using the Iterator Operator 2016

by Technical Evangelist on ‎07-11-2016 12:39 PM - edited on ‎02-21-2020 06:43 AM by Community Manager (4,003 Views)

Download model



One of the Operators introduced with ERDAS IMAGINE 2016 was the Iterator operator. An Iterator is basically a sub-model, however the difference is that the sub-model is going to be executed n times before proceeding further into the model. The number of executions, n, is controlled by inputting to the Iterator a List. The number of entries in the List determines how many times the sub-model iterates and the values in the list are (generally) the main values used in each run of the iterations. Iterating in this fashion is useful in numerous instances and this Article is the first of several that will look at examples of using the Iterator.


This Model applies a filter to an image, a user-definable number of times, incrementally. This specific example approximates an Eliminate process to fill in holes (of class 0, produced by a Clump and Sieve process) with class values from surrounding pixels, by applying a Majority filter only at pixels of Class 0 and excluding Class 0 from the Majority calculation.


Left View shows the original landcover classification, middle shows the results of Sieving (background set to green) and the right shows the result after running the Model to fill  in the background "holes"


To do this the Model makes use of two Iterator operators. The first iteration builds a list of “inputs” consisting of the original input image plus n-1 temporary filenames. The second Iterator then applies the filter n times, starting with the first image in the list and writing the output to the next (temp) filename in the list, and then repeating with that output as the next input. After the nth application of the filter the Model writes out the image that's last in the list. The incremental nature of applying the Majority filter "fills in" the DN 0 holes.




Screenshot below shows the contents of the "Iterator" sub-model which builds the Lists of input and output filenames



Screenshot below shows the contents of the "Iterator 2" sub-model which applies the Majority filter



The Preview option enables the user to experiment with the Count value until all holes are filled. Once a good Count has been determined the model can be Run to an output file.


Because of the use of several newly introduced Operators (including Iterator) this Model requires ERDAS IMAGINE 2016 or later to run.



Since the model is attempting to apply filters to fill in the locations where the input thematic image has DN values of 0, the input image cannot have DN value 0 defined as Value Based NoData. Having Value Based NoData set to 0 would result in an output where those locations are set to NoData again. If your input data has Value Based NoData you can either unset that using ImageInfo prior to using this model. Or you could add a Replace NoData With operator into the model to change NoData to 0. It would need to go inside the Iterator 2 sub-model.


It's also worthwhile highlighting that the Iterator is not (currently) capable of being a While loop. The number if iterations can't be controlled dynamically by an event within the loop. So this cannot truly emulate the Eliminate utility, which keeps running the filter until a check for there being no "holes" left in the data is reached. The number of iterations is fixed by the length of the input List.


Input parameters:


Count: An integer value of 2 or greater which specifies the number of times to apply the Majority filter to the input data. Each run of the filter looks at any class 0 holes in the data and sets a neighboring class value to a one-pixel wide band. The next run fills in another one-pixel wide band. And so on until the hole is filled. Use Preview to determine the correct value to use and then Run to produce an output file.

Input Filename: Name of the input thematic image with background areas to be filled. This model is set to assume that Class (DN) 0 is the background value to be filled, but could be modified to apply at other values by modifying the ports on the Focal Majority operator.

Output Filename: Name of the output Thematic image file to create.





on ‎09-20-2016 05:13 AM

Help! I can't work out the logic in the create filename iterator. If count = 1, then we use the input file name and if > 1 then we use the temp file name list. However, the output file name looks like it creates nothing if count = 1. Is that right? 


Also, what's the purpose of the remove empty items operator?


Ta, Johnnie

by Technical Evangelist
‎09-20-2016 05:47 AM - edited ‎09-20-2016 05:49 AM

Correct. That's why the input requirement constraint is "An integer value of 2 or greater ". You can't Iterate just once. There's no point using a Model this complext if all you want to do is run the filter once.

on ‎09-20-2016 06:23 AM

True that! But surely iterator#2 needs an output file name for count = 1?

by Technical Evangelist
on ‎09-20-2016 09:38 AM


When count = 1, the output filename is NULL.

The Remove Empty Items operator takes the NULL out of the list and moves everything else up one.

So now output file #1 is input file #2 and so forth.

on ‎09-20-2016 11:16 AM

Ahhhhhh! Ta so. J