Git Product home page Git Product logo

pycrown's Introduction

manaakiwhenua-standards

PyCrown - Fast raster-based individual tree segmentation for LiDAR data

Author: Dr Jan Schindler (formerly Zörner) (mailto:[email protected])

Published under GNU GPLv3

Summary

PyCrown is a Python package for identifying tree top positions in a canopy height model (CHM) and delineating individual tree crowns.

The tree top mapping and crown delineation method (optimized with Cython and Numba), uses local maxima in the canopy height model (CHM) as initial tree locations and identifies the correct tree top positions even in steep terrain by combining a raster-based tree crown delineation approach with information from the digital surface model (DSM) and terrain model (DTM).

Citation:

Zörner, J.; Dymond, J.; Shepherd J.; Jolly, B. PyCrown - Fast raster-based individual tree segmentation for LiDAR data. Landcare Research NZ Ltd. 2018, https://doi.org/10.7931/M0SR-DN55

Research Article:

Zörner, J., Dymond, J.R., Shepherd, J.D., Wiser, S.K., Bunting, P., Jolly, B. (2018) Lidar-based regional inventory of tall trees - Wellington, New Zealand. Forests 9, 702-71. https://doi.org/10.3390/f9110702

Purpose and methods

A number of open-source tools to identify tree top locations and delineate tree crowns already exist. The purpose of this package is to provide a fast and flexible Python-based implementation which builds on top of already well-established algorithms.

Tree tops are identified in the first iteration through local maxima in the smoothed CHM.

