Writing TIFF Appears Black But imagesc does not MATLAB - 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!

Related

Matlab: Raw image DNG BitsPerSample 10 not support

As Steve Eddin posted in http://blogs.mathworks.com/steve/2011/03/08/tips-for-reading-a-camera-raw-file-into-matlab/, I followed and copied the following code, running in MATLAB 2014b. As a result, I got the following error."Reading images with a BitsPerSample of 10 is not supported". I checked info and info.SubIFDs{1}, I got the following results in INFOS. I have searched on google for the problem, but there is no enough information. Therefore, I posted this problem here and hope someone gives me solutions.
CODE::
warning off MATLAB:tifflib:TIFFReadDirectory:libraryWarning
t = Tiff('Non-Invasive DNGs/WP_20150311_15_03_14_Raw__highres.dng','r');
offsets = getTag(t,'SubIFD');
setSubDirectory(t,offsets(1));
cfa = read(t);
close(t);
ERROR::
Error using Tiff.constructBlankOutputImage (line 2448)
Reading images with a BitsPerSample of 10 is not supported.
Error in Tiff/readAllStrips (line 2046)
imageData = obj.constructBlankOutputImage(imageSize,bps,sampleFormat);
Error in Tiff/read (line 1507)
[varargout{:}] = obj.readAllStrips();
Error in readDNG (line 11)
cfa = read(t);
INFOS_1::
FileSize: 43676288
Format: 'tif'
FormatVersion: []
Width: 960
Height: 544
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 1
BitsPerSample: [8 8 8]
Compression: 'Uncompressed'
PhotometricInterpretation: 'RGB'
StripOffsets: 69248
SamplesPerPixel: 3
RowsPerStrip: 544
StripByteCounts: 1566720
XResolution: []
YResolution: []
ResolutionUnit: 'Inch'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: [255 255 255]
MinSampleValue: [0 0 0]
Thresholding: 1
Offset: 8
Make: 'Nokia'
Model: 'Lumia 1020'
Software: 'Nokia RAW-to-DNG converter v01.00.06'
DateTime: '2015:03:11 15:03:11'
SubIFDs: {[1x1 struct]}
DigitalCamera: [1x1 struct]
DNGVersion: [1 3 0 0]
DNGBackwardVersion: [1 3 0 0]
UniqueCameraModel: 'Nokia Lumia 1020'
ColorMatrix1: [1.6183 -0.7414 0.1000 -0.2665 1.1286 0.3664 0.1114 0.0951 0.8522]
ColorMatrix2: [0.9390 -0.1511 -0.0964 -0.3983 1.2153 0.1384 -0.0880 0.4120 0.5662]
CameraCalibration1: [1.0361 0 0 0 1 0 0 0 0.9688]
CameraCalibration2: [0.9851 0 0 0 0.9926 0 0 0 0.9763]
AnalogBalance: [1 1 1]
AsShotNeutral: [0.8476 1 0.6481]
BaselineExposure: -1
BaselineNoise: 1
BaselineSharpness: 1
LinearResponseUnit: 0.9999
ShadowScale: 1
DNGPrivateData: [1x1246 double]
MakerNoteSafety: 1
CalibrationIlluminant1: 17
CalibrationIlluminant2: 21
AliasLayerMetadata: [58 48 51 58 49 49 32 49 53 58 48 51 58 49 49 0]
UnknownTags: [7x1 struct]
INFOS_2::SubIFDs{1}
FileSize: 43676288
Format: 'tif'
FormatVersion: []
Width: 7728
Height: 4352
BitDepth: 10
ColorType: 'CFA'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: 10
Compression: 'Uncompressed'
PhotometricInterpretation: 'CFA'
StripOffsets: 1635968
SamplesPerPixel: 1
RowsPerStrip: 4352
StripByteCounts: 42040320
XResolution: []
YResolution: []
ResolutionUnit: 'Inch'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 1023
MinSampleValue: 0
Thresholding: 1
Offset: 56578
CFAPlaneColor: [0 1 2]
CFALayout: 1
BlackLevelRepeatDim: [2 2]
BlackLevel: [0 0 0 0]
WhiteLevel: 1023
DefaultScale: [1 1]
DefaultCropOrigin: [0 0]
DefaultCropSize: [7728 4352]
BayerGreenSplit: 1
AntiAliasStrength: 1
BestQualityScale: 1
UnknownTags: [4x1 struct]

Matlab only opens first frame of multi-page tiff stack

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

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.

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.