简体   繁体   中英

Python valueError using hstack() (ValueError: all the input array dimensions except for the concatenation axis must match exactly)

I got the following error:

Traceback (most recent call last):
File "/home/odroid/trackAndFollow/getPositions.py", line 34, in 
<module>
tfVeloToCamera = np.hstack((np.vstack((rmVeloToCamera, 
zero_array)),np.transpose(translationVector_veloToCamera)))
File "/usr/lib/python2.7/dist-packages/numpy/core/shape_base.py", line 
280, in hstack
return _nx.concatenate(arrs, 1)
ValueError: all the input array dimensions except for the 
concatenation axis must match exactly

Code:

rotationVector_veloToCamera = 
np.array([[[-1.77611954,0.30024612,0.76069987]]])
translationVector_veloToCamera = np.array([[ 
0.0146381,0.02553223,0.16231193]])

rmVeloToCamera,jac = cv2.Rodrigues(rotationVector_veloToCamera)
tfVeloToCamera = np.hstack((np.vstack((rmVeloToCamera, 
zero_array)),np.transpose(translationVector_veloToCamera)))

I read somewhere that the reason is the shape or dtype. The shape of the 2 variables inside the hstack is (4,3) (3,1) and both have dtype=float64 .

Any idea whats causing this problem?

If the shapes of the arrays you're trying to stack are (4, 3) and (3, 1) , then they don't have any matching array dimensions, as 4 != 3 and 3 != 1 . If you were to take the transpose of the first, giving dimensions (3, 4) and (3, 1) , they should stack.

np.hstack((np.zeros((4, 3)), np.zeros((3,1))))
Traceback (most recent call last):
...
ValueError: all the input array dimensions except for the concatenation axis must match exactly

np.hstack((np.zeros((4, 3)).T, np.zeros((3,1))))
Out[56]: 
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

As an aside, you might want to look at shifting over to using np.concatenate and specifying an axis, instead of np.vstack and np.hstack .

Ex:

np.concatenate((np.zeros((4, 3)), np.zeros((3,1)).T), axis=0)
Out[20]: 
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM