Skip to content

Data Processing

This tutorial shows how to process the data read from the Senxor device.

Read the data

See the Basic Stream tutorial for how to read the data.

header, frame = dev.read()

The frame is a 2D float32 np.ndarray, which is the temperature in Celsius.

Indexing and slicing the data

You can index and slice the data like a normal NumPy array.

frame[0, 0] # The temperature at the top-left corner
frame[0, -1] # The temperature at the top-right corner
frame[-1, 0] # The temperature at the bottom-left corner
frame[-1, -1] # The temperature at the bottom-right corner
frame[0] # The first row
frame[:, 0] # The first column

Statistics of the data

frame.min() # The minimum temperature
frame.max() # The maximum temperature
frame.mean() # The average temperature
frame.std() # The standard deviation of the temperature
frame.var() # The variance of the temperature
frame.median() # The median temperature

Data processing

Since the data is a numpy array, you can use NumPy functions, open-cv functions, or scikit-image functions to process the data.

Also, pysenxor provides a set of data processing functions for simple data processing tasks.

These functions are implemented using NumPy internally, usually, if your project uses opencv, using the opencv built-in image processing functions will get better performance.

Convert the data to uint8 grayscale image

from senxor.proc import normalize
uint8_image = normalize(frame, dtype=np.uint8)

Convert the data to float32 grayscale image

from senxor.proc import normalize
float32_image = normalize(frame, dtype=np.float32)

Enlarge the image

from senxor.proc import enlarge
enlarged_image = enlarge(frame, scale=3) # The scale must be an integer >= 1.

Apply a colormap to the image

When processing thermal images, we often need to apply a colormap to convert the grayscale image to a color image.

pysenxor provides some built-in colormaps, and a function to apply the colormap to the image.

from senxor.proc import colormaps, apply_colormap

cmap = colormaps["inferno"]
normalized_frame = normalize(frame, dtype=np.float32)
colored_image = apply_colormap(normalized_frame, lut=cmap) # The result is a RGB image.

Available built-in colormaps:

  • autumn
  • bone
  • cividis
  • cool
  • hot
  • inferno
  • ironbow
  • jet
  • magma
  • pink
  • plasma
  • rainbow
  • rainbow2
  • spring
  • turbo
  • viridis

Display or save the image

You can use the cv2 or matplotlib to display or save the image.

using cv2

from senxor.proc import normalize, apply_colormap, colormaps
import cv2

cmap = colormaps["inferno"]

header, frame = dev.read()
normalized_frame = normalize(frame, dtype=np.uint8)
colored_image = apply_colormap(normalized_frame, lut=cmap)

cv2.imshow("senxor", colored_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

using matplotlib

from senxor.proc import normalize, apply_colormap, colormaps
import matplotlib.pyplot as plt

cmap = colormaps["inferno"]

header, frame = dev.read()
normalized_frame = normalize(frame, dtype=np.float32)
colored_image = apply_colormap(normalized_frame, lut=cmap)

plt.imshow(colored_image)
plt.show()

Parse the header

Parsing the header is optional and aimed at advanced use. The header returned by read() is a raw 1D numpy array of uint16; it carries hardware-related metadata such as supply voltage (VDD) and die temperature. When you need these or other frame metadata (e.g. frame counter, timestamp, min/max temperature, CRC), use parse_header() to get a structured SenxorHeader object.

from senxor.proc import parse_header

header, frame = dev.read()
if header is not None:
    parsed = parse_header(header)
    print(parsed.frame_counter)
    print(parsed.die_temp)

SenxorHeader attributes:

  • frame_counter — frame counter value
  • vdd — VDD voltage
  • die_temp — die temperature in °C
  • timestamp — timestamp value
  • maxVal — maximum temperature in °C
  • minVal — minimum temperature in °C
  • crc — CRC value

More examples

Data processing using opencv

import cv2

header, frame = dev.read()

normalized_frame = cv2.normalize(frame, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
enlarged_image = cv2.resize(normalized_frame, (frame.shape[1] * 3, frame.shape[0] * 3), interpolation=cv2.INTER_NEAREST)
colored_image = cv2.applyColorMap(enlarged_image, cv2.COLORMAP_JET)

cv2.imshow("senxor", colored_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Data processing using scikit-image

import skimage as ski

header, frame = dev.read()

normalized_frame = ski.exposure.rescale_intensity(frame, in_range='image', out_range='uint8')
enlarged_image = ski.transform.resize(normalized_frame, (frame.shape[1] * 3, frame.shape[0] * 3))

Summary

  • colormaps is a dictionary of built-in colormaps.
  • normalize() normalizes the data to the range [0, 255].
  • enlarge() enlarges the image.
  • apply_colormap() applies a colormap to the image.
  • parse_header() parses the raw header from read() into a SenxorHeader with frame_counter, vdd, die_temp, timestamp, maxVal, minVal, crc.