Big tiff read and view in Matlab - matlab

I have downloaded a btf file (big tiff) from the links below, how can I read it and "imshow" it? is there a way to convert it to tiff format as the btf is not that common?
Link:
https://drive.google.com/file/d/0ByhuP_NuuARtSW9aeTdPUTlRdWM/view?usp=drive_web
http://www.photomacrography.net/forum/viewtopic.php?t=28990&sid=cca737a2e0bc7ea3e2e41f0d6e75f5a9
I used this code:
t = Tiff('d:/Image_687.btf','w8');
imageData = read(t);
and got this error:
Error using tifflib
Unable to retrieve PhotometricInterpretation.
Error in Tiff/getTag (line 838)
tagValue = tifflib('getField',obj.FileID,Tiff.TagID.(tagId));
Error in Tiff/read (line 1487)
photo = obj.getTag('Photometric');
Error in Untitled2 (line 2)
imageData = read(t);

The real issue with your code is the second parameter that you have passed to Tiff. As the documentation states, the second parameter indicates in what mode to open the file. You have specified w8 which the documentation states is:
open TIFF file for writing a BigTIFF file; discard existing contents.
This means that it is deleting your image before you even start! If you want to use the Tiff class, you'll want to either use no second parameter or the r parameter to open the file for reading.
t = Tiff('Image_687.btf');
t = Tiff('Image_687.btf', 'r');
That being said, in general it is better to try to load it with a higher level function such as imread. The Tiff class is a much lower-level function that can be a little harder to manipulate but may provide some needed specialty functionality.
im = imread('Image_687.btf');
size(im)
3072 4080 3
I had to do a little manipulation for display because the RGB values weren't between 0 and 255
im = double(im);
im = uint8(255 * im ./ max(im(:)));
imshow(im);

Related

It appears imageLabeler cannot handle a transformed datastore?

