forked from liu-yikang/rat_rsfmri_preprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyimoverlay.m
More file actions
199 lines (169 loc) · 5.02 KB
/
myimoverlay.m
File metadata and controls
199 lines (169 loc) · 5.02 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
function [hF,hB] = myimoverlay(B,F,climF,climB,cmap,alpha,haxes)
% IMOVERLAY(B,F) displays the image F transparently over the image B.
% If the image sizes are unequal, image F will be scaled to the aspect
% ratio of B.
%
% [hF,hB] = imoverlay(B,F,[low,high]) limits the displayed range of data
% values in F. These values map to the full range of values in the
% current colormap.
%
% [hF,hB] = imoverlay(B,F,[],[low,high]) limits the displayed range of
% data values in B.
%
% [hF,hB] = imoverlay(B,F,[],[],map) applies the colormap to the figure.
% This can be an array of color values or a preset MATLAB colormaps
% (e.g. 'jet' or 'hot').
%
% [hF,hB] = imoverlay(B,F,[],[],[],alpha) sets the transparency level to
% alpha with the range 0.0 <= alpha <= 1.0, where 0.0 is fully
% transparent and 1.0 is fully opaque.
%
% [hF,hB] = imoverlay(B,F,[],[],[],[],ha) displays the overlay in the
% axes with handle ha.
%
% [hF,hB] = imoverlay(...) returns the handles to the front and back
% images.
%
%
% Author: Matthew Smith / University of Wisconsin / Department of Radiology
% Date created: February 6, 2013
% Last modified: Jan 2, 2015
%
%
% Examples:
%
% % Overlay one image transparently onto another
% imB = phantom(256); % Background image
% imF = rgb2gray(imread('ngc6543a.jpg')); % Foreground image
% [hf,hb] = imoverlay(imB,imF,[40,180],[0,0.6],'jet',0.6);
% colormap('parula'); % figure colormap still applies
%
%
% % Use the interface for flexibility
% imoverlay_tool;
%
%
% See also IMOVERLAY_TOOL, IMAGESC, HOLD, CAXIS.
ALPHADEFAULT = 0.4; % Default transparency value
CMAPDEFAULT = 'jet';
if nargin == 0,
try
imoverlay_tool;
return;
catch
errordlg('Cannot find imoverlay_tool.', 'Error');
end
end
% Check image sizes
if size(B,3) > 1
error('Back image has %d dimensions!\n',length(size(B)));
end
if size(F,3) > 1
error('Front image has %d dimensions!\n',length(size(F)));
end
if ~isequal(size(B),size(F))
fprintf('Warning! Image sizes unequal. Undesired scaling may occur.\n');
end
% Check arguments
if nargin < 7
haxes = [];
end
if nargin < 6 || isempty(alpha)
alpha = ALPHADEFAULT;
end
if nargin < 5 || isempty(cmap)
cmap = CMAPDEFAULT;
end
if nargin < 4 || isempty(climB)
climB = [min(B(:)), max(B(:))];
end
if nargin < 3 || isempty(climF)
climF = [min(F(:)), max(F(:))];
end
if abs(alpha) > 1
error('Alpha must be between 0.0 and 1.0!');
end
F(F<climF(1)) = climF(1); % 03/20/16
% Create a figure unless axes is provided
if isempty(haxes) || ~ishandle(haxes)
f=figure('Visible','off',...
'Units','pixels','Renderer','opengl');
pos = get(f,'Position');
set(f,'Position',[pos(1),pos(2),size(B,2),size(B,1)]);
haxes = axes;
set(haxes,'Position',[0,0,1,1]);
movegui(f,'center');
% Create colormap
cmapSize = 100; % default size of 60 shows visible discretization
if ischar(cmap)
try
cmap = eval([cmap '(' num2str(cmapSize) ');']);
catch
fprintf('Colormap ''%s'' is not supported. Using ''jet''.\n',cmapName);
cmap = jet(cmapSize);
end
end
end
% To have a grayscale background, replicate image to 3-channels
B = repmat(mat2gray(double(B),double(climB)),[1,1,3]);
% Display the back image
axes(haxes);
hB = imagesc(B);axis image off;
% set(gca,'Position',[0,0,1,1]);
% Add the front image on top of the back image
hold on;
hF = imagesc(F,climF);
% If images are different sizes, map the front image to back coordinates
set(hF,'XData',get(hB,'XData'),...
'YData',get(hB,'YData'))
% Make the foreground image transparent
alphadata = alpha.*(F >= climF(1));
set(hF,'AlphaData',alphadata);
colormap(haxes,cmap);
if exist('f')
set(f,'Visible','on');
end
% Novel colormaps
%
% JET2 is the same as jet but with black base
function J = jet2(m)
if nargin < 1
m = size(get(gcf,'colormap'),1);
end
J = jet(m); J(1,:) = [0,0,0];
end
% JET3 is the same as jet but with white base
function J = jet3(m)
if nargin < 1
m = size(get(gcf,'colormap'),1);
end
J = jet(m); J(1,:) = [1,1,1];
end
% PARULA2 is the same as parula but with black base
function J = parula2(m)
if nargin < 1
m = size(get(gcf,'colormap'),1);
end
J = parula(m); J(1,:) = [0,0,0];
end
% HSV2 is the same as HSV but with black base
function map = hsv2(m)
map =hsv;
map(1,:) = [0,0,0];
end
% HSV3 is the same as HSV but with white base
function map = hsv3(m)
map =hsv;
map(1,:) = [1,1,1];
end
% HSV4 a slight modification of hsv (Hue-saturation-value color map)
function map = hsv4(m)
if nargin < 1, m = size(get(gcf,'colormap'),1); end
h = (0:m-1)'/max(m,1);
if isempty(h)
map = [];
else
map = hsv2rgb([h h ones(m,1)]);
end
end
end