<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shaig Hamzaliyev on Medium]]></title>
        <description><![CDATA[Stories by Shaig Hamzaliyev on Medium]]></description>
        <link>https://medium.com/@h.shaig93?source=rss-eabce712bd89------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SJ_jeat8sw0cnk0MtaM26A.jpeg</url>
            <title>Stories by Shaig Hamzaliyev on Medium</title>
            <link>https://medium.com/@h.shaig93?source=rss-eabce712bd89------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 16:29:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@h.shaig93/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Connected component analysis implementing from scratch with python]]></title>
            <link>https://medium.com/@h.shaig93/connected-component-analysis-implementing-from-scratch-with-python-c5931785493b?source=rss-eabce712bd89------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5931785493b</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[remote-sensing]]></category>
            <category><![CDATA[photogrammetry]]></category>
            <category><![CDATA[computer-vision]]></category>
            <dc:creator><![CDATA[Shaig Hamzaliyev]]></dc:creator>
            <pubDate>Mon, 01 Apr 2024 13:22:56 GMT</pubDate>
            <atom:updated>2024-04-06T11:47:36.718Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Implementing Connected Component Analysis for Image Processing from Scratch with Python</strong></h3><p>In this article I will try to give some intuitive introduction to connected component analysis (CCA). In this tutorial I will use binary image as an example. But the main goal of this tutorial is to give a code example and the explaining the logic, then one can transfer this to grey scale images too. CCA is an algorithm for labeling the objects in an image, where if there is a path from point <strong><em>p </em></strong>to <strong><em>q </em></strong>through the same component then point A and B will have the same label (Fig. 1).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t6YDjjCBScaAHgEWkBA7rw.jpeg" /><figcaption>Figure 1. 8-connected object pixels and connected components.[1]</figcaption></figure><p>In the binary images for CCA we are not interest in background pixels, we want to concentrate on forground pixels to find connected obgects. We will evaluate each pixel seperately. There is two ways to find connected objects, first by checking 4 and second is 8 neighbor pixels to the center pixel. Figure 1 shows how connected objects are chosen with 8 neighbors from p to q.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v-OsbXX6Fqxt9BBjo4jnwA.png" /><figcaption>Figure 2. 4 neighbor and 8 neighbor cases.</figcaption></figure><p>Now as we are going through each pixel in our grid we can just check top and left pixel for 4 neighbor case and for 8 neighbor case left, top-left, top, top-right pixels will be checked let’s visualize it first and right the small function for it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-V_MXmMAraa-x5rxXIwdrA.png" /><figcaption>Figure 3. Image example for 4 and 8 neighbor cases, where grey pixels are pixels to be checked and red is center pixel.</figcaption></figure><p>Now the code for 8 neighbor case, in this function value 256 is taken from output_image variable in connected_component_analysis_binary which you will see later:</p><pre>def check_neighbors(output_image, i, j):<br>    neighbors = []<br>    rows, cols = output_image.shape<br>    # Check top neighbor<br>    if i &gt; 0 and output_image[i-1, j] != 256:<br>        neighbors.append(output_image[i-1, j])<br>    # Check left neighbor<br>    if j &gt; 0 and output_image[i, j-1] != 256:<br>        neighbors.append(output_image[i, j-1])<br>    # Check north-west neighbor<br>    if i &gt; 0 and j &gt; 0 and output_image[i-1, j-1] != 256:<br>        neighbors.append(output_image[i-1, j-1])<br>    # Check north-east neighbor<br>    if i &gt; 0 and j &lt; cols - 1 and output_image[i-1, j+1] != 256:<br>        neighbors.append(output_image[i-1, j+1])<br>    return neighbors</pre><p>in this code we just need to give image as an argument and index values for center pixel. Later on we will use this function in our CCA function. Now let me show you what image am I working with:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*VLVvVtfMuuM0sN22b7s7DA.gif" /><figcaption>Croses binary image from [2]</figcaption></figure><p>Now the max and min values for this image is [ 1, 255] (check <em>numpy.unique</em> function for help). To visualize it let’s import some libraries and read the image:</p><pre>import skimage as ski<br>import os<br>from matplotlib import pyplot as plt<br>import numpy as np<br><br>filename = os.path.join(os.getcwd(), &#39;cir1.gif&#39;)<br>cross = ski.io.imread(filename)<br># if you wan to plot<br># ski.io.imshow(cross)<br># plt.show()</pre><p>As we have our image lets get into checking part of algorithm, and how we actually check things in our code:</p><p>This algorithm will scan the image twice. In the first scanning Process: The algorithm scans the binary image row by row until it finds a white pixel (value 255 in our case and black is 1) denoted as “p”. Labeling Rules for Pixel “p”: If all four neighboring pixels of “p” are black (value 0), assign a new label to “p”. If only one neighboring pixel has a value of 1, assign its label to “p”. If more than one neighboring pixel has a value of 1, assign one of the labels to “p” and record the equivalences between labels. Equivalence Resolution: After completing the scan, equivalent label pairs are grouped into equivalence classes. Each equivalence class is assigned a unique label. Second Scan: Another scan through the image is performed. During this scan, each label is replaced with the label assigned to its equivalence class. Labeling Consistency: Ensure consistency in labeling by relabeling pixels with equivalent labels to the smallest label among them. Now the code for first scan:</p><pre>def equivalence_table_post_processing(equivalence_table):<br>    keys = list(equivalence_table.keys())<br>    for key in keys:<br>        for other_key, other_value in equivalence_table.items():<br>            if key != other_key and key in other_value:<br>                equivalence_table[other_key].update(equivalence_table[key])<br>                #equivalence_table[other_key].update([other_key])<br>                equivalence_table.pop(key)<br>                break  # Exit inner loop after merging sets<br>    return equivalence_table<br><br>def connected_component_analysis_binary(input_image):<br>    A = {}  <br>    K = 0<br>    output_image = np.ones(shape=input_image.shape) * 256  # Initialize output image<br>    <br>    # Iterate over each pixel in the input image<br>    for i in range(input_image.shape[0]):<br>        for j in range(input_image.shape[1]):<br>            if input_image[i, j] &gt; 1:  # If the pixel is part of a connected component<br>                neighbors = check_neighbors(output_image, i, j)<br>                if len(neighbors) == 0:<br>                    K += 1<br>                    output_image[i, j] = K<br>                else:<br>                    output_image[i, j] = min(neighbors)<br>                    for x in neighbors:<br>                        if x != output_image[i, j]:<br>                            if output_image[i, j] in A:<br>                                if x not in A[output_image[i, j]]:<br>                                    A[output_image[i, j]].add(x)<br>                            else:<br>                                A[output_image[i, j]] = {x}<br>    <br>    A = equivalence_table_post_processing(A)<br>    return output_image, A<br><br># call the function with circle image as an input<br>image_output = connected_component_analysis_binary(cross)</pre><p>Now let’s see what A (our equivalency table)is returning:</p><pre>print(image_output[1])<br><br>{1.0: {2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 11.0, 14.0, 15.0, 16.0, 17.0, 18.0},<br> 7.0: {9.0, 10.0, 12.0, 13.0, 19.0, 20.0},<br> 21.0: {22.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 34.0, 36.0},<br> 23.0: {24.0, 25.0, 32.0, 33.0, 35.0},<br> 37.0: {40.0, 44.0, 45.0},<br> 38.0: {39.0, 41.0, 42.0, 43.0},<br> 46.0: {47.0, 61.0, 63.0},<br> 48.0: {49.0,<br>  50.0,<br>  51.0,<br>  52.0,<br>  53.0,<br>  54.0,<br>  55.0,<br>  56.0,<br>  57.0,<br>  58.0,<br>  59.0,<br>  60.0,<br>  62.0,<br>  64.0,<br>  65.0,<br>  66.0,<br>  67.0}}</pre><p>As you can see we have eight objects, but that one object has different labels now with our second scan we will group all labels that we see in our dictionary.</p><pre>def relable_pixels(image, equivalency_dictionary):<br>    for i in range(image.shape[0]):<br>        for j in range(image.shape[1]):<br>            for key, value in equivalency_dictionary.items():<br>                if image[i, j] in value:<br>                    image[i, j] = key<br>    return image<br><br>im = relable_pixels(image_output[0], image_output[1])</pre><p>Now let’s visualize and see what we got, but I will remap values for visualizing as they have big differences:</p><pre>import numpy as np<br>import matplotlib.pyplot as plt<br>from matplotlib.colors import ListedColormap<br><br># Remap the values in your image array<br>remapped_values = np.unique(im)<br>remapped_im = np.digitize(im, bins=[0, 7, 21, 23, 37, 38, 46, 48, 255])  # Assuming im is your image array<br><br># Define discrete colormap<br>cmap = ListedColormap([&#39;red&#39;, &#39;orange&#39;, &#39;yellow&#39;, &#39;green&#39;, &#39;cyan&#39;, &#39;blue&#39;, &#39;purple&#39;, &#39;magenta&#39;, &#39;white&#39;])<br><br># Plot the image with the custom colormap<br>plt.imshow(remapped_im, cmap=cmap, interpolation=&#39;nearest&#39;)<br><br># Add colorbar for reference<br>cb = plt.colorbar(ticks=range(1, 10))  # Assuming 1-9 are the new labels you can change accordin your needs<br>cb.ax.set_yticklabels(range(1, 10))<br>plt.savefig(&quot;cross.png&quot;, dpi=300)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N6xqsoUumNf9oj-0FfQXDQ.png" /><figcaption>8 crosses labelled to different colors, and background is white color.</figcaption></figure><p>Now there you go I hope it was useful. But if you want to go a bit more deep, then I would encourage you to check Cyrill Stachniss lecture in this <a href="https://www.youtube.com/watch?v=-F20DZhzbEs&amp;t=1899s">link</a>. He explains it really nice here I just tried to implement it and give short explanation for better intuition.</p><h3>References</h3><p>[1] L. He, X. Ren, Q. Gao, X. Zhao, B. Yao, and Y. Chao, ‘‘The connectedcomponent labeling problem: A review of state-of-the-art algorithms,’’ Pattern Recognit., vol. 70, pp. 25–43, Oct. 2017</p><p>[2] <a href="https://homepages.inf.ed.ac.uk/rbf/HIPR2/label.htm">https://homepages.inf.ed.ac.uk/rbf/HIPR2/label.htm</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5931785493b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating a DSM from Point Clouds: A Practical Guide to Gridding and Visualization]]></title>
            <link>https://medium.com/@h.shaig93/creating-a-dsm-from-point-clouds-practical-guide-c68d9e84a463?source=rss-eabce712bd89------2</link>
            <guid isPermaLink="false">https://medium.com/p/c68d9e84a463</guid>
            <category><![CDATA[point-cloud]]></category>
            <category><![CDATA[remote-sensing]]></category>
            <category><![CDATA[earth-observation]]></category>
            <category><![CDATA[geospatial]]></category>
            <category><![CDATA[earth-observation-data]]></category>
            <dc:creator><![CDATA[Shaig Hamzaliyev]]></dc:creator>
            <pubDate>Fri, 05 Jan 2024 16:08:16 GMT</pubDate>
            <atom:updated>2024-01-06T15:52:10.317Z</atom:updated>
            <content:encoded><![CDATA[<p>Today I will show you how create a Digital Surface Model (DSM) from point clouds. First we will try to get an understanding of what DSM is then we will get into more practical part of the discussion.</p><h3><strong>What is a DSM?</strong></h3><p>DSM is a model that describes the surface with all that is on that surface. Now to be more clear with DSM you are not just interested with geomorphological chracteristics of the certain are but you also take all trees, bushes, big rocks that lies on that surface. However the DTM (Digital Terrain Model) is where you have only the earths surface so no trees or rocks or bushes. Here I will create a DSM. To generate a Digital Surface Model (DSM) from point clouds, the initial step involves dividing the area of interest into a grid. Subsequently, each grid cell is assigned an elevation value, which is derived from the 3D coordinates (X, Y, Z) of the points within or near the cell. This process results in a comprehensive representation of the Earth’s surface, capturing features such as terrain, buildings, and vegetation. To get better understanding of how gridding works first I will create a synthetic data and implement gridding my self.</p><p>Let’s import the necessary libraries first:</p><pre>import numpy as np<br>import matplotlib.pyplot as plt<br>from matplotlib import ticker, cm<br>from mpl_toolkits.axes_grid1 import make_axes_locatable<br>from scipy.spatial import cKDTree</pre><p>Creating gridding area and some sample points:</p><pre># grid range in x and y direction<br>x = np.arange(8)<br>y = np.arange(8)<br># creating high number of points (70) for this grid area <br># with x, y, z coordinates<br>xp = np.random.random(size=70)*8 - 0.5<br>yp = np.random.random(size=70)*8 - 0.5<br>zp = np.random.randint(10, size = 70)<br># grid coodinates<br>xc = np.arange(8) # x coordinates <br>yc = np.arange(8) # y coordinates<br>grc = np.meshgrid(xc, yc) # grid coordinates</pre><p>Now as a next step we will use nearest neighbor interpolation to find the closest point to the grid centers:</p><pre>tree = cKDTree(np.c_[xp, yp])<br>dd, ii = tree.query(np.c_[grc[0].ravel(), grc[1].ravel()], k=1)<br>x_close = xp[ii]<br>y_close = yp[ii]</pre><p>Visualization for better understanding:</p><pre>plt.figure(figsize = (12, 12))<br>plt.xticks(x-0.5)<br>plt.yticks(y-0.5)<br>plt.scatter(xp, yp, label=&#39;Observed Points&#39;)<br>plt.scatter(grc[0], grc[1], label=&#39;Grid Centers&#39;)<br>plt.grid(color=&#39;k&#39;, linestyle=&#39;-&#39;, linewidth=2)<br>plt.xlim((-.5,7.5))<br>plt.ylim((-.5,7.5))<br><br><br><br>for i in range(len(ii)):<br>    plt.plot([grc[0].ravel()[i], xp[ii[i]]], [grc[1].ravel()[i], yp[ii[i]]], c=&#39;g&#39;)<br><br>#plt.text(-0.11, 7.5-0.31, &#39;A )&#39;, bbox=dict(fill=False, edgecolor=&#39;r&#39;, linewidth=1.25), fontsize=18)<br><br>plt.legend(bbox_to_anchor=(.95, 1.02), loc=&#39;upper left&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FqR6QtuNWH1hKLgOzJHMnA.png" /><figcaption>Blue points are observed and orange points are the grid centers and the green lines are showing the connection between grid center and the closest point to that center.</figcaption></figure><p>Then once you assign one value to each grid you get your raster. Let’s visualize it:</p><pre>from matplotlib import ticker, cm<br>from mpl_toolkits.axes_grid1 import make_axes_locatable<br>fig, ax = plt.subplots(1, figsize = (12, 12))<br>ax.set_xticks(x-0.5)<br>ax.set_yticks(y-0.5)<br>elv = zp[ii]<br>elv.shape = (x.shape[0], y.shape[0])<br>im = ax.imshow(np.flip(elv, 0), cmap=cm.get_cmap(&#39;PuBu_r&#39;, 9))<br>ax.grid(color=&#39;k&#39;, linestyle=&#39;-&#39;, linewidth=2)<br>ax.tick_params(&#39;both&#39;, bottom=False, top=False, left=False, right=False,<br>                labelbottom=False, labeltop=False, labelleft=False, labelright=False)<br>divider = make_axes_locatable(ax)<br>cax = divider.append_axes(&quot;right&quot;, size=&quot;5%&quot;, pad=0.05)<br>cbar = fig.colorbar(im, ticks=np.arange(10), ax = ax, cax = cax)<br>cbar.ax.set_yticklabels([&#39;0 m    &#39;, &#39;1 m    &#39;, &#39;2 m    &#39;, &#39;3 m     &#39;, &#39;4 m&#39;, &#39;5 m&#39;, &#39;6 m&#39;, &#39;7 m&#39;, &#39;8 m&#39;, &#39;9 m&#39;])  <br>cbar.ax.set_ylabel(&#39;Elevation of interpolated Points&#39;, rotation=270, fontsize=15)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ImCR82JDckOXdSCdaCIbbw.png" /><figcaption>Raster result from above synthetic points.</figcaption></figure><h3><strong>Gridding the Real world data.</strong></h3><p>For next step I will use real world data that I downloaded from OpenTopography. If you want to see where you can get free data you can read my story about it:</p><p><a href="https://medium.com/@h.shaig93/open-source-point-clouddatasets-344642695183">Open Source Point Cloud Datasets</a></p><p>If you want to work with the data that I have you can get it from <a href="https://drive.google.com/drive/folders/1CVLV8DSe3FFbGibv4omAhQ1k523quAZq?usp=sharing">here</a>. Let’s see what the data looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VYJZ3uQizi9CI1aXm16oKQ.png" /><figcaption>Point cloud data from Colrado (OpenTopography).</figcaption></figure><p>Now let’s do the gridding to real world point cloud data but this time I will use scipy griddata instead of writing my own gridding operation:</p><pre>import numpy as np<br>from scipy.interpolate import griddata<br>import matplotlib.pyplot as plt<br>import laspy<br>path = &quot;pointsColoradowithoutBuilding.las&quot;<br>las = laspy.read(path)<br>points = las.xyz<br>amplitude = las.intensity<br>red = las.red<br>green = las.green<br>blue = las.blue<br>from scipy.interpolate import griddata<br>import matplotlib.pyplot as plt<br><br>x = points[:, 0]<br>y = points[:, 1]<br>z = points[:, 2]<br><br># Define grid parameters<br>xmin, xmax = np.nanmin(x), np.nanmax(x)<br>ymin, ymax = np.nanmin(y), np.nanmax(y)<br>grid_resolution = .5<br><br># Create a grid<br>xi, yi = np.meshgrid(np.arange(xmin, xmax, grid_resolution), np.arange(ymin, ymax, grid_resolution))<br><br># Interpolate elevation values<br>zi = griddata((x, y), z, (xi, yi), method=&#39;linear&#39;)<br>plt.figure(figsize=(10, 8))<br>new_zi = np.nan_to_num(zi, nan=np.nanmax(zi))<br>new_zi = np.clip(new_zi, a_min=None, a_max=z.max()+1)<br>plt.imshow(new_zi, extent=[xi.min(), xi.max(), yi.min(), yi.max()], cmap=&quot;terrain&quot;)<br>plt.colorbar(label=&#39;Elevation&#39;)<br>plt.title(&#39;Digital Elevation Model (DEM) with imshow&#39;)<br>plt.xlabel(&#39;X-coordinate&#39;)<br>plt.ylabel(&#39;Y-coordinate&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lw4EHCzp1QheoL-6oxWjag.png" /><figcaption>Gridded Point cloud data.</figcaption></figure><p>Here I used linear interpolation for gridding from scipy library. And the result looks really good. Here you go I hope you enjoyed this practical guide.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c68d9e84a463" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LiDAR Full-Waveform (gentle introduction)]]></title>
            <link>https://medium.com/@h.shaig93/lidar-full-waveform-gentle-introduction-fb566c005fa7?source=rss-eabce712bd89------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb566c005fa7</guid>
            <category><![CDATA[lidar]]></category>
            <category><![CDATA[remote-sensing]]></category>
            <category><![CDATA[geospatial]]></category>
            <category><![CDATA[geospatial-data]]></category>
            <dc:creator><![CDATA[Shaig Hamzaliyev]]></dc:creator>
            <pubDate>Wed, 27 Dec 2023 11:58:50 GMT</pubDate>
            <atom:updated>2024-01-05T15:40:08.634Z</atom:updated>
            <content:encoded><![CDATA[<p>LiDAR full-waveform might not be a familiar term for many people who have heard about LiDAR. In this article, I will provide some theoretical understanding of the matter and discuss its potential advantages.</p><h3>What is LiDAR Full-Waveform?</h3><p>LiDAR is an active remote sensing method where an aperture sends out a laser beam and records the returning information. The beam essentially functions as a wave, and points are derived to create the point cloud. When we obtain a point cloud from this wave, it is referred to as discrete LiDAR. In some cases, people prefer to work with the entire wave instead of a single point. Discrete LiDAR data consists of unstructured points, providing geometrical and intensity information. LiDAR full waveform, on the other hand, involves recording the entire wave along with its intensity information. Let’s explore the figure above to gain a better intuition:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*MhBFkwkG2TbmG_4bFUgsZQ.png" /><figcaption>LiDAR full-waveform and discrete return (multiple return case)¹.</figcaption></figure><p>In this figure, we observe a multiple return case. Whenever the laser beam interacts with an object, discrete returns provide points, while in the full-wave case, it produces waves. In each interaction with the object, the wave exhibits peaks locally, and depending on the characteristics of the object, the wave takes on different shapes. The wave consists of discrete points, each typically sampled at one-nanosecond intervals. From this, we can calculate the sampling distance:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*Y8FsmMORkKUc6oTj0SfdtQ.png" /><figcaption>Sample distance calculation.</figcaption></figure><p>In this equation, “c” represents the speed of light (3 × 1⁰⁸). We multiply the distance by two because the pulse travels the same distance back and forth. According to this calculation, the sampling distance is 15 centimeters.</p><p>As the pulse travels from the scanner and back, the signal may encounter some noise due to interactions with atmospheric particles, but this is typically negligible. Additionally, when the returning signal interacts with different objects, it may appear more varied. Another consideration is that if the object has a rough surface, the signal is expected to be cleaner, with a higher intensity value and a narrower wave width:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*tM6prQfTLQm6e2vtS1M_zg.png" /><figcaption>The difference of the pulse shape based on the target. Hard target (building, ground etc) and some soft targets (foliage)².</figcaption></figure><p>In the literature, when working with full-waveform data, a Gaussian function is commonly used to fit the signal. The Gaussian fit performs well, as investigated by Wagner et al.³. According to their analysis, the fit residual is almost close to the tails and the peak of the signal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*GYEKRlY6phNF_s6G4ubvqg.png" /><figcaption>Full wave form in normalized values. The bottom figure is the residual from a Gaussian fit to the signal(Wagner et al., 2006)</figcaption></figure><p>The example above represents a single return case, which is comparatively easier to deal with. In multiple return cases, the superposition of Gaussian functions is used. Utilizing additional information about the width (standard deviation) and signal height (the mean or peak) can potentially lead to better classification and segmentation results compared to when only the geometric representation of a point is used.</p><p>This short article aims to provide a brief introduction to LiDAR full-waveform. In the next step, I will visualize some data that I downloaded from NEON. Then, I used a library from this link to create an HDF file from NEON pulse waves. You can access the HDF data from <a href="https://drive.google.com/drive/folders/1CVLV8DSe3FFbGibv4omAhQ1k523quAZq?usp=sharing">here </a>if you want to explore it yourself. Now, let’s first visualize the outgoing and returning signals:</p><pre># importing some libraries<br>import pandas as pd<br>import numpy as np<br>import matplotlib.pyplot as plt<br>import h5py<br><br>path = &quot;C:/Users/saiqh/Downloads/neon20.hdf&quot;<br>h = h5py.File(path, &#39;r&#39;)<br><br>amp = h[&#39;Amplitude&#39;]<br>xyz = h[&#39;XYZ&#39;]<br>idx = h[&#39;Index&#39;]<br><br>fg, ax = plt.subplots(1, 2, figsize=(13, 7))<br>z_min = np.ones(1) * 9999<br>z_max = np.zeros(1)<br>amp_min = np.ones(1) * 9999<br>amp_max = np.zeros(1)<br>for i in range(1440, 1450):<br>    a = amp[idx[i]:idx[i+1], :]<br>    z = xyz[idx[i]:idx[i+1], 2]<br>    z = z[a[:,1] == 0]<br>    a = a[a[:,1] == 0, 0]<br>    if z.min() &lt; z_min:<br>        z_min = z.min()<br>    if z_max &lt; z.max():<br>        z_max = z.max()<br>    if a.min() &lt; amp_min:<br>        amp_min = a.min()<br>    if amp_max &lt; a.max():<br>        amp_max = a.max()<br>    ax[0].plot(z, a, label = &#39;pulse %i&#39; % i)<br><br>ax[0].set_xlabel(&#39;Elevation [m]&#39;)<br>ax[0].set_ylabel(&#39;Amplitude&#39;)<br>print(&#39;Z minimum (Outgoing (ASCII reader)) - &#39;, z_min, &#39; - Intensity min (Outgoing(ASCII reader)) - &#39;, amp_min)<br>print(&#39;Z maximum (Outgoing (ASCII reader)) - &#39;, z_max, &#39; - Intensity max (Outgoing(ASCII reader)) - &#39;, amp_max)<br>ax[0].set_title(&#39;Outgoing (ASCII reader)&#39;)<br>z_min = np.ones(1) * 9999<br>z_max = np.zeros(1)<br>amp_min = np.ones(1) * 9999<br>amp_max = np.zeros(1)<br>for i in range(1440, 1450):<br>    a = amp[idx[i]:idx[i+1], :]<br>    z = xyz[idx[i]:idx[i+1], 2]<br>    z = z[a[:,1] &gt; 0]<br>    a = a[a[:,1] &gt; 0, 0]<br>    if z.min() &lt; z_min:<br>        z_min = z.min()<br>    if z_max &lt; z.max():<br>        z_max = z.max()<br>    if a.min() &lt; amp_min:<br>        amp_min = a.min()<br>    if amp_max &lt; a.max():<br>        amp_max = a.max()<br>    ax[1].plot(z, a, label = &#39;pulse %i&#39; % i)<br>ax[1].set_xlabel(&#39;Elevation [m]&#39;)<br>ax[1].set_ylabel(&#39;Amplitude&#39;)<br>print(&#39;Z min (Returning (ASCII reader)) - &#39;, z_min, &#39; - Intensity min (Returning(ASCII reader)) - &#39;, amp_min)<br>print(&#39;Z max (Returning (ASCII reader)) - &#39;, z_max, &#39; - Intensity max (Returning(ASCII reader)) - &#39;, amp_max)<br>ax[1].set_title(&#39;Returning (ASCII reader)&#39;)<br>plt.legend()<br>plt.tight_layout()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gGQGetsaBriU7WHdLSxjOw.png" /></figure><p>Now, looking at the outgoing and returning signals, we can observe that the outgoing signals are much cleaner and have high intensity. As mentioned earlier, the returning signal interacts with the atmosphere and the objects. Let’s visualize these signals in 3D:</p><pre>a = amp[idx[1440]:idx[1450]]<br>p = xyz[idx[1440]:idx[1450]]<br>p = p[a[:,1] &gt; 0, :]<br>a = a[a[:,1] &gt; 0, 0]<br>x, y, z = p.T<br><br>fg = pl.figure(1, (8, 6))<br>ax = fg.add_subplot(111, projection = &#39;3d&#39;)<br>ax.scatter(x, y, z, c = np.log10(a))<br>ax.set_xlabel(&#39;UTM X [m]&#39;)<br>ax.set_ylabel(&#39;UTM Y [m]&#39;)<br>ax.set_zlabel(&#39;Elevation [m]&#39;)<br><br>pl.tight_layout()<br>pl.savefig(&quot;3d.png&quot;, dpi=300)<br>pl.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r0ALKws6LNlhAR1TeWJpMA.png" /><figcaption>3D visualization of LiDAR FWF returning signals.</figcaption></figure><p>This article has achieved its purpose by providing theoretical information about LiDAR full-waveform. In the next article, I will provide a step-by-step guide for processing such data.</p><p>Thank you for reading!</p><p>[1]: Daly, S. F., Vuyovich, C., and Finnegan, D. (2011). Situk River Hydrology Following Closure of Russell Fiord by Hubbard Glacier. ERDC-CRRELTech. Rep. ADA53802.</p><p>[2]: Yang, X., Strahler, A. H., Schaaf, C. B., Jupp, D. L., Yao, T., Zhao, F., Wang, Z., Culvenor, D. S., Newnham, G. J., Lovell, J. L., Dubayah, R. O., Woodcock, C. E., and Ni-Meister, W. (2013). Three-dimensional forest reconstruction and structural parameter retrievals using a terrestrial full-waveform lidar instrument (Echidna®). Remote Sens. Environ 135, pp. 36–51.</p><p>[3]: Wagner, W., Ullrich, A., Ducic, V., Melzer, T., and Studnicka, N. (2006). Gaussian decomposition and calibration of a novel small-footprint full-waveform digitising airborne laser scanner. ISPRS Journal of Photogrammetry &amp; RemoteSensing 60 (2), 100–112.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb566c005fa7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Point Cloud Classification with Machine Learning and Semantic3D Data (practical guide)]]></title>
            <link>https://medium.com/@h.shaig93/point-cloud-classification-with-machine-learning-and-semantic3d-data-practical-guide-7f4238610bbb?source=rss-eabce712bd89------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f4238610bbb</guid>
            <category><![CDATA[computer-graphics]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[point-cloud]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[remote-sensing]]></category>
            <dc:creator><![CDATA[Shaig Hamzaliyev]]></dc:creator>
            <pubDate>Thu, 21 Dec 2023 20:59:49 GMT</pubDate>
            <atom:updated>2023-12-21T20:59:49.842Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article I will try to show a practical supervised point cloud classification work flow. For this I needed to use a already labelled data which I went for <a href="https://www.semantic3d.net/">Semantic3D </a>website. You can see my article for free LiDAR data websites from this <a href="https://medium.com/@h.shaig93/open-source-point-clouddatasets-344642695183">link</a>.</p><p>Keep in mind that this is more a practical example and I am not going to try much optimization or creating new features for better classification results. This is more of an introduction to the data and what can be done with it. You can use codes from here for to read, prepare and visualize the data.</p><h3><strong>1. Semantic3D data preparation.</strong></h3><p>As a first step we I will try to give some methods that will read and prepare the data for the classification step. But before we should download a data which you can find it in <a href="https://www.semantic3d.net/view_dbase.php?chl=2">here</a>. I will use <a href="https://www.semantic3d.net/data/point-clouds/training1/bildstein_station3_xyz_intensity_rgb.7z">bildstein_station3_xyz_intensity_rgb</a> data and you can dowload all the labels in <a href="https://www.semantic3d.net/data/sem8_labels_training.7z">here</a>.</p><p>First we will import neccessary libraries:</p><pre>import numpy as np<br>import pandas as pd<br>import matplotlib.pyplot as plt<br>import laspy<br>from sklearn.preprocessing import StandardScaler<br>from sklearn.pipeline import make_pipeline<br>from sklearn.ensemble import RandomForestClassifier<br>from sklearn.model_selection import train_test_split</pre><p>Now lets read the data and its labels to see what we are going to work with:</p><pre>path = &quot;bildstein_station3_xyz_intensity_rgb.txt&quot;<br>label_path = &quot;bildstein_station3_xyz_intensity_rgb.labels&quot;<br><br>label_df = pd.read_csv(label_path, delimiter=&#39;\s+&#39;, header=None, names=[&quot;labels&quot;])<br>df = pd.read_csv(path, delimiter=&#39;\s+&#39;, header=None, names=[&#39;x&#39;, &#39;y&#39;, &#39;z&#39;, &#39;intensity&#39;, &#39;red&#39;, &#39;green&#39;, &#39;blue&#39;])<br><br>print(np.unique(label_df, return_counts=True))</pre><p>Well now in the last line I am printing unique lables and how many times they occur in the labels data frame. You can see that there arelots of 0 labels. According to the website :</p><p>Class labels are: {1: man-made terrain, 2: natural terrain, 3: high vegetation, 4: low vegetation, 5: buildings, 6: hard scape, 7: scanning artefacts, 8: cars}¹.</p><p>As this article is a more practical guide on how to classify poin clouds with Machine learning and the data is already too big, I decided just to remove the 0 labels. However before going into this I want to save my file with its RGB colors as las file (laspy.__version__ = “2.5.1”)and visualize it in the CloudCompare:</p><pre>def save(fn, points_with_rgb):<br>    x, y, z = points_with_rgb[:, 0], points_with_rgb[:, 1], points_with_rgb[:, 2]<br>    <br>    # Assuming rgb has columns red, green, blue<br>    red_channel = points_with_rgb[:, 4].astype(&#39;uint8&#39;)<br>    green_channel = points_with_rgb[:, 5].astype(&#39;uint8&#39;)<br>    blue_channel = points_with_rgb[:, 6].astype(&#39;uint8&#39;)<br><br>    las = laspy.create(point_format=2, file_version=&#39;1.2&#39;)<br>    las.header.scale = [0.001, 0.001, 0.001]  # Adjust these values based on your data<br>    las.header.offset = [x.min(), y.min(), z.min()]<br>    las.x = x<br>    las.y = y<br>    las.z = z<br>    las.red = red_channel<br>    las.green = green_channel<br>    las.blue = blue_channel<br>    las.write(fn)<br><br># Example usage:<br># Assuming some is your point cloud array and rgb is the RGB data<br>save(&quot;bildStein.las&quot;, df.to_numpy())<br><br></pre><p>Let’s see what our file looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fdA2Swha9CKeu6RnT-h4ww.png" /><figcaption>Bildstein data¹ Semantic3D.</figcaption></figure><p>Wewill remove unlabled points (label=0) for training to make data smaller:</p><pre>points_xyz_only_labelled = df.to_numpy()[label_df[&quot;labels&quot;].to_numpy()!=0]<br>labeled_points = label_df.to_numpy()[label_df[&quot;labels&quot;].to_numpy()!=0]</pre><h3><strong>2. Classification with Machine Learning</strong></h3><p>Now as we visualized the data we can train and tes the data. First we will split our data to training and testing parts, we will define test_size 0.33 which means that we will be training our algoritm with 67 percent of the data and then test it with the remaining 33 percent:</p><pre>X_train, X_test, y_train, y_test = train_test_split(point_cloud_with_intensity, label, test_size=0.33, random_state=42)</pre><p>Next is defining our pipeline. For the pipeline I will use Random Forest algorithm for classification (you can try different algorithm like SVM etc.) and StandardScaler() for normalization of the data.</p><pre>clf = make_pipeline(StandardScaler(), RandomForestClassifier())</pre><p>Now we will simply train our algorithm with the traiining dataset and then predict the testing data labels:</p><pre>clf.fit(X_train, y_train)<br>y_pred = clf.predict(X_test)</pre><p>Last step is to evaluate the accuracy:</p><pre>from sklearn.metrics import accuracy_score<br># Evaluate the accuracy<br>accuracy = accuracy_score(y_test, y_pred)<br>print(f&quot;Accuracy: {accuracy}&quot;)</pre><p>The score turns out to be 0.5745952426854118. It is not a high score but I will not play around much to try to increase the score. As a next step I will try to apply our algorithm to the Test data and save it as a las file.</p><pre># train data with intensity value (4th column)<br>y_test_data = clf.predict(dfTest.iloc[:, 3].to_numpy().reshape(-1, 1))<br><br>def save(fn, points_with, labels):<br>    x, y, z = points_with[:, 0], points_with[:, 1], points_with[:, 2]<br>    labels = labels.astype(int)  # Ensure labels are integers<br><br>    # Use the &quot;tab20&quot; colormap for distinct colors<br>    custom_cmap = plt.get_cmap(&quot;tab20&quot;)<br><br>    # Map labels to RGB without quantization<br>    unique_labels = np.unique(labels)<br>    label_mapping = {label: custom_cmap(i / (len(unique_labels) - 1))[:3] for i, label in enumerate(unique_labels)}<br>    rgb = np.array([label_mapping.get(label) for label in labels])<br><br>    las = laspy.create(point_format=2, file_version=&#39;1.2&#39;)<br>    las.header.scale = [0.001, 0.001, 0.001]<br>    las.header.offset = [x.min(), y.min(), z.min()]<br>    las.x = x<br>    las.y = y<br>    las.z = z<br>    las.red = (rgb[:, 0] * 65535).astype(&#39;uint16&#39;)<br>    las.green = (rgb[:, 1] * 65535).astype(&#39;uint16&#39;)<br>    las.blue = (rgb[:, 2] * 65535).astype(&#39;uint16&#39;)<br>    las.write(fn)<br><br># Example usage:<br># Assuming dfTest is a DataFrame with columns x, y, z, and labelsPredicted is an array of integer labels<br>save(&quot;testedResult.las&quot;, dfTest.iloc[:, :3].to_numpy(), labelsPredicted)</pre><p>And the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xM9fle--86g75qNCB8PY9Q.png" /><figcaption>Classified Test data¹.</figcaption></figure><p>Now what does this mean? First of all keep in mind that we trained our algorithm with only one parameter which was the intensity. It means that when the laser beam interacts with the different surfaces it returns different type of a wave depending on the type of the surface (soil, beton, vegetation, wood etc.). And each point intensity is derived from that wave. How it is derived I will not get into this now as I am thinking to write more comprehensive article about it. Now what we see in the picture is that ground and roof of the buildings has similar color, it might be because the material has similar physical properties. Also we can see that windows are distinguishible too which might happen because of the same reason as we mentioned. Overall I would say that the classification is good as we can distinguish different features of the objects in the scene.</p><p>I hope this was fun and useful. You can play around with model creation for better results or as mentioned above try to create a new features. You can also read some papers that are using Semantic3D data and see different approaches. I am also planning to write an article how we can create extra parameters for better classification results in context of point clouds.</p><p>[1]: T. Hackel, N. Savinov, L. Ladicky, J. Wegner, K. Schindler, and M. Pollefeys, “Semantic3D.net: A new large-scale point cloud classification benchmark,” ISPRS, 2017.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f4238610bbb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Open Source Point Cloud Datasets]]></title>
            <link>https://medium.com/@h.shaig93/open-source-point-clouddatasets-344642695183?source=rss-eabce712bd89------2</link>
            <guid isPermaLink="false">https://medium.com/p/344642695183</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[computer-graphics]]></category>
            <category><![CDATA[point-cloud]]></category>
            <category><![CDATA[computer-vision]]></category>
            <dc:creator><![CDATA[Shaig Hamzaliyev]]></dc:creator>
            <pubDate>Sun, 17 Dec 2023 23:08:41 GMT</pubDate>
            <atom:updated>2024-08-03T15:17:38.541Z</atom:updated>
            <content:encoded><![CDATA[<p>When I started to study and work with point clouds I was getting data from my professor mainly. Although it was quite nice I still wanted to see more different scenes. Then started my journey to look for free open source data.</p><p>Now I want to share my favourites with you guys, I will start with my faviurite one:</p><h3><strong>1. </strong>Semantic3D (www.semantic3d.net/).</h3><figure><img alt="Examplary scene from a Semantic3d (www.semantic3d.net/)" src="https://cdn-images-1.medium.com/max/1024/1*DhLzHnpYT1_9UrNlJ5jBqw.png" /><figcaption>An exemplary scene from Semantic3D (<a href="https://www.semantic3d.net/">www.semantic3d.net/</a>).</figcaption></figure><p>This data is meant for classification purposes. Every point in these files have been classified by professionals one by one. It is perfect for Machine Learning and Computer Vision enthusiasts. Thanks to that you can assest your classification results. You can visit the website or read the <a href="https://ethz.ch/content/dam/ethz/special-interest/baug/igp/photogrammetry-remote-sensing-dam/documents/pdf/Papers/Hackel-etal-cmrt2017.pdf">research paper</a> for more information.</p><h3><strong>2. OpenTopography</strong></h3><p>OpenTopography is another website where you can get free point clouds. I would say as a natural scientist this is very nice website to get a free data around the world.</p><figure><img alt="The available data around the world (portal.opentopography.org/datasets)." src="https://cdn-images-1.medium.com/max/1024/1*2veSBRjd0qQP9xsjnQnGWQ.png" /><figcaption>Available datasets on the map (<a href="https://portal.opentopography.org/datasets">portal.opentopography.org/datasets</a>).</figcaption></figure><p>You can look for different continents, countries and topographies based on your research interest.</p><h3><strong>3. NEON</strong></h3><p>NEON is one of the most useful sites for ecology lovers. In the amazing website you can find lots of different datasets including discrete return LiDAR or as you know simply point clouds (<a href="https://data.neonscience.org/data-products/DP1.30003.001">data.neonscience.org/data-products/DP1.30003.001</a>). Now the reason that I specifically mentioned the term discrete is you can also find fullwaveform data sets in here (which was my MSc thesis). NEON is abbreviation for National Ecological Observatory Network thus you will find mostly data from USA. Another nice thing is that you can get data for same areas in different dates thus you can work on cool time series project too if you are interested.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AxIY0ncZKxdTiuPd7gxZJQ.png" /><figcaption>NEON data sheet according to location and time (<a href="https://data.neonscience.org/data-products/DP1.30003.001">data.neonscience.org/data-products/DP1.30003.001</a>).</figcaption></figure><h3><strong>4. Oakland 3-D Point Cloud Dataset — CVPR 2009 subset</strong></h3><p>Here you can also find labeled point cloud data. You can read more about it in this <a href="https://www.cs.cmu.edu/~vmr/datasets/oakland_3d/cvpr09/doc/">link</a>. You can train your model and play with it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wKVyhdLlwrSy7vgt.png" /><figcaption>Oakland 3-D point cloud (<a href="https://www.cs.cmu.edu/~vmr/datasets/oakland_3d/cvpr09/doc/">https://www.cs.cmu.edu/~vmr/datasets/oakland_3d/cvpr09/doc/</a>)</figcaption></figure><p>I hope you will find these three websites quite useful and fun. See you in next article ;)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=344642695183" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Point Cloud Voxelization with Python (numpy & scipy)]]></title>
            <link>https://medium.com/@h.shaig93/voxelization-with-python-numpy-scipy-b7f013890da6?source=rss-eabce712bd89------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7f013890da6</guid>
            <category><![CDATA[remote-sensing]]></category>
            <category><![CDATA[voxel]]></category>
            <category><![CDATA[3d]]></category>
            <category><![CDATA[computer-graphics]]></category>
            <category><![CDATA[point-cloud]]></category>
            <dc:creator><![CDATA[Shaig Hamzaliyev]]></dc:creator>
            <pubDate>Sat, 05 Aug 2023 16:51:52 GMT</pubDate>
            <atom:updated>2024-02-03T11:59:03.310Z</atom:updated>
            <content:encoded><![CDATA[<p>This article shows how to voxelize point cloud data using only numpy and scipy to have more proper intuition. Here we will learn what voxelization is and what it is good for then we will dive into coding first with a synthetic data with reverse engineering style then we will apply it to a real-world data.</p><p>Voxelization is basically a discretization of continuous data such as point clouds. To be more explicit we divide 3D space into cubes and assign each cubes center point a value according to its neighbouring points. Now I can imagine that this sounded a bit confusing so I will visualize it by using some synthetic data in python.</p><h3>Voxel visualization for better understanding.</h3><p>First of all to get a better intuition we will visualize empty cubes in space by using some basic python libraries. First let’s import some necessary libraries:</p><pre>import numpy as np<br>import matplotlib.pyplot as plt<br>from mpl_toolkits.mplot3d import Axes3D<br>from itertools import product, combinations<br>from scipy.spatial import cKDTree</pre><p>Now to create empty cubes in space we will use matplotlibs voxel visualization tools which you can find it from this <a href="https://matplotlib.org/stable/gallery/mplot3d/voxels.html">link</a>. The code:</p><pre>nc = 1<br>one = np.ones((nc+1, nc+1, nc+1))<br>fig = plt.figure()<br>ax = fig.gca(projection=&#39;3d&#39;)<br>ax.voxels(one, alpha = 0.12, edgecolor=&quot;k&quot;, shade=True) # Voxel visualization<br>ax.set_title(&#39;Voxels&#39;)<br>plt.tight_layout()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*6toQzAmR51Bc59fcxtLYEA.png" /></figure><p>What we see in above image is we have 8 cubes which makes this <em>2X2X2</em> cubic space. Lets add voxel centers to our cubes:</p><pre>fig = plt.figure()<br>ax = fig.gca(projection=&#39;3d&#39;)<br>xc = np.arange(1, nc+2) - .5 <br>yc = np.arange(1, nc+2) - .5 <br>zc = np.arange(1, nc+2) - .5 <br>xc_, yc_, zc_ = np.meshgrid(xc, yc, zc)<br>ax.scatter(xc_.ravel(), yc_.ravel(), zc_.ravel(), marker = 10, c = &#39;r&#39;, s = 100) # Voxel Centers<br>ax.voxels(one, alpha = 0.12, edgecolor=&quot;k&quot;, shade=True) # Voxels<br>plt.tight_layout()<br>ax.set_title(&#39;Voxels and Voxel centers&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*WdCbvsb7odHB1_bqxa81pA.png" /></figure><p>Our voxel centers are the red triangles which are in the middle of cubes. This is discretized space however we still miss something. Yes, you remembered correct we need to have some synthetic point clouds inside our voxel space.</p><pre>x = np.random.randint((nc+1)*10, size = (nc+3, nc+3, nc+3))/10<br>y = np.random.randint((nc+1)*10, size = (nc+3, nc+3, nc+3))/10<br>z = np.random.randint((nc+1)*10, size = (nc+3, nc+3, nc+3))/10<br>fig = plt.figure()<br>ax = fig.gca(projection=&#39;3d&#39;)<br>ax.voxels(one, alpha = 0.12, edgecolor=&quot;k&quot;, shade=True)<br>ax.scatter(x.ravel(), y.ravel(), z.ravel(), c = &#39;k&#39;)<br>ax.scatter(xc_.ravel(), yc_.ravel(), zc_.ravel(), marker = 10, c = &#39;r&#39;, s = 100)<br>ax.set_title(&#39;Random points in Voxel space&#39;)<br>plt.tight_layout()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*T6NuERmHMuUQP1GQCArmWw.png" /></figure><p>Now we have points in our voxel space. Now the question is how one can decide what value each voxel needs to get? For that there are various interpolation techniques that can be used (which I am planning to write another article about). But here we will not go deep into this instead we will try a simple nearest neighbour interpolation. Nearest neighbour interpolation is basically an idea that the closest point (some specific black point) to the center of the voxel (red triangle) is going to be assigned as a voxel value. When we talk about the value that will be assigned it is <strong>not </strong>the <em>geometric</em> values of the black point but other values. In case of point clouds each point is derived from a laser beam thus other than geometric (<em>x, y, z</em>) values each point has also an intensity value. To be more explicit in the above example as we can see there is more than one point in each cube and each with its own intensity value and the voxel center will get the intensity value of the closest however it will keep its own geometric values/position.</p><p>Now let&#39;s do the nearest neighbour interpolation and visualize it to see how it looks:</p><pre>fig = plt.figure()<br>ax = fig.gca(projection=&#39;3d&#39;)<br>ax.voxels(one, alpha = 0.12, edgecolor=&quot;k&quot;, shade=True)<br>ax.scatter(x.ravel(), y.ravel(), z.ravel(), c = &#39;k&#39;)<br>ax.scatter(xc_.ravel(), yc_.ravel(), zc_.ravel(), marker = 10, c = &#39;r&#39;, s = 100)<br><br># create a KDTree datastructure for query<br>tree = cKDTree(np.c_[x.ravel(), y.ravel(), z.ravel()])<br># query of the one (k=1) closest point to each voxel center<br>dd, ii = tree.query(np.c_[xc_.ravel(), yc_.ravel(), zc_.ravel()], k=1)<br><br># to find corresponding values by indexing<br>x_close = x.ravel()[ii]<br>y_close = y.ravel()[ii]<br>z_close = z.ravel()[ii]<br><br># visualization<br>for i in range(len(ii)):<br>    ax.plot3D([xc_.ravel()[i], x.ravel()[ii[i]]], [yc_.ravel()[i], y.ravel()[ii[i]]], [zc_.ravel()[i], z.ravel()[ii[i]]], c=&#39;g&#39;)<br>ax.set_title(&#39;Voxels, Voxel centers and Closest points&#39;)<br>plt.tight_layout()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*ATe-qkka-FN5vDZxz_FceA.png" /></figure><p>Here we added green lines to show the distance from the closest point to voxel centers in each cube too for better intuition.</p><p>Untill now we kind of reverse engineered voxelization steps, means that we first divided a space found voxel centers and then added points. However in real world we first we have a data and then according to that input we divide the space. Also keep in mind that we still haven’t really divided the space ourselves.</p><h3>Real world example</h3><p>For real world example we will try to implement voxelization for a tree.</p><p>We will go again step by step and slow. First let’s see what a data looks like. I am using displaz and Cloud Compare for visualizing the point clouds:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/1*OQx6VrZpM-ku13srWPbA8w.png" /><figcaption>Tree Point CLoud from Heidelberg, by 3DGeo Research Group Heidelberg</figcaption></figure><p>We can see the tree now. To be able to voxelize this beautiful point cloud we need to upload it to the python environment first, for this we will be using laspy library to read the data:</p><pre>import laspy<br>import matplotlib.pyplot as plt<br>import numpy as np<br>from scipy.spatial import cKDTree</pre><p>after importing the necessary libraries lets see how laspy reads the data:</p><pre>filename = &quot;2021-06-18_Sequoia_VZ-400_t_sub_2cm.laz&quot;<br>las = laspy.read(filename)</pre><p>As simple as that. We already read the data, however las files generally contains more than you think lets right small code snippet to see what we have then you will understand what I meant:</p><pre>dimensions = las.header.point_format.dimensions<br>for dimension in dimensions:<br>    print(dimension.name)</pre><p>Output:</p><pre>X<br>Y<br>Z<br>intensity<br>return_number<br>number_of_returns<br>synthetic<br>key_point<br>withheld<br>overlap<br>scanner_channel<br>scan_direction_flag<br>edge_of_flight_line<br>classification<br>user_data<br>scan_angle<br>point_source_id<br>gps_time<br>Deviation<br>Reflectance<br>Amplitude</pre><p>As you can see we have a lot of information but for now we will be using <em>x, y, z</em> and <em>Amplitude</em>:</p><pre>points = las.xyz<br>amplitude = las.Amplitude</pre><p>now let’s do the voxelization:</p><pre>def voxel(ar, vwidth): # voxelization of the data<br>    &#39;&#39;&#39;<br><br>    Parameters<br>    ----------<br>    ar : numpy array<br>        2 dimensional array which represents 3D unstructured data.<br>    vwidth: integer or float<br>        width of the desired voxel<br>    Returns<br>    -------<br>    voxels - voxelized 3D array.<br><br>    &#39;&#39;&#39;<br>    x = ar[:, 0]<br>    y = ar[:, 1]<br>    z = ar[:, 2]<br>    a = ar[:, 3]<br>    xi = ((x-x.min())/vwidth).astype(&#39;int&#39;)<br>    yi = ((y-y.min())/vwidth).astype(&#39;int&#39;)<br>    zi = (((z-z.min()))/vwidth).astype(&#39;int&#39;)<br>    vcnt = np.zeros((xi.max()+1, yi.max()+1, zi.max()+1))<br>    vsum= np.zeros((xi.max()+1, yi.max()+1, zi.max()+1))<br>    for k in range(len(xi)):<br>        vcnt[xi[k], yi[k], zi[k]] += 1<br>        vsum[xi[k], yi[k], zi[k]] += a[k]<br>    vavg = vsum / vcnt<br>    vavg[vcnt &lt; 1] = np.nan<br>    vx = np.arange(x.min(), x.max(), vwidth) <br>    vy = np.arange(y.min(), y.max(), vwidth) <br>    vz = np.arange(z.min(), z.max(), vwidth)<br>    vxofs,vyofs,vzofs=np.meshgrid(vx, vy, vz, indexing=&#39;ij&#39;)<br>    voxels = np.stack((vxofs, vyofs, vzofs, vavg))<br>    return(voxels)<br><br># implement the method to our data<br>voxelized = voxel(points, .7)</pre><p>Now we got our voxel array which is not 2D anymore. What I mean is before the 3D data was represented in 2D array called points if you will check shape of this array it is (<em>n, 3</em>). This is a matrix which has n number rows and 3 columns however our new array voxelized has a shape of (<em>3, n, m, p</em>).</p><p>Now lets do nearest neighbour interpolation:</p><pre>def nnbh_intrp(ar, vxl, vwidth): # nearest neighbour interpolation<br>    &#39;&#39;&#39;<br><br>    Parameters<br>    ----------<br>    ar : numpy array<br>        2 dimensional array which represents 3D unstructured data <br>        (5th column is derivative of the pulse).<br>    vxl: voxel coordinates - 4D data (3, n, m, p)<br>    vwidth: voxel size<br>    Returns<br>    -------<br>    pnt_ofs - interpolated voxel values with coordinates<br><br>    &#39;&#39;&#39;<br>    x = ar[:, 0]<br>    y = ar[:, 1]<br>    z = ar[:, 2]<br>    a = ar[:, 3]<br>    tree = cKDTree(np.c_[x, y, z])<br>    dd, ii = tree.query(np.c_[vxl[0].ravel(), vxl[1].ravel(), vxl[2].ravel()], k=1)<br>    print(dd.shape, a.shape)<br>    a[ii[dd&gt;=vwidth/2]] = np.nan<br>    vann = a[ii].reshape(vxl[0].shape)    <br>    pnt_ofs = np.stack((vxl[0], vxl[1], vxl[2], vann))<br>    return(pnt_ofs)<br><br># implementation<br>nnbh = nnbh_intrp(np.c_[points, las.Amplitude], voxelized, .7)</pre><p>Now our new variable has an amplitude too. The shape of nnbh variable will be (<em>4, n, m, p</em>).</p><p>We already managed to voxelized our data now it is time to save it as a las file and visualize:</p><pre><br>def save(fn, voxelized_data, cmap=plt.cm.magma_r):<br>    voxel_flat = np.c_[voxelized_data[0].flatten(), voxelized_data[1].flatten(), voxelized_data[2].flatten()]<br>    amplitude_vxl = voxelized_data[3].flatten()<br>    v = np.array(amplitude_vxl[:], dtype=&#39;float&#39;)<br>    x, y, z = voxel_flat[:, 0][~np.isnan(v)], voxel_flat[:, 1][~np.isnan(v)], voxel_flat[:, 2][~np.isnan(v)]<br>    v = v[~np.isnan(v)]<br>    v -= np.nanmin(v)<br>    v /= np.nanmax(v)<br>    rgb = cmap(v)<br>    rgb = rgb[:, :3]<br>    rgb *= 65535<br>    rgb = rgb.astype(&#39;uint&#39;)<br>    las = laspy.create(point_format=2, file_version=&#39;1.2&#39;)<br>    las.header.scale = [0.001, 0.001, 0.001]<br>    las.header.offset = [x.min(), y.min(), z.min()]<br>    las.x = x<br>    las.y = y<br>    las.z = z<br>    las.red = rgb[:, 0]<br>    las.green = rgb[:, 1]<br>    las.blue = rgb[:, 2]<br>    las.write(fn)<br><br># implementation<br>save(&quot;voxelizedTree.las&quot;, nnbh)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*o52iWIkJtrXN2WcJzXpcRQ.png" /><figcaption>Voxelized tree model.</figcaption></figure><p>We managed to get the voxelized output and I would say that it looks beautiful. Lets check it on the original tree too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*XFmo7FyI8l-Ls74ck7jzXw.png" /><figcaption>Voxelized tree on the original tree from 3DGeo Research Group Heidelberg.</figcaption></figure><p>There it is! We managed to get a voxel model and visual with its original version really gives an intuition about the difference of the voxelized and original version.</p><p>Little note would be that although voxelizationis great way to subsample the pointclouds when you increase the size of the voxel the resolution goes down. In this case depending on what you are aiming voxel width needs to be optimal for the usecase.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7f013890da6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>