np.split(x, [ ])用法

np.split(x, [3, 5, 6, 10])

split(ary, indices_or_sections, axis=0) :把一个数组从左到右按顺序切分 
#ary:要切分的数组 
#indices_or_sections:如果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置(左开右闭) 
#axis:沿着哪个维度进行切向,默认为0,横向切分。为1时,纵向切分

import numpy as np
x = np.arange(8)
y = np.split(x,[3,4,6,10])
print('y = ',y)
y1 = np.split(x,[3,5,6,10])
print('y1 = ',y1)
y2 = np.split(x,[3,5,6])
print('y2 = ',y2)
y3 = np.split(x,[3,5,6,7])
print('y3 = ',y3)

---------------------------------------

y =  [array([0, 1, 2]), array([3]), array([4, 5]), array([6, 7]), array([], dtype=int32)]

y1 =  [array([0, 1, 2]), array([3, 4]), array([5]), array([6, 7]), array([], dtype=int32)]
y2 =  [array([0, 1, 2]), array([3, 4]), array([5]), array([6, 7])]
y3 =  [array([0, 1, 2]), array([3, 4]), array([5]), array([6]), array([7])]

``` def create_coord(self): return o3d.geometry.TriangleMesh.create_coordinate_frame(size=1.0) def project_to_slice_plane(self, points, mode, param): """将三维点投影到切片平面,返回投影后的三维坐标和二维特征坐标""" if mode == 'x': projected = np.copy(points) projected[:, 0] = param # X坐标固定 features = projected[:, [1, 2]] # 取Y,Z作为二维特征 elif mode == 'y': projected = np.copy(points) projected[:, 1] = param # Y坐标固定 features = projected[:, [0, 2]] # 取X,Z作为二维特征 elif mode == 'polar': # 计算极坐标投影参数 ct, st = np.cos(param), np.sin(param) s = points[:, 0] * ct + points[:, 1] * st # 沿极角方向的投影 projected = np.column_stack(( points[:, 0] * ct ** 2 + points[:, 1] * ct * st, points[:, 0] * ct * st + points[:, 1] * st ** 2, points[:, 2] )) features = np.column_stack((s, points[:, 2])) # 取S,Z作为二维特征 return projected, features def fit_curve(self, features, mode): if len(features) < 2: return [] # 改进排序逻辑 sort_idx = np.argsort(features[:, 0]) sorted_features = features[sort_idx] # 改进分割逻辑 if len(sorted_features) < 2: return [] diffs = np.diff(sorted_features[:, 0]) split_index = np.argmax(diffs) + 1 if len(diffs) > 0 else 0 curves = [] for group in [sorted_features[:split_index], sorted_features[split_index:]]: if len(group) < 3: continue # 使用线性插值作为后备方案 try: f = interp1d(group[:, 0], group[:, 1], kind='linear', fill_value='extrapolate') x_new = np.linspace(group[:, 0].min(), group[:, 0].max(), 100) y_new = f(x_new) # 改进滤波参数 window_length = min(21, len(y_new)) if window_length > 3 and window_length % 2 == 1: y_smooth = savgol_filter(y_new, window_length, 2) else: y_smooth = y_new curves.append(np.column_stack((x_new, y_smooth))) except Exception as e: print(f"曲线拟合优化后仍失败: {str(e)}") return curves def calculate_curvature(self, curve): """计算曲线的曲率""" x = curve[:, 0] y = curve[:, 1] dx = np.gradient(x) dy = np.gradient(y) ddx = np.gradient(dx) ddy = np.gradient(dy) curvature = np.abs(ddx * dy - dx * ddy) / (dx**2 + dy**2)**(3/2) return curvature def split_curve_by_curvature(self, curve, threshold=0.05): """根据曲率分割曲线""" curvature = self.calculate_curvature(curve) diff_curvature = np.abs(np.diff(curvature)) split_indices = np.where(diff_curvature > threshold)[0] + 1 sub_curves = np.split(curve, split_indices) return sub_curves def calculate_length(self, curve): """计算曲线的长度""" x = curve[:, 0] y = curve[:, 1] dx = np.diff(x) dy = np.diff(y) ds = np.sqrt(dx**2 + dy**2) length = np.sum(ds) return length```这个分段的程度不够好,很多曲率变化的地方被并入同一曲线计算曲率
03-27
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值