Startup Configuration

When imported, the thermal_cam_config.py file provides the Thermal Camera's initial power-up alarm threshold as well as minimum and maximum display range values. The power-up configuration parameters can be changed by editing the file with your favorite text editor.

Values are in degrees Fahrenheit.

# thermal_cam_config.py
# ### Alarm and range default values in Farenheit ###
ALARM_F = 120
MIN_RANGE_F = 60
MAX_RANGE_F = 120

# ### Display characteristics
SELFIE = False  # Rear camera view; True for front view

Converter Helpers

The thermal_cam_converters.py module consists of two temperature converters, one for Celsius to Fahrenheit and the other for Fahrenheit to Celsius. The value to be converted is passed as an argument to the appropriate helper.  Because the Thermal Camera's sensor has limited accuracy, a rounded integer value is returned.

# thermal_cam_converters.py


def celsius_to_fahrenheit(deg_c=None):  # convert C to F; round to 1 degree C
    return round(((9 / 5) * deg_c) + 32)


def fahrenheit_to_celsius(deg_f=None):  # convert F to C; round to 1 degree F
    return round((deg_f - 32) * (5 / 9))

Pseudocolor Spectrum Converter

Showing a visual image of temperatures requires the use of a spectrum of gradual color changes that correspond to the range of temperatures to be displayed. Since the colors are representative of the measured temperature, the collection of colors is called a pseudocolor spectrum. The pseudocolor spectrum of heated iron was used for this thermographic imaging project.

The color of a heated iron bar as a temperature scale originated with blacksmiths to determine when the metal can be shaped, joined, or hardened. Cold iron starts as a blueish color that changes to purple, red, orange, yellow, and eventually glows white-hot as the temperature increases.

temperature___humidity_interpolation_grid_example.019.jpeg
The RGB values used to create the iron pseudocolor spectrum.

For this project, a helper was created that converts a temperature index value of 0.0 to 1.0 to the RGB values needed to create the iron pseudocolor spectrum on the PyGamer's color TFT display.

Within the iron_spectrum.py file are two helpers, the primary index_to_rgb() code that converts the index to the corresponding RGB value and a map_range() helper used to calculate values within the index_to_rgb() helper.

map_range()

The map_range() helper accepts an input value inside of a specified input range and returns a proportional value constrained by a specified output range. The input value x is contained in the range in_min to in_max. The returned output value is constrained to the range out_min and out_max.

def map_range(x, in_min, in_max, out_min, out_max):
    """
    Maps and constrains an input value from one range of values to another.
    (from adafruit_simpleio)
    :return: Returns value mapped to new range
    :rtype: float
    """
    in_range = in_max - in_min
    in_delta = x - in_min
    if in_range != 0:
        mapped = in_delta / in_range
    elif in_delta != 0:
        mapped = in_delta
    else:
        mapped = 0.5
    mapped *= out_max - out_min
    mapped += out_min
    if out_min <= out_max:
        return max(min(mapped, out_max), out_min)
    return min(max(mapped, out_max), out_min)

index_to_rgb()

The index_to_rgb helper accepts an index input value from 0.0 to 1.0, returning a 24-bit RGB color value. Within this helper, the input value is converted to an internal spectrum, represented by the band variable. The spectrum band value ranges from 0 to 600, arbitrarily selected to provide a simple way to understand the gradual color shifting within the spectrum.

Within each sub-band, the red, green, and blue components are established and calculated. In the red to orange sub-band (300 to 399) for example, the red value is held at 1.0, blue at 0.0, where green changes proportionally from 0.0 to 0.5 as the band value increases.

if band >= 300 and band < 400:  # red to orange
    red = 1.0 ** gamma
    grn = map_range(band, 300, 400, 0.0, 0.5) ** gamma
    blu = 0.0

The gamma parameter is applied to improve the visual perception of the color spectrum, improving the continuity or smoothness of the spectrum, helping to compensate for the differences between human color perception and the source display's rendition of color. For the PyGamer's TFT display, a gamma value of 0.5 works nicely. A gamma value of 1.0 seems to work the best with the MatrixPortal's 32 x 64 RGB LED display.

Finally, the resulting 0.0 to 1.0 values for red, green, and blue determined within a sub-band are used to calculate the returned 24-bit RGB value.

def index_to_rgb(index=0, gamma=0.5):
    """
    Converts a temperature index to an iron thermographic pseudocolor spectrum
    RGB value. Temperature index in range of 0.0 to 1.0. Gamma in range of
    0.0 to 1.0 (1.0=linear), default 0.5 for color TFT displays.
    :return: Returns a 24-bit RGB value
    :rtype: integer
    """

    band = index * 600  # an arbitrary spectrum band index; 0 to 600

    if band < 70:  # dark gray to blue
        red = 0.1
        grn = 0.1
        blu = (0.2 + (0.8 * map_range(band, 0, 70, 0.0, 1.0))) ** gamma
    if band >= 70 and band < 200:  # blue to violet
        red = map_range(band, 70, 200, 0.0, 0.6) ** gamma
        grn = 0.0
        blu = 1.0 ** gamma
    if band >= 200 and band < 300:  # violet to red
        red = map_range(band, 200, 300, 0.6, 1.0) ** gamma
        grn = 0.0
        blu = map_range(band, 200, 300, 1.0, 0.0) ** gamma
    if band >= 300 and band < 400:  # red to orange
        red = 1.0 ** gamma
        grn = map_range(band, 300, 400, 0.0, 0.5) ** gamma
        blu = 0.0
    if band >= 400 and band < 500:  # orange to yellow
        red = 1.0 ** gamma
        grn = map_range(band, 400, 500, 0.5, 1.0) ** gamma
        blu = 0.0
    if band >= 500:  # yellow to white
        red = 1.0 ** gamma
        grn = 1.0 ** gamma
        blu = map_range(band, 500, 580, 0.0, 1.0) ** gamma

    return (int(red * 255) << 16) + (int(grn * 255) << 8) + int(blu * 255)

This guide was first published on Jun 09, 2021. It was last updated on 2021-06-09 17:11:13 -0400.

This page (Other Modules) was last updated on Feb 27, 2022.

Text editor powered by tinymce.