Image Segmentation: Felzenszwalb Algorithm
Three pixels are enough to work through a graph-based segmentation by hand and see how two bookkeeping errors in the Java code can move a boundary.
A color picker gives me three numbers for a pixel. Deciding where the object around that pixel ends takes more information. A single step from dark gray to light gray might mark the edge of a painted sign. The same step might occur a hundred times inside a patch of gravel. For my Java coursework I explored how to make that decision with a graph, using the algorithm described by Pedro Felzenszwalb and Daniel Huttenlocher. Efficient Graph-Based Image Segmentation.
Turning the image into edges
The program opens an image through a file chooser and applies a Gaussian blur with ImageJ. Each pixel becomes a vertex. I connect it to the pixel on its right, the one below it, and the two diagonals to its right, wherever those coordinates fall inside the image. When the whole grid has been visited, an interior pixel is connected to all eight surrounding pixels. Generating edges in just four directions visits each neighboring pair once. Neighbor construction.
I sort the edges by increasing weight. Each pixel starts in its own component, so the cheapest edges get the first chance to form regions. A disjoint-set forest stores a parent, rank, and size for each entry. Finding a representative lets me ask whether two far-apart pixels have already become connected through a chain of accepted edges. Joining two representatives updates the region size in one place.
How much graph does a photograph need?
There’s a useful size check hidden in those four directions. For an image three pixels wide and two pixels high, I expect four horizontal edges, three vertical edges, and four diagonals. The graph builder should produce eleven. At a corner, some neighbor coordinates fall outside the grid. With a row only one pixel high, all the vertical and diagonal edges disappear. Those two shapes check the boundary logic without needing to squint at a segmented photograph.
The same arithmetic explains a practical cost. For a grid of width w and height h, the count is (w − 1)h + w(h − 1) + 2(w − 1)(h − 1). A 1920 by 1080 image gives 8,285,402 edge records before the first merge. The Java version keeps these records in a list and sorts the whole list. Each edge also carries endpoint objects and references used to retrieve coordinates and colors. If I were adapting the project to handle larger photographs, I’d start by measuring graph construction and sorting separately from union operations. Replacing the coordinates of an endpoint with its integer pixel index is one way to change the storage cost while preserving the neighborhood and weight calculations. Graph construction and sorting.
Three pixels and one boundary
The decision at the next edge depends on both components it touches. The threshold for a component is its largest accepted edge weight plus k / size. A single pixel starts with internal difference zero. For a concrete first-pass example, take three pixels in a row, two successive edge weights of 4 and 11, and a value of 12 for k.
The edge of weight 4 fits under both singleton thresholds of 12. Joining those two pixels gives a component of size two. Its new threshold is 4 + 12 / 2, or 10. When the edge of weight 11 comes up, the third pixel would accept it, since 11 is below 12. The pair already formed has the stricter limit of 10, so the boundary stays. Changing the second edge to 9 would merge all three. That one calculation shows why the sort order and the component sizes both matter.
How the scale parameter changes a decision
The program starts with k = 1500. The value of 12 above was deliberately small so I could trace a rejection in just three vertices. With the application’s setting, a component of 100 pixels gets an allowance of 15 on top of its largest accepted weight. At 1,000 pixels that allowance shrinks to 1.5. Suppose two such regions both have internal difference 7. An edge of weight 14 clears the smaller region’s threshold of 22, but exceeds the larger region’s threshold of 8.5. Increasing the number of similar pixels behind one side of a boundary can change the decision at its edge.
The Gaussian blur happens before these weights are even calculated. Blurring a sharp transition changes the neighboring colors presented to the graph, while changing k changes the allowance used when combining the resulting components. To understand a result, I would hold the input and blur fixed while varying k, then hold k fixed while varying the blur. The component counts and their areas give me something comparable across runs even when the preview colors change. The starting parameters and processing order.
What the Java version actually computes
Two details in my implementation can change that result. The edge weight was meant to be the Euclidean distance between RGB triples. The source subtracts the starting red channel from the ending green channel, and the starting green from the ending blue. Put two identical pixels with RGB values (10, 40, 90) next to each other. Their intended distance is zero. The current expression gives the square root of 30 squared plus 50 squared, about 58.3. An all-gray example hides the error because its three channel values coincide. The edge-weight calculation.
The second detail concerns which representative survives a merge. On equal ranks, join(a, b) makes b the parent. The segmentation loop writes the new threshold to a and reads the size stored there. In the three-pixel example, the next lookup can therefore reach the surviving root with its old threshold of 12 and accept the edge of weight 11. Looking up the representative after the join would put the new threshold on the component the next edge actually sees. Union and threshold updates.
My application also makes a cleanup pass for components smaller than five pixels, then assigns a random display color to each final representative. To check the three-pixel calculation, I would inspect component membership immediately after the first pass. By the time the preview window opens, cleanup can have joined those tiny regions, and the colors can change each time the program runs. The representatives keep a much clearer record of which boundary the merge rule chose. Cleanup and rendering.