# Combine masked images into one, with each mask having a unique color.

7 views (last 30 days)
Alex Lefebvre on 12 May 2022
Commented: Alex Lefebvre on 12 May 2022
Good day,
I'm tracking the migration of a spill into a soil column by taking pictures. The spill is dyed red and so I use Hue threholds to produce masked images and binary images (to track the spill in each image), then bwboundaries to produce contours. It gives me something like the image attached (left part) once I compile a few image contours. However, in addition to having contours, I'm hoping to assign a color to each area so it looks like the attached picture (on the right). I would rather track the progression of the spill by the fill color instead of the contour color (contours can all be black). However, I don't know how to proceed. What function do you recommend? If there's a more efficient way of doing this than what I've described so far, please let me know, my experience with mathlab is limited. Thank you.
##### 2 CommentsShowHide 1 older comment
Alex Lefebvre on 12 May 2022
Those solutions are good for me! That's good thinking, you really understood what I meant. Thank you.

DGM on 12 May 2022
If those would work for you, then you might also consider something like these:
% do a simple mean of colored frames
% a set of binary images
M = cat(4,m1,m2,m3,m4);
sz = [size(M,1) size(M,2)];
% get a colormap and colorize the frames
cmap = parula(4);
C = zeros([sz 3]);
for k = 1:4
C(:,:,:,k) = M(:,:,:,k).*permute(cmap(k,:),[1 3 2]);
end
% for sake of viewing, just take the mean
meanimg = mean(C,4);
% which filled "contour" level gets drawn on top?
imshow(meanimg)
% ignore underlying black regions when taking mean
% a set of binary images
M = cat(4,m1,m2,m3,m4);
sz = [size(M,1) size(M,2)];
% get a colormap and colorize the frames
cmap = parula(4);
C = zeros([sz 3]);
for k = 1:4
C(:,:,:,k) = M(:,:,:,k).*permute(cmap(k,:),[1 3 2]);
end
% for sake of viewing, just take the mean
meanimg = mean(C,4);
% but compensate for underlying black regions
Mm = mean(M,4);
Mz = repmat(Mm==0,[1 1 3]);
meanimg = meanimg./Mm;
meanimg(Mz) = 0;
% which filled "contour" level gets drawn on top?
imshow(meanimg)
I'm sure the latter example could be simplified, but I'm not really used to thinking about these compositions with base MATLAB tools.
Alex Lefebvre on 12 May 2022
Those are all really good suggestions, thank you. The previous options you mentioned worked like a charm, but I'll give those recent ones a try as well. But they are all good options! It comes down to the type of spill and what options best illustrate how it migrates. Worth trying it all.