Masking multiple parts of image

65 views (last 30 days)
nms09
nms09 on 19 May 2022
Commented: nms09 on 23 May 2022
Hello, I am new to MATLAB and working on a project where I have to mask multiple small parts of the same image. I have looked in many places and have not found a way to do this yet, either through code or manually, but I think I might be looking in the wrong places or the solution is easier than what I think it is - can anyone point me towards a function/documentation/post to do this? Do I need a different toolbox/product in order to accomplish this? Thank you in advance!
  3 Comments
nms09
nms09 on 19 May 2022
Basically I want to add several small circles in various parts of the image (the parts of the image I'd like to cover) so that I can layer another image over it after the relevant parts have been masked - was that descriptive enough or do you need more information?
Image Analyst
Image Analyst on 19 May 2022
Edited: Image Analyst on 19 May 2022
Need more info. So after you read this:
attach your image and describe the parts you want to mask, and say whether you'd like them to be masked manually or automatically.
See the Image Segmentation Tutorial in my File Exchange:

Sign in to comment.

Accepted Answer

DGM
DGM on 19 May 2022
Edited: DGM on 19 May 2022
As for creating a mask with multiple circular regions, you can use the ROI tools.
You could do this interactively something like this:
A = imread('peppers.png'); % get an image
ncircles = 4; % how many circles?
imshow(iminv(A)) % set up axes
for k = 1:ncircles
ROI = drawcircle(gca); % manually draw circles
% collect the union of masks
if k == 1
mask = createMask(ROI);
else
mask = mask | createMask(ROI);
end
end
imshow(mask) % show the resulting mask
Obviously, I can't do interactive tasks on the forum editor, but that would be the hypothetical result.
If you already know your circle locations and want to create the mask programmatically, you can still use the same ROI tools:
A = imread('peppers.png'); % get an image
% center and radius (in pixels)
C = [151 276; 216 104; 333 225; 367 98];
R = [47; 39; 36; 64];
imshow(iminv(A)) % set up axes
for k = 1:numel(R)
ROI = images.roi.Circle(gca); % create an ROI object
ROI.Radius = R(k); % specify its size and location
ROI.Center = C(k,:);
% collect the union of masks
if k == 1
mask = createMask(ROI);
else
mask = mask | createMask(ROI);
end
end
imshow(mask) % show the resulting mask
... and in this case, I chose to replicate the coordinates I manually picked in the first example.
As for how to use the mask to combine images, you could use logical indexing. This is limited in a number of ways. It can't support linear (smooth) masks, and it makes the need for explicit array expansion more cumbersome.
FG = imread('purppep.png'); % this is the foreground to overlay
% this relies on both images having the same numeric class
% and requires that both images have the same number of channels
% this will only work if mask is a logical array or binarized unit-scale float
mask = repmat(mask,[1 1 3]);
outpict = BG;
outpict(mask) = FG(mask);
imshow(outpict)
Doing basic linear (multiplicative) composition is one simple way that works with both binary and linear masks:
FG = imread('purppep.png');
% say you have a smooth or antialiased mask
mask = imgaussfilt(im2double(mask),5); % make a smooth mask out of the logical one
% this implicit expansion would require bsxfun prior to R2016b
outpict = im2double(FG).*mask + im2double(BG).*(1-mask);
outpict = im2uint8(outpict);
imshow(outpict)
Except on the forum, I don't use either method. MIMT tools replacepixels() and imblend() can be used for most blending/composition needs.
FG = imread('purppep.png');
% say you have a smooth or antialiased mask
mask = imgaussfilt(im2double(mask),5); % make a smooth mask out of the logical one
% succinct, class-agnostic, depth-agnostic
% also supports linear RGB
outpict = replacepixels(FG,BG,mask,'linear');
imshow(outpict)

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!