-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptosplit.m
124 lines (99 loc) · 3.19 KB
/
optosplit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
%OPTOSPLIT
%
% Given a calibration file, splits a 3-dimensional timelapse of images
% (X,Y,t) into a 4-dimensional timelapse of images (X,Y,t,c).
function [ imout ] = optosplit( varargin )
% check inputs
numvarargs = length( varargin );
if numvarargs == 0
% prompt for user file selection
% select alignment file from ImageJ
[ name, path ] = uigetfile( {
'*.mat','.mat alignment';...
'*.tif','.tif alignment';...
'*.ali','.ali alignment' },...
'Please select the alignment file' );
alignFile = strcat( path, name );
% select image file
[ name, path ] = uigetfile( '.tif',...
'Please select the image file' );
imFile = strcat( path, name );
% select output file
[ name, path ] = uiputfile( '.tif',...
'Please select output file' );
imout = strcat( path, name );
elseif numvarargs == 3
% files were provided
alignFile = varargin{ 1 };
imFile = varargin{ 2 };
imout = varargin{ 3 };
else
error( 'optosplit takes three input arguments - file locations for the alignment file, the image file, and output location' );
end
% get alignment
[ ~, ~, ext ] = fileparts( alignFile );
if strcmp( ext, '.ali' )
% read in the java object containing the alignment box
% open alignment file
fin = java.io.FileInputStream( alignFile );
% prepare object streamer
ois = java.io.ObjectInputStream( fin );
% read off first rectangle
r1 = jr2mr( ois.readObject() )
% and second rectangle
r2 = jr2mr( ois.readObject() )
% close the streamers
clear( 'fin', 'ois' );
elseif strcmp( ext, '.tif' )
% run the .tif file through alignment.m
[ r1, r2 ] = alignment( alignFile );
elseif strcmp( ext, '.mat' )
% read in the rectangles from the pre-generated mat file
load( alignFile );
end
% delete the file if it currently exists
if isequal( exist( imout, 'file' ), 2 )
delete( imout )
end
% check how many time steps there are
timeSteps = length( imfinfo( imFile ) );
if numvarargs == 0
% only display waitbar if we're working interactively
h = waitbar(0,'Splitting Images');
end
for t = 1:timeSteps
% load the time frame
im = imread( imFile, t );
% pick out the two images
i1 = imcrop( im, r1 );
i2 = imcrop( im, r2 );
% add them to the output file
imwrite( i1, imout, 'WriteMode', 'Append' );
imwrite( i2, imout, 'WriteMode', 'Append' );
if numvarargs == 0
% update progress
waitbar( t/timeSteps, h )
end
end
% close the wait bar
if numvarargs == 0
close(h)
end
% fiddle with the metadata to make imageJ display it as a hyperstack
% open image for modification
tiffFile = Tiff( imout, 'r+' );
% add the necessary metadata
tiffFile.setTag( 'ImageDescription', sprintf('ImageJ=1.46k\nimages=%i\nchannels=2\nframes=%i\nhyperstack=true\nloop=false', timeSteps*2, timeSteps) )
% commit changes
tiffFile.rewriteDirectory();
% close handler
tiffFile.close();
end
function [ imout ] = imcrop( im, r )
% crops an image given a java rectangle class OR a matlab struct containing
% x, y, width and height
imout = im(r.y1:r.y2, r.x1:r.x2);
end
function [ r ] = jr2mr( r )
r = MyRect( r.x, r.x+r.width, r.y, r.y+r.height );
end