I want to generate a 2D numpy array filled with tuples. Each square represents a pixel, which is related to another 2D coordinate with the tuple. 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 : 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 ? Thank you !
Do you really want tuples
, or just a 3d array? 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?
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. np.array([X,Y]).transpose([1,2,0])
should do the same as your vstack
.
Is this what you want?
mgrid
can do the interpolation with the j
parameter:
In [307]: Y,X=np.mgrid[0:100:6j,80:0:5j]
In [308]: np.array([X,Y])
np.linspace
is also handy. Look also at ogrid
and meshgrid
.
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.