0

I have two Python scripts with nearly identical code. One of them is working, the other fails with the error message

Traceback (most recent call last):
  File "np.py", line 24, in <module>
    sec = pc[np.dot(pc1, vp) < tol]   # select points close to section
IndexError: boolean index did not match indexed array along dimension 1; dimension is 3 but corresponding boolean dimension is 1

The code which runs without error:

import numpy as np

a = np.loadtxt('lidar.txt', delimiter=',')
tol = 3
vp = np.array([7.32793492e-01, -6.80451099e-01, 3.08811740e+06])
a1 = np.hstack((a[:,0:2], np.full((a.shape[0], 1), 1)))
#sel = np.absolute(np.dot(a1, vp)) < tol
#sec = a[sel]
sec = a[np.absolute(np.dot(a1, vp)) < tol]
print(sec)

And the other which fails:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" filter point on a vertical section
    command line parameters: input_file, x1, y1, x2, y2, tolerance
    vertical plain is defined by (x1,y1) and (x2,y2)
"""
import sys
import numpy as np
from math import hypot

x1 = 548750.0
y1 = 5129300.0
x2 = 548425.0
y2 = 5128950.0
tol = 3.0
# set up equation for vertical plain vp[0] * x + vp[1] * y + vp[2] = 0
vp = np.zeros((3, 1))
vp[0] = y1 - y2
vp[1] = x2 - x1
vp[2] = x1 * y2 - x2 * y1
vp = vp / hypot(vp[0], vp[1])               # normalize
pc = np.loadtxt('lidar.txt', delimiter=',') # load point cloud from text file
pc1 = np.hstack((pc[:,0:2], np.full((pc.shape[0], 1), 1)))  # homegenous coords
sec = pc[np.dot(pc1, vp) < tol]   # select points close to section
for i in range(sec.shape[0]):               # print out result
    print("{:.3f} {:.3f} {:.3f}".format(pc[i][0], pc[i][1], pc[i][2]))

I can't find the difference between the two solutions. I work on Ubuntu 20.04 with Python 3.8.5 and NumPy 1.17.4 What is wrong in my second code?

The lidar.txt data file is available on github: https://github.com/OSGeoLabBp/tutorials/blob/master/english/data_processing/lessons/code/lidar.txt

1 Answer 1

2

It looks like your issue is with the shape of array vp.

>>> vp = np.array([7.32793492e-01, -6.80451099e-01, 3.08811740e+06])
>>> vp.shape
(3,)
>>> np.zeros((3, 1)).shape
(3, 1)

Remove the “1” from your np.zeros call and it should work:

>>> np.zeros((3,)).shape
(3,)
>>> np.zeros((3,))
array([ 0.,  0.,  0.])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.