System designHow-to

Measure blob area in a binary image by pixel counting

Count white pixels per blob or in total, calibrate mm² per pixel, and check the result with the 2/D error rule and a caliper-measured reference part.

LumaSpec Vision10 min read Share on LinkedIn

On this page 10 sections
  1. 01What you need
  2. 02Step 1: Threshold into a clean binary image
  3. 03Step 2: Choose the counting route
  4. 04Step 3: Set connectivity, minimum area and morphology
  5. 05Step 4: Calibrate pixels to mm²
    1. Worked example: 100 mm FOV, 12 MP camera
  6. 06Step 5: Prove the number before you ship it
  7. 07Common mistakes
  8. 08Where the count runs
  9. 09The decision
  10. 10Questions

A blob's area in a binary image is its count of white pixels: 8,000 white pixels at a calibrated 0.025 mm per pixel are 5.0 mm². The whole measurement is three moves — threshold the image so every part becomes one clean white region, count the pixels per region or in total, and convert with the pixel size you calibrated once on the line. This how-to gives you both counting routes, the connectivity and filter settings that change results, a worked calibration with the arithmetic shown, and the checks that keep the number honest when lighting, focus or geometry drift. You do not need a full vision library: a threshold, an array and a loop are enough for the total, and a particle-analysis call adds per-blob numbers.

What you need

  • A gray-scale image whose part and background separate cleanly at one gray level, or the raw frame and the means to threshold it.
  • One convention: parts white (255), background black (0). Invert if your library marks dark objects instead.
  • The pixel size in mm per pixel, from the field of view (FOV) or from a reference part — step 4 shows both routes.
  • Somewhere to run the count: a vision library with particle (connected-component) analysis, or any environment that reads the image into a 2-D array and loops over it.
  • If the part moves, a global shutter: motion blur spreads a fast edge over several pixels even at a 1/1000 s exposure.

Step 1: Threshold into a clean binary image

Lighting does more for blob area than any algorithm. Work through the capture in order:

  1. Light for contrast. A light table behind the part gives a silhouette with a two-peak gray histogram; diffuse front light suits surface marks and printed features. The threshold then cuts in the valley between peaks, not on a slope.
  2. Threshold into black and white: choose the valley level, set parts to white (255) and background to 0, and keep the raw gray frame for re-thresholding later.
  3. Clean the mask: a 3 × 3 erosion removes isolated single pixels and thin spurs, a matching dilation restores the edges, and a hole-fill closes interior gaps when the silhouette should be solid.
  4. Keep a live binary view open while you tune, so you see exactly the mask the counter will see — the live binary image preview article shows the set-up.

If the part moves during exposure, capture with a global-shutter model such as the MV-CH120-11UC, a 12 MP USB3 camera — rolling-shutter skew shears fast-moving parts and biases every area you compute.

Step 2: Choose the counting route

Decide what area means for the recipe before writing code:

Route Output Use it for
Total white-pixel count One number for the frame Coverage, stains, residue, total foreground area
Particle analysis (connected components) Area, centroid, bounding box and count per blob Part counting, largest-blob logic, size sorting

Particle analysis labels every connected white region and reports each region's pixel area, so a frame holding three parts returns three areas plus the total. A plain count sums every white pixel and cannot separate touching or nearby blobs, because counting does no labeling.

Without a vision library, read the image into a 2-D array and add one to a counter each time a pixel sits at or above the threshold. Over a 4,000 × 3,000 frame that is one pass through 12 million elements — fast in any compiled or vectorised language. The same loop can also report positions: this walkthrough of locating pixel coordinates from a 2-D pixel array shows how array indexes map back to image positions.

When the recipe needs shape as well as size, step up to contours — circle detection with contour analysis covers that route. For region-wise counts inside one frame, counting lines per region with blob analysis is the nearest worked recipe.

Step 3: Set connectivity, minimum area and morphology

