简体   繁体   English

生成包含元组的2D numpy数组

[英]generate 2D numpy array containing tuples

I want to generate a 2D numpy array filled with tuples. 我想生成一个填充了元组的2D numpy数组。 Each square represents a pixel, which is related to another 2D coordinate with the tuple. 每个方块表示一个像素,该像素与具有元组的另一个2D坐标相关。 I only know a few couples pixel/tuple. 我只知道几对像素/元组。 So my array has to interpolate those points, and has to be somehow linear elsewhere. 所以我的数组必须插入这些点,并且必须在某种程度上是线性的。 I have begun with this : 我已经开始这样做了:

rows, cols : nb of rows and columns that the 2D array should have
maxx, maxy : maximum of the x and y real coordinates. Their range is [0:maxx] and [0:maxy]
interpolation = [((row1,col1),(x1,y1)),((row2,col2),(x2,y2))]
X = (rows-1-np.mgrid[0:rows,0:cols][0])/(rows-1)*maxx
Y = np.mgrid[0:rows,0:cols][1]/(cols-1)*maxy
return np.vstack(([X.T], [Y.T])).T

But there are no tuples in the grid, and the couples don't interpolate properly the coordinates. 但是网格中没有元组,并且耦合不会正确地插入坐标。 Actually the tuples are the centers of circles on a grid, like this one : 实际上元组是网格上的圆心,就像这样: opencv的校准网格 And I know the real coordinates of all circles. 而且我知道所有圈子的真实坐标。 My goal is to have a matrix with all the real coordinates of the pixels of an image, so as to make a 3d scanner :-) Does anyone have an idea please ? 我的目标是拥有一个矩阵,其中包含图像像素的所有实际坐标,以便制作一个3d扫描仪:-)有没有人有想法? Thank you ! 谢谢 !

Do you really want tuples , or just a 3d array? 你真的想要tuples ,还是只需一个3d数组? You can make a 2d array with 'tuples' as elements, but that's a structured array. 您可以使用'元组'作为元素制作一个二维数组,但这是一个结构化数组。 Looks more like you want an array that is (rows, cols, 2) in shape? 看起来更像你想要一个(rows, cols, 2)形状的数组?

Integer division may be biting you. 整数师可能会咬你。

In [282]: rows, cols = 5.,6.  # make float
In [283]: maxx, maxy = 80.,100.
In [284]: X = (rows-1-np.mgrid[0:rows,0:cols][0])/(rows-1)*maxx
In [285]: Y = np.mgrid[0:rows,0:cols][1]/(cols-1)*maxy
In [288]: np.vstack(([X.T],[Y.T]))  
Out[288]: 
array([[[  80.,   60.,   40.,   20.,    0.],
        [  80.,   60.,   40.,   20.,    0.],
        [  80.,   60.,   40.,   20.,    0.],
        [  80.,   60.,   40.,   20.,    0.],
        [  80.,   60.,   40.,   20.,    0.],
        [  80.,   60.,   40.,   20.,    0.]],

       [[   0.,    0.,    0.,    0.,    0.],
        [  20.,   20.,   20.,   20.,   20.],
        [  40.,   40.,   40.,   40.,   40.],
        [  60.,   60.,   60.,   60.,   60.],
        [  80.,   80.,   80.,   80.,   80.],
        [ 100.,  100.,  100.,  100.,  100.]]])

I left off the final .T because (2,6,5) displays more compactly. 我离开了决赛.T因为(2,6,5)显示更紧凑。 np.array([X,Y]).transpose([1,2,0]) should do the same as your vstack . np.array([X,Y]).transpose([1,2,0])应该和你的vstack

Is this what you want? 这是你想要的吗?

mgrid can do the interpolation with the j parameter: mgrid可以使用j参数进行插值:

In [307]: Y,X=np.mgrid[0:100:6j,80:0:5j]
In [308]: np.array([X,Y])

np.linspace is also handy. np.linspace也很方便。 Look also at ogrid and meshgrid . 再看看ogridmeshgrid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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