Prepare Data for Classification
This topic pertains to image classification. Before running an unsupervised or supervised classification, you should consider including a variety of attributes from the source image rather than spectral information alone. This is optional, but it often yields more accurate classification results. Attributes are unique characteristics that can help distinguish between different classes in an image. Examples of attributes include elevation, texture, and shape.
You can create a multi-band attribute image, where each band represents a different attribute. This is called a layer stack in ENVI. Each layer (band) must have the same number of rows and columns, the same pixel size, and the same spatial reference. Here is an example of a layer stack with six attributes:
See Classification Tutorial 1: Create an Attribute Image for instructions on creating an attribute image.
Tip: If you have multiple bands of multispectral or hyperspectral data, be sure to apply radiometric or atmospheric correction to all of the spectral data before creating a layer stack.
Classification will occur on the attribute image. You must guide the classification by identifying areas on the image that are known to belong to certain categories. This requires training data.
Collect Training Data
You probably have some idea of how to categorize your data, whether it pertains to different types of land use, different surface features, or geologic units. These categories form the basis of informational classes that you want to derive from imagery. With supervised classification, you can collect samples of pixels that you know belong to each class. This training data is used to classify pixels of unknown identity into known classes. In ENVI you can draw regions of interest (ROIs) on pixels that, to the best of your knowledge, represent known features.
Suppose that you want to classify an urban scene into five different classes that represent unique surface features:
- Asphalt
- Concrete
- Grass
- Tree
- Building
You can collect training data by drawing ROIs for each known feature type on a high-resolution orthorectified photograph that is co-registered with the image you will classify. Or you could conduct a field study to collect ground-truth samples, then convert the data into ROIs.
See Classification Tutorial 2: Collect Training Data for instructions on creating ROIs with training data.
When the ROIs overlay the attribute image, ENVI determines the pixel values within each ROI boundary for each attribute. Using the Building class, for instance, the following diagram shows the pixel values for six attributes in one ROI polygon.
A one-dimensional array (or vector) of pixel values is called an example:
| |
EVI
|
Height
|
Blue reflectance
|
Green reflectance
|
Red reflectance
|
NIR reflectance
|
|
Example
|
-0.02
|
4.64
|
0.21
|
0.24
|
0.25
|
0.23
|
Or:
Example = [-0.02, 4.64, 0.21, 0.24, 0.25, 0.23]
The ENVIExtractExamplesFromRaster procedure accepts an image and training data ROIs. It extracts examples from all of the attribute image pixels that fall within the training data boundaries. It determines the classes that you defined in the ROI file, and it assigns an integer class value to each class that ranges from 1 to n number of classes. In this scenario, the class values are as follows:
|
Class name
|
Class value
|
|
Asphalt
|
1
|
|
Concrete
|
2
|
|
Grass
|
3
|
|
Tree
|
4
|
|
Building
|
5
|
Thus, each example has been assigned to a particular class based on the training data ROIs. The example shown above has a class value of 5 because it belongs to the Building class. ENVIExtractExamplesFromRaster builds an array of examples and a corresponding array of class values. The next section provides some code examples to illustrate this concept.
Extract Examples and Class Values
The following code extracts examples and class values from an attribute image and training data ROIs. The data files are available from our website or on the ENVI Resource DVD in the classification directory. Follow these steps:
- Copy the data files to your local drive.
- Start IDL.
- Copy and paste the code below into the IDL Editor.
- Update the file paths with the location where you saved the sample data.
- Select all of the code, then right-click and select Run Selected Text.
e = ENVI()
file = 'AttributeImage.dat'
raster = e.OpenRaster(file)
ROIfile = 'TrainingDataROIs.xml'
rois = e.OpenROI(ROIfile)
outExamples = ENVIExtractExamplesFromRaster(raster, rois)
The result (outExamples) is an ENVIExamples object. You can find out the number of examples, attributes, and class values using various methods and properties.
Type the following code at the IDL command line to print the number of examples:
numExamples = outExamples.NEXAMPLES
Print, numExamples
Result: 34040
Print the number of class values:
classValues = outExamples.NCLASS_VALUES
Print, classValues
Result: 34040
This confirms that the number of class values equals the number of examples. Now print the number of classes:
numClasses = outExamples.NCLASSES
Print, numClasses
Result: 5
The training data in this example has five classes. Print their names:
classNames = outExamples.CLASS_NAMES
Print, classNames, /IMPLIED_PRINT
Result:
asphalt
concrete
grass
tree
building
Print the number of attributes in the attribute image:
numAttributes = outExamples.NATTRIBUTES
Print, numAttributes
Result: 6
The attribute image has six bands, each representing a different attribute. Print their names:
attributeNames = outExamples.ATTRIBUTE_NAMES
Print, attributeNames, /IMPLIED_PRINT
Result:
Enhanced Vegetation Index
Height
Blue reflectance
Green reflectance
Red reflectance
NIR reflectance
Next Steps
The next step is to define and train the classifier. See Define and Train the Classifier.