Two settings change the result more than any coding style:

  1. Connectivity. 4-connectivity joins pixels edge-to-edge only; 8-connectivity also joins diagonals. The total white count is identical either way — connectivity only decides where one blob ends and the next begins. Diagonal edge chains on slanted parts are the deciding case, and connectivity 4 or 8 in blob analysis walks the choice for circular markers.
  2. Minimum area. Drop regions smaller than any real feature. In the worked example below the smallest real blob spans 8,000 pixels, while a 3 × 3 opening erases specks too thin to hold a 3 × 3 square, so a 500-pixel minimum sits about 55× above the debris an opening can leave and 16× below the smallest real feature.
  3. Morphology. Erosion then dilation (an opening) clears specks; dilation then erosion (a closing) seals one-pixel breaks inside a blob. Dark speck detection with thresholding and morphology shows both on real images.

Step 4: Calibrate pixels to mm²

Pixel counts turn into mm² with one multiplication, once the pixel size is known:

mm per pixel = FOV width (mm) ÷ horizontal pixel count blob area (mm²) = white pixel count × (mm per pixel)²

Two routes produce mm per pixel. From optics: divide the known FOV width by the horizontal pixel count — the same calibration chain as measuring object size from a camera image, and the coin-size sorting from pixels to mm application runs it end to end. From a reference: count the pixels a known part covers and divide the known area by that count. Run both once; agreement within a couple of percent confirms the set-up.

Worked example: 100 mm FOV, 12 MP camera

The LS-CMR-0166 resolves 4000 × 3000 over USB3 at 30 fps. At a 100 mm FOV width that gives:

  • Pixel size: 100.0 mm ÷ 4,000 px = 0.025 mm per pixel.
  • Pixel area: 0.025 × 0.025 = 0.000625 mm² per pixel.
  • Blob area: 8,000 white pixels × 0.000625 mm² = 5.0 mm². That is an equivalent circle of √(4 × 5.0 ÷ π) = 2.5 mm diameter, about 101 px across, so quantization alone puts ±2 % (±0.1 mm²) on the reading.
  • Reference cross-check: a 40 × 25 mm shim has 1,000 mm² and must cover 1,000 ÷ 0.000625 = 1,600,000 pixels. Count it once; keep the calibration if the measured count lands within 2 %.
Area scan cameras12 MP USB3 area scan camera, IMX226, 1/1.7" rolling shutter, 30 fps, 4000 × 3000 resolution (LS-CMR-0166)LS-CMR-0166
Sensor format
1/1.7"
Interface
USB3
Resolution
12 MP
Shutter
Rolling
$560.00View partBuild a system with it

Camera sizing runs the same division backwards. A target of 0.025 mm per pixel over a 100 mm FOV needs 4,000 horizontal pixels; the calculator below turns a pixel-size target into a pixel count and lists cameras that meet it.

Camera resolution for the smallest feature
Units
Pixels needed
–
Sensor size
– MP
Smallest sensor that fits
–
Pixel on the part
– mm
Pixels across the feature, that sensor
–

Pixels needed = field / feature × pixels per feature on each axis (field incl. margin). The lens must resolve the pixels too: Nyquist = 1000 / (2 × pixel µm) lp/mm.

Find cameras with enough pixels Open the full calculator

Metrology practice keeps the pixel size near one tenth of the measurement tolerance. At 0.025 mm per pixel that supports tolerances around ±0.25 mm; the camera resolution by the pixel rule guide sizes the sensor for tighter cases.

Step 5: Prove the number before you ship it

  1. Threshold plateau. Measure one reference part at the setpoint, then at plus and minus 20 gray levels. An area that stays inside your tolerance band across the sweep tracks the part; an area that slides with the threshold means the contrast is too soft.
  2. Reference agreement. The caliper-measured shim from step 4 should land within about 2 % once blobs span 100 px or more. If it does not, suspect geometry before code.
  3. Squareness and distance. Keep the camera axis square to the part plane. A 10° tilt multiplies every area by cos 10° = 0.985 — a 1.5 % loss — and a 1 % working-distance error moves area by about 2 %, because area scales with the square of the distance.
  4. One statistic, logged the same way on every part: blob area excluding holes, or filled area. Pick once and keep it, because the two can differ by the whole hole.

