Qu

image.gradient_magnitude

Builtin index · Images

result = image.gradient_magnitude(img)

Input: img · Output: result

What it is

img is an Image. Gradient magnitude sqrt(gx^2 + gy^2) from the Sobel gradient over img's BT.601 luma — the same value image.sobel(img)/image.sobel(img, direction="both") returns, under its own more discoverable name.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = image.gradient_magnitude(img)
img Imagenot documented yet
result Mat
Returns a Mat. 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

output
1020 1020
0
true
0 1 1
2 2

from images

How it works

gradient_magnitude(img) -- sqrt(gx^2 + gy^2) from the Sobel gradient, as its own name rather than only reachable via sobel(img)/sobel(img, direction="both") (which this is identical to) -- the design doc lists both spellings as acceptable and this is the more discoverable one for "I just want the edge strength."

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.