leomariga/pyRANSAC-3D

Getting Runtime error when fitting.

Opened this issue · 3 comments

Encountered the error when plane fitting:

/usr/local/lib/python3.8/dist-packages/pyransac3d/plane.py:59: RuntimeWarning: invalid value encountered in true_divide
vecC = vecC / np.linalg.norm(vecC)

Code Attached:

def plane_fit(pointList):
[X, Y, Z, I] = pointList
p_arr = np.empty((len(X), 3), float)
P_color_arr = np.empty((0, 3), int)

print('print len of full points')
print(len(X))
for i in range(len(X)):
    p_arr = np.append(p_arr, np.array([[X[i], Y[i], Z[i]]]), axis=0)
    P_color_arr = np.append(p_arr, np.array([[I[i], 0, 0]]), axis=0)

plane1 = pyrsc.Plane()

# Load saved point cloud and visualize it
pcd_load = o3d.geometry.PointCloud()
pcd_load.points = o3d.utility.Vector3dVector(p_arr)
pcd_load.colors = o3d.utility.Vector3dVector(P_color_arr)
print("pcd created")
# o3d.visualization.draw_geometries([pcd_load])
points = np.asarray(pcd_load.points)


best_eq, best_inliers = plane1.fit(points, 1)
X_new = []
Y_new = []
Z_new = []
I_new = []
print("Printing len of inliners")
print(len(best_inliers))
print("Equation:")
print(best_eq)
for i in range(len(p_arr)):
    if i in best_inliers:
        X_new.append(p_arr[i][0])
        Y_new.append(p_arr[i][1])
        Z_new.append(p_arr[i][2])
        I_new.append(1)


print(len(X_new))
return [X_new, Y_new, Z_new, I_new]

I also face this problem, have you solved this? Thanks a lot!

@River-mao Not yet, but working on it

This error occurs when the vecA and vecB vectors are parallel, resulting in a zero vectorC after the cross product. I fixed it by adding the following lines after vecC is computed, on line 57 of planes.py:

        (57.)       # if vectors A and B are pallel vector C will be a zero vector resulting in a divide by zero error in the next step
        (58.)       if np.any(vecC) == False:
        (59.)           continue

I will open a pull request with the fix.