Qu

image_from_matrix

Builtin index · Images

result = image_from_matrix(matrix)

Input: matrix · Output: result

What it is

Builds a grayscale Image directly from matrix, a 2-D Mat of numbers; each value is rounded and clamped to 0–255 and written to R=G=B for that pixel (matrix rows become image rows, columns become image columns).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = image_from_matrix(matrix)
matrix Image, matrix, Mat, or numbernot documented yet
result Image or matrix
Returns a new Image sized cols x rows to match the matrix's own shape. For a real pixel buffer you can further process/save, as opposed to imagesc's colormap display.

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

image_from_matrix(matrix) — builds a grayscale Value::Image directly from a numeric matrix's own values (assumed already in the 0-255 range, clamped/rounded otherwise) — R=G=B per pixel, the same "grayscale is just RGB with equal channels" convention image::Image::to_grayscale uses. The false-color (colormap-mapped) equivalent for arbitrary-range data is imagesc, which pushes a plot Heatmap instead of a real Value::Image — this builtin is for when a real pixel buffer is actually wanted (to save_image/further process), not just to display.

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