Matlab only opens first frame of multi-page tiff stack - matlab

I've created multi-page tiff files with a macro in ImageJ, and I'm now trying to open it using matlab, but I can only access the first frame.
Here is the result of imfinfo(filename). Accordingly, I get
length(imfinfo(filename)) = 1
Filename: [1x129 char]
FileModDate: '28-nov-2013 12:27:51'
FileSize: 6.7905e+09
Format: 'tif'
FormatVersion: []
Width: 512
Height: 512
BitDepth: 8
ColorType: 'grayscale'
FormatSignature: [77 77 0 42]
ByteOrder: 'big-endian'
NewSubFileType: 0
BitsPerSample: 8
Compression: 'Uncompressed'
PhotometricInterpretation: 'BlackIsZero'
StripOffsets: 932625
SamplesPerPixel: 1
RowsPerStrip: 512
StripByteCounts: 262144
XResolution: []
YResolution: []
ResolutionUnit: 'None'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 255
MinSampleValue: 0
Thresholding: 1
Offset: 8
ImageDescription: 'ImageJ=1.47q
images=25900
slices=25900
loop=false
However if I open the same tif file in ImageJ, then I can read and scroll through the 25900 frames...The weird thing is that matlab can read previous multipage tiff I had created in imageJ without my batch macro...
I don't understand what's happening...any help would be greatly appreciated !
Thanks,
Steven

This is actually ImageJ's fault. For large TIFFs, instead of using the BigTiff standard to save the stack, ImageJ instead saves a raw file with a fake TIFF header containing the first frame, and happily names it .tif. You can discuss with the ImageJ developers why they think this is a good idea.
To read these stacks into Matlab, you can use memmapfile or MappedTensor.

You have to loop over all the images in the stack:
fname = 'my_file_with_lots_of_images.tif';
info = imfinfo(fname);
imageStack = [];
numberOfImages = length(info);
for k = 1:numberOfImages
currentImage = imread(fname, k, 'Info', info);
imageStack(:,:,k) = currentImage;
end

Related

Writing TIFF Appears Black But imagesc does not MATLAB

I want to write a tiff again after having read it into MATLAB. The tiff files were generated by some other software:
[flnm,locn]=uigetfile({'*.tif','Image files'}, 'Select an image');
fp=fullfile(locn,flnm);
I = double(imread(fp));
info = imfinfo(fp);
.
.
.
J2 = im2int16(J2);
J2(:,:) = uint16((J2(:,:)./max(max(J2(:,:),[],1)))*65536);
T = Tiff((fullfile(strcat(locn,v_f), (sprintf('%d.tif',i)))), 'w');
tagstruct.ImageLength = size(J2, 1);
tagstruct.ImageWidth = size(J2, 2);
tagstruct.Compression = Tiff.Compression.None;
tagstruct.SampleFormat = Tiff.SampleFormat.Int;
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = info.BitsPerSample; % 32;
tagstruct.SamplesPerPixel = info.SamplesPerPixel; % 1;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
T.setTag(tagstruct);
T.write(J2);
T.close();
This line which is supposed to rescale the intensity range:
J2(:,:) = uint16((J2(:,:)./max(max(J2(:,:),[],1)))*65536);
was borrowed from here: https://stackoverflow.com/a/19949536/3319527. But my images still come out as black. This should not be the case because with imagesc though the signals are quite noisy but the data output to the screen makes sense but the tiffs I write to file are quite useless.
Image Parameters:
Filename: [1x172 char]
FileModDate: '24-Jan-2014 14:39:09'
FileSize: 32003
Format: 'tif'
FormatVersion: []
Width: 125
Height: 125
BitDepth: 16
ColorType: 'grayscale'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: 16
Compression: 'Uncompressed'
PhotometricInterpretation: 'BlackIsZero'
StripOffsets: [8 2008 4008 6008 8008 10008 12008 14008 16008 18008 20008 22008 24008 26008 28008 30008]
SamplesPerPixel: 1
RowsPerStrip: 8
StripByteCounts: [2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 1250]
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 65535
MinSampleValue: 0
Thresholding: 1
Offset: 31768
ImageDescription: '
From tiff info:
TIFF File: '000001.tif'
Mode: 'r'
Current Image Directory: 1
Number Of Strips: 16
SubFileType: Tiff.SubFileType.Default
Photometric: Tiff.Photometric.MinIsBlack
ImageLength: 125
ImageWidth: 125
RowsPerStrip: 8
BitsPerSample: 16
Compression: Tiff.Compression.None
SampleFormat: Tiff.SampleFormat.UInt
SamplesPerPixel: 1
PlanarConfiguration: Tiff.PlanarConfiguration.Chunky
ImageDescription:
This is frame #0000
Orientation: Tiff.Orientation.TopLeft
Thanks!

