SUM or BWAREA?

9 views (last 30 days)
cla
cla on 5 Jun 2012
Hi all, i have to count pixels in a binary image. What it is better to use: - SUM that counts all the pixels with value 1; - BWAREA. Thanks for your help!

Answers (1)

Image Analyst
Image Analyst on 5 Jun 2012
The correct answer is that it depends on how you want to consider the area. They give different values. If you want to count the "1" pixels in a binary image use sum(). bwarea() uses a totally different algorithm than sum. From the help:
total = bwarea(BW) estimates the area of the objects in binary image BW. total is a scalar whose value corresponds roughly to the total number of on pixels in the image, but might not be exactly the same because different patterns of pixels are weighted differently.
Class Support
BW can be numeric or logical. For numeric input, any nonzero pixels are considered to be on. The return value total is of class double.
Algorithms
bwarea estimates the area of all of the on pixels in an image by summing the areas of each pixel in the image. The area of an individual pixel is determined by looking at its 2-by-2 neighborhood. There are six different patterns, each representing a different area:
Patterns with zero on pixels (area = 0)
Patterns with one on pixel (area = 1/4)
Patterns with two adjacent on pixels (area = 1/2)
Patterns with two diagonal on pixels (area = 3/4)
Patterns with three on pixels (area = 7/8)
Patterns with all four on pixels (area = 1)
So of course bwarea will take longer. Personally, I use sum() in preference to bwarea().
  2 Comments
Sean de Wolski
Sean de Wolski on 5 Jun 2012
ImageAnalyst, would you mind sharing the reason behind your preference toward sum? Please and thank you!
Andrei Bobrov
Andrei Bobrov on 5 Jun 2012
+1

Sign in to comment.

Categories

Find more on Denoising and Compression 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!