matlab中,在灰度解剖图上叠加阈值图,by by DR. Rajeev Raizada

简介:

1.参考 reference

 

  1. tutorial主页:http://www.bcs.rochester.edu/people/raizada/fmri-matlab.htm

  2.speech_brain_images.mat数据:speech_brain_images.mat

  3.showing_brain_images_tutorial显示大脑图像代码:showing_brain_images_tutorial.m 。

  4.overlaying_Tmaps_tutorial.m叠加t检验统计图:overlaying_Tmaps_tutorial.m

 

2.程序执行过程 run step by step

 

 目的:在灰度解剖图上叠加显示彩色阈值图。有一个难点:Unfortunately, it isn't quite that easy, because Matlab only lets you have one colormap per figure window.这里提出了这样的解决方案:就是把灰度图映射成真实的RGB真彩图。

 

2.1 对解剖图和功能图进行灰度值归一化,归一化至[1,64]区间,然后,将两个图像转成RGB图像,显示归一化后的解剖图,灰度方式显示,不是RGB:

%%%%%%% First, load in the file containing the brain images
load speech_brain_images.mat

%%%% Let's look at the 18th axial slice.
%%%% This goes through Heschl's gyrus, which is auditory cortex

axial_slice_number = 18;

%%%% Read the values from the 3D matrix subj_3danat
anat_slice_vals = subj_3danat(:,:,axial_slice_number);

%%%% Use squeeze to get rid of any redundant 3rd dimension that
%%%% might be left over. This is described in
%%%% more detail in showing_brain_images_tutorial.m

anat_slice_2D = squeeze(anat_slice_vals);


%%%% In order to make it easier for us to match up the
%%%% anatomical values to the 64-entries in Matlab's standard
%%%% gray colormap, we're going to scale the anatomical image
%%%% values so that they are all integers going from 1 to 64

%%%% First scale it from 0 to 1 by dividing by the maximum value.
%%%% Because it's a 2D matrix, we have to perform the max() operation
%%%% twice, once for each dimension. Then we end up with one max value,
%%%% which we can use as a divisor.

anat01 = anat_slice_2D ./ max(max(anat_slice_2D));

%%%% Next, multiply by 63 and round to the nearest whole number
anat0_63 = round( 63 * anat01 );

%%%% Finally, add 1, in order to make a matrix that goes from 1 to 64

anat64 = anat0_63 + 1;


%%%%%%%%%%%% Now let's do the same for the thresholded T-map

Tmap_slice_vals = speech_Tmap(:,:,axial_slice_number);

Tmap_slice_2D = squeeze(Tmap_slice_vals);

T_threshold = 1; %%% Let's pick a fairly low threshold, because it helps
%%% for showing the effect of transparent overlay later.

%%%% To threshold the map, make a logical 0/1 matrix that says
%%%% whether each voxel is above-threshold or not, and multiply
%%%% it element-by-element by the raw T-map values.
%%%% This is explained in a more step-by-step way 
%%%% in the companion program showing_brain_images_tutorial.m

thresholded_Tmap = ( Tmap_slice_2D > T_threshold ) .* Tmap_slice_2D;

Tmap01 = thresholded_Tmap ./ max(max(thresholded_Tmap));

Tmap0_63 = round( 63 * Tmap01 );

Tmap64 = 1 + Tmap0_63;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ok, now we have made two matrices scaled from 1 to 64,
% one for the anatomical image, and the other for the T-map image
%
% The next step is to make a full RGB-image for each image,
% by looking up the corresponding rows of the gray and hot colormaps.
%
% An RGB-image is a 3D-matrix.
% It has three "colour slabs" that are joined up along the third dimension.
% The first slab is an image full of Red values.
% The second slab is an image full of Green values.
% And the third slab is an image of Blue values.

% First make 3D matrices full of zeros, to hold the values
% that we are about to create for the anatomical and T-map RGB images.
% These have three slabs in the 3rd dimension, and are the same size as 
% the anatomical and the Tmap matrices in the first two dimensions.

anat_RGB = zeros(size(anat64,1), size(anat64,2), 3);
%%% This will hold the anatomical image's RBG values

Tmap_RGB = zeros(size(Tmap64,1), size(Tmap64,2), 3);
%%% This will hold the T-map image's RBG values

