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( return render_template(
'results.jinja2', 'results.jinja2',
# img_orig_path=img_orig_path,
# img_res_path=img_res_path,
img_orig_b64=img_orig_b64, img_orig_b64=img_orig_b64,
img_res_b64=img_res_b64, img_res_b64=img_res_b64,
legend=key legend=key

View File

@ -2,7 +2,6 @@ import math
import cv2 as cv import cv2 as cv
import numpy as np import numpy as np
from numba import jit from numba import jit
import json
@jit(nopython=True) @jit(nopython=True)
def rg_ratio_normalize( def rg_ratio_normalize(
@ -19,21 +18,23 @@ def rg_ratio_normalize(
tmin = MAX_TEMP tmin = MAX_TEMP
tmax = 0 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 i in range(len(imgarr)):
for j in range(len(imgarr[i])): for j in range(len(imgarr[i])):
px = imgarr[i][j] px = imgarr[i][j]
# normalize R & G pixels # 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) 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 # 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 # remove pixels outside calibration range
if MAX_TEMP != None and temp_C > MAX_TEMP or MIN_TEMP != None and temp_C < MIN_TEMP: if (MAX_TEMP != None and temp_C > MAX_TEMP) or (MIN_TEMP != None and temp_C < MIN_TEMP):
temp_C = 0 temp_C = MIN_TEMP
# update min & max # update min & max
if temp_C < tmin and temp_C >= 0: if temp_C < tmin and temp_C >= 0:
@ -41,25 +42,34 @@ def rg_ratio_normalize(
if temp_C > tmax: if temp_C > tmax:
tmax = temp_C 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 return imgnew, tmin, tmax
@jit(nopython=True) @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 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( try:
(i_ng/i_nr) ** 3 return 362.73 * math.log10(
) + 2186.7 * math.log10( (i_ng/i_nr) ** 3
(i_ng/i_nr) ** 2 ) + 2186.7 * math.log10(
) + 4466.5 * math.log10( (i_ng/i_nr) ** 2
(i_ng / i_nr) ) + 4466.5 * math.log10(
) + 3753.5 (i_ng / i_nr)
) + 3753.5
except:
return default
def ratio_pyrometry_pipeline( def ratio_pyrometry_pipeline(
file_bytes, file_bytes,
@ -99,20 +109,18 @@ def ratio_pyrometry_pipeline(
# write colormapped image # write colormapped image
img_jet = cv.applyColorMap(img, cv.COLORMAP_JET) img_jet = cv.applyColorMap(img, cv.COLORMAP_JET)
# cv.imwrite(f'{file_name}-cropped-transformed-ratio.{file_ext}', img_jet)
# --- Generate temperature key --- # --- Generate temperature key ---
# adjust max & min temps to be the same as the image # adjust max & min temps to be the same as the image
# tmin_adj = tmin / (smoothing_radius ** 2) # Generate key
# tmax_adj = tmax / (smoothing_radius ** 2) # step = (tmax - tmin) / (key_entries-1)
# Generate 6-step key step = (MAX_TEMP - MIN_TEMP) / (key_entries)
step = (tmax - tmin) / (key_entries-1)
temps = [] temps = []
key_img_arr = [[]] key_img_arr = [[]]
for i in range(key_entries): for i in range(key_entries):
res_temp = tmin + (i * step) 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)) temps.append(math.floor(res_temp))
key_img_arr[0].append([res_color, res_color, res_color]) key_img_arr[0].append([res_color, res_color, res_color])
@ -122,7 +130,7 @@ def ratio_pyrometry_pipeline(
tempkey = {} tempkey = {}
for i in range(len(temps)): for i in range(len(temps)):
c = key_img_jet[0][i] 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 # original, transformed, legend
return img_orig, img_jet, tempkey return img_orig, img_jet, tempkey