1

I am trying to read czi format images, But because they need a lot of memmory I tried reading them in memmap file.

Here is the code I used>

import czifile as czi
fileName = "Zimt3.czi"
# read file to binary
file = czi.CziFile(fileName)
imageArr = file.asarray(out="/media/my drive/Temp/temp.bin")

Now imageArr is a variable with dimensons of (9,3,29584,68084,1) in memmap. These are high resolution microscopic images from Carl Zeiss device.

Here is an screenshot of more specifications. ImageArr specifications

I think this means that imageArr contains 9 images with the dimention of (29584,68084,3) But I cant extract this kind of numpy array to visualize as an image. Can you please help me convert (9,3,29584,68084,1) in memmap to (29584,68084,3) images please.

Maryam S
  • 23
  • 9
  • When you work with different image format, some times when you read an image file, the dimension of the file is bigger than RBG matrix. In these situations, you can average on other additional dimensions. I did it in my experiments and found it work fine. – PyMatFlow Mar 15 '20 at 10:52
  • With an regular arraay I'd `arr.squeeze().transpose(0,2,3,4)` - that is remove the size 1 dimension, and move the size 3 dimension to the end. But that makes a new array; initially a view, but almost any use will require a copy. I'm not sure how the `memmap` location changes things. You could apply this change to individually to the 9 images. – hpaulj Mar 15 '20 at 15:58

1 Answers1

0

It looks like a very large file. If you just want to visualize it, you can use slideio python package (http://slideio.com). It makes use of internal image pyramids. You can read the image partially with high resolution or the whole image with low resolution. The code below rescales the image so that the width of the delivered raster will be 500 pixels (the height is computed to keep the image size ratio).

import slideio
import matplotlib.pyplot as plt

slide = slideio.open_slidei(file_path="Zimt3.czi",driver_id="CZI")
scene = slide.get_scene(0)
block = scene.read_block(size=(500,0))

plt.imshow(scene.read_block())

Be aware that matplotlib can display images if they have 1 or 3 channels. A CZI file can have an arbitrary number of channels. In this case you have to select what channels you want to display:

block = scene.read_block(size=(500,0), channel_indices=[0,2,5])

Another problem with visualization can be if your file is a 3 or 4D image. In this case, slideio returns 3d or 4d numpy array. Matplotlib cannot display 3d or 4d images. You will need to look for a specific visualization package or select a z-slice and/or time-frame:

block = scene.read_block(size=(500,0), channel_indices=[0,2,5], slices=(0,1), frames=(1,2))

For more details see the package documentation.

Stanislav Melnikov
  • 198
  • 1
  • 2
  • 9