How can I place 2D .tif images in a z-stack using numpy and matplotlib? - arrays

I have microscopy data in a .tif file that I can view in ImageJ. This tif file contains slices in the z plane that are 2 microns apart. ImageJ has the functionality to produce a stack of these images and allow the user to scroll through the z plane. the user can also rotate the image and peer through the slices - imagine looking at sheets of paper on a table, and going eye level so you see all the sheets stacked onto each other. This is what I am trying to reproduce in Python.
There are 15 different slices in this .tif file. I have so far managed to create an np array these slices. The shape of my array is currently (1024,1024,15). I have then tried to use np.concatenate and set axis to 2.:
x = np.concatenate([images_array[i] for i in range(15)], axis=2)
However, the error message thrown is 'axis 2 is out of bounds for array of dimension 2'. The problem i"m running into is that from np.shape(), it seems like I have a 3D array, so why can't I stack it?
Thanks

Related

How to mark some grid points on netcdf map?

I can make 2D dimensional netcdf maps of some quantity. I open it in panoply and there is color map of that quantity. But I cannot visualize some boolean value.
Can I somehow mark particular grid points with some symbol on the map (it can be diamond, square, triangle... whatever), is there a way how to do it in Fortran90? I accept also python related help.
Again: I mean there would be color map (from real values) (which I can do) and at the same time some values will have e. g. triangle on it.
If I understand the question correctly, then you can easily do that with Python and using some plotting library (e.g Matplotlib). With Fortran it is extremely tricky as it does not natively support plotting in my mind.
Basically with Python you just have to :
read the wanted variables (coordinates and the field itself)
make the map of the field i.e make the plot
find the locations you want to highlight and just add those locations to the plot

Iterating through sequence of Images in tensorflow

I have a database with images numbered from 1 till 7500.
I need to feed these images into my model in tensorflow in the following manner:
grab the 1st 100 images, that is, from 1 till 100, then grab another 100 images such that the next batch is from 1 till 101. As well, the following batch is from 2 till 102 and so on...
The purpose for using the following behavior is that I am using a recurrent neural network where the images to be fed are faces detected from a video. Therefore, I need to feed sequences of images such that these images are directly following one another.
Any help is much appreciated!!
I don't have a perfect solution for your question, but this one might help you.
I'm assuming that you are using tfrecords to build inputs because if not, feeding numpy to model doesn't meet this problem.
supporing your image files are list like this ["image_0", ..., "imgae_N"], you can build i-th tf.example with ["image_i", ..., "image_i+100"] as a feature.
After dequeuing, you get a tensor contains the names of there images, and then unstack them, read image content from there image names with tf.read_file and decode them to images with tf.image.decode_image, and concat them back into one tensor and send it to your model as input.

Big SCNGeometry SceneKit for iOS

I am working on a cocoa/iOS projet.
I have a common swift class which manage a Scenekit scene.
I want to draw a big terrain (about 5000x5000 points).
I have 2 triangles per 4 points. I have created a scngeometry object for the whole terrain (is it a good thing ?)
I decided to store those points in a 6-Float structure (x,y,z and r,g,b). I tried to create an empty array or to allocate a big array at the begining : i got the same issue.
I work with Int datatype for indices array.
The project works fine on Cocoa but i get memory errors on iOS. I think this is because of the need to have a big and contigous array for vertex.
I tried to create several chunks of geometry objects but scene kit does not like if we erase a previous buffer.
What is the best practice in this case ?
Is there a way to store vertex on the mass storage instead of memory arrays/buffers ?
Thanks
So...twice as many terrain points as there are pixels on a shiny new 5K display? That's a huge amount of memory to be using at once on iOS. And you won't be able to see that resolution on an iOS device.
So how about:
Break your 25 million pixel terrain into smaller tiles, each in its own SCNNode. Loop through the tiles, create one SCNNode, throw away the 6-Float array for that tile and move to the next.
Use SCNLevelOfDetail to produce much simpler versions of those nodes, for display when they're very far away.
Do the construction work on OS X. Archive your scene (NSSecureCoding). Bundle that scene into the iOS app.
Consider using reference nodes in your main SCNScene, and archive each tile as a separate SCNScene file.
Hopefully you're already using triangle strips, not triangles, to build your geometry.

How do load an image into a 2D array element?

I'm currently doing my A-Level Computing project for which I am making my own version of the classic game Space Invaders.
To create the wave of space invaders I want to use a 2D array of images, where the images are loaded from a disk and then displayed on the form but I am unsure of how load the images into the array and then display the array on a form.
The current arrays are:
ImagePaths:array [1..3] of string =('SpaceInvader1.jpg', 'SpaceInvader2.jpg', 'SpaceInvader3.jpg');
Wave:array[1..11, 1..5] of TImage; x,y:integer;
What I would like to know is: how would I load an image into an array element? eg how would I load 'SpaceInvader1.jpg' to array element [1,1]?
Any help would be greatly appreciated.
If you're going to be coding graphical animations, you probably won't want to do it directly on the form. Trying to move things around can be tricky, especially if you want to animate smoothly and not get a lot of flicker and graphical artifacts.
It would be better to use a rendering library. The workflow goes like this:
Create a rendering context on the form. This is a control that provides a surface for graphical animations to run on.
Load your three images into memory.
Create an array of objects to represent the game data associated with your monsters. (Position, movement direction and speed, etc.) It can be flat or 2D, whichever works better for you.
Set up a render loop that goes like this:
For each monster in the array, draw it at its position.
Draw the player's ship and all projectiles.
Check for collisions and handle appropriately.
Check for player input and handle appropriately.
You can find plenty of information on rendering libraries for Delphi in the forums at Pascal Game Development.
You have the following declarations:
ImagePaths:array [1..3] of string =(
'SpaceInvader1.jpg',
'SpaceInvader2.jpg',
'SpaceInvader3.jpg'
);
Wave: array[1..11, 1..5] of TImage;
You want to know how to populate the array of images. It is quite wasteful to create 55 images when 3 will suffice. So instead of that, use indirection. Store references to the images. And TImage is a visual component, and so not appropriate for a sprite.
I would hold the images in an array like this:
Sprites: array [1..3] of TBitmap;
And populate it
JPEGImage := TJPEGImage.Create;
try
for i := 1 to 3 do
begin
JPEGImage.LoadFromFile(ImagePaths[i]);
Sprites[i] := TBitmap.Create;
Sprites[i].Assign(JPEGImage);
end;
finally
JPEGImage.Free;
end;
Then populate your Wave array like this, for example:
Wave: array[1..11, 1..5] of TBitmap;
....
for i := 1 to 11 do
for j := 1 to 5 do
Wave[i,j] := Sprites[1];// or whichever sprite you want
Of course your sprites may be better with a real name rather than in an array.
Some other comments:
JPEG is a bad format for a game sprite. It is a lossy format. A plain Windows bitmap would be fine, as would a GIF or PNG.
I'd much rather see the images as embedded resources. Then your executable can stand alone.
I'd also far rather see your Waves array holding the state of each invader. And then you would create a function that would render that state onto a canvas.

Displaying an Elevation grid in ParaView

I'm new to ParaView and completely lost with all the different data formats. All I want to do is display an elevation grid which is produced by a program. I store the elevation grid in a two dimensional array of floats which is indexed by x and y coordinates and stores the z coordinate. In other words elevationGrid[x][y] stores the height above the point (x, y).
Which file format should I use for this and how is it defined? It would be ideal if someone could give an example file for, say, a 3x3 grid.
A first approach with a 5x5 grid and equation z = x^2+y^2, using a very simple input format. This is a general approach, not especially dedicated to structured grid.
The following has been done with Paraview 3.14.1.
1) Save your data in csv format, i.e. :
"x","y","z"
-0.5,-0.5,0.5
-0.30000001,-0.5,0.34000001
-0.1,-0.5,0.26
[...]
0.1,0.5,0.26
0.30000001,0.5,0.34000001
0.5,0.5,0.5
2) Open in Paraview your csv file
Fill the required import options.
3) Convert your table to geometrical points
Apply Filters > Alphabetical > Table to points
You will be asked to give each variables for each coordinates.
4) Display 3D view to see your points
Create a new visualization view (add a new tab) and choose "3D View".
Activate your TableToPoints filter clicking on the little eye near its name in the pipeline.
If evething is okay, at this point you will see your scatter plot.
5) Last step: create a surface
Apply Filters > Alphabetical > Delaunay 2D
And using default options, one finally obtains:
EDIT:
I remember the name of the dedicated function to create elevation map... It is the Wrap by scalar function. You can combine it with some above steps to get more easily what you want. I could give you an example if necessary.

Resources