%%% Now, let's get hold of copies of Matlab's gray and hot
%%% colormap matrices. As far as I know, we need to make a figure
%%% first in order to do this. That's fine, because we want to 
%%% look at our newly created 1-to-64 scaled images anyway.

figure(1);
clf;
image(anat64); %%% Note that this is image(), not imagesc() !!!
%%% This looks the same as if we had used imagesc,
%%% because the image values are *already* scaled
%%% to cover the whole colormap, from black to white
colormap gray;
axis('image');
colorbar;
title('The anatomical image after we have scaled it from 1 to 64');

 

% Now that we are in colormap gray, let's make a copy of the colormap matrix

gray_cmap = colormap; %%% This reads in the current colormap matrix

  

2.2 显示归一化后的功能图,hot方式显示,不是RGB:

figure(2);
clf;
image(Tmap64); %%% Again, note that we are using image(), not imagesc()
colormap hot;
axis('image');
colorbar;
title('The T-map image after we have scaled it from 1 to 64');

%%%%% Make a copy of the new current colormap matrix, which is now hot.
hot_cmap = colormap;

  

2.3 构造RGB解剖图,并按照RGB方式显示解剖图:

for RGB_dim = 1:3, %%% Loop through the three slabs: R, G, and B

%%% Each entry in each 1-to-64-scaled matrix gives us a row 
%%% in the colormap matrix to go look-up. 
%%% That row has three columns: the R, G and B values for that colour.
gray_cmap_rows_for_anat = anat64;
hot_cmap_rows_for_Tmap = Tmap64;

%%% We'll read in one of these R, G, or B values at a time,
%%% depending on which colour-slab we're building.
%%% The colormap entry we want is in the RGB_dim-th column,
%%% and in the row that we determined above.
%%% Note that we're actually looking up all the rows at once,
%%% because we're giving Matlab an entire matrix in the row-position.

colour_slab_vals_for_anat = gray_cmap(gray_cmap_rows_for_anat, RGB_dim);
colour_slab_vals_for_Tmap = hot_cmap(hot_cmap_rows_for_Tmap, RGB_dim);

%%%% These colour-slab values turn out to be in column vector format,
%%%% and need to be reshaped into being the same size as image matrices.

anat_RGB(:,:,RGB_dim) = reshape( colour_slab_vals_for_anat, size(anat64));
Tmap_RGB(:,:,RGB_dim) = reshape( colour_slab_vals_for_Tmap, size(Tmap64));

end; % End of loop through the RGB dimension.


%%%% Now that we've made our RGB images, let's look at them
%%%% Note that these types of images do *not* have colormaps.
%%%% Every image pixel has three numbers attached to it, not just one,
%%%% and those three numbers define the colour for that pixel directly,
%%%% by giving its R, G and B values.

figure(3);
clf;
image(anat_RGB); 
axis('image');
title('The anatomical RGB image, with no colormap')

  

 

2.4 并按照RGB方式显示功能图:

figure(4);
clf;
image(Tmap_RGB); 
axis('image');
title('The T-map RGB image, with no colormap')

  

 

2.5 对阈值图做透明化处理,然后叠加到解剖图上:

Tmap_opacity = 0.4; % 0.4 is a reasonable opacity value to try first

%%%% Make a place-holder matrix to hold the compound weighted-sum image

compound_RGB = zeros(size(anat64,1), size(anat64,2), 3);
%%% The anat and the T-map are the same size,
%%% we could have chosen either.

%%% Now build up the compound image, one colour-slab at a time, like above.
%%% Where the T-map is below threshold, we only want the anatomical's values.
%%% Where the T-map is above-threshold, we want a weighted sum 
%%% of the T-map RGB values and the anatomical's RGB values.

for RGB_dim = 1:3, %%% Loop through the three slabs: R, G, and B

compound_RGB(:,:,RGB_dim) = ...
(thresholded_Tmap==0) .* ... % Where T-map is below threshold
anat_RGB(:,:,RGB_dim) + ... 
(thresholded_Tmap>0).* ... % Where T-map is above threshold
( (1-Tmap_opacity) * anat_RGB(:,:,RGB_dim) + ...
Tmap_opacity * Tmap_RGB(:,:,RGB_dim) );
% Opacity-weighted sum of anatomical and T-map
end;

%%%% Before displaying our newly-made compound image,
%%%% we have to make sure that none of the RGB values exceeds 1.

compound_RGB = min(compound_RGB,1);


%%%% Now let's look at the fruit of our labours!

