📘
Merge Similar Pixels
MediumUnion-Find, Similarity
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
- 1≤len(pixels)≤1000
- 0≤pixels[i]≤255
- 0≤threshold≤255
Example:
Input:
pixels = [10, 12, 50, 52], edges = [[0,1], [1,2], [2,3]], threshold = 5
Output:
2
Reasoning:
Pixels 0,1 merge (diff=2). Pixels 2,3 merge (diff=2). But 1,2 don't merge (diff=38>5).
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.