Qu

imhist

Builtin index · Images

result = imhist(img, [n_bins=256])

Input: img, n_bins · Output: result

What it is

img is an Image; n_bins is an optional positive integer (default 256; may be given positionally or as a named argument), the number of histogram bins. Computes raw per-bin BT.601-luma intensity counts.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = imhist(img, [n_bins=256])
img Imageimg is an Image.
n_bins optional (default 256)n_bins is an optional positive integer (default 256.
result Vec
Returns a Vec of length n_bins — data, not a rendered plot. Pipe the result into hist/bar to actually draw it.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

mb = [0, 0, 0, 0, 0; 0, 0, 0, 0, 0; 0, 0, 255, 0, 0; 0, 0, 0, 0, 0; 0, 0, 0, 0, 0]
binary_img = image_from_matrix(mb)
grown = imdilate(binary_img, 1)
print(imhist(grown, n_bins=4))   # [16, 0, 0, 9] -- only the two extreme bins are ever used

output
[16, 0, 0, 9]

from images

mg = [10, 60, 110, 160, 210; 20, 70, 120, 170, 220; 30, 80, 130, 180, 230; 40, 90, 140, 190, 240; 50, 100, 150, 200, 250]
gray_img = image_from_matrix(mg)
grown = imdilate(gray_img, 1)
print(imhist(grown, n_bins=4))   # [0, 6, 7, 12] -- three separate bins in use, no black survives at all

output
[0, 6, 7, 12]

from images

m = [40, 42, 41, 43; 44, 40, 250, 41]
img = image_from_matrix(m)
fixed = imadjust(img, in_low=0.0, in_high=1.0)
print(imhist(fixed, n_bins=8))   # [0, 7, 0, 0, 0, 0, 0, 1] -- unchanged, still clustered in one bin

output
[0, 7, 0, 0, 0, 0, 0, 1]

from images

m = [40, 42, 41, 43; 44, 40, 250, 41]
img = image_from_matrix(m)
auto = imadjust(img)
print(imhist(auto, n_bins=8))   # [7, 0, 0, 0, 0, 0, 0, 1] -- stretched out to both ends of the range

output
[7, 0, 0, 0, 0, 0, 0, 1]

from images

How it works

imhist(img, [n_bins=256]) — raw per-bin BT.601-luma intensity COUNTS as a Value::Vec (data, not a rendered plot; this codebase's separate hist/bar builtins do the rendering — pipe imhist's result into either).

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