Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flirRawToTemperature to use shutter temperature #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 57 additions & 29 deletions flir2tif/Get_FLIR.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self):
self.calibrationX = 0.0
self.calibrationb1 = 0.0
self.calibrationb2 = 0.0

self.shuttertemperature = 0.0

def options():

Expand Down Expand Up @@ -185,6 +185,7 @@ def get_calibrate_param(metadata):
try:
if 'terraref_cleaned_metadata' in metadata:
fixedmd = metadata['sensor_fixed_metadata']
sensor_variable_meta = metadata['sensor_variable_metadata']
if fixedmd['is_calibrated'] == 'True':
return calibparameter
else:
Expand All @@ -199,6 +200,7 @@ def get_calibrate_param(metadata):
calibparameter.calibrationX = float(fixedmd['calibration_X'])
calibparameter.calibrationb1 = float(fixedmd['calibration_beta1'])
calibparameter.calibrationb2 = float(fixedmd['calibration_beta2'])
calibparameter.shuttertemperature = float(sensor_variable_meta['shutter_temperature_[K]'])
return calibparameter

except KeyError as err:
Expand Down Expand Up @@ -302,59 +304,85 @@ def flir_data_visualization(im, outfile_path, Gmin, Gmax):
return np.array(img_data)

# convert flir raw data into temperature C degree, for date after September 15th
# code converted from Matlab https://github.com/terraref/computing-pipeline/blob/master/scripts/FLIR/FlirRawToTemperature.m
def flirRawToTemperature(rawData, calibP):

R = calibP.calibrationR
B = calibP.calibrationB
F = calibP.calibrationF
J0 = calibP.calibrationJ0
J1 = calibP.calibrationJ1


# Constant camera-specific parameters determined by FLIR
# Plank constant - Flir
R = calibP.calibrationR # function of integration time and wavelength
B = calibP.calibrationB # function of wavelength
F = calibP.calibrationF # positive value (0 - 1)
J0 = calibP.calibrationJ0 # global offset
J1 = calibP.calibrationJ1 # global gain

# Constant Atmospheric transmission parameter by Flir
X = calibP.calibrationX
a1 = calibP.calibrationa1
b1 = calibP.calibrationb1
a2 = calibP.calibrationa2
b2 = calibP.calibrationb2


# Constant for VPD computation (sqtrH2O)
H2O_K1 = 1.56
H2O_K2 = 0.0694
H2O_K3 = -0.000278
H2O_K4 = 0.000000685

H = 0.1
T = 22.0
D = 2.5
E = 0.98

K0 = 273.15


K0 = 273.15 # Kelvin to Celsius temperature constant

# Environmental factors
H = 0.1 # Relative Humidity from the gantry (0 - 1)
T = calibP.shuttertemperature - K0 # air temperature in degree Celsius from the gantry
D = 2.5 # ObjectDistance - camera/canopy (m)
E = 0.98 # bject emissivity, vegetation is around 0.98, bare soil around 0.93...

im = rawData

AmbTemp = T + K0
AtmTemp = T + K0


# Step 1: Atmospheric transmission - correction factor from air temp, relative humidity and distance sensor-object;
# Vapour pressure deficit call here sqrtH2O => convert relative humidity and air temperature in VPD - mmHg - 1mmHg=0.133 322 39 kPa
H2OInGperM2 = H*math.exp(H2O_K1 + H2O_K2*T + H2O_K3*math.pow(T, 2) + H2O_K4*math.pow(T, 3))
# Atmospheric transmission correction: tao
a1b1sqH2O = (a1+b1*math.sqrt(H2OInGperM2))
a2b2sqH2O = (a2+b2*math.sqrt(H2OInGperM2))
exp1 = math.exp(-math.sqrt(D/2)*a1b1sqH2O)
exp2 = math.exp(-math.sqrt(D/2)*a2b2sqH2O)

tao = X*exp1 + (1-X)*exp2

obj_rad = im*E*tao

tao = X*exp1 + (1-X)*exp2 # Atmospheric transmission factor

# Step2: Correct raw pixel values from external factors
# General equation : Total Radiation = Object Radiation + Atmosphere Radiation + Ambient Reflection Radiation

# Object Radiation: obj_rad
# obj_rad = Theoretical object radiation * emissivity * atmospheric transmission
obj_rad = im*E*tao # For each pixel

# Theoretical atmospheric radiation: theo_atm_rad
theo_atm_rad = (R*J1/(math.exp(B/AtmTemp)-F)) + J0
atm_rad = repmat((1-tao)*theo_atm_rad, 640, 480)


# Atmosphere Radiation: atm_rad
# atm_rad = (1 - atmospheric transmission) * Theoretical atmospheric radiation
atm_rad = repmat((1-tao)*theo_atm_rad, 480, 640)

# Theoretical Ambient Reflection Radiation: theo_amb_refl_rad
theo_amb_refl_rad = (R*J1/(math.exp(B/AmbTemp)-F)) + J0
amb_refl_rad = repmat((1-E)*tao*theo_amb_refl_rad, 640, 480)


# Ambient Reflection Radiation: amb_refl_rad
# amb_refl_rad = (1 - emissivity) * atmospheric transmission * Theoretical Ambient Reflection Radiation
amb_refl_rad = repmat((1-E)*tao*theo_amb_refl_rad, 480, 640)

# Total Radiation: corr_pxl_val
corr_pxl_val = obj_rad + atm_rad + amb_refl_rad

pxl_temp = B/np.log(R/(corr_pxl_val-J0)*J1+F)

# Step 3:RBF equation: transformation of pixel intensity in radiometric temperature from raw values or
# corrected values (in degree Celsius)
pxl_temp = B/np.log(R/(corr_pxl_val-J0)*J1+F) - K0 # for each pixel

return pxl_temp


def get_bounding_box(center_position, fov):
# NOTE: ZERO_ZERO is the southeast corner of the field. Position values increase to the northwest (so +y-position = +latitude, or more north and +x-position = -longitude, or more west)
# We are also simplifying the conversion of meters to decimal degrees since we're not close to the poles and working with small distances.
Expand Down