The Matlab app imageLabeler is supposed to support the following format:
imageLabeler(imgStore)
I have an imgStore, defined as follows:
imds = imageDatastore(cellArrayOfImageFilenames);
imgStore = transform(imds, #(x)demosaic(x,'rggb'));
I have to do this, because my images are stored as bayer encoded images, and this is the only way I've figured out to get the imgStore to return these images as 3 channel RGB images. However, when I try and initalize imageLabeler, I get this error:
>> imageLabeler(imgStore)
Error using imageLabelerInternal
Expected input name to be one of these types:
char
Instead its type was matlab.io.datastore.TransformedDatastore.
Error in vision.internal.imageLabeler.imageLabelerInternal
Error in imageLabeler (line 58)
vision.internal.imageLabeler.imageLabelerInternal(varargin{:});
TLDR:
How do I get imageLabeler to handle my bayer encoded images?
The way to fix this, is with the imageDatastore 'ReadFcn' parameter. The documentation for imageDatastore explicity tells you to NOT do this, as it slows down Neural Network stuff. Here's the Matlab doc text:
Using ReadFcn to transform or pre-process 2-D images is not
recommended. For file formats recognized by imformats, specifying
ReadFcn slows down the performance of imageDatastore. For more
efficient ways to transform and pre-process images, see Preprocess
Images for Deep Learning (Deep Learning Toolbox).
So, all that said, here's the workaround:
imgStore = imageDatastore(cellArrayOfImageFilenames ...
, 'ReadFcn', #(x)demosaic(imread(x),'rggb')));

How to convert PPM images to JPG in Matlab?

I have some PPM images (stereo) that I read with imread() and I want to save the same images in JPEG with different Quality factors.
Here is my code.
%Read PPM image
L = imread(filename_L);
%Create JPEG Q85 from PPM
filename_L85 = strcat(filename_L,'_ppm_to_jpeg.jpg');
imwrite(L,filename_L85,'JPEG','Quality',85);
And here the error I get.
Error using imwrite>parse_inputs (line 528)
The colormap should have three columns.
Error in imwrite (line 418)
[data, map, filename, format, paramPairs] = parse_inputs(varargin{:});
Error in testFinale (line 75)
imwrite(L,filename_L85,'JPEG','Quality',85);
How can I write JPEG images previously read in PPM format?
Thanks
Could it be that is just has to do with your case of 'JPEG', the documentation of imwrite specifies parameters for file type as lowercase.
Apart from that you might not even need it as the file type is derived from the extension which in this case is set explicitly to .jpg already.
So you might either go for:
imwrite(L,filename_L85,'jpeg','Quality',85);
or perhaps even easier:
imwrite(L,filename_L85,'Quality',85);

MATLAB loading and saving a single image from a 32 bit tiff stack

I'm using MATLAB_R2011a_student. I have some image stacks saved as 32 bit tiffs, some over 1000 frames. I would like to be able to pull out a specific frame from the stack and save it as a 32 bit tiff or some readable format where there would be no data loss from the original. Currently my code looks like this:
clear, clc;
k=163;
image=('/Users/me/Filename.tiff');
A = uint8(imread(image, k));
B=A(:,:,1);
J=imadjust(B,stretchlim(B),[]);
imwrite(J,'/Users/me/163.tif','tif');
(I'm assuming reading it as 8 bit, and the way I'm saving are not the best way to do this)
Either way this code works for a seemingly random number of frames (for example in one file.tiff the above code works for frames 1-165 but none of the frames after 165, for a different file.tiff the code works for frames 1-8 but none of the frames after 8) I'm also getting a strange horizontal line in the output image when this does work:
??? Error using ==> rtifc
Invalid TIFF image index specified.
Error in ==> readtif at 52
[X, map, details] = rtifc(args);
Error in ==> imread at 443
[X, map] = feval(fmt_s.read, filename, extraArgs{:});
Thanks!
The best way (in my opinion) to handle tiff stacks is to use the Tiff library available since a few years. I must admit that I don't know much about OOP but I managed to understand enough to load a tiff stack and manipulate it.That's the kind of simple demo I wish I had seen a year ago haha.
I the following example I load a single stack and store it all into a 3D array. I use imfinfo to fetch infos about the images, notably the number of images/stack and the actual image dimensions. If you want you can choose to load only one image using appropriate indices. Please try the code below and play around with it; you'll understand what I mean.
clear
clc
%// Get tiff files you wish to open
hFiles = dir('*.tif');
%// Here I only have 1 multi-tiff file containing 30 images. Hence hInfo is a 30x1 structure.
hInfo = imfinfo(hFiles(1).name);
%// Set parameters.
ImageHeight = hInfo(1).Height;
ImageWidth = hInfo(1).Width;
SliceNumber = numel(hInfo);
%// Open Tiff object
Stack_TiffObject = Tiff(hFiles.name,'r');
%// Initialize array containing your images.
ImageMatrix = zeros(ImageHeight,ImageWidth,SliceNumber,'uint32');
for k = 1:SliceNumber
%// Loop through each image
Stack_TiffObject.setDirectory(k)
%// Put it in the array
ImageMatrix(:,:,k) = Stack_TiffObject.read();
end
%// Close the Tiff object
Stack_TiffObject.close
Hope that helps.

opening and viewing 32 bit tiff image sequence in MATLAB

I have a singe image.tiff file, a video sequence exported as 32 bit tiff. I would like to open it as an image stack in MATLAB, and be able to navigate frame by frame. I believe implay() is the way to do this in matlab. If I try this I get "Error occurred while attempting to read file: image.tiff Details of error: Incorrect chunk size information in AVI file." Does implay() only work with the .avi format? do I need to covert this 32 tiff to a .avi before i can use implay()? or is there maybe some other (non-implay()) way of opening this as a stack?
Thanks
You could try to create an image stack and use implay to view it. The function accepts multiple types of arguments, for grayscale images it should be provided with an array of size N x M x K where K is the number of frames, (N,M) is the image size. For color images an array of size NxMx3xK is expected.
To create the array for the case with multiple files, each containing a frame you have multiple options, the simplest is probably to use the cat function for concatenation:
image_stack = [];
for i = 1: num_frames
curr_image = imread(sprintf('frame_%04d_color.tif', i));
image_stack = cat(4, image_stack, curr_image);
end
implay(image_stack);
This solution is a bit slower, than if the image_stack is allocated beforehand though.
For your case with a single TIFF file, the frames need to be extracted in a manner suitable for the storage format, but this is a separate problem from the video replay.

Processing a stack of images in matlab

I am currently working on an image processing task that requires me to process a stack of .png images all at once in Matlab (I have very limited matlab knowledge). I have looked at various sites trying to figure out how to do this. My most recent attempt was based on the answer in this link: http://www.mathworks.com/matlabcentral/answers/7665-images-to-stacks, however I keep getting the error:
"Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
My .png's are numbered sequentially (Heart 001.png, Heart 002.png,...) and my exact code is as follows:
I = zeros(240,320,253,'uint8');
for ii = 1:253
I(:,:,ii) = imread(sprintf('Heart %s.png'),num2str(ii,'%03i')));
end
Any help would be greatly appreciated!
Your image reading code looks fine, but the way you construct the file names is wrong. You are passing the result of num2str to imread as it's image format argument, but you intended to pass it to sprintf. How about you try imread(sprintf('Heart %03d.png', ii));?
I found the solution to be downloading imshow3D.m from http://www.mathworks.com/matlabcentral/fileexchange/41334-imshow3d--3d-imshow- ,
and then implementing the following code:
clear;
clc;
I = zeros(240,320,253,'uint8');
for k = 1:253
PNGFileName = strcat('Heart ',32, num2str(k), '.png');
imageData = imread(sprintf(PNGFileName));
Heart = imageData(:,:,1);
I(:,:,k) = Heart;
end
imshow3D(I)