imhist — Compute or display grayscale and indexed-image histograms.
imhist counts grayscale intensities or indexed-image colormap entries. Programmatic calls return double column vectors for counts and optional bin locations; host statement-form calls display a bar-chart histogram when presentation is available.
Syntax
imhist(I)
imhist(I, n)
imhist(X, map)
counts = imhist(I)
counts = imhist(I, n)
[counts, binLocations] = imhist(I)All supported imhist forms
imhist(I)
imhist(I, n)
imhist(X, map)
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
I- Type
Any- Required
- Yes
- Description
- Grayscale intensity image.
n- Type
NumericScalar- Required
- Yes
- Default
256- Description
- Number of bins.
X- Type
Any- Required
- Yes
- Description
- Indexed image matrix.
map- Type
NumericArray- Required
- Yes
- Description
- Colormap with one RGB row per indexed-image bin.
Returns
counts- Type
NumericArray- Description
- Histogram bin counts as a column vector.
binLocations- Type
NumericArray- Description
- Intensity or colormap-index bin locations as a column vector.
Returned values from imhist depend on how many outputs the caller requests.
Errors
RunMat:imhist:InvalidArgument- When
- Image input, bin count, or colormap arguments are malformed or unsupported.
- Message
imhist: invalid argument
RunMat:imhist:UnsupportedImage- When
- Input cannot be interpreted as a grayscale or indexed image.
- Message
imhist: unsupported image input
RunMat:imhist:PlotFailed- When
- Statement-form histogram rendering fails.
- Message
imhist: plotting failed
Forms and bins
imhist(I) uses 256 bins for numeric grayscale data and two bins for logical data. imhist(I,n) uses a positive integer-valued scalar n up to one million bins. [counts,binLocations] returns the class-wide bin centers as a second double column vector.
imhist(X,map) counts indexed values into one bin per row of a nonempty double Nx3 RGB colormap. Each colormap component must be finite and in [0,1]. Floating indexed values use one-based indices; uint8, uint16, and logical indexed values use zero-based indices. Nonfinite, fractional, and out-of-range indices reject.
Floating grayscale images must be finite normalized single or double values in [0,1]. Grayscale int8, int16, int32, uint8, uint16, and uint32 use their full class ranges; int64 and uint64 image classes reject without lossy conversion. Counts and locations are double arrays.
Execution and compatibility
A native typed-integer scalar bin count is a RunMat-only extension. MATLAB-compatible mode accepts the documented positive integer-valued floating scalar form and rejects the extension before data access.
Grayscale gpuArray input is downloaded non-destructively through its owning provider, counted on the host, and programmatic outputs are restored to the same owner when supported. Unsupported automatic restoration may retain the host result; operational provider failures remain errors. Indexed gpuArray input is unsupported. Histogram assembly terminates fusion chains; there is no histogram provider kernel.
For host input, a zero-output statement call displays a bar chart when a graphics host is available; no-plot, headless, and non-main-thread presentation unavailability completes without a visible figure. Existing resident zero-output calls return host counts instead of plotting. That resident behavior remains distinct from programmatic restored outputs.
The computation is a host materialization boundary, not an arbitrary CPU fallback for an existing provider histogram kernel. Download, output upload, graphics presentation, suspension, and error ordering use their existing shared owners. Graphics figures are local state, not portable values for worker or foreign-runtime transfer.
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 3Use fewer bins for a normalized image
I = [0 0.25 0.5 1.0];
[counts, bins] = imhist(I, 3);
countsExpected output:
ans =
1
2
1Histogram an indexed image
X = [1 2; 3 2];
map = [1 0 0; 0 1 0; 0 0 1];
[counts, bins] = imhist(X, map);
countsExpected output:
ans =
1
2
1Display a histogram
I = uint8([0 64 128; 128 192 255]);
counts = imhist(I);
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?⌄
Floating grayscale images represent normalized intensities in [0,1]. Use uint8 or uint16 for byte or word intensities.
How are indexed image offsets handled?⌄
Double and single indexed images use one-based indices. uint8, uint16, and logical indexed images use zero-based indices.
Does statement-form imhist always render a plot?⌄
Host calls render when a graphics backend and presentation context are available. Headless/no-plot presentation unavailability completes without a visible figure; existing resident zero-output calls return host counts.
Related Image functions
Source you can read
Every RunMat function's source is public. Read exactly how imhist is executed, line by line, in Rust.
- View the source for imhist in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.