A key challenge in low-level image processing is grouping pixels that are "locally coherent" based on their intensity values. This technique forms the basis of early segmentation methods like superpixels.
You are given a grayscale image represented by an M×N matrix where each cell contains a non-negative integer intensity value. Two pixels (r1,c1) and (r2,c2) are considered coherently connected if they are 4-directionally adjacent (horizontally or vertically) AND the absolute difference between their intensities is less than or equal to a threshold T.
Your task is, given a starting pixel (r,c), to find the size (total number of pixels) of the coherently connected region it belongs to. You should treat the image grid as an undirected graph where pixels are nodes and coherence defines the edges.
Constraints:
About Topic: This implementation models a simplified approach to bottom-up segmentation, where pixels are recursively grouped based on local similarity constraints (similarity/proximity) to form larger, meaningful regions, often preceding full object recognition.
image = [[6,7,8],[5,5,9],[1,2,3]] start = (1, 1) threshold = 3
9
Starting at (1,1) with value 5, all pixels are reachable through paths where adjacent intensity differences are ≤ 3.