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

5 views (last 30 days)
Alex Lefebvre
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 Comments
Alex Lefebvre
Alex Lefebvre on 12 May 2022
Those solutions are good for me! That's good thinking, you really understood what I meant. Thank you.

Sign in to comment.

Accepted Answer

DGM
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
m1 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/995325/m1.png')>128;
m2 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/995320/m2.png')>128;
m3 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/995315/m3.png')>128;
m4 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/995310/m4.png')>128;
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
m1 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/995325/m1.png')>128;
m2 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/995320/m2.png')>128;
m3 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/995315/m3.png')>128;
m4 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/995310/m4.png')>128;
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.
  1 Comment
Alex Lefebvre
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by