figure(5);
clf;
image(compound_RGB); 
axis('image');
title('The T-map transparently overlaid on the anatomical')

  

 

2.6 尝试不同的不透明度进行显示

figure(6);
clf;

for i = 1:4,

Tmap_opacity = 0.25*i; % This will give opacities: 0.25, 0.5, 0.75, 1

%%%%%%%% Go through a copy of the loop above to make a new compound image

for RGB_dim = 1:3, %%% Loop through the three slabs: R, G, and B

compound_RGB(:,:,RGB_dim) = ...
(thresholded_Tmap==0) .* ... % Where T-map is below threshold
anat_RGB(:,:,RGB_dim) + ... 
(thresholded_Tmap>0).* ... % Where T-map is above threshold
( (1-Tmap_opacity) * anat_RGB(:,:,RGB_dim) + ...
Tmap_opacity * Tmap_RGB(:,:,RGB_dim) );
% Opacity-weighted sum of anatomical and T-map
end;
compound_RGB = min(compound_RGB,1);

%%%%%%%% Plot the new compound image
subplot(2,2,i);
image(compound_RGB);
axis('image');
title(['T-map opacity = ' num2str(Tmap_opacity) ]);

end; %%% End of loop through i

  

 

2.7 做对比,那叠加后的解剖图与原始的阈值化的功能图进行比对:

figure(7);
clf;
subplot(2,1,1); %%% Show RGB image and fake colorbar in upper subplot
image(compound_RGB); 
axis('image');

% Now set the "colormap" to be hot.
% This won't affect the RGB image at all, because it doesn't use a colormap,
% but it will give us the right colours in the colorbar.

colormap hot;
h = colorbar; %%% h is the handle of the colorbar

%%%% The colorbar actually goes from 1 to 64, but we can label
%%%% it to go from zero to the maximum T-map value.

max_Tmap_value = max(max(Tmap_slice_vals));

desired_colorbar_labels = [ 0 : 2 : max_Tmap_value ];
%%% Start at 0, go up in steps of 2 until we hit the max value

%%%% We can fit these onto the 1-to-64 scale in the same way as we did above

corresponding_values_on_1_to_64_scale = ...
1 + ( 63 * desired_colorbar_labels / max_Tmap_value );

%%%% Now we can use "set" to make the YTick values and labels be what we want:

set(h,'YTick',corresponding_values_on_1_to_64_scale);
set(h,'YTickLabel',desired_colorbar_labels);

title(['Overlaid T-map with a fake colorbar ' ...
'that has the correct T-map scale on it!' ]);

%%%% In the lower subplot, let's show a real colormap plot and a real colorbar,
%%%% so that we can compare them.

subplot(2,1,2); % The lower subplot
imagesc(thresholded_Tmap); % Note that we are using imagesc here, so that
% we use the full range of the REAL colormap
axis('image');
colorbar;

title(['The same thresholded T-map, but now genuinely ' ...
'using the hot colormap, and with a real colorbar.' ]);

  

 

2.8 这里是提供一种替代方案,不用RGB显示,而是将colormap扩大,以容纳更多的颜色:

split_colormap = [ gray_cmap; hot_cmap ];

% So, the first 64 rows are all gray, and any (unscaled) image intensities
% between 1 and 64 will all be coloured from the gray part of the colormap.
% Any image intensities betwen 65 and 128 will be coloured from the hot part.
%
% Above, we already made versions of our anatomical and T-map images
% that were scaled from 1 to 64. 
% So, we just need to add another 64 to our T-map image, and
% make an image that has the anatomical's values in places where
% the T-map is below-threshold, and that has the T-maps's values 
% in places where the T-map is above-threshold.

Tmap_65_to_128 = 64 + Tmap64; % Tmap64 goes from 1 to 64

image_for_split_colormap = ...
(thresholded_Tmap==0) .* anat64 + ... % Where T-map is below threshold
(thresholded_Tmap>0).* Tmap_65_to_128; % Where T-map is above threshold

figure(8);
clf;
image(image_for_split_colormap); 
colormap(split_colormap);
axis('image');
colorbar;
title('Showing the overlaid T-map using a split colormap');

  

 

 

