# Masking multiple parts of image

18 views (last 30 days)
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!
Image Analyst on 19 May 2022
Edited: Image Analyst on 19 May 2022
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:

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
else
end
end
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
else
end
end
... 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
outpict = BG;
imshow(outpict)
Doing basic linear (multiplicative) composition is one simple way that works with both binary and linear masks:
% 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 = 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.
% 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
imshow(outpict)
##### 1 CommentShowHide None
nms09 on 23 May 2022
This helped a lot, thank you so much!

### Categories

Find more on ROI-Based Processing in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!