Is there a way to overlay one mask onto another?

3 views (last 30 days)
Teshan Rezel
Teshan Rezel on 4 Aug 2021
Edited: Teshan Rezel on 4 Aug 2021
I have two masks, as below. I want to make sure that the white area of the second mask is not more than the white in the first. Is there a way to overlay mask 1 over mask 2?
In other words, mask 1 is the mnaximum possible size of the obejct of interest, whilst mask 2 is guaranteed to be smaller than mask 1. Mask 2, however, has artifacts (remnants of the background from thresholding). I would like to "screen out" the background from mask 2, to overlay the black elements of mask 1 onto mask 2 so that mask 2 is at most as big as mask 1.

Accepted Answer

the cyclist
the cyclist on 4 Aug 2021
I don't really do image processing, but I think this straightforwardly answers your question, using only base MATLAB commands. There might be simpler ways, using the Image Processing Toolbox.
% Upload the image
image1 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/703072/1.png');
image3 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/703077/3.png');
% Check if a pixel is white (i.e. all values are 255)
isWhitePixel1 = all(image1==255,3);
isWhitePixel3 = all(image3==255,3);
% Count the number of white pixels in each image
numberWhitePixels1 = sum(isWhitePixel1(:));
numberWhitePixels3 = sum(isWhitePixel3(:));
% Because the image are different sizes, calculate the fraction of the
% image that is white rather than the pixel count.
fractionalWhiteArea1 = 3*numberWhitePixels1/prod(size(image1))
fractionalWhiteArea1 = 0.9499
fractionalWhiteArea3 = 3*numberWhitePixels3/prod(size(image3))
fractionalWhiteArea3 = 0.9601
You can see that image 1 has about 95% white area, and image 3 has about 96%. Note that image 3 has some gray values ([191,191,191]), but image 1 has only black and white.
  1 Comment
Teshan Rezel
Teshan Rezel on 4 Aug 2021
@the cyclist Thanks for this! Apologies, I must not have been very clear in my question. I'll rephrase it to clarify!

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!