Pages

Showing posts with label ENVI. Show all posts
Showing posts with label ENVI. Show all posts

Wednesday, 8 October 2014

eCognition tutorial: Exporting eCognition features as images with array functionalities

There are instances when one would like to export different features from eCognition as a images to perform some tasks outside eCognition. eCognition doesn't provide a way to export many object features as images but it is only possible to export as a thematic raster in which each object has a unique ID, and features values are stored in a separated CSV file. To convert it into images, one has to mapped ID raster tiff file and data from CSV file.

If you want to get eCogntion features as images in an automatic way that can export any numbers of features as images in one go, then here is a way. For the purpose, we are going to utilize array handing capabilities of eCogntion and export each feature as separate tiff (My_1.tiff, My_2.tiff …. and so on) file. The rule set can be downloaded here. The rule set is flexible in the sense that you just have to update an array to store your feature list of interest. Number features can be any number (10, 20 or even 100 features). We will merge it afterwards in open source QGIS.

  • Perform a segmentation
  • Create a array and store features you want to export as images
  • Loop over the array
    1. For each feature, create a temporary image file
    2. Export the temporary file as a unique name
    3. Repeat until all features in the array are executed
  • Now in QGIS, we use GDAL to stack individual images into one single image. We will use merge function of GDAL (Raster>Miscellaneous> Merge).
    Complete ruleset within eCognition
Ruleset  for exporting features as images 
GDALmerge function within QGIS
Color composite of three merge features
Thus produced merge features image now can be used for classification  in ENVI, ERDAS IMAGINE or writing custom script in Python or MATAB. Personally I use such images within Python using scikit-learn library.

Friday, 15 August 2014

eCognition tutorial: Exporting eCognition classification file to ENVI

The problem:

I was wondering if anyone could help me export a usable file from eCognition Developer for use in ENVI 5.0? I've classified an image using Multiresolution Segmentation followed by the Classification algorithm using selected samples from the Standard Nearest Neighbour and everything worked so far. I try and export it as an ENVI-supported file type (e.g. *.tif) the raster seems to have "lost" all the classification- leaving a grayscale image- useless! I have been able to open a *.jpg file, but as it is an image it has lost any previous classfication from Ecognition.

Solution:

This is not even a problem but for people who are just starting with ENVI is a big HEADACHE. It’s just a matter of a symbology. If you have been using the ENVI for a while you should know that ENVI uses two files for any image. One binary file and another hdr file where different information about the binary image are stored. One can simply open hdr files with a notepad. Normal raster images have “file type = ENVI Standard” whereas classification images have “file type = ENVI Classification” and some other information such number of classes, class names and class colours. So the classified tiff file that one exports from eCognition does not have those information, hence ENVI opens the classified tiff file as a normal file. There are values differentiating classes but colour information are lost. This is a problem if you want to do some other operation in ENVI that required a classification image as input.

So we need to convert it ENVI Classification type, somehow. So here is a way to do it.

  1. Do your classification in eCognition.
  2. Export it using “export thematic raster files” algorithm in eCognition with Export Type Don’t forget to select your classes in Class Filter parameters
  3. Once you export it, you will have two files. One *.tiff file and other *.csv fil. Open *.csv file, there you will find class names and RGB colour for each class.
  4. Open *.tif file in ENVI.
  5. Then File> File Save as > ENVI Standard>Import file > Pick recently open tif file. Give a name and save it. Now you changed tiff file to ENVI file but remember, its still a normal ENVI standard file.
  6. Open the file created in step 5. Then File> Edit Header Info > Select File type as ENVI classification. It will prompt you for number of classes, class names, colour information. Provide those information by using *.CSV file that you opened in step 3.
  7.   Now you have a ENVI classification image with same class names and colour as in eCognition. Now smile and go for a coffee.

Example of a CSV file
Converting tiff file to ENVI file
Modifying header files
Editing class names and color information

Initial and Final image

Saturday, 31 December 2011

Opening multispectral or hyperspectral ENVI files in MATLAB


ENVI/IDL is one of the most used remote sensing software package which has a nice programming interface known as IDL. Though I have been using IDL for quite some time now, I still prefer MATLAB over IDL because MATLAB has extensive help documentation with lots of useful demonstration which I think is lacking in IDL.

There is a function in MATLAB which not many people are aware of, which is called Multibandread. Multibandread can be use for any type of binary files such as bsq, bil and bip. As long as you know how the data is organized in the binary file, its data type, rows columns and no. of bands in the file you can read the file in MATLAB with Multibandread.  Multibandread requires following informations.

X = multibandread(filename, size, precision, offset, interleave, byteorder)

With ENVI files, you can find all these information in the header file of that ENVI file. Using that information, you can pass this information by manually typing or you can write a custom function which will open the header file, fetch those information from the header file and pass it to Multibandread. I usally do it with latter approach.
Opening whole remote sensing images in MATLAB is a bit tricky. Consider you have a hyperspectral data with 116 bands and you try to read whole image at one go. MATLAB will run out of memory. The wise thing is to read the image in a block by block (MxN), performing required operation in the block and later combining all blocks. 

One cautionary note, I have seen people doing some silly mistakes such as reading an ENVI file with 10 bands in a variable (image) and using imshow (image) to visualize it. They spend countless hours wondering why the Multibandread is not being able read their ENVI files. But they forget that imshow function can display only three band one time. So if you want to view first three band then you have to use

  Imshow( image(:,:,1:3),[])

So, try Multibandread function if you are MATLAB savvy person and prefer MATLAB over IDL.
If you run into any kind of problem, I am ready for help.