numpy 如何利用直方图测量噪声箱盒的内部尺寸

llycmphe  于 2024-01-08  发布在  其他
关注(0)|答案(1)|浏览(100)

我有一个bin框,其中所有方向的方向都被认为是零,我想使用x,y和z的直方图来测量框的内部尺寸。


的数据



x,y内部尺寸是沿峰之间的内部(几乎)平坦直方图区域的沿着距离,而z轴上是最低平坦点和最大z之间的距离。
你能告诉我如何测量垃圾箱的内部尺寸吗?谢谢。
这里是N点坐标X,Y,Z的形状(N,3)的npy file

import numpy as np
import matplotlib.pyplot as plt

bin_pts = np.load('bin_box.npy')

axes = plt.subplots(1, 3, figsize=(12, 4))[1]
axes[0].hist(bin_pts[:, 0], bins=200, edgecolor='red', alpha=0.5)
axes[0].set_title('Histogram X')
axes[0].set_xlabel('Value')
axes[0].set_ylabel('Frequency')
axes[1].hist(bin_pts[:, 1], bins=200, edgecolor='green', alpha=0.5)
axes[1].set_title('Histogram Y')
axes[1].set_xlabel('Value')
axes[1].set_ylabel('Frequency')
axes[2].hist(bin_pts[:, 2], bins=200, edgecolor='blue', alpha=0.5)
axes[2].set_title('Histogram Z')
axes[2].set_xlabel('Value')
axes[2].set_ylabel('Frequency')

plt.tight_layout()
plt.show()

字符串

xlpyo6sf

xlpyo6sf1#

import numpy as np
import matplotlib.pyplot as plt

bin_pts = np.load('bin_box.npy')

def largest_component(arr_1d, visu=True):
    hist, vals = np.histogram(arr_1d, bins=500)
    original_hist = deepcopy(hist)
    mean_hist = np.mean(hist)
    # Boolean Histogram
    hist[hist<=mean_hist]=1
    # Remove Peaks
    hist[hist>mean_hist]=0
    # Zero Padding
    binary_1d_array_ = np.insert(hist, 0, 0)
    binary_1d_array_ = np.append(binary_1d_array_, 0)
    # Difference
    diff = np.diff(binary_1d_array_)

    # Find the start and end indices of each consecutive sequence of ones
    start_indices = np.where(diff == 1)[0]
    end_indices = np.where(diff == -1)[0] - 1

    # Find the index of the largest consecutive sequence
    largest_index = np.argmax(end_indices - start_indices)
    if(visu):
        plt.plot(original_hist)
        plt.axhline(y=mean_hist, color='g', linestyle='-')
        plt.axvline(x=start_indices[largest_index], color='r', linestyle='-')
        plt.axvline(x=end_indices[largest_index], color='r', linestyle='-')
        plt.show()
    return vals[start_indices[largest_index]], vals[end_indices[largest_index]]

X = bin_pts[:, 0]
Y = bin_pts[:, 1]
Z = bin_pts[:, 2]
        
x_s_val, x_end_val = self.largest_component(X)
y_s_val, y_end_val = self.largest_component(Y)
z_s_val, _ = self.largest_component(Z)
z_end_val = np.max(Z)

字符串

相关问题