# Find the percentage of area occupied by an object

2 views (last 30 days)
Elysi Cochin on 28 Dec 2022
Commented: Image Analyst on 29 Dec 2022
From the below image, how can I find - what percentage of the yellow region does the total pink region occupy?
Matt J on 28 Dec 2022
Moved: Matt J on 28 Dec 2022
None. How can a region be both pink and yellow at the same time?

Image Analyst on 28 Dec 2022
@Elysi Cochin no offense but you've asked almost 400 questions over 10 years. I've given you many, many (hundreds) answers to trivial, simple things like this. I would have been able to answer this question after less than a week of using MATLAB. I'm sure your 10 years experience of doing image processing with MATLAB (almost as much as me) have given you the skills to answer such a trivial question as this. Actually I think you should be offering your experience to answer these kinds of questions for other people by now rather than ask them. But you've answered only 4 questions.
So I'm beginning to think that this is just an account that some image processing professor set up for the entire class to use. Is that true? Assuming this is the case and you're a new student, not the same person from 10 years ago, here is some simple code to do it:
% Read in original RGB image.
subplot(2, 1, 1);
imshow(rgbImage)
impixelinfo;
% Convert to gray level.
grayImage = rgb2gray(rgbImage);
subplot(2, 1, 2);
imshow(grayImage, [])
impixelinfo;
% Compute area of pink.
pinkArea = nnz(grayImage == 201)
pinkArea = 7148
% Compute area of yellow.
percentage = 100 * pinkArea / yellowMaskArea
percentage = 24.2239
% Now fill in the yellow mask to get the area of the yellow rectangle, as if the pink were not there.
yellowMask = imfill((grayImage == 196), 'holes');
yellowRectArea = 36656
percentage = 100 * pinkArea / yellowRectArea
percentage = 19.5002
Image Analyst on 29 Dec 2022
Yes, that's what I did - you just renamed the variables. And for the percentage, it just depends on how you define it. Neither way is right or wrong - they're just different. How do you define it? If you don't know, then no one else will know since it's your project and code. This isn't your homework is it (where we need to guess the professor's intended definition of percent)? Because no one would spend 10 years taking image processing classes.
But you never answered my question. Is this account associated with a single person over all those 10 years, or is this a class account where many people use it over the years? Can you please tell me?

Adam Danz on 28 Dec 2022
Assuming that this is an image and that the pink color is a single RGB color value, you could compute the number of pixels with that pink color and the size of the yellow rectangle to compute the percent area of the pink within the yellow.
percentArea = pinkPixelCount / yellowPixelCount * 100;

### Categories

Find more on Explore and Edit Images with Image Viewer App in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by