Common mistakes

  • Letting auto-exposure float. Exposure moves gray levels, the threshold cuts somewhere new, and the area follows the lighting rather than the part. Lock exposure or strobe the light; auto-exposure ROI instability shows the feedback loop that makes this drift worse.
  • Counting before the mask is clean. A 4-pixel speck is 0.0025 mm² at 0.025 mm per pixel — nothing alone, but hundreds of specks read as a real trend.
  • Letting two touching parts count as one blob. Separate with an opening sized to pinch the neck, a watershed-class split, or a bounding-box check against the single-part envelope.
  • Shipping pixel counts as areas. Pixel counts are unitless until they meet a calibrated pixel area; keep mm² out of limits and logs until calibration is locked.
  • Ignoring rolling shutter on a fast line. The step 1 global-shutter note is not cosmetic — shear grows with belt speed and the area error grows with it.

Where the count runs

A PC recipe takes a GigE or USB3 area scan camera and keeps the logic in software where it can change with the product. A smart camera or vision sensor runs the same threshold-and-count on the device and ships numbers, not images, down the wire. For on-device counting, the MV-SC5060XM-08M-WBN pairs a 6 MP mono sensor with an 8 mm lens, and the LS-SMC-0063 pairs 1.6 MP with a 16 mm lens and white light at 60 fps. Both size by the same pixel budget: FOV width divided by the pixels the smallest feature needs. The camera finder filters both families by resolution, interface and shutter.

Smart cameras & vision sensorsHikrobot MV-SC5060XM-08M-WBN SC5000X series Smart Camera, 6 MP, Mono, 8 mm focal length, M12-mountMV-SC5060XM-08M-WBN
Interface
GigE
Resolution
6-10 MP
$1,629.00View part
Smart cameras & vision sensors1.6 MP smart camera, 60 fps, 16 mm lens, white light (polarized, transparent light) (LS-SMC-0063)LS-SMC-0063
Interface
GigE
Resolution
Up to 3 MP
$1,110.00View part

The decision

Threshold at a histogram valley with backlight, clean the mask with an opening, then measure: particle analysis when per-blob numbers matter, a plain white-pixel sum when only total coverage does. Calibrate once and carry the checks with it — plateau sweep, shim agreement, square camera — and the count stays a measurement instead of an artifact. For hardware on a new pixel budget, start at the camera configurator; more measurement recipes sit in the systems topic, and sizing questions go to the contact page.

Questions

How does the threshold level change the measured area?

The edge lands where the gray gradient is steepest. On a sharp backlit silhouette the boundary shifts by a fraction of a pixel when the threshold moves ±20 gray levels; on a soft front-lit edge it can move several pixels. Sweep the threshold across that band on one reference part: an area that stays inside tolerance means the recipe follows the part, while a sliding area means the contrast needs work first.

How many pixels across does a blob need for an accurate area?

Quantization puts a relative uncertainty of roughly 2/D on a blob D pixels across. At 20 pixels expect about ±10 %, at 50 px about ±4 %, at 100 px about ±2 %, and at 200 px about ±1 %. Ten pixels across is enough to confirm presence; for an area tolerance tighter than 5 %, size the optics so the smallest blob of interest spans 100 pixels or more.

Do holes inside the blob count as area?

Only if you fill them. A filled count adds internal hole pixels back — right for an outer silhouette such as a washer's footprint, wrong when the hole is the feature, like a gasket bore. Most libraries report the blob's area, its holes' area and the filled area separately, so record which statistic the pass/fail limit refers to, and never swap between them after the recipe is released.

Can a smart camera measure blob area without a PC?

Yes. Smart cameras and vision sensors run thresholding and particle statistics on the device and send numbers, not images, over the network link. The constraint is pixels: size the lens and stand-off so the smallest blob spans enough pixels for its tolerance, then let the camera return the count and area each cycle. This suits fixed recipes on stable lines; a PC with an area scan camera stays the choice when recipes change often.

Why do two methods give slightly different areas for the same blob?

Small differences are edge convention. A pixel count includes every pixel the threshold marks; a contour fit walks the boundary and can integrate it at half-pixel precision, so it reports a fractionally different area. On blobs under 50 pixels across, a few percent of difference between methods is normal. Run both once on a reference part, record the offset, and make one method the standard the pass/fail limit is written against.