We re-implement the crown delineation algorithms from Dalponte and Coomes (2016) in Python. The original code was published as R-package itcSegment (https://cran.r-project.org/package=itcSegment) and was further optimized for speed in the lidR R-package (https://cran.r-project.org/package=lidR).

Our Cython and Numba implementations of the original algorithm provide a significant speed-up compared to itcSegment and a moderate improvement over the version available in the lidR package.

We also adapted the crown algorithm slightly to grow in circular fashion around the tree top which gives crown a smoother, more natural looking shape.

We add an additional step to correct for erroneous tree top locations on steep slopes by taking either the high point from the surface model or the centre of mass of the tree crown as new tree top.

Reference:

Dalponte, M. and Coomes, D.A. (2016) Tree-centric mapping of forest carbon density from airborne laser scanning and hyperspectral data. Methods in Ecology and Evolution, 7, 1236-1245.

Main outputs

  • Tree top locations (stored as 3D ESRI .shp-file)
  • Tree crowns (stored as 2D ESRI .shp-file)
  • Individual tree classification of the 3D point cloud (stored as .las-file)

Contributors

  • Dr Jan Zörner (Manaaki Whenua - Landcare Research, Lincoln, New Zealand)
  • Dr John Dymond (Manaaki Whenua - Landcare Research, Palmerston North, New Zealand)
  • Dr James Shepherd (Manaaki Whenua - Landcare Research, Palmerston North, New Zealand)
  • Dr Ben Jolly (Manaaki Whenua - Landcare Research, Palmerston North, New Zealand)

Requirements

It is assumed that you generated a canopy height model (CHM), digital surface model (DSM) and digital terrain model (DTM) from the LiDAR dataset before running PyCrown. If you want to classify individual trees in the point cloud, it is recommended to normalize heights to height above ground elevation (also done externally).

For processing laser scanning data we recommend the open-source software SPDLib (http://www.spdlib.org).

Installation and environment set-up

Python 3.6 is required.

Tested on: Windows 10, Debian 9 (Stretch), Fedora 28, Ubuntu 18.04 & 16.04

Environment set-up

With Conda package manager (recommended)

Create the environment and install all required packages

conda env create

Activate the environment

Windows: activate pycrown-env

Linux: source activate pycrown-env

With Python's venv and pip

Create the environment

python -m venv pycrown-env

Linux: source pycrown-env/bin/activate

Windows: pycrown-env\Scripts\activate.bat

Install all required packages

python -m pip install --upgrade pip

pip install -r requirements.txt

Run Tests

There are only some rudimentary tests provided at the moment, but it is advised to check that everything works:

python setup.py test

Install PyCrown

Build and install the PyCrown module with:

python setup.py install

Common problems

laspy.util.LaspyException: Laszip was not found on the system

On some platforms (e.g. Ubuntu 16.04) the installation of laspy does not include laszip/laszip-cli. See the issue report on github for more infos.

In this case, please follow these steps:

  • wget http://lastools.org/download/LAStools.zip
  • unzip LAStools.zip && cd LAStools && make
  • cp bin/laszip /home/USERNAME/miniconda3/envs/pycrown-env/bin/

If you encounter this error under Windows, please download LAStools.zip, extract the archive and copy the file "laszip.exe" from the "bin"-directory to the conda environment, e.g. C:\Users<username>\AppData\Local\Continuum\miniconda3\envs\pycrown-env\Scripts\ or C:\Users<username>\Miniconda3\envs\pycrown-env\Scripts

Error while building 'pycrown._crown_dalponte_cython' extension

Building the Cython module requires C++ build tools which may need to be installed on your system.

The Windows error message on Windows provides instructions: error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/ During the setup process, please select 'C++ Build Tools'.

TypeError: a bytes-like object is required, not 'FakeMmap' when trying to load .laz files

There seems to be an incompatibility between laspy and numpy in recent versions. The combination numpy==1.16.4 and laspy==1.5.1 works for me. I suggest either not using .laz files for the time being or downgrading to the appropiate package versions. Please also refer to this github issue: laspy/laspy#112

Getting Started

You can find an IPython Notebook demonstrating each step of the tree segmentation approach in the example folder.

You can also run the example python script directly. Results are stored in the example/result folder.

cd example

python example.py

Main processing steps

Step 1: Smoothing of CHM using a median filter

Step 1

Step 2: Tree top detection using local maxima filter

Step 2

Step 3: Tree Crown Delineation using an adapted Dalponte and Coomes (2016) scheme

Step 3

Step 4: Tree top correction of trees on steep slopes

Step 4

Step 5: Smoothing of crown polygons using first returns of normalized LiDAR point clouds

Step 5

Step 6: Classification of individual trees in the 3D point cloud (visualized with CloudCompare)

Classified Point Cloud Classified Point Cloud

pycrown's People

Contributors

zjans avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pycrown's Issues

Filter size not an integer

I've ran the example.py code using the example files, but when importing my own CHM, DSM, DTM and las files I keep getting an error in line 28 (PC.filter_chm(5))
this relates to line 467 in pycrown.py.
the exception error is:
Filter size not an integer
But it is. I tried to rewrite line 28 like so: PC.filter_chm(int(5)).
But still doesn't work.
Do you know what could be the issue.
Thanks

Excuse me, about PyCrown asking for help

hallo!Please forgive me for my poor English.I have had the pleasure of reading the Pycrowd-fast Raster-based Individual Tree Segmentation for LiDAR Data that you have published on GitHub.However, after preprocessing with the LAS data in your package, the following error occurs when running the code:

Traceback (most recent call last):
File "F:/pycrown/pycrown-master/example/example.py", line 22, in
PC = PyCrown(F_CHM, F_DTM, F_DSM, F_LAS, outpath='result')
File "E:\py36\lib\site-packages\pycrown\pycrown.py", line 125, in init
self.epsg = int(proj.GetAttrValue('AUTHORITY', 1))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

I hope you can give me some advice. Thank you very much.

Running example.py with my own data

I have prepared my own data (attached) and just changed the inputs in the example.py. My guess is that the LAS-file need some special treatment, but I'm not sure what is needed. I get an error saying:

Traceback (most recent call last):
  File "example_ks.py", line 83, in <module>
    PC.crowns_to_polys_smooth(store_las=True)
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\pycrown-0.2-py3.6-win-amd64.egg\pycrown\pycrown.py", line 826, in crowns_to_polys_smooth
    op='within', how="inner")
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\geopandas\tools\sjoin.py", line 69, in sjoin
    tree_idx = rtree.index.Index(stream)
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\rtree\index.py", line 264, in __init__
    self.handle = self._create_idx_from_stream(stream)
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\rtree\index.py", line 728, in _create_idx_from_stream
    return IndexStreamHandle(self.properties.handle, stream)
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\rtree\index.py", line 854, in __init__
    self._ptr = self._create(*args, **kwargs)
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\rtree\core.py", line 29, in check_void
    raise RTreeError(msg)
rtree.core.RTreeError: Error in "Index_CreateWithStream": IllegalArgumentException: RTree::BulkLoader::bulkLoadUsingSTR: Empty data stream given.
Exception ignored in: <bound method Handle.__del__ of <rtree.index.IndexStreamHandle object at 0x0000013004AF5B70>>
Traceback (most recent call last):
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\rtree\index.py", line 875, in __del__
    self.destroy()
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\rtree\index.py", line 863, in destroy
    if self._ptr is not None:
AttributeError: 'IndexStreamHandle' object has no attribute '_ptr'

Any ideas?

data_krsv.zip

Speed comparison with lidR

The readme mentions that pycrown offers a 'moderate improvement' in speed over the R package lidR. Do you happen to have any benchmarks comparing the speed of the crown delineation algorithms between packages? Thanks!

Can't not find the classification result

Hi!

I installed pycrown and all requirements on Ubuntu 18.04, and the test runs fine without errors.
Then I executed the example by python example.py, and got all the output.
However, I can not find the classification result you show in step6 after loading the tree.las in cloudcompare.
There are only intensity and none in Scalar Field option.

How to fix this?

Thank you for your time and concern
Yujie Cao

Can't install due to cl.exe file

Hi, I've installed this in the past but had to uninstall anaconda for other reasons and I'm getting issues on installation when running python setup.py install
I am wondering if you have had similar issues and had found a solution. I keep getting this error:
error: Setup script exited with error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.28.29333\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2

Ambiguous parameters

Hello,

In the function crown_delineation(), there are 3 parameters as following:
th_seed : factor 1 for minimum height of tree crown
th_crown : factor 2 for minimum height of tree crown
th_tree : minimum height of tree seed (in m)
I wonder the meaning and function of each parameter and the difference between these three parameters, so that we can further tune them to a suitable extent according to our dataset.

Best regards

code freezes on PC.correct_tree_tops()

I get a fast turn around getting numbers of trees detected but this part of the code keeps loading forever. In task manager it shows nothing is loading. I've tried skipping this piece of code , but then the same happens on PC.screen_small_trees(hmin=3., loc='top').
I had no issues running it 2 weeks ago. but for some reason, now it freezes. Is there any known update in the necessary libraries that would need updating?

Cant import in spyder

Hi!

So I installed pycrown and all requirements. The test runs fine and returns no errors.
Unfortunately when I try to "from pycrown import PyCrown" in spyder I get :
cannot import name 'PyCrown' from 'pycrown' (unknown location)

How to fix this?

I am new to the coding world so please excuse me if its a stupid questions.

Thanks in advance!
David

GeometryTypeValidationError

When I adjusted the parameters about median filter or ws in tree_detection function, I often encounter the following issue.

The error occoured in PC.export_tree_crowns(crowntype='crown_poly_smooth').
The content is GeometryTypeValidationError: Record's geometry type does not match collection schema's geometry type: 'LineString' != 'Polygon'

And part of the output file tree_crown_poly_smooth.shp will be missing. I am not sure which parameter is set wrong or not accurate.

If you know the principle, please help me.

Crowns' and tree tops' indices don't match

The indices ("DN" field) of corresponding pairs of crown and tree top don't match up. Below is the result of a spatial join on the tree and crown outputs from the library's example.
Screenshot from 2021-04-01 17-00-26

Failed running example - las py "not 'FakeMmap'"

Hi!

I tried running the example.py using miniconda (following the instructions) on Windows 10.
Python 3.6.9
laspy 1.6.0
LAStools.zip (http://lastools.github.io/download/LAStools.zip downloaded at 2019-12-17)

I get an error:

(pycrown-env) C:\Users\SEKRSV\pycrown\example>python example.py
Traceback (most recent call last):
  File "example.py", line 22, in <module>
    PC = PyCrown(F_CHM, F_DTM, F_DSM, F_LAS, outpath='result')
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\pycrown-0.2-py3.6-win-amd64.egg\pycrown\pycrown.py", line 155, in __init__
    self._load_lidar_points_cloud(las_file)
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\pycrown-0.2-py3.6-win-amd64.egg\pycrown\pycrown.py", line 189, in _load_lidar_points_cloud
    las = laspy.file.File(str(fname), mode='r')
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\laspy\file.py", line 64, in __init__
    self.open()
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\laspy\file.py", line 75, in open
    self._reader = base.Reader(self.filename, mode=self._mode)
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\laspy\base.py", line 273, in __init__
    self.setup_read_write(vlrs,evlrs, read_only=True)
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\laspy\base.py", line 321, in setup_read_write
    self.data_provider.point_map()
  File "C:\Users\SEKRSV\AppData\Local\Continuum\miniconda3\envs\pycrown-env\lib\site-packages\laspy\base.py", line 163, in point_map
    offset=self.manager.header.data_offset)
TypeError: a bytes-like object is required, not 'FakeMmap'

Any ideas how to fix it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.