the end 完整代码

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
%%%% Tutorial on how to show statistical maps overlaid
%%%% on top of anatomical images in Matlab.
%%%% Written by Rajeev Raizada, July  24 2002 .
%%%%
%%%% For a gentle intro to displaying brain images in Matlab,
%%%% look at the program  "showing_brain_images_tutorial.m"
%%%%
%%%% Overlaying T-maps on top of anatomical images is
%%%% quite a bit more tricky than simply showing anatomicals,
%%%% so look at the  "showing_brain_images_tutorial.m"  program
%%%% before  this  one,  if  you want a general intro to image displays.
%%%% This program is not intended to be a self-contained intro
%%%% to Matlab and image-displaying.
%%%% For an intro, you should refer to showing_brain_images_tutorial.m
%%%%
%%%% To run either program, you need the file containing the brain images:
%%%% speech_brain_images.mat ( 2 .3Mb)
%%%%
%%%% Please mail any comments or suggestions to: raj @nmr .mgh.harvard.edu
 
%%%% Often we want to overlay a coloured statistical map
%%%% on top of a grayscale anatomical map.
%%%% If you want a nice, graphical-user- interface  based program that
%%%% will  do  this  for  you, then check out Matthew Brett's display_slices.m,
%%%% which you can download from:
%%%% http: //www.mrc-cbu.cam.ac.uk/Imaging/display_slices.html
%%%%
%%%% The program below shows how you can make such overlays yourself.
%%%% In showing_brain_images_tutorial.m we saw how to use different
%%%% Matlab colormaps. So, you might think that to overlay a T-map
%%%% on top of an anatomical image, we would simply draw the anatomical
%%%% using colormap gray, and then draw the T-map on top of it using
%%%% colormap hot.
%%%% Unfortunately, it isn't quite that easy, because Matlab only lets
%%%% you have one colormap per figure window.
%%%% One way of getting round  this  obstacle would be to make
%%%% a big compound colormap made up of the gray and hot colormaps
%%%% joined together. That's shown at the end of  this  program, in Figure  8 .
%%%%
%%%% However, a more flexible approach is not to use a colormap at all,
%%%% but instead to specify a real RGB (red-green-blue) colour value
%%%%  for  every voxel in the image. The main advantage of  this
%%%% is that it lets you make the coloured statistical overlay transparent,
%%%% by making a weighted-combination of the anatomical
%%%% and statistical images.
%%%% (In Matlab  6 , there is a  new  way of making images transparent,
%%%% using a  new  image-property called  "alphadata" . But the RGB method that
%%%% is shown in  this  program is more general, and works with Matlab  5  too).
%%%%
%%%% To figure out what RGB values to give, we can read in the RGB values
%%%% from the Matlab colormap matrices. That way, we get to use the
%%%% nice colour-scheme of a pre-made Matlab colormap, without being
%%%% bound by the inflexibility that using a real colormap imposes.
%%%%
%%%% The structure of a Matlab colormap matrix is as follows:
%%%% There are  64  rows, and three columns.
%%%% When we image a matrix unscaled, i.e. using image() instead of imagesc(),
%%%% Matlab takes each matrix-element, sees what whole-number from  1  to  64
%%%% its value is closest to, and looks up the values in that row of the
%%%% colormap matrix. The three values in that row are the RGB values
%%%%  for  the colour that  this  element will have when it's drawn in the image.
%%%% E.g. suppose the element has the value  25 . Then that element will
%%%% get shown with the RGB values from the 25th row of the colormap matrix.
%%%%
%%%% The trick that we'll use below will be to scale our anatomical and T-map
%%%% images from  1  to  64 , then read in the RGB values from the corresponding
%%%% rows of the gray and hot colormap matrices.
%%%%
%%%% Then we'll have an RGB image  for  the anatomical that will be
%%%% made only of shades of gray, because it will be made from rows
%%%% of the gray colormap matrix. And we'll have an RGB image  for  the T-map
%%%% that will be made out of rows from the hot colormap matrix.
%%%%
%%%% Then we can make a weighted sum of these two images and display it.
 
%%%%%%% First, load in the file containing the brain images
load speech_brain_images.mat
 
%%%% Let's look at the 18th axial slice.
%%%% This goes through Heschl's gyrus, which is auditory cortex
 
axial_slice_number =  18 ;
 
%%%% Read the values from the 3D matrix subj_3danat
anat_slice_vals = subj_3danat(:,:,axial_slice_number);
 
%%%% Use squeeze to get rid of any redundant 3rd dimension that
%%%% might be left over. This is described in
%%%% more detail in showing_brain_images_tutorial.m
 
