Is there a way to overlay one mask onto another?

3 次查看(过去 30 天)
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.

采纳的回答

the cyclist
the cyclist 2021-8-4
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 个评论
Teshan Rezel
Teshan Rezel 2021-8-4
@the cyclist Thanks for this! Apologies, I must not have been very clear in my question. I'll rephrase it to clarify!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by