数据处理中的正则表达

最近在处理激光雷达点云数据时,点云数据在csv文件中长这个样子:

如果直接用Python处理csv非常方便。通过Pandas很容易处理:

import numpy as np
import pandas as pd
import open3d as o3d


pcd = o3d.geometry.PointCloud()    # pcd类型的数据。

with open("2.csv", encoding="utf-8") as f:
    data = pd.read_csv(f, header=None).values.tolist()
    print(data[:5])
    np_points = np.array(data)[:, 1:4]
    print(np_points.shape)
    pcd.points = o3d.utility.Vector3dVector(np_points)
    o3d.visualization.draw_geometries([pcd])
    # o3d.io.write_point_cloud('E:/project/3.ply', pcd)

通过python下pandas处理效率高,速度快,简单操作。

但是我需要研究学习通过halcon中正则表达实现对csv文件的正则匹配,所以需要看到halcon代码中的每个变量的变换情况,同时好好学习一下正则表达。

在halcon代码中:通过文件读取之后输出出来的是tuple的字符数组类型列表构成的向量VecOutLine。把向量转为tuple元组。如变量监控中的P。生成的字符串。这个字符串就需要我们来正则处理来提取数字,以构成X、Y、Z。

dev_update_off ()

Filename := './0.csv' // 点云数据的名称,.txt、.csv、.asc等格式的都可以

NumColumns := 5 //如果点云的每行数据有3个数字就写3 (只有xyz 的数据)

count_seconds (Then)
open_file (Filename, 'input', FileHandle)

VecOutLine.clear()
repeat
    fread_line (FileHandle, VecOutLine.at(VecOutLine.length()), IsEOF)
until (IsEOF)
convert_vector_to_tuple (VecOutLine, P)
S := P[354540]
stop()

P1 := split(P, '\n')
P2 := split(P1, ',')

P3 := number(regexp_replace(P2,'^\\s*(.+?)\\s*\n*$', '$1'))

// tuple_number负责字符转数字,tuple_string负责数字转字符。
// tuple_number (P2, P3)

// P4 := number(P2)
// convert_vector_to_tuple({P2},P4)


IndexIsString := find(type_elem(P3),H_TYPE_STRING)
if (IndexIsString > -1)
     throw ('could not convert "' + P3[IndexIsString] + '" to a number')
endif

X := P3[[1:NumColumns:|P3|-1]]
Y := P3[[2:NumColumns:|P3|-1]]
Z := P3[[3:NumColumns:|P3|-1]]
stop()

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值