forked from sharktank-bic/qMTLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeMontage.m
187 lines (65 loc) · 2.44 KB
/
makeMontage.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
function [img,coords] = makeMontage(imgCube, sliceList, fileName, numAcross, backVal)
%img = makeMontage(imgCube, [sliceList], [fileName], [numAcross], [backVal])
%
% Compiles a montage image from the images in imgCube.
% (imgCube is x by y by sliceNum)
% sliceList, if specified, determines which slices to extract.
% (it defaults to all slices if it's empty or omitted)
% The image can be displayed with something like:
%
% figure; imagesc(img); colormap(gray); axis equal; axis off;
%
% If a fileName is specified, the image will be saved to that file.
% Note that the image file format type depends on the filename extension
% (see imwrite).
%
% see also makeMontageIfiles
if(~exist('sliceList', 'var')) sliceList = []; end
if(~exist('numAcross', 'var')) numAcross = []; end
if(~exist('fileName', 'var')) fileName = []; end
if(~exist('backVal', 'var')) backVal = []; end
if(size(imgCube,4)==3)
[img(:,:,1),coords] = makeMontage(imgCube(:,:,:,1), sliceList, fileName, numAcross,backVal);
img(:,:,2) = makeMontage(imgCube(:,:,:,2), sliceList, fileName, numAcross,backVal);
img(:,:,3) = makeMontage(imgCube(:,:,:,3), sliceList, fileName, numAcross,backVal);
return;
end
% Sanity check
if(any(size(imgCube)>10000))
error('At least one dimension of input image is >10,000- refusing to continue...');
end
header = 0;
if(isempty(sliceList))
sliceList = [1:size(imgCube, 3)];
end
sz = size(imgCube);
r = sz(1); c = sz(2);
nImages = length(sliceList);
if(isempty(numAcross))
numAcross = ceil(sqrt(nImages)*sqrt((r/c)));
end
numDown = ceil(nImages/numAcross);
if(isempty(backVal)) backVal = 0; end
img = ones(r*numDown,c*numAcross)*backVal;
eval(['img = ',class(imgCube),'(img);']);
count = 0;
for ii = 1:length(sliceList)
curSlice = sliceList(ii);
count = count+1;
x = rem(count-1, numAcross)*c;
y = floor((count-1)/ numAcross)*r;
img(y+1:y+r,x+1:x+c) = imgCube(:,:,curSlice);
coords(ii,:) = [x+1,y+1];
end
if(~isempty(fileName))
imgMin = min(img(:));
imgMax = max(img(:));
img2 = uint8(floor((double(img)+imgMin)./(imgMax-imgMin)*255));
%figure; colormap(repmat([0:1/255:1]',1,3)); image(img2); axis image; axis off;
[p,f,e] = fileparts(fileName);
if(isempty(e))
fileName = [fileName, '.jpg'];
end
imwrite(img2, fileName);
disp(['Wrote montage to ' fullfile(pwd, fileName)]);
end