anat_slice_2D = squeeze(anat_slice_vals);
 
 
%%%% In order to make it easier  for  us to match up the
%%%% anatomical values to the  64 -entries in Matlab's standard
%%%% gray colormap, we're going to scale the anatomical image
%%%% values so that they are all integers going from  1  to  64
 
%%%% First scale it from  0  to  1  by dividing by the maximum value.
%%%% Because it's a 2D matrix, we have to perform the max() operation
%%%% twice, once  for  each dimension. Then we end up with one max value,
%%%% which we can use as a divisor.
 
anat01 = anat_slice_2D ./ max(max(anat_slice_2D));
 
%%%% Next, multiply by  63  and round to the nearest whole number
anat0_63 = round(  63  * anat01 );
 
%%%% Finally, add  1 , in order to make a matrix that goes from  1  to  64
 
anat64 = anat0_63 +  1 ;
 
 
%%%%%%%%%%%% Now let's  do  the same  for  the thresholded T-map
 
Tmap_slice_vals = speech_Tmap(:,:,axial_slice_number);
 
Tmap_slice_2D = squeeze(Tmap_slice_vals);
 
T_threshold =  1 ;    %%% Let's pick a fairly low threshold, because it helps
                     %%%  for  showing the effect of transparent overlay later.
 
%%%% To threshold the map, make a logical  0 / 1  matrix that says
%%%% whether each voxel is above-threshold or not, and multiply
%%%% it element-by-element by the raw T-map values.
%%%% This is explained in a more step-by-step way
%%%% in the companion program showing_brain_images_tutorial.m
 
thresholded_Tmap = ( Tmap_slice_2D > T_threshold ) .* Tmap_slice_2D;
 
Tmap01 = thresholded_Tmap ./ max(max(thresholded_Tmap));
 
Tmap0_63 = round(  63  * Tmap01 );
 
Tmap64 =  1  + Tmap0_63;
 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ok, now we have made two matrices scaled from  1  to  64 ,
% one  for  the anatomical image, and the other  for  the T-map image
%
% The next step is to make a full RGB-image  for  each image,
% by looking up the corresponding rows of the gray and hot colormaps.
%
% An RGB-image is a 3D-matrix.
% It has three  "colour slabs"  that are joined up along the third dimension.
% The first slab is an image full of Red values.
% The second slab is an image full of Green values.
% And the third slab is an image of Blue values.
 
% First make 3D matrices full of zeros, to hold the values
% that we are about to create  for  the anatomical and T-map RGB images.
% These have three slabs in the 3rd dimension, and are the same size as
% the anatomical and the Tmap matrices in the first two dimensions.
 
anat_RGB = zeros(size(anat64, 1 ), size(anat64, 2 ),  3 );
                     %%% This will hold the anatomical image's RBG values
                     
Tmap_RGB = zeros(size(Tmap64, 1 ), size(Tmap64, 2 ),  3 );
                     %%% This will hold the T-map image's RBG values
 
%%% Now, let 's get hold of copies of Matlab' s gray and hot
%%% colormap matrices. As far as I know, we need to make a figure
%%% first in order to  do  this . That's fine, because we want to
%%% look at our newly created  1 -to- 64  scaled images anyway.
 
figure( 1 );
clf;
image(anat64);  %%% Note that  this  is image(), not imagesc() !!!
                 %%% This looks the same as  if  we had used imagesc,
                 %%% because the image values are *already* scaled
                 %%% to cover the whole colormap, from black to white
colormap gray;
axis( 'image' );
colorbar;
title( 'The anatomical image after we have scaled it from 1 to 64' );
 
% Now that we are in colormap gray, let's make a copy of the colormap matrix
 
gray_cmap = colormap;   %%% This reads in the current colormap matrix
 
%%%%%%%% Now let's  do  the same  for  the hot colormap
 
figure( 2 );
clf;
image(Tmap64);  %%% Again, note that we are using image(), not imagesc()
colormap hot;
axis( 'image' );
colorbar;
title( 'The T-map image after we have scaled it from 1 to 64' );
 
%%%%% Make a copy of the  new  current colormap matrix, which is now hot.
hot_cmap = colormap;
 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Ok, now we have copies of the gray and hot colormap matrices,
