使用Laspy模块在Python中使用Houdini自定义LiDAR导入器

时间:2020-07-11 10:36:04

标签: python classification lidar houdini

我正在尝试使用Python为Houdini构建自定义LiDAR导入器。 到目前为止,Laspy模块(https://pypi.org/project/laspy)可以通过在Houdini中读取和写入*。* las文件并过滤分类来很好地完成工作。

但是我必须读写*。* las文件并再次导入,而不是立即获得Houdini内部的积分。

现在,我想知道是否可以获取LiDAR点的xyz位置,以将其作为Houdini内部的点。

我试图在Laspy手册中找到有用的信息,但找不到任何示例或函数。

我用*。* csv文件进行了类似的操作,该文件具有xyz位置以构建一个简单的GPS读取器,以将位置输出为Houdini中的点(带有csv模块)。

我在屏幕快照中附加了原始的.las(灰色)和经过过滤的output.las(红色屋顶)以及Laspy手册中的脚本示例。

也许不是Laspy,而是更优雅的解决方案? 我在Houdini中使用Python 3,但2.7也可以。

更新,从这里回答几乎是完美的 https://forums.odforce.net/topic/46475-custom-lidar-importer-with-python/?tab=comments#comment-217104

from laspy.file import File
import numpy as np

node = hou.pwd()
geo = node.geometry()

file_path = geo.attribValue("file_path")
inFile = File(file_path, mode='r')

# --- load point position
coords = np.vstack((inFile.x, inFile.y, inFile.z)).transpose()
scale = np.array(inFile.header.scale)
offset = np.array(inFile.header.offset) # there is no offset in simple.las example from laspy library
# offset = np.array([1000, 20000, 100000])  # just for testing that offset works

# geo.setPointFloatAttribValues("P", np.concatenate(coords))    # same as Lidar Import SOP - seems that it ignores scale (and offset?)
geo.setPointFloatAttribValues("P", np.concatenate(coords*scale+offset))

# --- load color
color = np.vstack((inFile.red, inFile.green, inFile.blue)).transpose()
geo.addAttrib(hou.attribType.Point, "Cd", (1.0,1.0,1.0), False, False)  # add color atttribute
geo.setPointFloatAttribValues("Cd", np.concatenate(color / 255.0)) # transform from 1-255 to 0.0-1.0 range)

唯一无法解决的问题是inFile.classifications == x 那让胡迪尼崩溃了。

1 个答案:

答案 0 :(得分:0)

Odforce的Petr完成了LiDAR Importer。 https://forums.odforce.net/topic/46475-custom-lidar-importer-with-python/

要加载和阅读要点:

from laspy.file import File
node = hou.pwd()
geo = node.geometry()
geo.createPoint()   # dummy point for point generate

classification = hou.evalParm("classification")
file_path = hou.evalParm("lidar_file")
inFile = File(file_path, mode='r')
# store how many points lidar attribute has
geo.addAttrib(hou.attribType.Global, "nb_of_points", 0, False, False)
geo.setGlobalAttribValue("nb_of_points", len(inFile.points[inFile.Classification == classification]))

# store file path
geo.addAttrib(hou.attribType.Global, "file_path", "", False, False)
geo.setGlobalAttribValue("file_path", file_path)

# store classification
geo.addAttrib(hou.attribType.Global, "classification", 0, False, False)
geo.setGlobalAttribValue("classification", classification)

# find availible information
pointformat = inFile.point_format
for spec in inFile.point_format:
    print(spec.name)

并加载属性:

from laspy.file import File
import numpy as np

node = hou.pwd()
geo = node.geometry()

file_path = geo.attribValue("file_path")
inFile = File(file_path, mode='r')
classification = geo.attribValue("classification")

selection = inFile.Classification == classification
points = inFile.points[selection]



# --- load point position
coords = np.vstack((inFile.x, inFile.y, inFile.z)).transpose()
scale = np.array(inFile.header.scale)
offset = np.array(inFile.header.offset) # there is no offset in simple.las example from laspy library
# offset = np.array([1000, 20000, 100000])  # just for testing that offset works

# geo.setPointFloatAttribValues("P", np.concatenate(coords))    # same as Lidar Import SOP - seems that it ignores scale (and offset?)
geo.setPointFloatAttribValues("P", np.concatenate(coords[selection]*scale+offset))

# --- load color
missing_color = ["red", "green", "blue"]
for spec in inFile.point_format:
    if spec.name in missing_color:
        missing_color.remove(spec.name)

if not missing_color:       
    color = np.vstack((inFile.red, inFile.green, inFile.blue)).transpose()
    geo.addAttrib(hou.attribType.Point, "Cd", (1.0,1.0,1.0), False, False)  # add color atttribute
    geo.setPointFloatAttribValues("Cd", np.concatenate(color[selection] / 255.0)) # transform from 1-255 to 0.0-1.0 range)

# --- load intensity
geo.addAttrib(hou.attribType.Point, "intensity", 0.0, False, False)  # add intensity atttribute
geo.setPointFloatAttribValues("intensity", inFile.intensity[selection] / 512.0) # transform from 1-255 to 0.0-1.0 range)