# Find the percentage of area occupied by an object

조회 수: 3(최근 30일)
Elysi Cochin 2022년 12월 28일
댓글: Image Analyst 2022년 12월 29일
From the below image, how can I find - what percentage of the yellow region does the total pink region occupy?
##### 댓글 수: 1표시 없음숨기기 없음
Matt J 2022년 12월 28일
Moved: Matt J 2022년 12월 28일
None. How can a region be both pink and yellow at the same time?

댓글을 달려면 로그인하십시오.

### 채택된 답변

Image Analyst 2022년 12월 28일
@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.
yellowMask = grayImage == 196;
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
##### 댓글 수: 1표시 없음숨기기 없음
Image Analyst 2022년 12월 29일
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?

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

Adam Danz 2022년 12월 28일
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.

댓글을 달려면 로그인하십시오.

### 범주

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