% with their  64  rows each, we have anatomical and T-map images scaled
% from  1  to  64 , so that we can match each value to a colormap matrix row,
% and we have empty place-holder matrices to store the looked-up RGB values.
% So, we're ready to look-up and store the appropriate RGB values.
 
for  RGB_dim =  1 : 3 ,  %%% Loop through the three slabs: R, G, and B
 
     %%% Each entry in each  1 -to- 64 -scaled matrix gives us a row
     %%% in the colormap matrix to go look-up.  
     %%% That row has three columns: the R, G and B values  for  that colour.
     gray_cmap_rows_for_anat = anat64;
     hot_cmap_rows_for_Tmap = Tmap64;
     
     %%% We'll read in one of these R, G, or B values at a time,
     %%% depending on which colour-slab we're building.
     %%% The colormap entry we want is in the RGB_dim-th column,
     %%% and in the row that we determined above.
     %%% Note that we're actually looking up all the rows at once,
     %%% because we're giving Matlab an entire matrix in the row-position.
     
     colour_slab_vals_for_anat = gray_cmap(gray_cmap_rows_for_anat, RGB_dim);
     colour_slab_vals_for_Tmap = hot_cmap(hot_cmap_rows_for_Tmap, RGB_dim);
     
     %%%% These colour-slab values turn out to be in column vector format,
     %%%% and need to be reshaped into being the same size as image matrices.
     
     anat_RGB(:,:,RGB_dim) = reshape( colour_slab_vals_for_anat, size(anat64));
     Tmap_RGB(:,:,RGB_dim) = reshape( colour_slab_vals_for_Tmap, size(Tmap64));
 
end;  % End of loop through the RGB dimension.
 
 
%%%% Now that we 've made our RGB images, let' s look at them
%%%% Note that these types of images  do  *not* have colormaps.
%%%% Every image pixel has three numbers attached to it, not just one,
%%%% and those three numbers define the colour  for  that pixel directly,
%%%% by giving its R, G and B values.
 
figure( 3 );
clf;
image(anat_RGB);   
axis( 'image' );
title( 'The anatomical RGB image, with no colormap' )
 
figure( 4 );
clf;
image(Tmap_RGB);   
axis( 'image' );
title( 'The T-map RGB image, with no colormap' )
 
 
% You might be thinking: my RGB images in Figs. 3  and  4
% look exactly like my colormap images in Fig. 1  and  2  !!
% Why did I go to all that work just to make images that end up
% looking exactly the same as the ones I started with ?
 
% The answer is that now we can make a compound image that is
% a mixture of the anatomical image and the T-map transparently
% overlaid on top of it.
%
% We achieve the transparency by taking a weighted sum of the
% anatomical image and the T-map image.
% The more weighting we give to the T-map, the more opaque it will look
% when we overlay it on top of the anatomical.
 
% Let's make the opacity range from  0  to  1 , with  0  being fully transparent.
 
Tmap_opacity =  0.4 ;     %  0.4  is a reasonable opacity value to  try  first
 
%%%% Make a place-holder matrix to hold the compound weighted-sum image
 
compound_RGB = zeros(size(anat64, 1 ), size(anat64, 2 ),  3 );
                     %%% The anat and the T-map are the same size,
                     %%% we could have chosen either.
                     
%%% Now build up the compound image, one colour-slab at a time, like above.
%%% Where the T-map is below threshold, we only want the anatomical's values.
%%% Where the T-map is above-threshold, we want a weighted sum
%%% of the T-map RGB values and the anatomical's RGB values.
                 
for  RGB_dim =  1 : 3 ,  %%% Loop through the three slabs: R, G, and B
 
     compound_RGB(:,:,RGB_dim) = ...
         (thresholded_Tmap== 0 ) .* ...    % Where T-map is below threshold
             anat_RGB(:,:,RGB_dim) + ... 
         (thresholded_Tmap> 0 ).* ...      % Where T-map is above threshold
             ( ( 1 -Tmap_opacity) * anat_RGB(:,:,RGB_dim) + ...
                Tmap_opacity * Tmap_RGB(:,:,RGB_dim) );
                         % Opacity-weighted sum of anatomical and T-map
end;
 
%%%% Before displaying our newly-made compound image,
%%%% we have to make sure that none of the RGB values exceeds  1 .
 
compound_RGB = min(compound_RGB, 1 );
 
 
%%%% Now let's look at the fruit of our labours!
 
