Tuesday, 18 November 2014

eCognition Tutorial: Find the closest classified object


Solution:

There are different ways to tackle this problem. First one should be aware that there are two different methods of computing image objects distance. Center of Gravity and Smallest Enclosing Rectangle. Scour the help doc to find what those two methods are and choose the method you want. You can set up this in the beginning of your rule set using rule set options algorithm. Obviously you need to first create a feature ' Distance to'  with the class you are interested in.  Afterwards, we going to use "find domain extreme" algorithm for finding the closest object. 

For this problem, i am going to use the same image that i used in the last blog. We are going use different concepts such as multi-threshold segmentation, variables,  and PPO. For every blob, we will find a nearest blob in the image.Subsequently, we will export results as image so that you can see what is happening.

find domain extrema
Steps:

  • multi-threshold segmentation to get  all blobs and classify as class1
  • create variable to count loop ( it will be later use to export images with distinct names)
  • Use PPO to loop through class1
    •     assign current object as curr_class
    •     update variable 
    •     find domain extreme to find nearest blob that belong to class1
    •     export image


Project to find the nearest object

Color: Red- Class 1
Color: Green:cur_class
Color :Magenta: near ( blob closest to cur-class)

gif demonstrating nearest object


Wednesday, 12 November 2014

Tutorial eCognition : Finding area of classified objects within eCognition

Problem:

I have done some classification with many classes. Now i want to know the area of individual class. Please tell a way to find it and store in a text file. 

Solution

For this kind of problem, many people would resort to finding a way in ArcGIS with exported shapes files with classification. In ArcGIS, one has to do series of operations to get the desired output (add area column and populate with area for every features, sum up area for each class). These operation are not complex but if you are not good with ArcGIS, then doing all that is a tough nut to crack.

Good thing is that there is feature in eCognition ‘area of classified objects’ that can be used for this purpose. But for that you have to create that feature for every class that you have in your project. Imagine you have more than 10 classes. Creating that features for ten different classes is boring, isn’t it? At least for me, it is cumbersome. On top of that imagine you just finished with the project you are working with and you have to perform same thing again in another project with 15 classes with different names. Gosh, you have to create another 15 features again. Not fun, right? So what’s the solution? You guess it right, we can use array functionality within eCognition to loop over all classes and create that feature for every class in one go. The rule set,we will create, will work in every project regardless of numbers of classes you have in your project. Sounds interesting? Keep on reading.

eCognition introduce concept of variables and array from version 8 ( ? not sure). With array functionality, you can gather all classes present in  you project in one go and make a particular feature with each class. I have seen many eCognition projects developed by other people for last three years and not many people use these new features of eCognition. It very handy for many cases. So here is a rule that that does the following. You can even create a customized algorithm from it and apply it to every project at the end of your classification.
  1.  create an array to store all you classes
  2.  create a temp class
  3. create a feature area of classified objects ( you can specify unit you want) based on temp class
  4.  loop each class from step 1 and store class into temp class. Get information about area from feature in step three and store in an array.
  5.  write class array and area array in a CSV with export project statistics
Area of classified objects for each class in a csv file.

Rule set in action

Loop over all classes one at a time

Store areas of current class in an array
export area information store in the array in a csv file

I have written another use of array functionality to  export a particular feature fore every image bands in your project in this blog


Wednesday, 5 November 2014

Python tutorial: Converting a raster dataset to XYZ in Python

One of the most popular posts in my blog is about converting a raster image into XYZ text file.  Converting raster image to XYZ file may be necessary because machine learning algorithms (outside proprietary software) requires input to be a table. I had written a post about converting a raster file into XYZ using ARCGIS some three years ago, which seems to attract many visitors to my blog.

Here I will write a way to do it using Python. For reading geo-referenced raster file, we will use rasterio package which is a wrapper for gdal that provides clean and fast I/O for geospatial raster images. The package is written in cython therefore it is very fast. Reading a raster file with rasterio is a one liner code. You can download binary of rasterio here. Its a binary file so installing rasterio is just a matter of clicking binary executable file. After reading the raster file we will get bounding box of the image and compute XY of each pixel and later write in a csv file together with pixel values. The code works for any number of bands in the image.

I hope this piece of code is useful for you. In near future I will show you to do exact same thing within QGIS. Stay tuned.


Output XYZ file with band values

In addition, the above code can be combine with the code (Data exchange between MATLAB and Python: Reading and writing .mat files with Python)  so that  you can easily export multi-spectral and hyperspectral data as a mat file for MATLAB. Many people seem to be having problem with reading remote sensing image with multiband read function in MATLAB. If you are one them, use above codes and bypass multiband read function to get your remotes images straight in MATLAB.