Label Connected Regions
The mask contains three products and many single-pixel flecks.
Read the full lesson and open its transcript below. A narrated video will be added after production.
What you will learn
The mask contains three products and many single-pixel flecks.
A practical question
Our workshop camera shows three parts on a table, plus several isolated bright flecks. The binary mask calls all of them foreground. How many actual objects did it find? Connected-component labeling gives every connected group its own label, so we can inspect and measure them separately.
The useful idea
We need to choose what counts as connected. With four-neighbor connectivity, pixels join across shared sides. With eight-neighbor connectivity, diagonal touches join too. Two parts that meet only at a corner can therefore become one labeled group under the second rule. Neither choice is universally correct; use the one that matches the objects and your mask definition.
Work through the example
Draw two diagonal foreground pixels on a tiny grid and count the groups under both rules. Then label the real workshop mask and inspect small components against the original photograph. A one-pixel fleck is a connected region mathematically, even if the inspection should ignore it. Connectivity is a choice about which pixels count as neighbors. With four-neighbor connectivity, a diagonal contact does not join two groups; with eight-neighbor connectivity, it can. Draw a few diagonal squares on paper and count the groups both ways. Then inspect whether the diagonal touch in the real mask belongs to one object or two. A component count without its connectivity rule is incomplete and can change the reported number of defects. Now check the idea on a concrete example: Count groups with four-neighbor and eight-neighbor connectivity to see diagonal contacts differ. A possible trap is worth naming: A component is only a connected region, not proof of a real product. The answer follows from that evidence: Connected-component labeling groups foreground pixels under a chosen neighborhood rule.
Limits and next step
Record the connectivity and any size filter before reporting a count. Next, we'll trace the boundaries of the kept regions and see how stray pixels affect their outlines.
Worked example
Count groups with four-neighbor and eight-neighbor connectivity to see diagonal contacts differ.
Quick check: test your understanding
Try it yourself: Consider this case: Count groups with four-neighbor and eight-neighbor connectivity to see diagonal contacts differ. Explain how you would check the result. In your answer, address this limitation: A component is only a connected region, not proof of a real product.
Check the reasoning
Connected-component labeling groups foreground pixels under a chosen neighborhood rule. A component is only a connected region, not proof of a real product.
What is the main idea behind label connected regions?
Connected-component labeling groups foreground pixels under a chosen neighborhood rule.
How is it used in the example?
Count groups with four-neighbor and eight-neighbor connectivity to see diagonal contacts differ.
What limitation should be checked?
A component is only a connected region, not proof of a real product.
Further reading
Digital Image Processing, 4th edition — Rafael C. Gonzalez and Richard E. Woods.
Original ToolsInVision teaching examples. Editorial approach · Suggest a correction
Accessible referenceLesson 65 transcript303 words · Narration ready for the upcoming videoOpen transcript
Our workshop camera shows three parts on a table, plus several isolated bright flecks. The binary mask calls all of them foreground. How many actual objects did it find? Connected-component labeling gives every connected group its own label, so we can inspect and measure them separately.
We need to choose what counts as connected. With four-neighbor connectivity, pixels join across shared sides. With eight-neighbor connectivity, diagonal touches join too. Two parts that meet only at a corner can therefore become one labeled group under the second rule. Neither choice is universally correct; use the one that matches the objects and your mask definition.
Draw two diagonal foreground pixels on a tiny grid and count the groups under both rules. Then label the real workshop mask and inspect small components against the original photograph. A one-pixel fleck is a connected region mathematically, even if the inspection should ignore it. Connectivity is a choice about which pixels count as neighbors. With four-neighbor connectivity, a diagonal contact does not join two groups; with eight-neighbor connectivity, it can. Draw a few diagonal squares on paper and count the groups both ways. Then inspect whether the diagonal touch in the real mask belongs to one object or two. A component count without its connectivity rule is incomplete and can change the reported number of defects. Now check the idea on a concrete example: Count groups with four-neighbor and eight-neighbor connectivity to see diagonal contacts differ. A possible trap is worth naming: A component is only a connected region, not proof of a real product. The answer follows from that evidence: Connected-component labeling groups foreground pixels under a chosen neighborhood rule.
Record the connectivity and any size filter before reporting a count. Next, we'll trace the boundaries of the kept regions and see how stray pixels affect their outlines.