Find the percentage of area occupied by an object
5 views (last 30 days)
Show older comments
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1245422/image.png)
1 Comment
Accepted Answer
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.
rgbImage = imread('Elysi.png');
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)
% Compute area of yellow.
yellowMask = grayImage == 196;
yellowMaskArea = nnz(yellowMask)
percentage = 100 * pinkArea / yellowMaskArea
% 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 = nnz(yellowMask)
percentage = 100 * pinkArea / yellowRectArea
1 Comment
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?
More Answers (1)
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;
where yellowPixelCount is the height*width of the yellow rectangle. Compute the pinkPixelCount from the image data. If you need guidance with that, please provide more info about your image or upload a mat file containing your image data.
0 Comments
See Also
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!