matlab imshow not working with certain tiff files

I get the following errors when trying to run imshow with some tiff files:
??? Error using ==> imageDisplayValidateParams>validateCData at 114
Unsupported dimension
Error in ==> imageDisplayValidateParams at 31
common_args.CData = validateCData(common_args.CData,image_type);
Error in ==> imageDisplayParseInputs at 79
common_args = imageDisplayValidateParams(common_args);
Error in ==> imshow at 199
[common_args,specific_args] = ...
Error in ==> CellArea at 6
imshow('A1 x20.tiff')
I initially stored the image data in a matlab variable using imread and when that didn't work with imshow I used it to just get the image directly with the filename; the error messages are the same.
The problem images I'm trying to analyze are 1032x778 tiff files. I made a sample tif image using Paint and the function has no problems with it. Does anybody know what is causing these errors and how to get the image to display? Thanks
Here is the infinfo output for one of the images, as requested
Filename: 'A1 x20.tiff'
FileModDate: '14-Oct-2013 15:49:26'
FileSize: 3211714
Format: 'tif'
FormatVersion: []
Width: 1032
Height: 778
BitDepth: 32
ColorType: 'truecolor'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: [8 8 8 8]
Compression: 'Uncompressed'
PhotometricInterpretation: 'RGB'
StripOffsets: 8
SamplesPerPixel: 4
RowsPerStrip: 4.2950e+009
StripByteCounts: 3211584
XResolution: []
YResolution: []
ResolutionUnit: 'None'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: [255 255 255 255]
MinSampleValue: 0
Thresholding: 1
Offset: 3211592
doing x = imread('A1 x20.tiff') and then whos x gives
Name x
Size 778x1032x4
Bytes 3211584
Class uint8
Attributes
For some reason your tiff file has four channels (nothing to do with multiple frames): size(x,3)==4. I guess the fourth is an alpha-channel.
imshow can display either gray scale images, indexed images (with size(x,3)==1) or true-color images (with size(x,3)==3). Your image had 4 channels and thus imshow failed.
Asking inshow to work only on the first three channels made the trick:
>> imshow( x(:,:,1:3) );

cannot open tiff files compressed in this fashion (2) - imageJ/FIJI

I have two sets of images generated from MATLAB - one set is when I save manually from the figure window into a tif file and the other set has images saved in tif format by using imwrite function in MATLAB.
When I try to use the first set of images to perform some operations in Fiji (Image J) it works but when I try doing the same on the second set, I get an error saying 'cannot open tiff files compressed in this fashion (2)'. Is there a plugin that I need to install?
imfinfo for first set - created by saveas tif from figure window:
Filename: [1x68 char]
FileModDate: [1x20 char]
FileSize: 51376
Format: 'tif'
FormatVersion: []
Width: 719
Height: 477
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: [1x4 double]
ByteOrder: [1x13 char]
NewSubFileType: 0
BitsPerSample: [8 8 8]
Compression: 'PackBits'
PhotometricInterpretation: 'RGB'
StripOffsets: [69x1 double]
SamplesPerPixel: 3
RowsPerStrip: 7
StripByteCounts: [69x1 double]
XResolution: 96
YResolution: 96
ResolutionUnit: 'Inch'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: [1x3 double]
MinSampleValue: [0 0 0]
Thresholding: 1
Offset: 50592
ImageDescription: [1x22 char]
imfinfo for second set - created by imwrite. imwrite(bw,fullfile(filename),'tiff');
Filename: [1x75 char]
FileModDate: [1x20 char]
FileSize: 25586
Format: 'tif'
FormatVersion: []
Width: 832
Height: 587
BitDepth: 1
ColorType: [1x9 char]
FormatSignature: [1x4 double]
ByteOrder: [1x13 char]
NewSubFileType: 0
BitsPerSample: 1
Compression: [1x8 char]
PhotometricInterpretation: [1x11 char]
StripOffsets: [66x1 double]
SamplesPerPixel: 1
RowsPerStrip: 9
StripByteCounts: [66x1 double]
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 1
MinSampleValue: 0
Thresholding: 1
Offset: 24868
I am not sure, if you got the answer yet. I had a similar problem. Try saving your file via:
imwrite(bw,fullfile(filename),'tiff', 'Compression','none');
or try compression using 'packbits'
imwrite(bw,fullfile(filename),'tiff', 'Compression','packbits');
Try this plugin for ImageJ - IJ Plugins: Image I/O.
EDIT
It look like your image is binary (BitDepth=1). IMWRITE by default saves binary image in TIFF format with ccitt compression. Such images are not supported by IMageJ. (See, for example, this thread for a possible reason).
If you need to save your images with imwrite try to set BitDepth to 24 and Compression to packbits. Compare other fields in imfinfo outputs and use different ones in imwrite if necessary.

