Animation of image matrices in python - python

I have a list of (two dimensional) matrices which I want to combine into an animation. The matrices are integer valued with a specified lower and upper bound (in my case from zero to six). I would now like to build an animation where each of the frames consists of the contents of the matrix.
Now, I have managed to get this working if each frame is merely showing the numeric values via matplotlib's built-in matplotlib.pyplot.matshow. Namely, I defined an update function
def update_frames(frame):
animation_image.set_array(matrices[frame])
return animation_image,
where matrices contains the list of integer valued matrices I want to animate. Then, I could pass this into the animation module via
animation_image = ax.imshow(matrices[0])
ani1 = animation.FuncAnimation(fig, update_frames, frames=len(matrices), blit=True)
to obtain the desired result.
I now, however, want to take this a step further by replacing each square on the grid with a specific image, whereas all the images are resized to be of a fixed square shape. For example, at each frame I want to draw a grid of images where the image for each grid cell depends on the integer in the matrix.
Here is a sample animation that I get when using the code snippet above:
I do not have finalized versions of the pictures I would like to show up in the grid yet, but I was thinking of textures that resemble these:
Optimally, I would then end up with grids looking like this (aside from the different placement of objects on the grid as compared to the animation above).
Any help would be greatly appreciated!

Related

Creating Grid Based grid over race track

Given an 2 arrays of (x,y) points (numpy arrays), for a racetrack representing the inner and outer borders of a racetrack, I want to plot these points onto a grid system. There is a problem however, with these points, you would normally draw a "line" between them but that cannot exactly happen with a grid system.
The inner track looks like:
[[0.83937341 2.87564301]
[0.85329181 2.74359298]
[0.8711707 2.61805296]
[0.89493519 2.49186611]
[0.92430413 2.36440611]
[0.95832813 2.2375989 ]
[0.99367839 2.12898302]
[1.03462696 2.02958798]
[1.08152199 1.93906105]
[1.13470805 1.85674906]
[1.17767704 1.80507398]
[1.21820199 1.77083302]
...
As you can see, the points are very fine, 0.02 meters makes all the difference, so in order to scale this to a grid to use, I figured that I would need to muliply each of these by like 1000 maybe, plot that on the grid, then figure out which sqaures of the grid to fill in to connect the points (maybe using a*?)
I tried using pygame, and even visualizing the grid, but when I tried to use more than 500 rows, the program crashed. I don't need to necessary visualize the program, I just want it to meet the specifications.

How to create a curved line vector plot of a triangle in Python?

Question
Suppose one has 3 random coordinates with 3 random functions that describe the continuous lines between them*, how would one create a vector plot in Python that allows for smooth lines after infinite zooming in?
Example
The functions should be rotated and translated from their specification to map onto the edge/line in the geometry. For example, one curved line may be specified as -x(x-5)=0 which describes the line from (x,y) coordinates:(2,6) to (5,2) (which has length 5). Another curved line from (x,y) coordinates:(2,2) to (2,6) may be specified as sin(x/4*pi)=0. One can assume all bulges point outward (of the triangle in this case).
Approach
I can perform a translation and rotation of the respective functions to the lines of the coordinates, and then save the plt as a .eps or .pdf, however before doing that, I thought it would be wise to ask how these functions are represented and how these plots are generated, as I expect the dpi setting may simply turn it into a (very) high resolution plot, instead of something that still provides smooth lines after infinite scrolling.
Doubt
I can imagine using a sinusoid does not allow for infinite smooth scrolling as they may be stored numerically. If the representation is finite for sinusoids but analytical/symbolic for polynomials, I would be happy to constraint this question to polynomials only to get smooth infinitely scrollable images (like fractals).

How to automatically resize a plot on the important part, not showing the white parts?

I am currently trying to plot figures like this :
where i'm generating some random polytope located inside [-1,1]x[-1,1], then applying some optimization algorithms, and then plotting everything.
The problem is that because the polytopes are randomly generated, they can be very small or not centered, and it would be therefore convenient if i could remove the blank part on the sides.
I know it's possible to do it when saving a plot with something like plt.savefig('image.png', bbox_inches='tight'), but i would like to display it directly without white spaces (it's in a jupyter notebook so it would be more convenient).
I'm using a meshgrid to plot the data, and i have a 'None' at every blank pixel. I guess it could be possible to find some algorithm which finds the smallest square enclosing my polytope but i don't really want to go this way.
Do you have any ideas on how to do it using matplotlib ?

Calculating and Plotting 2nd moment of image

I am trying to plot the 2nd moments onto a image file (the image file is a numpy array for brightness distribution). I have a rough understanding that 2nd moment is sort of like moment of inertia (Ixx,Iyy) which is a tensor but I am not too sure how to calculate it and how it would translate into two intersecting lines with the centroid at its intersection. I tried using scipy.stats.mstats.moment but I am unsure what to put as axis if I just want two 2nd moments that intersects at centroid.
Also it returns an array but I am not exactly sure what the values in the array signify, and how that relate to what I am going to plot (because the scatter method in the plotting module takes in at least 2 corresponding values in order to be plotted) ?
Thank you.

Render a mayavi scene with a large pipeline faster

I am using mayavi.mlab to display 3D data extracted from images. The data is as follows:
3D camera parameters as 3 lines in the x, y, x direction around the camera center, usually for about 20 cameras using mlab.plot3d().
3D coloured points in space for about 4000 points using mlab.points3d().
For (1) I have a function to draw each line for each camera seperately. If I am correct, all these lines are added to the mayavi pipeline for the current scene. Upon mlab.show() the scene takes about 10 seconds to render all these lines.
For (2) I couldn't find a way to plot all the points at once with each point a different color, so at the moment I iterate with mlab.points3d(x,y,z, color = color). I have newer waited for this routine to finish as it takes to long. If I plot all the points at once with the same color, it takes about 2 seconds.
I already tried to start my script with fig.scene.disable_render = True and resetting fig.scene.disable_render = False before displaying the scene with mlab.show().
How can I display my data with mayavi within a reasonable waiting time?
The general principle is that vtk objects have a lot of overhead, and so you for rendering performance you want to pack as many things into one object as possible. When you call mlab convenience functions like points3d it creates a new vtk object to handle that data. Thus iterating and creating thousands of single points as vtk objects is a very bad idea.
The trick of temporarily disabling the rendering as in that other question -- the "right" way to do it is to have one VTK object that holds all of the different points.
To set the different points as different colors, give scalar values to the vtk object.
x,y,z=np.random.random((3,100))
some_data=mlab.points3d(x,y,z,colormap='cool')
some_data.mlab_source.dataset.point_data.scalars=np.random.random((100,))
This only works if you can adequately represent the color values you need in a colormap. This is easy if you need a small finite number of colors or a small finite number of simple colormaps, but very difficult if you need completely arbitrary colors.

Categories

Resources