<?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 Jose Llorens on Medium]]></title>
        <description><![CDATA[Stories by Jose Llorens on Medium]]></description>
        <link>https://medium.com/@jose-llorens-ripolles?source=rss-2a5aff8667d6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UHwceHm6GDqxDsJDwykeDg.jpeg</url>
            <title>Stories by Jose Llorens on Medium</title>
            <link>https://medium.com/@jose-llorens-ripolles?source=rss-2a5aff8667d6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 05:04:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jose-llorens-ripolles/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[Stockpile volume with Open3D]]></title>
            <link>https://jose-llorens-ripolles.medium.com/stockpile-volume-with-open3d-fa9d32099b6f?source=rss-2a5aff8667d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa9d32099b6f</guid>
            <category><![CDATA[volume]]></category>
            <category><![CDATA[3d]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[stockpile]]></category>
            <category><![CDATA[open3d]]></category>
            <dc:creator><![CDATA[Jose Llorens]]></dc:creator>
            <pubDate>Sun, 25 Apr 2021 11:23:12 GMT</pubDate>
            <atom:updated>2021-05-11T14:09:21.538Z</atom:updated>
            <content:encoded><![CDATA[<h3>Stockpile volume computation with Open3D</h3><p>First of all, you’ll need a Point Cloud of the stockpile. I’m going to use a .ply of an IntelRealSense-captured small stockpile. However, any other imaging technique resulting in a Point Cloud would be equally valid.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/0*oIvS-FmQ6pvJV1He.jpg" /></figure><p>Check the code <a href="https://github.com/JoseLlorensRipolles/stockpile-volume-computation/">at my Github</a>.</p><h3><strong>1. Orienting the Point Cloud</strong></h3><p>Let’s open our Point Cloud and check where in the cartesian 3D space it is located. Open3D has out-of-the-box tools for Point Cloud visualization. We will also add axes to our visualization so that we can locate the stockpile:</p><pre>pcd = o3d.io.read_point_cloud(&quot;data/stockpile.ply&quot;)<br>axes = o3d.geometry.TriangleMesh.create_coordinate_frame()<br>o3d.visualization.draw_geometries([pcd, axes])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*eaoOxZzYimdfFu9qCkzvzg.png" /></figure><p>We can see our stockpile and the floor. We can also see that it is unoriented with respect to the axes. In order to compute the volume, we need to make the floor parallel to the XY plane and to translate it to the origin. To do so we need to segment the floor. Lucky again, Open3D has us covered:</p><pre>plane_model, inliers = pcd.segment_plane(distance_threshold=0.01,<br>                                         ransac_n=3,<br>                                         num_iterations=10000)</pre><pre>[a, b, c, d] = plane_model<br>plane_pcd = pcd.select_by_index(inliers)<br>plane_pcd.paint_uniform_color([1.0, 0, 0])<br>stockpile_pcd = pcd.select_by_index(inliers, invert=True)<br>stockpile_pcd.paint_uniform_color([0, 0, 1.0])</pre><pre>o3d.visualization.draw_geometries([plane_pcd, stockpile_pcd, axes])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*puIiP8xu6g9JLVLG6p0dgQ.png" /></figure><p>Now that we have segmented the floor plane, we can use a few <em>mathemagics </em>to bring the floor to the XY plane by rotating and translating the Point Cloud:</p><pre>plane_pcd = plane_pcd.translate((0,0,d/c))<br>stockpile_pcd = stockpile_pcd.translate((0,0,d/c))</pre><pre>cos_theta = c / math.sqrt(a**2 + b**2 + c**2)<br>sin_theta = math.sqrt((a**2+b**2)/(a**2 + b**2 + c**2))<br>u_1 = b / math.sqrt(a**2 + b**2 )<br>u_2 = -a / math.sqrt(a**2 + b**2)</pre><pre>rotation_matrix = np.array([[cos_theta + u_1**2 * (1-cos_theta), u_1*u_2*(1-cos_theta), u_2*sin_theta],<br>                            [u_1*u_2*(1-cos_theta), cos_theta + u_2**2*(1- cos_theta), -u_1*sin_theta],<br>                            [-u_2*sin_theta, u_1*sin_theta, cos_theta]])</pre><pre>plane_pcd.rotate(rotation_matrix)<br>stockpile_pcd.rotate(rotation_matrix)<br>o3d.visualization.draw_geometries([plane_pcd, stockpile_pcd, axes])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0nlilCa9xm0E8S3OtncpCw.png" /></figure><p>We can discard the floor, as we are only concerned about the stockpile.</p><pre>o3d.visualization.draw_geometries([stockpile_pcd])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nrrX_jDNMSU9PawkTe2xOQ.png" /></figure><p>As we can see, we have some wrongly segmented points, especially in the border of the 3D image. We will need to remove these outliers. You know where this is going, Open3D has a function just for this:</p><pre>cl, ind = stockpile_pcd.remove_statistical_outlier(nb_neighbors=30,<br>                                                    std_ratio=2.0)<br>stockpile_pcd = stockpile_pcd.select_by_index(ind)<br>o3d.visualization.draw_geometries([stockpile_pcd])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E0pCOI82UmZqhn-SeXwINw.png" /></figure><p>Okay, we finally have our stockpile Point Cloud segmented, and correctly oriented. We can now jump to the volume computation part. Yay!</p><h3>2. Computing the volume</h3><p><strong>We can not compute the volume from a Point Cloud, we need a mesh. </strong>To get from Point Cloud to a mesh we can use surface reconstruction algorithms. Surface reconstruction is an <strong>ill-posed problem, </strong>meaning there is no perfect solution and algorithms are based on heuristics. However, we can avoid these algorithms by exploiting one special characteristic of a stockpile (or any terrain really), which is that the surface is bound by the XY plane. No 2 points will have the same x and y, and looking to the surface from above to the XY plane we will see a “plain ”surface, which is to say that if at any point we cross the XY plane perpendicularly, we will only cross our surface <strong>once.</strong></p><p>We will build the triangularization for the surface reconstruction in the 2D space. We will project out PCD to the XY plane by removing the Z value. We will get the triangulation of our 2D points with the <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.Delaunay.html">Delaunay</a> algorithm. Then we will keep the triangles outputted by Delaunay but we will use the 3D vertices instead of the 2D ones. This approach is sometimes called “Delaunay 2.5D”</p><p>This may sound convoluted, but it is easy to see in the code:</p><pre>downpdc = stockpile_pcd.voxel_down_sample(voxel_size=0.05)<br>xyz = np.asarray(downpdc.points)<br>xy_catalog = []<br>for point in xyz:<br>    xy_catalog.append([point[0], point[1]])<br>tri = Delaunay(np.array(xy_catalog)</pre><p><em>tri </em>will give us the indexes of the triangles. In the 2D space this looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*pe8oEzzUL7vw3gslNsGlbA.png" /></figure><p>Using the triangles with the 3D points will give us the surface of our stockpile:</p><pre>surface = o3d.geometry.TriangleMesh()<br>surface.vertices = o3d.utility.Vector3dVector(xyz)<br>surface.triangles = o3d.utility.Vector3iVector(tri.simplices)<br>o3d.visualization.draw_geometries([surface], mesh_show_wireframe=True)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/1*EKrmbu9J9XnJJhVjhZPNTw.gif" /></figure><p>Once we have the surface, we are ready to get the volume. Different approaches can be followed. I will compute the volume of each triangle of the surface to the XY plane (height 0). Then just add the volumes of all the triangles.</p><p>Triangles in Open3D meshes define the indexes of the vertices, not the vertices, so we need to translate the triangles so that their values are actually 3D points and not indices to the vertices list:</p><pre>def get_triangles_vertices(triangles, vertices):<br>    triangles_vertices = []<br>    for triangle in triangles:<br>        new_triangles_vertices = [vertices[triangle[0]], vertices[triangle[1]], vertices[triangle[2]]]<br>        triangles_vertices.append(new_triangles_vertices)<br>    return np.array(triangles_vertices)</pre><p>Then we will need a function to compute the volume under each triangle. Following <a href="https://www.mathpages.com/home/kmath393.htm">the article</a> of the <em>Bourbakian </em>mathematician Kevin Brown, we can define such function as:</p><pre>def volume_under_triangle(triangle):<br>    p1, p2, p3 = triangle<br>    x1, y1, z1 = p1<br>    x2, y2, z2 = p2<br>    x3, y3, z3 = p3<br>    return abs((z1+z2+z3)*(x1*y2-x2*y1+x2*y3-x3*y2+x3*y1-x1*y3)/6)</pre><p>Then, getting the volume of our stockpile is as easy as adding the volumes of all the triangles:</p><pre>volume = reduce(lambda a, b:  a + volume_under_triangle(b), get_triangles_vertices(surface.triangles, surface.vertices), 0)</pre><pre>print(f&quot;The volume of the stockpile is: {round(volume, 4)} m3&quot;)</pre><p>I hope that you enjoyed this article 😃. Feel free to contact me if any point is not clear enough!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa9d32099b6f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>