RunMat
GitHub

imhist — Compute or display grayscale and indexed-image histograms.

imhist returns histogram counts for grayscale images and indexed images. Statement-form calls render a bar-chart histogram when plotting is available.

Syntax

counts = imhist(I)
counts = imhist(I, n)
[counts, binLocations] = imhist(I)
[counts, binLocations] = imhist(I, n)
counts = imhist(X, map)
[counts, binLocations] = imhist(X, map)

Inputs

NameTypeRequiredDefaultDescription
IAnyYesGrayscale intensity image.
nNumericScalarYes256Number of bins.
XAnyYesIndexed image matrix.
mapNumericArrayYesColormap with one RGB row per indexed-image bin.

Returns

NameTypeDescription
countsNumericArrayHistogram bin counts as a column vector.
binLocationsNumericArrayIntensity or colormap-index bin locations as a column vector.

Returned values from imhist depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:imhist:InvalidArgumentImage input, bin count, or colormap arguments are malformed or unsupported.imhist: invalid argument
RunMat:imhist:UnsupportedImageInput cannot be interpreted as a grayscale or indexed image.imhist: unsupported image input
RunMat:imhist:PlotFailedStatement-form histogram rendering fails.imhist: plotting failed

How imhist works

  • imhist(I) returns a 256-by-1 count vector for numeric grayscale images.
  • imhist(I, n) returns counts using n positive integer bins.
  • [counts, binLocations] = imhist(I) also returns the image-class bin centers.
  • Floating-point grayscale images must be finite normalized single or double data in [0, 1].
  • uint8 and uint16 grayscale images use class ranges [0, 255] and [0, 65535] respectively.
  • Logical grayscale images use two bins for false and true values.
  • imhist(X, map) treats X as an indexed image and returns one bin per row of the Nx3 colormap.
  • Indexed images reject non-finite, non-integer, and out-of-range indices. Colormap values must be finite RGB values in [0, 1].
  • Statement-form imhist(...) displays the histogram and returns no value. Builds without plotting support complete successfully without rendering.

Does RunMat run imhist on the GPU?

imhist gathers gpuArray inputs before validating image shape and class-specific ranges.

imhist terminates fusion chains because histogram counts depend on all image elements.

GPU memory and residency

imhist returns host-resident count and bin-location tensors because histogram assembly currently runs on the host.

Examples

Count Intensities In A Byte Image

I = uint8([0 1 1; 2 2 2]);
[counts, bins] = imhist(I);
[peak, idx] = max(counts);
[bins(idx), peak]

Expected output:

ans =
     2     3

Use Fewer Bins For A Normalized Image

I = [0 0.25 0.5 1.0];
[counts, bins] = imhist(I, 3);

Expected output:

counts =
     1
     2
     1

Histogram An Indexed Image

X = [1 2; 3 2];
map = [1 0 0; 0 1 0; 0 0 1];
[counts, bins] = imhist(X, map);

Expected output:

counts =
     1
     2
     1

Display A Histogram

I = uint8([0 64 128; 128 192 255]);
imhist(I);

Using imhist with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how imhist changes the result.

Run a small imhist example, explain the result, then change one input and compare the output.

FAQ

Why does imhist reject double values greater than 1?

MATLAB-style floating-point grayscale images are normalized image data. Use uint8 or uint16 when the values represent byte or word intensities.

How are indexed image offsets handled?

Double and single indexed images use one-based indices into the colormap. uint8, uint16, and logical indexed images use zero-based indices.

Does statement-form imhist always render a plot?

It renders when the plotting backend is available. Headless, no-plot, or non-main-thread contexts complete successfully without producing a value.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how imhist is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.