image.sobel
result = image.sobel(img, [direction="both"])
Input: img, direction · Output: result
What it is
img is an Image. direction is an optional named string, "x", "y", or "both" (default). Computes the 3x3 Sobel gradient over img's BT.601 luma (replicate/clamped border). direction="x"/"y" return the raw signed gradient component; the default "both" returns the gradient MAGNITUDE sqrt(gx^2 + gy^2) (identical to image.gradient_magnitude(img)). Named direction=, not axis= — axis= is a reserved keyword the call machinery intercepts itself for sum/mean-style row/column reductions and would never reach this function.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = image.sobel(img, [direction="both"]) | img Imageimg is an Image.direction string, optional (default "both")direction is an optional named string, "x", "y", or "both" (default). | result Mat | Returns a Mat, same height x width shape as img. Added in v0.4.0. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
import image
# A vertical edge: left half dark, right half bright.
m = zeros(4, 8)
for y in 0 to 3
for x in 4 to 7
m[y, x] = 255
end for
end for
img = image_from_matrix(m)
gx = image.sobel(img, direction="x")
gy = image.sobel(img, direction="y")
print(gx[0, 3], gx[0, 4]) # 1020 1020 -- the edge, straddled from both sides
print(gy[0, 3]) # 0 -- no variation along y at all
mag = image.gradient_magnitude(img)
print(mag[0, 3] == gx[0, 3]) # true -- gy is zero here, so magnitude reduces to |gx|
# A colorspace round trip: pure red is hue 0, full saturation, full value.
red = image_new(2, 2, r=255, g=0, b=0)
hsv = image.rgb2hsv(red)
print(hsv.h[0, 0], hsv.s[0, 0], hsv.v[0, 0]) # 0 1 1
back = image.hsv2rgb(hsv)
print(back.width, back.height) # 2 2
1020 1020 0 true 0 1 1 2 2
from images
How it works
sobel(img, [direction="both"]) / scharr(img, [direction="both"]) -- gradient over the image's BT.601 luma. direction="x"/"y" return the raw (signed) component Mat; the default "both" returns the gradient MAGNITUDE sqrt(gx^2 + gy^2) (see gradient_magnitude below for the same thing under its own name), which is the reading docs/design/toolkit-image.md's edges(canny | sobel | scharr) line implies for a plain sobel(img) call with no direction named. Scharr uses the same shape with the rotationally-more-symmetric 3x3 Scharr kernel in place of Sobel's. Raw floats, not an 8-bit preview image -- same reasoning as image::luma: a caller that wants a displayable edge map already has canny/edge_detect, and clamping here would throw away the sign and the exact magnitude a hand-computed test needs to check against. Named direction=, not axis=: axis= is a reserved keyword the call machinery itself intercepts (it powers the sum(M, axis=0)-style row/column reductions -- see AXIS_HONOURING/ALWAYS_ACCEPTED_KEYS), stripping it out of style into a separate numeric-only channel BEFORE any builtin dispatch ever runs, this one included. A sobel axis= kwarg would silently never reach this match arm at all -- found by a real failing test here, not by inspection: axis="y" returned the SAME magnitude axis="both" did, because style_str(style, "axis") was reading a key that had already been removed.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.