How to show the image slice from the below example?

Hello, I want to show the image slice from the below example, where CT and SPECT both are image array with the same width, size and spacing.
% This is a part of the code
% Visualize the CT and SPECT together
for m = 1:199
CT_img = new_CT(:,:,m);
SPECT_img = SPECT(:,:,m);
final_img = imfuse(CT_img, SPECT_img);
% Visualize CT and SPECT together
imshow(final_img, [-800 1000]);
end
% Now, I want to see the overlapped slices one by one, so I want somethig like:
show_20th_slice = imshow(final_img(:,:,20)) %...>> but this says: "Index in position 3 exceeds array bounds (must not exceed 3)."
How can I do this?

 Accepted Answer

% This is a part of the code
% Visualize the CT and SPECT together
for m = 1:199
CT_img = new_CT(:,:,m);
SPECT_img = SPECT(:,:,m);
final_img(:,:,:,m) = imfuse(CT_img, SPECT_img);
% Visualize CT and SPECT together
imshow(final_img(:,:,:,m), [-800 1000]);
end
% Now, I want to see the overlapped slices one by one, so I want somethig like:
show_20th_slice = imshow(final_img(:,:,:,20))
I am not sure at the moment why you are getting back color images instead of grayscale, but your error message would have been different if you were getting back grayscale.

4 Comments

Thank you for the reply but I am not yet sure why I am getting the following:
Index in position 3 exceeds array bounds (must not exceed 3).
Error in method (line 64)
show_20th_slice = imshow(final_img(:,:,20));
Any help will be appreciated.
You are getting it because you did not copy my code properly. Notice I had
show_20th_slice = imshow(final_img(:,:,:,20))
1 2 3
but you are using
show_20th_slice = imshow(final_img(:,:,20));
1 2
For reasons I do not know at the moment, each of your imfuse() calls is returning a 3D (RGB) image -- an array that is something by something by 3. The code I wrote pushes all of those 3D (RGB) images together along the 4th dimension, giving you something x something x 3 x 199. You then tried to index that third dimension at 20 instead of indexing the 4th dimension at 20.
One additional thing: When I use imfuse the colormap of CT image becomes green and SPECT becomes pink. Is there a way to change the colormap to grey and hot or jet? See attached.
Thank you, Walter, for your help. Have a great weekened.
im1 = imread('cameraman.tif');
im2 = rgb2gray(imread('flamingos.jpg'));
im2 = imresize(im2, size(im1));
fused_by_function = imfuse(im1, im2);
fused_manually = cat(3, im2, im1, im2);
subplot(2,1,1);
imshow(fused_by_function);
title('imfuse()')
subplot(2,1,2)
imshow(fused_manually);
title('manual fuse')
max(abs(double(fused_by_function(:)) - double(fused_manually(:))))
ans = 12
We can see from this that imfuse(A,B) is effectively the same as putting B into the red and blue panes and A into the green pane.
And that in turn tells us that using a different colormap would not be easy, because it is not really "calculating" a color for each pixel, and is more "just letting it happen". So you would need a quite different approach.
Perhaps something like:
alpha = 0.7;
cmap1 = gray(256);
cmap2 = jet(256);
im1rgb = ind2rgb(im1, cmap1);
im2rgb = ind2rgb(im2, cmap2);
blended = alpha * im1rgb + (1-alpha) * im2rgb;
figure()
subplot(3,1,1)
imshow(im1rgb); title('image1 recolored');
subplot(3,1,2);
imshow(im2rgb); title('image2 recolored');
subplot(3,1,3);
imshow(blended); title('blended')

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!