How can I apply a logical mask to an image variable?
14 views (last 30 days)
Show older comments
dila suay
on 24 Feb 2022
Commented: Kaitlin Wang
on 24 Feb 2022
Hello,
I am trying to apply a logical mask to an image variable, however I couldnt manage to do it so far.
I have tried
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
maskedRgbImage = rgbImage.*mask
Also, I've tried to apply mask to the rgbImage.CData directly. All of them are giving me errors. What else I can try?
Thank you so much
2 Comments
Walter Roberson
on 24 Feb 2022
Your rgbImage is a handle to a deleted image() object -- not an array of data.
Accepted Answer
Walter Roberson
on 24 Feb 2022
rgb = rgbImage.CData;
maskedImage = rgb .* cast(repmat(c, [1 1 size(rgb,3)]),'like',rgb);
This cannot be an image() object unless you want to do something like
maskedRgbImage = copyobj(rgbImage, gca);
rgb = rgbImage.CData;
maskedImage.CData = rgb .* cast(repmat(c, [1 1 size(rgb,3)]),'like',rgb);
... but that would be directly on top of the old image.
2 Comments
Walter Roberson
on 24 Feb 2022
rgb = rgbImage.CData;
maskedImage = rgb .* cast(repmat(mask, [1 1 size(rgb,3)]),'like',rgb);
or
maskedRgbImage = copyobj(rgbImage, gca);
rgb = rgbImage.CData;
maskedImage.CData = rgb .* cast(repmat(mask, [1 1 size(rgb,3)]),'like',rgb);
More Answers (1)
Image Analyst
on 24 Feb 2022
You don't need to do both of these:
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
maskedRgbImage = rgbImage.*mask
All you need is the first one. Don't do the second one. It's not right and would need "fixing".
1 Comment
Kaitlin Wang
on 24 Feb 2022
sir you are my hero. i have been looking at your answers for weeks and learning so much. thank you
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!