Merge Similar Pixels
Problem Statement
In image segmentation, adjacent pixels with similar intensities are often merged into the same region.
Given a list of pixel values and a list of edges (i, j) indicating which pixels are adjacent, merge pixels into groups where the absolute intensity difference is at most threshold.
Return the number of distinct groups after merging.
Applications
- Region-based segmentation
- Color quantization
- Superpixel generation
Constraints
Example
pixels = [10, 12, 50, 52], edges = [[0,1], [1,2], [2,3]], threshold = 5
2
- Inputs:
pixelsholds 4 intensities, , indexed 0 to 3.edgeslists which pixels touch: 0–1, 1–2 and 2–3, a simple chain.thresholdis the largest intensity gap that still counts as "similar". - Which edges qualify: an edge merges its pixels only when .
- 0–1: , merge.
- 1–2: , no merge, even though the pixels are adjacent.
- 2–3: , merge.
- Resulting groups: (the dark region) and (the brighter region), so the output is
2. - Notice that merging is transitive along qualifying edges: with intensities chained by edges and a threshold of , all three pixels form one group, even though . A pixel with no qualifying edge stays a group of its own.