Home Up Next


The magic "where" function

Suppose you have an 512X512 image and you want to perform operations ONLY on the pixels, let's say, which have an intensity lower than 4 but non equal to 0. Let's have a look at the min and max of the image:

      IDL> print , min(image) , max(image)
              0   18

In a first step, let's create the table of the indices of the pixel on which we have to work.

      IDL> index = where(image lt 4 and image ne 0)
      IDL> help,index
            INDEX           LONG      = Array[39253]

The index variable is a vector containing all the subscripts of the pixels matching the condition. You access them in the image and modify them. For example, let's give to all those elements the value 1:

      IDL>  image[index] = 1

You can also just know how many pixels are over 14:

      IDL> print,n_elements(image)
      IDL> print, n_elements(where(image gt 14))

You can also check if any element meet the criterion before applying a command:

      IDL> toto = where(image gt 14,counts)
      IDL> print,counts
      IDL> if counts ge 1 then ...

You will see that the combination of where with the structure is a powerful way to extract sub_arrays verifying some criteria.


IDL courses C. Morisset 2004 IA/UNAM V 2.2

Home Up Next