figure( 5 );
clf;
image(compound_RGB);   
axis( 'image' );
title( 'The T-map transparently overlaid on the anatomical' )
 
 
%%%% To look at the effect of varying the opacity, let's  try  a few values
 
figure( 6 );
clf;
 
for  i =  1 : 4 ,
 
     Tmap_opacity =  0.25 *i;   % This will give opacities:  0.25 0.5 0.75 1
 
     %%%%%%%% Go through a copy of the loop above to make a  new  compound image
     
     for  RGB_dim =  1 : 3 ,  %%% Loop through the three slabs: R, G, and B
 
         compound_RGB(:,:,RGB_dim) = ...
         (thresholded_Tmap== 0 ) .* ...    % Where T-map is below threshold
             anat_RGB(:,:,RGB_dim) + ... 
         (thresholded_Tmap> 0 ).* ...      % Where T-map is above threshold
             ( ( 1 -Tmap_opacity) * anat_RGB(:,:,RGB_dim) + ...
                Tmap_opacity * Tmap_RGB(:,:,RGB_dim) );
                         % Opacity-weighted sum of anatomical and T-map
     end;
     compound_RGB = min(compound_RGB, 1 );
 
     %%%%%%%% Plot the  new  compound image
     subplot( 2 , 2 ,i);
     image(compound_RGB);
     axis( 'image' );
     title([ 'T-map opacity = '  num2str(Tmap_opacity) ]);
     
end;    %%% End of loop through i
 
 
%%%%%%%%% Even though these RGB images don't have colormaps,
%%%%%%%%% we can make a fake colorbar showing the real T-map values.
%%%%%%%%% We'll make a colorbar, then use  "set"  and  "get"  to manipulate it.
 
% Let's have two subplots.
% In the upper subplot, we'll manufacture a fake colorbar
% that will show the the real T-map scaling.
%
% In the lower subplot, we'll show a real colorbar,
% so that we can check that they look the same
 
figure( 7 );
clf;
subplot( 2 , 1 , 1 );         %%% Show RGB image and fake colorbar in upper subplot
image(compound_RGB);   
axis( 'image' );
 
% Now set the  "colormap"  to be hot.
% This won 't affect the RGB image at all, because it doesn' t use a colormap,
% but it will give us the right colours in the colorbar.
 
colormap hot;
h = colorbar;   %%% h is the handle of the colorbar
 
%%%% The colorbar actually goes from  1  to  64 , but we can label
%%%% it to go from zero to the maximum T-map value.
 
max_Tmap_value = max(max(Tmap_slice_vals));
 
desired_colorbar_labels = [  0  2  : max_Tmap_value ];
         %%% Start at  0 , go up in steps of  2  until we hit the max value
 
%%%% We can fit these onto the  1 -to- 64  scale in the same way as we did above
 
corresponding_values_on_1_to_64_scale = ...
     1  +  (  63  * desired_colorbar_labels / max_Tmap_value );
 
%%%% Now we can use  "set"  to make the YTick values and labels be what we want:
 
set(h, 'YTick' ,corresponding_values_on_1_to_64_scale);
set(h, 'YTickLabel' ,desired_colorbar_labels);
 
title([ 'Overlaid T-map with a fake colorbar '  ...
        'that has the correct T-map scale on it!'  ]);
 
%%%% In the lower subplot, let's show a real colormap plot and a real colorbar,
%%%% so that we can compare them.
 
subplot( 2 , 1 , 2 );             % The lower subplot
imagesc(thresholded_Tmap);  % Note that we are using imagesc here, so that
                             % we use the full range of the REAL colormap
axis( 'image' );
colorbar;
 
title([ 'The same thresholded T-map, but now genuinely '  ...
        'using the hot colormap, and with a real colorbar.'  ]);
 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Finally, let's  try  using a  "split colormap" .
% This is simpler than the RGB approach, but can't give transparent overlays.
%
% To use a split colormap, we make a  new  128 -row colormap matrix
% by stacking the gray colormap on top of the hot colormap.
% Note that we already made matrices holding these two colormaps, above.
% We called the matrices gray_cmap and hot_cmap.
 
split_colormap = [ gray_cmap; hot_cmap ];
 
