Is there a way to overlay one mask onto another?

2 ビュー (過去 30 日間)
Teshan Rezel
Teshan Rezel 2021 年 8 月 4 日
編集済み: Teshan Rezel 2021 年 8 月 4 日
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.
  2 件のコメント
the cyclist
the cyclist 2021 年 8 月 4 日
Can you upload the two image files?
Teshan Rezel
Teshan Rezel 2021 年 8 月 4 日
@the cyclist done, thanks!

サインインしてコメントする。

採用された回答

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 件)

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by