Sobel maps sharp changes in brightness. Use a grayscale or RGB image.
dotnet add package RecursiveAI.OneImageRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneImage; Image.Load("rectangle.png") .DetectEdges(EdgeDetector.Sobel) .Save("edges.png"); Console.WriteLine("Wrote edges.png");
Unzip the example, then dotnet run.
Wrote edges.pngrectangle.png in the download is a 9×7 black image with a white 3×3 rectangle. Using zero-based coordinates, the white pixels occupy columns 3–5 and rows 2–4.
The full program
using OneImage; Image.Load("rectangle.png") .DetectEdges(EdgeDetector.Sobel) .Save("edges.png"); Console.WriteLine("Wrote edges.png"); var saved = Image.Load("edges.png"); var row = new Gray8[9]; saved.CopyPixelsTo<Gray8>(row, new ImageRect(0, 3, 9, 1)); Console.WriteLine($"At (2,3), beside the edge: {row[2].V}"); Console.WriteLine($"At (4,3), inside the flat area: {row[4].V}");
dotnet run prints:
Wrote edges.png At (2,3), beside the edge: 255 At (4,3), inside the flat area: 0
OneImage rejects CMYK and Lab input to DetectEdges with ImageOperationException.
Variations
Try Scharr. It uses different weights to measure brightness changes and still returns a grayscale map by default. Keep using OneImage;.
Image.Load("rectangle.png") .DetectEdges(EdgeDetector.Scharr) .Save("scharr.png");
Related recipes
The short answer
Load a grayscale or RGB image, call DetectEdges(EdgeDetector.Sobel), and save the result. By default, OneImage runs the detector on brightness values and returns a grayscale map without alpha. Dark pixels show little or no local brightness change; brighter ones show stronger changes. No separate grayscale conversion is needed.
How does Sobel edge detection work?
A Sobel filter checks a 3×3 neighborhood around each pixel. One set of weights compares the left and right sides; a second set compares the top and bottom. OneImage combines the two responses by taking their magnitude, so either a light-to-dark or a dark-to-light transition can show up bright. A neighborhood entirely inside a solid rectangle gives zero in both directions. Just outside the rectangle it can give a strong response, because the neighborhood reaches across the boundary. That’s why the sample prints 255 beside the rectangle and 0 at its center.
The detector sees changes, not objects. A cast shadow can make an edge and a low-contrast object boundary can make a faint one. An edge map is neither a sharpened copy of the image nor a black-and-white threshold mask: it can contain intermediate gray values. On 8-bit images such as this sample, results are clamped to the 0–255 range. At the canvas boundary, OneImage defaults to using the nearest edge pixel for any missing neighbor. A uniform area at the border therefore doesn’t turn into a bright outline merely because it meets the edge of the file.
For a black-and-white edge map, apply a threshold to the Sobel output; the adaptive threshold recipe shows both a single cutoff and a local cutoff.
What’s the difference between Sobel and Scharr?
Both detectors in OneImage read a 3×3 neighborhood and measure changes in two directions. The weights differ: in the Sobel kernel, the two corner samples on one side have weight 1 each and the middle sample has weight 2; in Scharr those weights are 3, 10, and 3. You can try both on the same input using the variation above. Both use grayscale by default, which measures changes in the stored values’ Rec. 709 luma, a weighted combination of red, green, and blue. A boundary can be visible in color but faint in this grayscale map when the two colors have similar luma.