% So, the first  64  rows are all gray, and any (unscaled) image intensities
% between  1  and  64  will all be coloured from the gray part of the colormap.
% Any image intensities betwen  65  and  128  will be coloured from the hot part.
%
% Above, we already made versions of our anatomical and T-map images
% that were scaled from  1  to  64 .
% So, we just need to add another  64  to our T-map image, and
% make an image that has the anatomical's values in places where
% the T-map is below-threshold, and that has the T-maps's values
% in places where the T-map is above-threshold.
 
Tmap_65_to_128 =  64  + Tmap64;   % Tmap64 goes from  1  to  64
 
image_for_split_colormap = ...
     (thresholded_Tmap== 0 ) .* anat64  + ...  % Where T-map is below threshold
     (thresholded_Tmap> 0 ).* Tmap_65_to_128;  % Where T-map is above threshold
 
figure( 8 );
clf;
image(image_for_split_colormap);   
colormap(split_colormap);
axis( 'image' );
colorbar;
title( 'Showing the overlaid T-map using a split colormap' );

  本文转自二郎三郎博客园博客,原文链接:http://www.cnblogs.com/haore147/p/3634471.html,如需转载请自行联系原作者

相关文章
|
编解码
MATLAB | 科研绘图第十一期动态趋势图
MATLAB | 科研绘图第十一期动态趋势图
546 0
|
存储 编解码
MATLAB | 科研绘图第十八期散点密度图
MATLAB | 科研绘图第十八期散点密度图
938 0
|
存储 编解码
MATLAB | 科研绘图第十九期散点密度图强化版
MATLAB | 科研绘图第十九期散点密度图强化版
364 0
|
编解码
MATLAB | 科研绘图第二十三期箭头图
MATLAB | 科研绘图第二十三期箭头图
402 0
|
算法 JavaScript 数据安全/隐私保护
基于GA遗传优化的最优阈值计算认知异构网络(CHN)能量检测算法matlab仿真
本内容介绍了一种基于GA遗传优化的阈值计算方法在认知异构网络(CHN)中的应用。通过Matlab2022a实现算法,完整代码含中文注释与操作视频。能量检测算法用于感知主用户信号,其性能依赖检测阈值。传统固定阈值方法易受噪声影响,而GA算法通过模拟生物进化,在复杂环境中自动优化阈值,提高频谱感知准确性,增强CHN的通信效率与资源利用率。预览效果无水印,核心程序部分展示,适合研究频谱感知与优化算法的学者参考。
|
9月前
|
机器学习/深度学习 算法 自动驾驶
基于导向滤波的暗通道去雾算法在灰度与彩色图像可见度复原中的研究(Matlab代码实现)
基于导向滤波的暗通道去雾算法在灰度与彩色图像可见度复原中的研究(Matlab代码实现)
463 8
|
10月前
|
canal 算法 vr&ar
【图像处理】基于电磁学优化算法的多阈值分割算法研究(Matlab代码实现)
【图像处理】基于电磁学优化算法的多阈值分割算法研究(Matlab代码实现)
267 1
|
9月前
|
计算机视觉
MATLAB实现图像分割:Otsu阈值法
Otsu方法(大津法)是一种广泛使用的自动图像阈值分割技术,它通过最大化类间方差来确定最佳阈值。
|
算法 数据安全/隐私保护
基于Logistic-Map混沌序列的数字信息加解密算法matlab仿真,支持对文字,灰度图,彩色图,语音进行加解密
本项目实现了一种基于Logistic Map混沌序列的数字信息加解密算法,使用MATLAB2022A开发并包含GUI操作界面。支持对文字、灰度图像、彩色图像和语音信号进行加密与解密处理。核心程序通过调整Logistic Map的参数生成伪随机密钥序列,确保加密的安全性。混沌系统的不可预测性和对初值的敏感依赖性是该算法的核心优势。示例展示了彩色图像、灰度图像、语音信号及文字信息的加解密效果,运行结果清晰准确,且完整程序输出无水印。
基于Logistic-Map混沌序列的数字信息加解密算法matlab仿真,支持对文字,灰度图,彩色图,语音进行加解密
|
算法 数据可视化 图形学
网络通信系统的voronoi图显示与能耗分析matlab仿真
在MATLAB2022a中,该程序模拟了两层基站网络,使用泊松分布随机生成Macro和Micro基站,并构建Voronoi图。它计算每个用户的信号强度,选择最强连接,并分析SINR和数据速率。程序还涉及能耗计算,包括传输、接收、处理和空闲能耗的分析。Voronoi图帮助可视化网络连接和优化能源效率。

热门文章

最新文章