Skip to content
Snippets Groups Projects
Commit 2d8f7131 authored by Yoann Guilhem's avatar Yoann Guilhem Committed by Nicola Vigano
Browse files

Fix gtReadTifVolume, consistent and faster

git-svn-id: https://svn.code.sf.net/p/dct/code/trunk@257 4c865b51-4357-4376-afb4-474e03ccb993
parent 48ae9ce7
No related branches found
Tags v0.3.1
No related merge requests found
function vol = gtReadTifVolume(filename)
% GTREADTIFVOLUME read tif volume (stack) and store it into a matrix
function vol = gtReadTifVolume(filename, varargin)
% GTREADTIFVOLUME read tif volume (stack) and store it into a matrix [X,Y,Z]
%
% Usage: vol=gtReadTifVolume(filename,[,'xrange',xrange,'yrange',yrange,'zrange',zrange])
% Use it exactly the same as gtHSTVolReader function.
%
% 14 February 2012, Yoann Guilhem, guilhem@esrf.eu
if exist(filename)
params.xrange = 'all';
params.yrange = 'all';
params.zrange = 'all';
params = parse_pv_pairs(params, varargin);
if ~exist('filename','var')
[tmp1, tmp2] = uigetfile('*','Select a .tif volume file');
filename = fullfile(tmp2,tmp1);
end
if ~exist(filename)
Mexc = MException('TIFF:wrong_file_name',['File ''' filename ''' does not exist!']);
throw(Mexc);
end
try
info = imfinfo(filename);
Xsize = info(1).Width;
Ysize = info(1).Height;
Zsize = numel(info);
vol = zeros(Ysize,Xsize,Zsize);
for i=1:size(vol,3)
vol(:,:,i) = imread(filename,i);
catch Mexc
disp('Unable to read information of selected volume.')
disp('Please check it exists in the directory.')
disp(' ')
Mexc1 = MException('TIFF:wrong_input', 'Unable to read files');
Mexc1 = addCause(Mexc1, Mexc);
throw(Mexc1);
end
XTifSize = info(1).Width;
YTifSize = info(1).Height;
ZTifSize = numel(info);
if strcmp(params.xrange,'all')
Xrange = 1:XTifSize;
else
Xrange = params.xrange;
end
if strcmp(params.yrange,'all')
Yrange = 1:YTifSize;
else
Yrange = params.yrange;
end
if strcmp(params.zrange,'all')
Zrange = 1:ZTifSize;
else
Zrange = params.zrange;
end
XVolSize = length(Xrange);
YVolSize = length(Yrange);
ZVolSize = length(Zrange);
switch info(1).BitDepth
case 8
vol = zeros(XVolSize,YVolSize,ZVolSize,'uint8');
case 12
vol = zeros(XVolSize,YVolSize,ZVolSize,'uint16');
case 16
vol = zeros(XVolSize,YVolSize,ZVolSize,'uint16');
otherwise
vol = zeros(XVolSize,YVolSize,ZVolSize);
end
%disp('Tiff')
%disp([info(1).Height info(1).Width numel(info)])
%disp([XTifSize YTifSize ZTifSize]);
%disp(size(imread(filename, 1)))
%disp('Tiff crop')
%disp(size(imread(filename, 1,'PixelRegion', {[Yrange(1) Yrange(end)], [Xrange(1) Xrange(end)]})))
%disp('Vol crop')
%disp(size(imread(filename, 1,'PixelRegion', {[Yrange(1) Yrange(end)], [Xrange(1) Xrange(end)]})'))
%disp([XVolSize YVolSize ZVolSize])
%tic
if nargin <= 1
for i=1:ZVolSize
vol(:,:,i) = imread(filename, Zrange(i))';
end
else
error('"%s" file does not exist!', filename);
for i=1:ZVolSize
vol(:,:,i) = imread(filename, Zrange(i), 'PixelRegion', {[Yrange(1) Yrange(end)], [Xrange(1) Xrange(end)]})';
% This second method should be faster but in fact...
%vol(:,:,i) = imread(filename, Zrange(i), 'Info', info, 'PixelRegion', {[Yrange(1) Yrange(end)], [Xrange(1) Xrange(end)]})';
end
end
%toc
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment