open only a field in a large tiff image
Show older comments
I would like open only fields in tiff images of very high definition.
In histology, it is possible to work on multi-image tiff formats. Each image corresponds to different spatial scales. Typically, the higher resolution corresponds to images of size 100.000 x 50.000. To preserve memory and processing time, it is possible to define fields in lower resolution to crop the corresponding image at the full size. However I did not found any function wich could crop such image without opening the all tall image. In particular the 'pixelRegion' argument of imread induce a memory overflow event for arbitrary small fields.
do you have a solution ?
Answers (1)
Nipun Katyal
on 9 Mar 2020
MATLAB provides out-of-core processing with the help of bigimage for formats like .tiff, It enables you to read blocks from the original image and process them without any worry about memory usage or processing power usage.
Here is an example on how to read the block
%Read the image
img = bigimage('sample.tif');
%Starting coordinates
coordStart = [250 250];
%Ending Coordinates
coordEnd = [750,750];
%Extract the region between the aforementioned coordinates
blk1 = getRegion(img,1,coordStart, coordEnd);
%Display the image
bigimageshow(bigimage(blk1))
4 Comments
Daniel Balvay
on 12 Mar 2020
Hauke Kolster
on 13 Mar 2021
Edited: Hauke Kolster
on 13 Mar 2021
There is a workaround using the libtiff functions:
1) On the command line, use the tiffsplit command export single image layers to disk:
> tiffsplit oldfile.tif
2) For a 5 layer tif image this results in the following files being created. The file on top of the stack is usually the image with the non-matching BitsPerSample value.
> ls x*.tif
-rw-rw-r-- 1 root root 148772370 Mar 13 00:22 xaaa.tif
-rw-rw-r-- 1 root root 10938076 Mar 13 00:22 xaab.tif
-rw-rw-r-- 1 root root 643172 Mar 13 00:22 xaac.tif
-rw-rw-r-- 1 root root 73596 Mar 13 00:22 xaad.tif
-rw-rw-r-- 1 root root 123624 Mar 13 00:22 xaae.tif
3) Use the tiffcp command to stack the files back into one image but stack only the ones wih same BitsPerSample values, i.e. leave out the last one (xaae.tif). Also important: use the -t option, see note below.
> tiffcp -t xaaa.tif xaab.tif xaac.tif xaad.tif newfile.tif
> ls newfile.tif
-rw-rw-r-- 1 root root 108629372 Mar 13 10:16 newfile.tif
4) In Matlab you can now load the image as a bigimage.
>> bimt = bigimage('newfile.tif')
bimt =
bigimage with properties:
SpatialReferencing: [1×4 imref2d]
UnloadedValue: [1×1×3 uint8]
LevelSizes: [4×2 double]
SourceDetails: [1×4 struct]
Channels: 3
CoarsestResolutionLevel: 4
FinestResolutionLevel: 1
DataSource: 'newfile.tif'
BlockSize: [4×2 double]
ClassUnderlying: "uint8"
Voila.
A quick note about step 3: Use the option -t to stack the tif images back into one file. The ndpi files from the Hamamatsu scanners save single-slab images. This might cause memory to overflow and crash the application if you display the level 1 data in your bigimage using bigimageshow. This is because the entire slab (which is the entire image!) will be read into memory in order to access the FOV data to be plotted even for a very small FOV. This is of course counter productive.
By choosing the -t option the data is saved as tiles in the tiff files with a default size of 256 by 256. Now, only the tiles that correspond to the FOV are read when accessing the level 1 data for display using bigimageshow and hardly any memory is being used.
Hope this helps.
Walter Roberson
on 13 Mar 2021
Daniel Balvay
on 1 Apr 2021
Categories
Find more on Hamamatsu Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!