MATLAB Answers

Setting certain pixels in a grayscale image to RGB (red) for MIP

24 views (last 30 days)
Alex G
Alex G on 6 Aug 2018
Edited: Thorsten on 9 Aug 2018
Hey again. I have a grayscale image and would like to highlight certain pixels by setting them to red, e.g.
save_array = image;
save_array(Omega==1) = %RED
Is there an easy way to do this? I will be taking the image (3D array), calculating the MIP (max intensity projection) by
mip_final = max(save_array, [], 3);
and would then like the red pixels to appear on the MIP (2D). So if a pixel is red anywhere in the z-dimension, it will appear red in the 2D MIP


Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 6 Aug 2018
Try this:
redChannel = grayImage; % Initialize
greenChannel = grayImage; % Initialize channel for green and blue channel.
redChannel(Omega) = 255;
greenChannel(Omega) = 0;
rgbImage = cat(3, redChannel, greenChannel, greenChannel);


Show 2 older comments
Alex G
Alex G on 7 Aug 2018
Of course, I'll try to get something to you soon. I'll try to explain it now though:
We have a 3D grayscale array in matlab. We have a binary array Omega of the same dimensions. I would like to make a MIP of that grayscale array, but whenever Omega is true in the xy-plane (irrespective of z), I would like the corresponding pixel to be red
...from writing this I guess I could take a MIP of the image, a MIP of Omega, and where Omega's MIP is true would be red in the final MIP?
Image Analyst
Image Analyst on 7 Aug 2018
OK, so you have a volumetric image, like from CT or MRI. So just mask the image
masked3DImage = grayImage3d; % Initialize
masked3DImage(~Omega) = -inf; % Mask
masked3DImage = max(masked3DImage , [], 3); % Do MIP
masked3DImage(~Omega) = 0; % minus infinity set to zero or whatever value you want outside of the mask.
Alex G
Alex G on 7 Aug 2018
This looks great. Also this might sound stupid, but where do I set the selected voxels to red (that's the issue I'm trying to resolve)? As a reminder, (Omega == 1) are the selected voxels
I believe I can just run masked3DImage through your first comment's code?
Thanks again.
P.S. I'm getting a "Attempt to grow array along ambiguous dimension" error. Probably because we reference Omega after making it a MIP. The MIP is 2D and Omega is 3D, so perhaps this is an issue?

Sign in to comment.

Thorsten on 8 Aug 2018
From the above discussion, I came up with this solution:
% fake some data
X = rand(10, 10, 23);
N = prod(size(X));
idx = randi(N, 1, round(0.01*N)); % 1% of all pixels are 1
X(idx) = 1;
Omega = X == 1; % find the values that are 1 in X
idx = sum(Omega, 3) > 0; % project to a 2D binary image
MIP = max(X, [], 3);
% create a color image from MIP
R = MIP; R(idx) = 1;
G = MIP; G(idx) = 0;
B = MIP; B(idx) = 0;
I = cat(3, R, G, B);


Show 5 older comments
Image Analyst
Image Analyst on 9 Aug 2018
Yes, of course it does, because the other pixels are opaque. For true volume visualization, you can't use MATLAB, which is limited to isosurfaces and cutaway views last I checked. You'll need to use a true volume visualization program like Avizo.
Alex G
Alex G on 9 Aug 2018
Thanks for the advice, I'll try out Avizo and see if my PI can get it for the lab. I do see your point - even if we increase transparency, it'll be difficult to see the voxels at the center of any 3D volume when viewed in 2D.
I realized I need to implement a density-weighted approach to my adaptive sampling, so hopefully the MIP visualization will work better after that :) I'm sure my PI doesn't want to pay for another license
Thorsten on 9 Aug 2018
You can visualize only the red blocks using plotcube from the FileExchange:
X = rand(10, 10, 23);
% X = rand(3000,1000,600);
N = prod(size(X));
idx = randi(N, 1, round(0.01*N)); % 1% of all pixels are 1
X(idx) = 1;
[x, y, z] = ind2sub(size(X), idx);
for i = 1:numel(x)
plotcube([1, 1, 1], [x(i) y(i) z(i)], .8, 'r')
if i == 1, hold on, end
axis equal
grid on

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by