Locating Pixel Values in an Image
Locating pixel values within an image helps to segment features. You can use IDL's WHERE function to determine where features characterized by specific values appear within the image. The WHERE function returns a vector of one-dimensional indices, locating where the specified values occur within the image. The values are specified with an expression input argument to the WHERE function. The expression is defined with the relational operators, similar to how masking is performed. See Masking Images for more information on relational operators.
Since the WHERE function only returns the one-dimensional indices, you must derive the column and row locations with the following statements.
where index is the result from the WHERE function and imageSize[0] is the width of the image.
The WHERE function returns one-dimensional indices to allow you to easily use these results as subscripts within the original image array or another array. This ability allows you to combine values from one image with another image. The following example combines specific values from the image within the worldelv.dat file with the image within the worldtmp.png file. The worldelv.dat file is in the examples/data directory and the worldtmp.png file is in the examples/demo/demodata directory. First, the temperature data is shown in the oceans and the elevation data is shown on the land. Then, the elevation data is shown in the oceans and the temperature data is shown on the land. Complete the following steps for a detailed description of the process.
Example Code
See combiningimages.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering combiningimages at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT combiningimages.pro.
- Determine the path to the file:
- Initialize the image size parameter:
- Import the elevation image from the file:
- Initialize the display:
- Create a window and display the elevation image:
- Determine the path to the other file:
- Import the temperature image:
- Display the temperature image:
- Determine where the oceans are located within the elevation image:
- Set the temperature image as the background:
- Replace values from the temperature image with the values from the elevation image only where the ocean pixels are located:
- Create another window and display the resulting temperature over land image:
- Determine where the land is located within the elevation image:
- Set the temperature image as the background:
- Replace values from the temperature image with the values from the elevation image only where the land pixels are located:
- Display the resulting temperature over oceans image:
WINDOW, 0, XSIZE = 2*imageSize[0], YSIZE = imageSize[1], $ TITLE = 'World Elevation (left) and Temperature (right)' TV, elvImage, 0
WINDOW, 1, XSIZE = 2*imageSize[0], YSIZE = imageSize[1], $ TITLE = 'Temperature Over Land (left) ' + 'and Over Oceans (right)' TV, image, 0
TV, image, 1The following figure shows two possible image combinations using the world elevation and temperature images.
Tip
You could also construct the same image using masks and adding them together. For example, to create the second image (temperature over oceans), you could have done the following:mask = elvImage GE 125
image = (tmpImage*(1 - mask)) + (elvImage*mask)
For large images, using masks may be faster than using the WHERE routine.

