How to calculate Black area?

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.

 Accepted Answer

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

Thanks so much! So I was on the wrong way all along! :(
But what about this way that I usually do for making binary:
Binary_image = im2bw(original_image,graythresh(original_image))
Does it also make dark ones 1 and white 0?
How can I calculate black area here? again by summing?
Thanks so much!
steven
With that function (which I never ever use by the way because it almost never picks the right threshold) I believe it pick bright pixels to be true/white/1. So you'd have to invert it. It usually picks the correct threshold only for very contrasty images with a bimodal histogram. Yours is like that so it may pick the correct threshold for you. Just be sure to invert the binary image before summing or calling bwarea().
Steven
Steven on 19 Dec 2013
Edited: Steven on 19 Dec 2013
Thanks again. A few questions if I may:
1. Is my own method still correct for calculating dark area?
2. How (in using different methods for creating binary) may I find out which one (black or white) are true/1? since it is indeed important!
3. By inverting you meant just for using sum or bwarea, right?
3. Regarding your first answer here, I am still confused. If in your way, dark pixels are 1 and white ones are 0, why do we still treat them inversely? I mean for example when we want to make somewhere black we make it 0 and for white we make it 1?
Also here the WHITE area is calculated using bwarea which are 1. Am I right?
Thanks again.
Image Analyst
Image Analyst on 19 Dec 2013
Edited: Image Analyst on 19 Dec 2013
1. No.
2. Create "check sample" images, say in Photoshop or somewhere, that have known areas and run these images through your algorithm.
3. Yes, you need to invert only when you need to invert.
3 (your second 3). Dark gray pixels are true, and light pixels are false. I don't treat them inversely. Foreground and background are not fixed. Foreground can be bright or dark, and background is the opposite. In your case the "foreground" - which means the pixels you are interested in - is the dark pixels. Foreground nearly always is represented by true/1/white pixels in a binary image. The background is the stuff you don't care about and want to ignore. This is nearly always false/0/black in binary images.
The WHITE area of the binary image is summed/counted when you call bwarea(). Remember that the white area of the binary image, if you follow my way (the standard way) of doing it, corresponds to the foreground/dark pixels of the grayscale image.
Steven
Steven on 19 Dec 2013
Edited: Steven on 25 Dec 2013
Thanks but I am now indeed more confused!!!!!
What I want is the black area in both gray scale and in binary. (the dark remain dark in binary, right?) so, regarding your last sentence, I want the BLACK area not white! You also say bwarea counts the white pixels! I am indeed sorry if I can't catch what you said but I am really confused!!! let me ask my questions in this way:
If I use my own way of binary (im2bw), how shall I calculate the BLACK area?
1.1) using bwarea or sum without inverting?
1.2) using bwarea or sum with inverting?
1.3) my own long method with inverting?
1.4) my own long method without inverting?
2. For your way, If I want to the black area, I just use bwarea or sum and no need for inverting. Right?
(a question is what to choose the value of 128 or whatever? how do I know the correct one?)
3. For instance:
I want to have the black area (semicircle) of this grayscale image:
No I turn it into a binary:
How to do so? again using bwarea or sum OR my old long method?
Again sorry for long comments and too many questions, but I was really confused and I just wanted to clarify for myself ;)
Thanks so much!
Image Analyst
Image Analyst on 19 Dec 2013
Edited: Image Analyst on 25 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.
Thanks!
So with this case above, does my old long method (mentioned at the first question) give the black area (semicircle)?
Thanks Setven
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)

Tags

Community Treasure Hunt

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

Start Hunting!