How to calculate area in a binary image?

I have two questions to ask:
1) How to calculate area at the white region in binary image?
2) How to convert the answer of area unit that get from matlab into cm^2 or mm^2 (SI UNIT)?
here I attach the original image and binary image (this image is marine growth on the fiberglass plate)

 Accepted Answer

Hi Asmalina,
1) To find the area in pixel size we just need to find the number of white pixel which would be
area = bwarea(bw);%bw is your binary threshold image
The unit would be pixel
2) To find the area in real world units , you need to find out how big one pixel is in appropraite units, if you working with DICOM image refer to this answer.
Another way is we can find the percentage of white area so if you have the plate area , you can find out the actual white area then.
to find out the percentage area:
percentageWhite = nnz(bw) / numel(bw);

5 Comments

thank you...
but what if I working in TIFF image..can I directly convert from TIFF to DICOM image?
Yes. Use imread() to read the file into a matrix in your program, then use dicomwrite() to save the matrix variable to disk in the dicom format. But you do not need to convert your TIFF image to dicom to do spatial calibration. However if the input image is already in dicom format, there's a good change the real world spatial calibration factors will be in the header of the dicom image file, so you can use those directly. If you don't know the spatial calibration factors somehow, and can't get them from the image file header/metadata, then see the spatial calibration demo in my Answer below.
thank you.. another queations..
I want to interpret above image by image processing (enhancement then segmentation) and then will calculate the area.. I want to ask your opinion which one the better way... either to convert the image(tiff) to DICOM before I start the processing or convert the image before I want to calculate the area ( to get area in SI unit)??
The file format on disk doesn't matter (though hopefully it's not a lossy bad format like JPG). Once it's read into MATLAB, it's just a regular numerical array. It doesn't matter what format it was on disk before that. So you either have the calibration data in the metadata of the image, in which case you can simply start using it, or you don't which means you'll have to create it, such as by drawing a known distance on the image.
i have another questions;
1)can I used this way to get the distance or area in real world units (SI unit)?
calibrationFactor = length of the image in cm/length of the image in pixel;
distanceInCm = distanceInPixels * calibrationFactor;
areaInSquareCm = areaInPixels * calibrationFactor ^ 2
2) what is distanceInPixels, is it same value with length of the image in pixel?
3) is it areaInPixels is the value i get after i used this formula
(area = bwarea(bw);%bw is your binary threshold image)?

Sign in to comment.

More Answers (1)

See my attached spatial calibration demo.

12 Comments

I have seen your demo, but i have question to ask..
can you explain more on line 145 to 148;
calibration.units = answer{1};
calibration.distanceInPixels = distanceInPixels;
calibration.distanceInUnits = str2double(answer{2});
calibration.distancePerPixel = calibration.distanceInUnits / distanceInPixels;
answer 1 and str2double answer 2 is stand for what?
inputdlg() returns a cell array called answer. To get the contents of the first edit field you need to use answer{1}, and answer{2} is the contents of the second edit field. They will be strings so you need to convert them to numbers with str2double() if you need them to be a number.
See the FAQ for a good intuitive explanation of cell arrays and matrices and when to use braces, parentheses, and brackets.
image analyst, i think you overlooked to answer my question above;
i have another questions;
1)can I used this way to get the distance or area in real world units (SI unit)?
calibrationFactor = length of the image in cm/length of the image in pixel;
distanceInCm = distanceInPixels * calibrationFactor;
areaInSquareCm = areaInPixels * calibrationFactor ^ 2
2) what is distanceInPixels, is it same value with length of the image in pixel?
3) is it areaInPixels is the value i get after i used this formula
(area = bwarea(bw);%bw is your binary threshold image)?
  1. Yes
  2. No. distanceInPixels is the length of the line you drew, in pixels. Not sure what you mean by the "length" of the 2-D image, but distanceInPixels is not the same as the height in rows (lines) of the image, or of the width (columns) of the image.
  3. area will be the area in pixels.
for number 2;
what I mean for "length" is length that I get from imdistline, is it the same?
and I want to confirm if it true or not;
length of the image in cm = 10cm
length of the image in pixel = 165 pixel
calibrationFactor = length of the image in cm/length of the image in pixel;
calibrationFactor = 10cm/ 462 pixel
calibrationFactor = 0.022
distanceInPixels = length of the image in pixel = 165 pixel
distanceInCm = distanceInPixels * calibrationFactor;
distanceInCm = 462 pixel * 0.022
distanceInCm = 10cm
areaInSquareCm = areaInPixels * calibrationFactor ^ 2
areaInSquareCm = 15336 pixel * 0.022 ^ 2
areaInSquareCm = 7.422 cmsquare
correct me if I wrong..
Not quite. If you drew a line along your known reference object that was 165 pixels long and that represents 10 cm, then the spatial calibration factor is 10/165 = 0.0606060606060606 (not 0.022).
Now if you drew another line that was 462 pixels long, it would be 462 * 0.0606060606060606 = 28 cm long (not 10 cm)
If you have a region that is 15336 pixels in area, then the area would be 15336 * 0.0606060606060606^2 = 56.33 square cm, not 7.422 square cm.
so can I assume that the answer I get from spatial calibration factor also used in pixel spacing?
Not sure I understand. They're the same thing. You said "also", so how are you getting two different things? You basically have one factor that is how many real world units there are per pixel. For linear distances you multiply pixels by that. For areas you multiply by that squared. There is only one factor, not two, so there is no "also".
okay sir, another question;
for total number pixel and number of white pixel , if it convert to SI unit.. what will be the unit?
because I noticed for total number of pixel is width multiple by height of the image.. so from the formula, isn't it formula of area?
Yes the units will now be calibrated so the units will now be square cm instead of pixels.
and number of white pixel?, it's will be same unit with total number of pixel (square cm) or only in cm?
The number of white pixels is, of course, unitless. It's just simply a count, like 235 or 14983 or whatever. It can have an area in pixels or square cm but the count is obviously just a number and has no units at all.

Sign in to comment.

Categories

Find more on Convert Image Type 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!