opening and viewing 32 bit tiff image sequence in MATLAB - 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.

Related

Stacking DICOM files into one multi-slice series

I'm trying to stack different DICOM files into one multi-slice series, so to visualize them on ITK-Snap. However, I don't seem to be able to obtain a functioning DICOM series.
I have sorted all files with regards to their slice positioning, and I have a number of ordered single .dcm files, with their original info. I substituted all their original series instance UIDs with one single uid, and their series number with one custom series number I have set to '999' (so to make them belong to one series). Image orientation is set to [1;0;0;0;1;0] for all files, and slice thickness at 8 mm for all files.
I have then created an array of info structures, with the original slice positionings [info(num)].
I have tried something like:
for i=1:num %where num is the number of dicom files
k = num2str(i);
dicomwrite(imm,k,info(i),'CreateMode','Copy'); %where imm is the matrix I obtained with dicomread
end
I have obtained a new set of dicom files, named as numbers from 1 to num, however when I try to open the series on ITK-snap, it runs into an exception stating the vector is too long. I can open single dicom files on ITK-snap, however when more than one images are part of the series, and the series is visualized as 256x212xnum where num is the number of files, I run into the exception.
What am I doing wrong?
What you are trying to do is called Multi-frame in the DICOM standard. In short you need to, next to making sure that all your image meta data is still correct, specify Number of Frames (0028,0008) and Frame Increment Pointer (0028,0009). Unfortunately the wording on how the Frame Increment Pointer tag exactly works is a bit vague:
The frames within a Multi-frame Image shall be conveyed as a logical sequence. The information that determines the sequential order of the frames shall be identified by the Data Element Tag or Tags conveyed by the Frame Increment Pointer (0028,0009). Each specific Image IOD that supports the Multi-frame Module specializes the Frame Increment Pointer (0028,0009) to identify the Attributes that may be used as sequences.
Even if only a single frame is present, Frame Increment Pointer (0028,0009) is still required to be present and have at least one value, each of which shall point to an Attribute that is also present in the Data Set and has a value. 1
Hope that helps.

read 2d grey images and combine them to 3d matrix

I have some problems with matlab 2015a Win10 X64 16GB Ram.
There is a bunch of images (1280x960x8bit) and I want to load them into a 3d matrix. In theory that matrix should store ~1.2GB for 1001 images.
What I have so far is:
values(:,:,:)= zeros(960, 1280, 1001, 'uint8');
for i = Start:Steps:End
file = strcat(folderStr, filenameStr, num2str(i), '.png');
img = imread(file);
values(:,:,i-Start+1) = img;
end
This code is working for small amount of images but using it for all 1001 images I get "Out of memory" error.
Another problem is the speed.
Reading 50 images and save them takes me ~2s, reading 100 images takes ~48s.
What I thought this method does is allocating the memory and change the "z-elements" of the matrix picture by picture. But obviously it holds more memory than needed to perform that single task.
Is there any method I can store the grey values of a 2d picture sequence to a 3d matrix in matlab without wasting that much time and ressources?
Thank you
The only possibility I can see is that your idexes are bad. But I can only guess because the values of start step and End are not given. If End is 100000000, Start is 1 and step is 100000000, you are only reading 2 images, but you are accessing values(:,:,100000000) thus making the variable incredibly huge. That is, most likely, your problem.
To solve this, create a new variable:
imagenames=Start:Step:End; %note that using End as a variable sucks, better ending
for ii=1:numel(imagenames);
file = strcat(folderStr, filenameStr, num2str(imagenames(ii)), '.png');
img = imread(file);
values(:,:,ii) = img;
end
As Shai suggests, have a look to fullfile for better filename accessing

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.

save a large cell matrix (string variables) in Matlab is very slow and size is massive

I have a big cell matrix (string variables) with 40,000,000 lines. I first check the size using whos('file'), and it tells me that the size of the matrix in the workspace is 4.5GB. Then, I use 'save('file',-v7.3) in order to export it to .mat file. It takes so long time and after 10 mins is still saving, so I check the file in the target directory, the file size is already 12GB and is still increasing. Can anybody tell me what happen? Is there any other way to save this matrix? It doesn't need to be a .mat file, it can be .txt or something else.
A small part of the matrix.
'00086810'
'00192610'
'00213T10'
'00339010'
'00350L10'
'00350P10'
'00428010'
'00431F10'
'00433710'
'00723110'
'00743710'
'00818210'
'00818810'
'01031710'
'01204610'
'01747610'
'01747F10'
'01852Q10'
'01853510'
'01887110'
'01888510'
'01890A10'
'01920510'
'02316010'
'02343R10'
'02361310'
'02391210'
'02407310'
'02407640'
'02408H10'
'02434310'
'02520W10'
'02581610'
Lets test
test='helloooooo'
whos('test')
Name Size Bytes Class Attributes
test 1x10 20 char
save('A','test')
size A file 184 bytes
Lets test bigger data.
symbols = ['a':'z' 'A':'Z' '0':'9'];
MAX_ST_LENGTH = 500;
stLength = randi(MAX_ST_LENGTH);
for ii=1:100
nums = randi(numel(symbols),[1 stLength]);
testcell{ii} = symbols (nums);
end
save('test','testcell')
whos('testcell')
Name Size Bytes Class Attributes
testcell 1x100 52200 cell
Size file 15.7Kb
It compresses data. BUT I realised that it depends in the data. however usually I got x3 compression
Can you show us you code, maybe you are saving things wrongly

How to save Sift feature vector for classification using Neural network

Matlab implementation of SIFT features were found from http://www.cs.ubc.ca/~lowe/keypoints/. with the help of stackoverflow. I want to save features to a .mat file. Features are roundness, color, no of white pixel count in the binary image and sift features. For the sift features I took descriptors in above code { [siftImage, descriptors, locs] = sift(filteredImg) } So my feature vector now is FeaturesTest = [roundness, nWhite, color, descriptors, outputs]; When saving this to .mat file using save('features.mat','Features'); it gives an error. Error is like this.
??? Error using ==> horzcat CAT
arguments dimensions are not
consistent. Error in ==>
user_interface>extract_features at 336
FeaturesTest = [roundness, nWhite,
color, descriptors, outputs];
As I can understand, I think the issue is descriptor feature vector size. It is <14x128 double>. 14 rows are for this feature, where as for others only one row is in .mat file. How can I save this feature vector to the .mat file with my other features?
Awaiting for the reply. Thanks in advance.
From what I can understand, it looks like you are trying to put the variables roundness, nWhite, color, descriptors, and outputs into a single vector, and all the variables have unique dimensions.
Maybe it would be better to use a cell or a structure to store the data. To store the data in a cell, just change square brackets to curly braces, like so:
FeaturesTest = {roundness, nWhite, color, descriptors, outputs};
However, that would require you to remember which cells were which when you pulled the data back out of the .mat file. A structure may be more useful for you:
FeaturesTest.roundness = roundness;
FeaturesTest.nWhite = nWhite;
FeaturesTest.color = color;
FeaturesTest.descriptors = descriptors;
FeaturesTest.outputs = outputs;
Then, when you load the .mat file, all of the data will be contained in that structure, which you can easily reference. If you needed to look at just the color variable, you would type FeaturesTest.color, press enter, and the variable would be displayed. Alternatively, you could browse the structure by double clicking on it in the workspace window.
Alternatively, you could just use the save command like so:
save(filename,roundness, nWhite, color, descriptors, outputs)
Hope this helps.