[PYTHON] Reproduce thermography-like color change with sigmoid function

1. Thermographic color representation

When sending the temperature measured by a temperature sensor to a server and visualizing it, there are times when you want to express the high and low temperatures in color, as in thermography. In addition, the expression of thermography is not limited to temperature, but it is effective when expressing the magnitude of numerical values by color. Intuitively, all you have to do is assign an arbitrary value to the hues arranged in the order of blue → green → red and get the values of the three colors. When I try to express it programmatically, it's a little annoying for what I want to do. Assuming that the range of values to be converted is in the range of 0.0 to 1.0 and each RGB color is in the range of 0 to 255, the thermography-like color expression makes the following color changes.

Value you want to convert R G B Colors expressed in RGB
0.0 0 0 255 Blue
0.25 0 200 200 light blue
0.5 0 255 0 Green
0.75 200 200 0 orange
1.0 255 0 0 Red

As a method of reproducing with this color change program, it seems that the method of describing by finely dividing the conditions with an if statement is often taken. However, I thought that it would be possible to reproduce finer color changes by approximating with some function. The following articles have already been posted. In this article, the parts where the color changes in various places are approximated by COS functions. However, even in this case, it is necessary to separate the parts that are stuck to 0 and the parts that are stuck to 1.

Convert value magnitude to thermography-like color

  1. Interval where the value changes continuously and smoothly Section attached to 3.1

I decided to apply the sigmoid function as a function that makes such a change.

3. Sigmoid function

The sigmoid function is a function created by modeling the properties of nerve cells in living organisms. It is used in the activation function of neural networks because it changes rapidly after a certain value.

The formula is as follows.

α becomes a gain, and by changing this value, the gradient of change can be changed.

  sigmoid(x) = \frac{\tanh(αx/2)+1}{2}
    -1 <= x <= 1

In addition, the above formula is the following graph.

sigmoid.png

4. Applying the sigmoid function to the color wheel

The final shape of the function to be aimed at is as shown in the graph below. gradation.png

The red color uses the sigmoid function as it is. The blue color uses the inverted function. For green, it is realized by adding the red and blue functions. See the code below for details.

When implemented in Python, it looks like this: You can check the graph above by running it on Jupyter. By changing the gain and offset values, you can change the degree of overlap of the value gradation. The sigmoid method implements the above sigmoid function expression as is. The colorBarRGB method creates a function for each color described above. The result is output in 0.0 to 1.0 for each color, so in the case of 16bit, the result of multiplying each color by 1024 will be the actual value.


import numpy as np
from pandas import DataFrame as df
import matplotlib.pyplot as plt
%matplotlib inline

gain = 10
offset_x= 0.2
offset_green = 0.6

def sigmoid(x, gain=1, offset_x=0):
    return ((np.tanh(((x+offset_x)*gain)/2)+1)/2)

def colorBarRGB(x):
    x = (x * 2) - 1
    red = sigmoid(x, gain, -1*offset_x)
    blue = 1-sigmoid(x, gain, offset_x)
    green = sigmoid(x, gain, offset_green) + (1-sigmoid(x,gain,-1*offset_green))
    green = green - 1.0
    return (blue,green,red)

#Input value is 0.0〜1.Range of 0
data = [colorBarRGB(x*0.001) for x in range(0,1000)]

color = df(data)
color.plot()

The result of converting the above result into an 8-bit (0 to 255) image is as follows. img2.jpg

I think that the above method can realize the expression of thermography with a fairly short code. Also, by adjusting the gain and offset, it seems that it is easy to make fine adjustments by changing the degree of gradation overlap.

Recommended Posts

Reproduce thermography-like color change with sigmoid function
Try to reproduce color film with Python
Write your own activation function with Pytorch (hard sigmoid)
Overflow error-free sigmoid function