fix BGR & alpha value trimming

master
michael 2022-10-12 19:29:39 -07:00
parent dd5e74cb1a
commit 950b56f4fa
2 changed files with 32 additions and 26 deletions

View File

@ -36,8 +36,6 @@ def ratio_pyro():
return render_template(
'results.jinja2',
# img_orig_path=img_orig_path,
# img_res_path=img_res_path,
img_orig_b64=img_orig_b64,
img_res_b64=img_res_b64,
legend=key

View File

@ -2,7 +2,6 @@ import math
import cv2 as cv
import numpy as np
from numba import jit
import json
@jit(nopython=True)
def rg_ratio_normalize(
@ -19,21 +18,23 @@ def rg_ratio_normalize(
tmin = MAX_TEMP
tmax = 0
imgnew = imgarr.copy()
# copy image into new array & chop off alpha values (if applicable)
imgnew = imgarr.copy()[:,:,:3]
for i in range(len(imgarr)):
for j in range(len(imgarr[i])):
px = imgarr[i][j]
# normalize R & G pixels
r_norm = (px[0] - I_Darkcurrent) * (f_stop ** 2) / (ISO * exposure_time)
g_norm = (px[1] - I_Darkcurrent) * (f_stop ** 2) / (ISO * exposure_time)
r_norm = (px[2] - I_Darkcurrent) * (f_stop ** 2) / (ISO * exposure_time)
# apply camera calibration func
temp_C = pyrometry_calibration_formula(g_norm, r_norm)
temp_C = pyrometry_calibration_formula(g_norm, r_norm, default=MIN_TEMP)
# remove pixels outside calibration range
if MAX_TEMP != None and temp_C > MAX_TEMP or MIN_TEMP != None and temp_C < MIN_TEMP:
temp_C = 0
if (MAX_TEMP != None and temp_C > MAX_TEMP) or (MIN_TEMP != None and temp_C < MIN_TEMP):
temp_C = MIN_TEMP
# update min & max
if temp_C < tmin and temp_C >= 0:
@ -41,25 +42,34 @@ def rg_ratio_normalize(
if temp_C > tmax:
tmax = temp_C
temp_C -= MIN_TEMP
# min intensity = 0
print(temp_C)
pix_i = temp_C - MIN_TEMP
imgnew[i][j] = [pix_i, pix_i, pix_i]
# imgnew[i][j] = [0, g_norm, r_norm]
imgnew[i][j] = [temp_C, temp_C, temp_C]
return imgnew, tmin, tmax
@jit(nopython=True)
def pyrometry_calibration_formula(i_ng, i_nr):
def pyrometry_calibration_formula(i_ng, i_nr, default=24.0):
"""
Given the green-red ratio, calculates an approximate temperature
in Celsius.
in Celsius. Defaults to room temperature if there's an error.
"""
return 362.73 * math.log10(
(i_ng/i_nr) ** 3
) + 2186.7 * math.log10(
(i_ng/i_nr) ** 2
) + 4466.5 * math.log10(
(i_ng / i_nr)
) + 3753.5
try:
return 362.73 * math.log10(
(i_ng/i_nr) ** 3
) + 2186.7 * math.log10(
(i_ng/i_nr) ** 2
) + 4466.5 * math.log10(
(i_ng / i_nr)
) + 3753.5
except:
return default
def ratio_pyrometry_pipeline(
file_bytes,
@ -99,20 +109,18 @@ def ratio_pyrometry_pipeline(
# write colormapped image
img_jet = cv.applyColorMap(img, cv.COLORMAP_JET)
# cv.imwrite(f'{file_name}-cropped-transformed-ratio.{file_ext}', img_jet)
# --- Generate temperature key ---
# adjust max & min temps to be the same as the image
# tmin_adj = tmin / (smoothing_radius ** 2)
# tmax_adj = tmax / (smoothing_radius ** 2)
# Generate 6-step key
step = (tmax - tmin) / (key_entries-1)
# Generate key
# step = (tmax - tmin) / (key_entries-1)
step = (MAX_TEMP - MIN_TEMP) / (key_entries)
temps = []
key_img_arr = [[]]
for i in range(key_entries):
res_temp = tmin + (i * step)
res_color = (tmax - (i * step)) / MAX_TEMP * 255
res_color = res_temp / MAX_TEMP * 255
temps.append(math.floor(res_temp))
key_img_arr[0].append([res_color, res_color, res_color])
@ -122,7 +130,7 @@ def ratio_pyrometry_pipeline(
tempkey = {}
for i in range(len(temps)):
c = key_img_jet[0][i]
tempkey[temps[i]] = f"rgb({c[0]}, {c[1]}, {c[2]})"
tempkey[temps[i]] = f"rgb({c[2]}, {c[1]}, {c[0]})"
# original, transformed, legend
return img_orig, img_jet, tempkey