Perspective projection of a 3D model to a 2D plane

I'm trying to project a 3D model to a 2D plane and I found I should use the projection equation C*((R*X)+T) to do so. C, which is the camera calibration matrix is calculated as follows:
C =[f 0 px;
0 f py;
0 0 1];
First, I want to ask about the focal length f used in the camera calibration matrix. Should I use it with the value in pixels or mm? If in mm how can I get it?
Second I don't really know what the px and py variables stand for I got some information about the data I'm working on bye the exifread function in MATLAB and these are the information I got:
Sharpness: 0
Contrast: 0
SceneCaptureType: 0
FocalLengthIn35mmFilm: 27
DigitalZoomRatio: 1
WhiteBalance: 0
ExposureMode: 0
SceneType: 1
FileSource: 3
SensingMethod: 2
PixelYDimension: 3000
PixelXDimension: 4000
ColorSpace: 1
FlashpixVersion: '0100'
FocalLength: 4.9000
Flash: 1
LightSource: 0
MeteringMode: 4
MaxApertureValue: 3.6150
ExposureBiasValue: 0
ApertureValue: 3.6150
ShutterSpeedValue: 2.3220
CompressedBitsPerPixel: 2.8149
ComponentsConfiguration: [1 2 3 0]
DateTimeDigitized: '2011:06:26 16:55:08'
DateTimeOriginal: '2011:06:26 16:55:08'
ExifVersion: '0221'
ISOSpeedRatings: 100
ExposureProgram: 2
FNumber: 3.5000
ExposureTime: 0.2000
Copyright: 'Copyright 2010'
YCbCrPositioning: 2
DateTime: '2011:06:26 16:55:08'
Software: ' 0.8913'
ResolutionUnit: 2
YResolution: 96
XResolution: 96
Orientation: 1
Model: 'SAMSUNG ES30/VLUU ES30'
Make: 'SAMSUNG'
Thumbnail: [1x1 struct]
Do px and py refer to any of them?
px and py are the coordinates of the principal point. On an ideal camera that would be the center of the image, so you can use width/2, height/2 for a start. For actual values you should use a calibration algorithm.
f should be in pixels.

about tiff image

imfinfo of my image gives the following:
Filename: 'drosophila.tif'
FileModDate: '10-Nov-2009 18:52:42'
FileSize: 264768
Format: 'tif'
FormatVersion: []
Width: 512
Height: 512
BitDepth: 8
ColorType: 'grayscale'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: 8
Compression: 'PackBits'
PhotometricInterpretation: 'BlackIsZero'
StripOffsets: [32x1 double]
SamplesPerPixel: 1
RowsPerStrip: 16
StripByteCounts: [32x1 double]
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 255
MinSampleValue: 0
Thresholding: 1
Offset: 264322
how many strips are there?
generic logic:
ceil(Height/RowsPerStrip)
The TIFF specifications states that the last strip need not be full (hence the CEIL call).
Or, the length of the StripOffsets from the info structure. As the name implies, this is a vector of byte offsets to each strip in the file (so there has to be one offset per strip).
32.
Height: 512
RowsPerStrip: 16
512 = 2^9; 16=2^4. Divide to get 2^5 which is 32.