image.rgb2hsv
result = image.rgb2hsv(img)
Input: img · Output: result
What it is
img is an Image. Per-pixel RGB→HSV conversion (the whole-Image counterpart to the scalar to_hsv), reusing the exact same conversion the scalar builtin uses.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = image.rgb2hsv(img) | img Image or scalarnot documented yet | h —hue, degrees 0–360v —each 0–1 | Returns a Record with fields h (hue, degrees 0–360), s, v (each 0–1), each a Mat the 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
rgb2hsv(img) / rgb2lab(img) -- per-pixel colorspace conversion, the whole-Image counterpart to the existing scalar to_hsv/to_lab (a single color's coordinates). Reuse color::rgb_to_hsv/rgb_to_lab exactly (no separate per-pixel math to drift out of sync with the scalar builtins), called once per pixel. Returns a Record of three same-shape Mats (h/s/v or l/a/b) rather than a Value::Image -- hue is 0-360 and Lab's a*/b* run roughly -128..127, neither of which an 8-bit RGB Image can hold without truncating, and this codebase's Image type has no space tag to mark it "not really RGB" (a known, deliberately deferred gap -- see qu_image's own module doc comment). image::hsv2rgb/ image::lab2rgb above accept exactly this shape back.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.