Skip to content
Snippets Groups Projects
Commit e0ecb21e authored by Eric Wait's avatar Eric Wait
Browse files

Added files from the local path

parent 85758a48
No related branches found
No related tags found
No related merge requests found
function createMetadata(root,imageData)
if (~exist(root,'dir'))
mkdir(root);
end
fileName = fullfile(root,[imageData.DatasetName '.txt']);
fprintf('Creating Metadata %s...',fileName);
fileHandle = fopen(fileName,'wt');
fprintf(fileHandle,'DatasetName:%s\n',imageData.DatasetName);
fprintf(fileHandle,'NumberOfChannels:%d\n',imageData.NumberOfChannels);
if (isfield(imageData,'ChannelColors'))
fprintf(fileHandle,'ChannelColors:');
if (size(imageData.ChannelColors,1)==1 && ~iscell(imageData.ChannelColors))
fprintf(fileHandle,'%s,',imageData.ChannelColors);
else
for i=1:length(imageData.ChannelColors)
fprintf(fileHandle,'%s,',imageData.ChannelColors{i});
end
end
fprintf(fileHandle,'\n');
end
fprintf(fileHandle,'NumberOfFrames:%d\n',imageData.NumberOfFrames);
% fprintf(fileHandle,'XDimension:%d\n',imageData.yDim);
% fprintf(fileHandle,'YDimension:%d\n',imageData.xDim);
fprintf(fileHandle,'XDimension:%d\n',imageData.XDimension);
fprintf(fileHandle,'YDimension:%d\n',imageData.YDimension);
fprintf(fileHandle,'ZDimension:%d\n',imageData.ZDimension);
fprintf(fileHandle,'XPixelPhysicalSize:%f\n',imageData.XPixelPhysicalSize);
fprintf(fileHandle,'YPixelPhysicalSize:%f\n',imageData.YPixelPhysicalSize);
fprintf(fileHandle,'ZPixelPhysicalSize:%f\n',imageData.ZPixelPhysicalSize);
if (isfield(imageData,'XPosition'))
fprintf(fileHandle,'XPosition:%f\n',imageData.XPosition);
end
if (isfield(imageData,'YPosition'))
fprintf(fileHandle,'YPosition:%f\n',imageData.YPosition);
end
if (isfield(imageData,'XDistanceUnits'))
fprintf(fileHandle,'XDistanceUnits:%s\n',imageData.XDistanceUnits);
end
if (isfield(imageData,'YDistanceUnits'))
fprintf(fileHandle,'YDistanceUnits:%s\n',imageData.YDistanceUnits);
end
if (isfield(imageData,'ZDistanceUnits'))
fprintf(fileHandle,'ZDistanceUnits:%s\n',imageData.ZDistanceUnits);
end
if (isfield(imageData,'XLength'))
fprintf(fileHandle,'XLength:%f\n',imageData.XLength);
end
if (isfield(imageData,'YLength'))
fprintf(fileHandle,'YLength:%f\n',imageData.YLength);
end
if (isfield(imageData,'ZLength'))
fprintf(fileHandle,'ZLength:%f\n',imageData.ZLength);
end
if (isfield(imageData,'StartCaptureDate'))
fprintf(fileHandle,'StartCaptureDate:%s\n',imageData.StartCaptureDate);
end
if (isfield(imageData,'TimeStampDeltas'))
for t=1:imageData.NumberOfFrames
for z=1:imageData.ZDimension
for c=1:imageData.NumberOfChannels
fprintf(fileHandle,'TimeStampDelta(%d,%d,%d):%f\n',c,t,z,imageData.TimeStampDeltas(z,c,t));
end
end
end
end
fclose(fileHandle);
fprintf('Done\n');
end
\ No newline at end of file
function [ imageOut ] = imageConvert( imageIn, typ )
%IMAGECONVERT converts image from current type into the specified type
w = whos('imageIn');
if (strcmpi(w.class,typ))
imageOut = imageIn;
else
imageIn = double(imageIn);
switch w.class
case 'uint8'
imtemp = imageIn ./ (2^8-1);
case 'uint16'
if (max(imageIn(:))<=2^12-1)
imtemp = imageIn ./(2^12-1);
else
imtemp = imageIn ./ (2^16-1);
end
case 'int16'
imtemp = (imageIn+(2^16/2 - 1)) ./ (2^16-1);
case 'uint32'
imtemp = imageIn ./ 2^32-1;
case 'int32'
imtemp = (imageIn+(2^32/2 -1)) ./ (2^32-1);
case 'single'
imtemp = (imageIn+abs(min(imageIn(:)))) ./ max(imageIn(:));
case 'double'
imtemp = (imageIn+abs(min(imageIn(:)))) ./ max(imageIn(:));
case 'logical'
imtemp = imageIn;
otherwise
error('Unkown type of image to convert!');
end
switch typ
case 'uint8'
imageOut = uint8(imtemp*(2^8-1));
case 'uint16'
imageOut = uint16(imtemp*(2^16-1));
case 'int16'
if (max(imtemp(:))<=0.5)
imageOut = int16(imtemp*(2^16-1));
else
imageOut = int16((imtemp-0.5)*(2^16/2-1));
end
case 'uint32'
imageOut = uint32(imtemp*(2^32-1));
case 'int32'
if (max(imtemp(:))<=0.5)
imageOut = int32(imtemp*(2^32-1));
else
imageOut = int32((imtemp-0.5)*(2^32/2-1));
end
case 'single'
imageOut = single(imtemp);
case 'double'
imageOut = imtemp;
case 'logical'
imageOut = imtemp>0;
otherwise
error('Unkown type of image to convert to!');
end
clear imtemp
end
end
function [imageData,rootDir] = readMetaData(root)
imageData = [];
rootDir = [];
if (~exist('root','var') || isempty(root))
rootDir = uigetdir('');
if rootDir==0,
[fileName,rootDir,filterIndex] = uigetfile('.txt');
if (filterIndex~=0)
fileHandle = fopen(fullfile(rootDir,fileName));
imageData = readfile(fileHandle);
end
return
end
elseif (~isempty(strfind(root,'.txt')))
fileHandle = fopen(root);
imageData = readfile(fileHandle);
pos = strfind(root,'\');
rootDir = root(1:pos(end));
imageData.imageDir = rootDir;
return
else
rootDir = root;
end
imageData = [];
dlist = dir(rootDir);
for i=1:length(dlist)
if (strcmp('..',dlist(i).name))
continue;
end
dSublist = dir(fullfile(rootDir,dlist(i).name,'*.txt'));
if isempty(dSublist), continue, end
for j=1:length(dSublist)
fileHandle = fopen(fullfile(rootDir,dlist(i).name,dSublist(j).name));
imageDatum = readfile(fileHandle);
if (isempty(imageDatum)), continue, end
if (isempty(imageData))
imageData = imageDatum;
else
imageData(length(imageData)+1) = imageDatum;
end
end
if strcmpi('.',dlist(i).name)
imageData.imageDir = rootDir;
else
imageData.imageDir = fullfile(rootDir,dlist(i).name);
end
end
rootDir = imageData.imageDir;
end
function imageDatum = readfile(fileHandle)
imageDatum = {};
if fileHandle<=0, return, end
data = textscan(fileHandle,'%s %s', 'delimiter',':','whitespace','\n');
fclose(fileHandle);
if isempty(data), return, end
for k=1:length(data{1})
val = str2double(data{2}{k});
if (isnan(val))
val = data{2}{k};
end
if (any(strfind(data{1}{k},'TimeStampDelta')))
if (~isfield(imageDatum,'TimeStampDeltas'))
imageDatum.TimeStampDeltas = zeros(imageDatum.ZDimension,imageDatum.NumberOfChannels,imageDatum.NumberOfFrames);
end
plane = textscan(data{1}{k},'TimeStampDeltas(%d,%d,%d)%s');
imageDatum.TimeStampDeltas(plane{1},plane{2},plane{3}) = val;
else
imageDatum.(data{1}{k}) = val;
end
end
imageData.imageDir = '.';
end
function [im, imageData] = tiffReader(outType,chanList,timeList,zList,path)
im = [];
imageData = [];
if (~exist('path','var') || ~exist(path,'file'))
path = [];
end
[imageData,path] = readMetaData(path);
if (isempty(imageData))
warning('No image read!');
return
end
if (~exist('chanList','var') || isempty(chanList))
chanList = 1:imageData.NumberOfChannels;
end
if (~exist('timeList','var') || isempty(timeList))
timeList = 1:imageData.NumberOfFrames;
end
if (~exist('zList','var') || isempty(zList))
zList = 1:imageData.ZDimension;
end
if (~exist('outType','var') || isempty(outType))
imInfo = imfinfo(fullfile(path,sprintf('%s_c%02d_t%04d_z%04d.tif',imageData.DatasetName,1,1,1)),'tif');
bytes = imInfo.BitDepth/8;
if (imInfo.BitDepth==8)
outType = 'uint8';
elseif (imInfo.BitDepth==16)
outType = 'uint16';
elseif (imInfo.BitDepth==32)
outType = 'uint32';
else
outType = 'double';
end
elseif (strcmp(outType,'double'))
bytes=8;
outType = 'double';
elseif (strcmp(outType,'uint32') || strcmp(outType,'int32') || strcmp(outType,'single'))
bytes=4;
elseif (strcmp(outType,'uint16') || strcmp(outType,'int16'))
bytes=2;
elseif (strcmp(outType,'uint8'))
bytes=1;
else
error('Unsupported Type');
end
imType = imread(fullfile(path,sprintf('%s_c%02d_t%04d_z%04d.tif',imageData.DatasetName,chanList(1),timeList(1),zList(1))),'tif');
typ = whos('imType');
clear imType
imtemp = zeros(imageData.YDimension,imageData.XDimension,length(zList),typ.class);
im = zeros(imageData.YDimension,imageData.XDimension,length(zList),length(chanList),length(timeList),outType);
fprintf('Type:%s ',outType);
fprintf('(');
fprintf('%d',size(im,2));
fprintf(',%d',size(im,1));
for i=3:length(size(im))
fprintf(',%d',size(im,i));
end
fprintf(') %5.2fMB\n', (imageData.XDimension*imageData.YDimension*length(zList)*length(chanList)*length(timeList)*bytes)/(1024*1024));
for t=1:length(timeList)
for c=1:length(chanList)
for z=1:length(zList)
try
imtemp(:,:,z) = imread(fullfile(path,sprintf('%s_c%02d_t%04d_z%04d.tif',imageData.DatasetName,chanList(c),timeList(t),zList(z))),'tif');
catch err
fprintf('\n****%s: %s\n',fullfile(path,sprintf('%s_c%02d_t%04d_z%04d.tif',imageData.DatasetName,chanList(c),timeList(t),zList(z))),err.identifier);
end
end
w = whos('imtemp');
if (strcmpi(w.class,outType))
im(:,:,:,c,t) = imtemp;
else
im(:,:,:,c,t) = imageConvert(imtemp,outType);
end
end
end
clear imtemp
end
function tiffWriter(im,prefix,imageData)
if (exist('imageData','var') && ~isempty(imageData))
idx = strfind(prefix,'\');
if (isempty(idx))
idx = length(prefix);
end
createMetadata(prefix(1:idx(end)),imageData);
end
sizes = size(im);
numDim = length(sizes);
if numDim<5
frames= 1;
else
frames = sizes(5);
end
if numDim<4
channels = 1;
else
channels = sizes(4);
end
if numDim<3
stacks = 1;
else
stacks = sizes(3);
end
imUint = uint8(im);
for t=1:frames
for c=1:channels
for z=1:stacks
fileName = sprintf('%s_c%02d_t%04d_z%04d.tif',prefix,c,t,z);
imwrite(imUint(:,:,z,c,t),fileName,'tif','Compression','lzw');
end
end
end
fprintf('Wrote %s_c%d_t%d_z%d.tif\n',prefix,channels,frames,stacks);
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment