nvladimus / npy2bdv

Fast writing of numpy 3d-arrays into HDF5 Fiji/BigDataViewer files.
GNU General Public License v3.0
34 stars 11 forks source link

Example of writing tiles with overlap request #4

Closed Sh4zKh4n closed 3 years ago

Sh4zKh4n commented 3 years ago

I wondered if you could provide an example of overlapping tiles for h5 conversion?

I can see you have channels, angles and so forth. What if you had some overlapping tiles that needed correlation bigstitcher? I can see you can add the views and in the code you have tiles, but there really isnt and examples of this. I am not sure if the tile should be a tuple (though it doesnt look like it) or an in of where it is. Being able to convert to the bigstitcher format that has some of the overlap already completed with the speed of writing here could be a big help.

nvladimus commented 3 years ago

Hi, Good question - I added a new Example 3.1 in the notebook. The tiles do not overlap in the example, they match perfectly. Making them overlap by (X, Y, Z) pixels is done simply by offsetting the last column of the transformation matrix by (X, Y, Z).

I hope this helps!

Sh4zKh4n commented 3 years ago

Hi @nvladimus , thanks for the example. Just to make sure I am getting this right, do you mean the last 4th column of the following matrix?:

unit_matrix = np.array(((1.0, 0.0, 0.0, 0.0), # change the 4. value for x_translation (px)
                        (0.0, 1.0, 0.0, 0.0), # change the 4. value for y_translation (px)
                        (0.0, 0.0, 1.0, 0.0)))# change the 4. value for z_translation (px)

And thatthe value should be changed to pixel overlap? Thats great! One last thing, I know this is a bit annoying. The file here is written out to a HDF file format. The tiles themselves havent actually been tranformed into a fused image and that they are seperate volumes? The reason I ask, is that I was looking for a way to get some zarr files into big stitcher, so this looks as a great option. I just have to work out the specifics of the output. Will have a go but would be interested to know your thoughts. Thanks for the example!

nvladimus commented 3 years ago

Hi, Yes, the last column of this matrix is the translation term. This particular unit_matrix does not do any transformation, but if you set, say, unit_matrix[0,3]=100, the tile will be moved by +100 px in space. The pixel overlap is something specific to a particular imaging experiment. This library cares only about is the transformations of individual views. If you know the space coordinates of your tile, plug them into the 4.th column of this matrix, and BigStitcher will know where to put the tile in space.

Re: second question, yes, the tiles are separate volumes in the H5 file. You are welcome!

Sh4zKh4n commented 3 years ago

That's great, right I get the transformation matrix now. Will try this out later tonight, will be interesting to see the speed of write. It should save time in copying to different hdf5 files then playing around with the file names to get the gridding. That's really helpful.

For some reason my environment, keeps getting stuck with loads of memory when convert from memmap. Raw to hdf5, so hopefully I can either skip the zarr file and convert after stitching to zarr in one go. Thanks you and will let you know how it goes.

Sh4zKh4n commented 3 years ago

Sorry to be a pain, so I ran the example thats fine, but when I try to understand it. You have a singular test image that you are tiling. Do you create a list of ndarrays and substitute into stack? Im being a bit slow here to understand where the stack of numpy arrays go. Very sorry to be annoying here.

nvladimus commented 3 years ago

No worries! I create each tile on the fly by slicing the stack array:

tile_stack = stack[:, j*tile_h:(j+1)*tile_h, i*tile_w:(i+1)*tile_w]

Then write each tile as a view:

bdv_writer.append_view(tile_stack,...

I could write it into a longer (and uglier) code to break down individual steps, but I tried to be brief, therefore the code is a bit dense. You can e.g. insert print(j*tile_h) etc. into the loop to see how the stack is being sliced, and save and plot individual tiles using matplotlib.

Sh4zKh4n commented 3 years ago

Ok perfect, good I wasn't being stupid! I have multiple columns and I think with the virtual stack method with appending, it should work. Will try again to tomorrow and either share here and/or the image forum. Thanks for helping ! I have to say the write was fast, I thought something had gone wrong until I realised it was really fast on the write to me external ssd!

nvladimus commented 3 years ago

You are welcome!