Structured Edge Detector¶
| Fact | Value |
|---|---|
| Also called | Structured forests, SED, Structured edge detection |
| Status | Available through an optional pretrained model |
| Family | Edge detection and boundary analysis |
| Engine | Science engine |
| Type | Nonlinear, learned, structured random forest |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | 16 |
| Code | engine/src/pixelnexus_engine/filters/structured_edge.py, structured_edge |
| Operator code | engine/src/pixelnexus_engine/filters/structured_edge.py, structured_edge_representation |
| Packages | OpenCV |
Overview¶
The Structured Edge Detector of Dollar and Zitnick learned what an edge is from people. It was trained on the BSDS500 photos, where people drew the outlines of objects. A forest of decision trees looks at small patches of the image and predicts whole patches of edges. So it marks the borders of objects and regions and stays quiet on texture, where gradient detectors such as Canny mark every change. The filter runs in the PixelNexus engine, with OpenCV.
It needs an optional model of 6.4 MB. The model downloads only when you click Download model in the Properties panel, which first shows its size and license. It goes to the engine's models folder on your own computer, which git ignores, and the engine checks its SHA-256 before it keeps it. The model comes from Dollar's Structured Edge Detection Toolbox, converted for OpenCV. Its license, the Microsoft Research License, allows non-commercial use only, such as teaching, research and personal experiments. That is why it is not part of PixelNexus, whose own code is under the MIT license.
How it works¶
- Turn the RGB image into color and gradient channels.
- Run 4 of the 8 trees at every second pixel, each predicting an edge patch.
- Average the overlapping edge patches into an edge probability.
- For thin edges, find the orientation and keep only the strongest values across each edge.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(E(x)\): The edge probability at pixel x
- \(y_t(p)\): The edge patch that tree t predicts at position p
- \(P(x)\): The predictions whose edge patches cover x
The image is turned into channels. They hold color in CIE LUV, the gradient magnitude, and the gradient in 4 orientations, at two scales. Differences between cells of a 5 x 5 grid in a patch add self-similarity features. Each tree of a random forest looks at a patch of 32 x 32 pixels. It predicts a whole edge patch y of 16 x 16 pixels, a structured output, instead of one label. The model has 8 trees. At each position, every 2 pixels, 4 of them run, so the edge patches overlap and every pixel collects many votes. E(x) averages the votes of the set P(x) of predictions that cover pixel x, which gives the edge probability. Dollar and Zitnick trained the forest on the BSDS500 images, where people drew the edges of objects. So it learned which changes in an image people call edges, and it ignores texture that Canny would mark.
A classic detector decides edge or no edge at each pixel from a local rule, such as a large gradient. A structured forest predicts a whole patch of edge labels at once, so its output has the shape of real outlines: lines that continue, corners and junctions. Training such trees needs a trick, since a patch of labels has no simple order. Dollar and Zitnick map the label patches into a simpler space and group them into a few classes at each split, so a tree can use the usual split rules. The details are in their paper.
Because the forest learned from outlines drawn by people, it treats texture as the inside of a region, not as edges. That is what the worked example shows. It also means that it works best on photos like those it learned from.
Kernel or operator¶
There is no kernel. The trees and their thresholds are in the model file. The Inspector shows the stages with the current settings.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Output output |
Edge probability | Edge probability, Thin edges, Edge orientation | The edge probability from 0 to 1 as gray, the edges thinned to one pixel, or the direction of the edges from 0 to 180 degrees as gray. |
Thinning radius nmsRadius |
2 | 1 to 10 px | How far across the edge the thinning looks for a stronger value. OpenCV uses 2. |
Border radius boundaryRadius |
0 | 0 to 20 px | Edges this close to the image edge are removed. OpenCV uses 0. |
Thinning multiplier m |
1 | 1 to 2 | A value must beat its neighbors across the edge times this to stay. OpenCV uses 1. |
Implementation¶
structured_edge in engine/src/pixelnexus_engine/filters/structured_edge.py loads the model with cv2.ximgproc.createStructuredEdgeDetection, once per engine process. It passes the RGB image as floats from 0 to 1, as OpenCV asks, to detectEdges. The model file sets its options. It has 8 trees, and 4 of them run at each position. Patches of 32 pixels predict edge patches of 16 pixels, with a stride of 2 and 4 gradient orientations. The thin edges use computeOrientation and then edgesNms with the thinning radius, the border radius and the multiplier. The orientation output shows 0 to pi from black to white.
The model is listed in catalog/models.yaml with its source, size, license and SHA-256. The engine route POST /api/v1/models/{id}/download fetches it, writes it to a temporary file, checks the SHA-256, and only then moves it into place. Without the model, the filter stops with a message that says to download it.
Worked example¶
| Area | Structured edges above 50 | Canny edge pixels |
|---|---|---|
| Stripes | 0 | 1296 |
| Fine noise | 0 | 1983 |
| Coarse | 9 | 918 |
| Flat | 0 | 0 |
| Borders between the quarters | 65 | 323 |
The test image of the figure. Each quarter is counted without a margin of 4 pixels, so its border with the next quarter is left out. The border row counts the bands 4 pixels wide along the two borders between the quarters. Structured edges count when the thinned edge is above 50 of 255.
Inside the four textures, the structured edges stay below 50 almost everywhere, while Canny marks hundreds to thousands of pixels in each. Along the borders between the quarters, where one texture meets another, the structured edges are strongest. This test image is synthetic, so the edge values are lower than on photos.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels. Each position runs 4 trees of fixed depth.
Artifacts and limitations¶
Limitations
- Needs the optional model, which is licensed for non-commercial use only.
- It learned from natural photos, so drawings, scans and test images can give odd results.
- The model is fixed. It cannot be tuned to other kinds of edges here.
Artifacts
- Soft, wide edges in the probability output, until they are thinned.
Tests¶
The catalog must list the model with its size, license and SHA-256. A download from a local file must be checked and moved into place, and a download with the wrong checksum must not be kept. Without the model, the filter must ask for the download. With the model downloaded, its SHA-256 must match, the three outputs must equal OpenCV's, and the thin edges must lie on the outline of a disk and not inside it. These last tests skip on a machine without the model. The engine API must list the model and refuse an unknown one. An E2E test checks that the studio shows the size and license, and downloads only on a click.
References¶
- Dollar, P., and Zitnick, C. L. Fast edge detection using structured forests. IEEE Transactions on Pattern Analysis and Machine Intelligence 37(8), 1558 to 1570, 2015. DOI 10.1109/TPAMI.2014.2377715.
- OpenCV documentation, cv::ximgproc::StructuredEdgeDetection.