ThermalFrame
public sealed class ThermalFrame : IDisposable
One thermal frame: its pixels and the metadata that came with it.
Dispose every frame. A frame holds native memory, and the SDK produces them as fast as the camera runs. Not disposing one is not a crash -- the finalizer will eventually get it -- but at 32 frames a second the collector will not keep up, and memory will climb. using on every frame is the whole discipline.
ThermalFrame.Pixels is a view over native memory, not a copy. It is valid until this frame is disposed, and no longer. See that property for what to do if you need the data to outlive the frame.
This type is not thread-safe. Frames are handed out one per consumer, so sharing one across threads is not a pattern the API leads you into.
Example
await foreach (ThermalFrame frame in camera.FramesAsync(token)) { using (frame) { Console.WriteLine($"frame {frame.Metadata.Sequence}: " + $"{frame.MinTemperature:F1} to " + $"{frame.MaxTemperature:F1} C"); } }
ThermalFrame: constants
public const short InvalidPixel = -32768
The raw sample value that means "this pixel could not be measured".
It is not a cold pixel and it is not a bad reading: it is the absence of one. Distortion correction in DistortionCorrection.Wide produces it wherever an output pixel's source falls outside the sensor, by design and on every frame -- 6.1 % of a PI 400 frame and 1.9 % of a PI 640i frame, measured.
You only meet this value if you read ThermalFrame.Pixels yourself. Every route from this binding to a temperature answers Single.NaN for such a pixel instead.
ThermalFrame: properties
public bool ExtremesKnown { get; }
Whether this frame has a coolest and warmest pixel at all.
false when nothing in the frame was measurable, and then ThermalFrame.MinTemperature, ThermalFrame.MaxTemperature, ThermalFrame.MinIndex and ThermalFrame.MaxIndex are all 0 (the two temperatures Single.NaN in Fahrenheit), which is not a coolest pixel of 0.0 °C in the top-left corner. The same distinction the nullable chip temperature draws one field over.
Not a rare case: radial distortion correction in Wide mode leaves about a sixth of every frame unmeasurable by design, and a whole frame can be unmeasurable before the startup calibration finishes.
public int Height { get; }
Frame height in pixels.
public bool IsDisposed { get; }
Whether this frame has been disposed and its pixels released.
public int MaxIndex { get; }
Where the warmest pixel is, as a linear index into ThermalFrame.Pixels.
Only a pixel when ThermalFrame.ExtremesKnown is true; see ThermalFrame.MinIndex.
public Nullable<ValueTuple<int, int>> MaxLocation { get; }
Where the warmest measured pixel is, or null.
public float MaxTemperature { get; }
The warmest temperature in the frame, in the SDK's temperature unit.
Populated on a replayed frame too. See ThermalFrame.MinTemperature.
public FrameMetadata Metadata { get; }
What the camera recorded alongside the pixels.
public int MinIndex { get; }
Where the coolest pixel is, as a linear index into ThermalFrame.Pixels.
Only a pixel when ThermalFrame.ExtremesKnown is true. Prefer ThermalFrame.MinLocation, which does the row-major arithmetic and answers null when there is no such pixel; before the sibling, that arithmetic was a snippet in the manual rather than anything you could call.
public Nullable<ValueTuple<int, int>> MinLocation { get; }
Where the coolest measured pixel is.
public float MinTemperature { get; }
The coolest temperature in the frame, in the SDK's temperature unit (Celsius unless Sdk.TemperatureUnit chose Fahrenheit).
Populated on a replayed frame too, since version 12: playback measures each decoded frame's own extremes, so a recording has a cold spot and a hot spot exactly as a live stream does, including recordings written before that change. Unmeasurable pixels are excluded; a frame with nothing measurable in it reports zero for both in Celsius and Single.NaN for both in Fahrenheit, where a zero would read as 32 F. ThermalFrame.ExtremesKnown is the test.
Read in the unit in force when it is read, as ThermalFrame.GetTemperature(…) is.
public int PixelCount { get; }
How many pixels the frame holds.
public ReadOnlySpan<short> Pixels { get; }
The raw pixels, in the SDK's internal fixed-point format, row-major from the top-left.
- Throws
ObjectDisposedException: The frame has been disposed.
Nothing is copied when you read this, which is the point: at 32 hertz on a 640x480 sensor, copying every frame is 19 MB/s of pure overhead.
The values are tenths of a degree offset by 1000, not degrees. Use ThermalFrame.ToCelsius(…) for one, or ThermalFrame.CopyTemperaturesTo(…) for the frame.
Not every sample is a measurement. A pixel the SDK could not measure carries ThermalFrame.InvalidPixel; test with ThermalFrame.IsMeasured(…) before letting a sample into any statistic. This is the one place in the binding where that value is visible -- ThermalFrame.ToCelsius(…) and the bulk reads turn it into Single.NaN.
To keep the data past this frame's lifetime, call ThermalFrame.ToArray or copy the span yourself. Storing the span is not possible -- it is a ref struct and the compiler will not let you put it in a field, which is exactly the protection wanted here.
public int Width { get; }
Frame width in pixels.
ThermalFrame: methods
public ThermalFrame Clone()
An independent copy of this frame.
Returns: A new frame with its own pixels, which you must dispose.
- Throws
ObjectDisposedException: The frame has been disposed.
Rarely needed: frames from ThermalCamera.FramesAsync(…) and ThermalCamera.AcquireFrameAsync(…) are already yours and last until you dispose them. This is for handing a frame to something whose lifetime you do not control.
Example
using ThermalFrame keep = frame.Clone(); _pending.Enqueue(keep); // disposed by whoever dequeues it
public void CopyTemperaturesTo(Span<float> destination)
Convert the whole frame to temperatures in the SDK's temperature unit (Celsius unless Sdk.TemperatureUnit chose Fahrenheit), into a buffer you own.
-
destination: Where to write. Must hold at leastThermalFrame.PixelCountvalues. -
Throws
ArgumentException:destinationis too small. -
Throws
ObjectDisposedException: The frame has been disposed.
This is the allocation-free way to keep a frame's data: rent a buffer once and reuse it for every frame, rather than calling ThermalFrame.ToArray and allocating per frame.
The SDK converts the frame, in one native call. This binding does not loop over ThermalFrame.Pixels applying ThermalFrame.ToCelsius(…) itself, which is what it used to do: the fixed-point scale is the SDK's internal business and may change, and the ThermalFrame.InvalidPixel guard is easy to forget -- two of the three bindings over this ABI did forget it and returned -3376.8 from a bulk read. The numbers are unchanged here, because this binding was the one that had it right.
A pixel the SDK could not measure is written as Single.NaN, the same answer ThermalFrame.GetTemperature(…) gives. Note what that does to a whole-buffer statistic: destination.ToArray().Min() is NaN if any pixel was unmeasured, because Enumerable and MathF both propagate it. That is deliberate -- it is a loud wrong answer rather than a quiet one -- but if what you want is the coldest pixel that was measured, read ThermalFrame.MinTemperature, which excludes them and costs nothing.
Example
float[] buffer = new float[camera.Width * camera.Height]; await foreach (ThermalFrame frame in camera.FramesAsync(token)) { using (frame) { frame.CopyTemperaturesTo(buffer); Analyse(buffer); // the buffer outlives the frame } }
public void Dispose()
Release the frame's native memory.
After this, ThermalFrame.Pixels and every accessor that reads the frame throw ObjectDisposedException. Disposing twice is safe.
public static short FromCelsius(float celsius)
Convert degrees Celsius to the SDK's raw sample format.
celsius: A temperature in degrees Celsius.
Returns: The equivalent raw sample; ThermalFrame.InvalidPixel when celsius is not a measurement at all (Single.NaN, or at or below absolute zero); or 32767 when it is hotter than 3176.7 C, which this format does not reach. Use ThermalFrame.IsEncodable(…) to tell those apart from an ordinary answer.
Truncates rather than rounds, matching the SDK exactly, so a round trip through this and ThermalFrame.ToCelsius(…) can lose up to a tenth of a degree.
It does not wrap, and it used to. A (short) cast of an out-of-range float is unspecified in an unchecked context; in practice 3200 C came back as -32536, which reads as -3353.6 C -- a plausible cold measurement with nothing to say it was not one. That was a measured defect, the sibling of one in the opposite direction.
The case that was live is Single.NaN. ThermalFrame.ToCelsius(…) answers NaN for an unmeasured pixel, deliberately, and radial distortion correction in Wide mode produces unmeasured pixels on every frame -- 15.8 % of a G7. So the round trip this documents used to turn "nothing was measured" into 0, an ordinary -100.0 C reading. It now returns ThermalFrame.InvalidPixel, which is what went in.
No value inside the encoding changed, so a threshold computed from a real temperature is the number it always was.
Example
// Count pixels above 60 C without converting the whole frame. short threshold = ThermalFrame.FromCelsius(60.0f); int hot = 0; foreach (short raw in frame.Pixels) { if (raw >= threshold) { hot++; } }
public float GetTemperature(int x, int y)
The temperature at one pixel, in the SDK's temperature unit (Celsius unless Sdk.TemperatureUnit chose Fahrenheit).
x: Column, from the left edge.y: Row, from the top edge.
Returns: The temperature in the SDK's temperature unit, or Single.NaN when the SDK could not measure that pixel.
- Throws
ArgumentOutOfRangeException: The coordinates are outside the frame. - Throws
ObjectDisposedException: The frame has been disposed.
Convenient for a handful of probes. For a whole frame, read ThermalFrame.Pixels and convert in a loop -- this costs a native call and a bounds check per pixel.
A pixel the SDK could not measure reads as Single.NaN, and has done since version 12 without this ever saying so. Test with Single.IsNaN(…), or read the raw sample and use ThermalFrame.IsMeasured(…) if you want to know why. See ThermalFrame.InvalidPixel for how much of a frame this is.
Example
float centre = frame.GetTemperature(frame.Width / 2, frame.Height / 2);
public static bool IsEncodable(float celsius)
Whether a temperature has a sample of its own in this encoding.
celsius: A temperature in degrees Celsius.
Returns: true when ThermalFrame.FromCelsius(…) will answer the sample for this temperature, to the format's usual tenth of a degree; false when it will answer ThermalFrame.InvalidPixel or saturate at 32767.
The companion that lets ThermalFrame.FromCelsius(…) stay total without lying, and the same shape the rest of the SDK uses for "does this value mean anything" -- a companion rather than a magic value inside the value space, because 0.0 and the bounds are legitimate readings.
There is no cold end to check: absolute zero puts the coldest measurable input at -1731.5 counts, so no measurement can saturate downwards and none can be driven onto ThermalFrame.InvalidPixel.
Example
if (!ThermalFrame.IsEncodable(threshold)) { // This camera's format cannot express that temperature. }
public static bool IsMeasured(short raw)
Whether a raw sample is a measurement at all.
raw: A value fromThermalFrame.Pixels.
Returns: false only for ThermalFrame.InvalidPixel.
The guard every statistic over ThermalFrame.Pixels needs, spelled once. ThermalFrame.MinTemperature and ThermalFrame.MaxTemperature already apply it.
Example
int measured = 0; foreach (short raw in frame.Pixels) { if (ThermalFrame.IsMeasured(raw)) { measured++; } }
public float[] ToArray()
The whole frame as temperatures in the SDK's temperature unit (Celsius unless Sdk.TemperatureUnit chose Fahrenheit), in a newly allocated array.
Returns: ThermalFrame.PixelCount temperatures, row-major from the top-left, with Single.NaN wherever the SDK could not measure the pixel.
- Throws
ObjectDisposedException: The frame has been disposed.
Allocates. In a frame loop prefer ThermalFrame.CopyTemperaturesTo(…) with a reused buffer, whose remarks also cover what the NaN does to a statistic taken over the whole array.
Example
float[] temperatures = frame.ToArray(); // Average() propagates NaN, so the unmeasured pixels are // dropped rather than averaged in. Console.WriteLine(temperatures.Where(t => !float.IsNaN(t)) .Average());
public static float ToCelsius(short raw)
Convert one raw sample to degrees Celsius.
raw: A value fromThermalFrame.Pixels.
Returns: The temperature in degrees Celsius, or Single.NaN when raw is ThermalFrame.InvalidPixel.
The SDK stores pixels as tenths of a degree offset by 1000, which is half the memory of a float and what the camera sends. This is the whole of that format, plus one check. The format is Celsius by definition, so this answers in Celsius whatever Sdk.TemperatureUnit says; the other reads of a frame follow the unit.
An unmeasured pixel comes out as Single.NaN, not as a number. The bare formula would give -3376.8 C, which is far outside any real range and yet reads as a measurement -- it plots, it averages, and it drags a minimum with it. That was NaN is the same answer ThermalFrame.GetTemperature(…) and RegionSample.Value give, for the same reason: every comparison against NaN is false, so it cannot be used by accident.
This is a deliberate difference from the C and C++ conversions of the same name, which apply the formula and nothing else. Those are format primitives sitting in the SDK's own hot loops, where the conversion has to stay total; this one is not in any such loop, and it is the only route from a raw sample to a temperature on the managed surface. Use ThermalFrame.IsMeasured(…) if you want the distinction rather than the NaN.
Example
// MathF.Max propagates NaN, so the unmeasured pixels have to // be skipped rather than compared away. float hottest = float.MinValue; foreach (short raw in frame.Pixels) { if (!ThermalFrame.IsMeasured(raw)) { continue; } hottest = MathF.Max(hottest, ThermalFrame.ToCelsius(raw)); }