MATLAB Answers

How to calculate Black area?

17 views (last 30 days)
Steven
Steven on 19 Dec 2013
Commented: Image Analyst on 25 Dec 2013
In one question in which I wanted to calculate the dark (black) area in a binary image, you guys answered to me:
"If all you need is the area of the dark region then you don't need to find the edge at all. You just need to threshold and sum
binaryImage = grayImage < 128; % or whatever.
darkArea = sum(binaryImage);
darkArea2 = bwarea(binaryImage); % Another way using different algorithm. "
Now a problem comes to me. I wonder:
We want the area of black region not white, so when we use sum (or bwarea), we are actually calculating the white area region. right? because white pixels are 1 and black ones are 0 and by summing we are summing the white ones not black ones.
Thus, the area of black region should be this:
image_size = size(binary_image)
whole_area = image_size(1)*image_size(2)
white_area = sum(sum(binary_image)); % or
% white_area = bwarea(binary_image);
black_area = whole_area - white_area;
Am I right?
Sorry for such a trivial question, but I was really confused!
Thanks so much.

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 19 Dec 2013
No, that's not right. It's as I told you at first.
When you do
binaryImage = grayImage < 128; % Find dark pixels. Dark = true, 1, white.
you're creating a matrix that is "true" wherever the image is dark . If you sum that, it treats the "true" pixels as 1, and thus, counts them - counts the dark pixels. So you're getting the sum of the "true/1/white" pixels in the binary image which means your getting the count of the dark pixels of the gray scale image. Doing it your complicated way would count the bright pixels. By the way, if you wanted the binary image to be false where the gray scale image was dark, you'd flip the less than sign and then sum the inverse, which is much simpler than doing the multi-step process you did.
binaryImage = grayImage > 128; % Find bright pixels instead of dark pixels.
numDarkPixels = sum(~binaryImage(:)); % Notice I had to invert the image with~.

  8 Comments

Show 5 older comments
Image Analyst
Image Analyst on 19 Dec 2013
If you threshold it like that, where you're getting the light gray stuff instead of the black/dark gray stuff, then summing or using bwarea() will get you the area of the white in the binary image, which is the area of the light gray in the gray scale image. If you want the area of the dark gray, you have to invert the binary image so that the left half circle is white. Then use sum() or bwarea() because they count whatever is white in the binary image, which may or may not be what is lightest in the gray scale image, depending on how you did the thresholding.
Steven
Steven on 19 Dec 2013
Thanks!
So with this case above, does my old long method (mentioned at the first question) give the black area (semicircle)?
Thanks Setven
Image Analyst
Image Analyst on 25 Dec 2013
Maybe - I'm not sure what your binaryImage refers to. Maybe you can just post your whole m-file and I can fix it.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!