87 lines
2.3 KiB
Python
87 lines
2.3 KiB
Python
import sys
|
|
import json
|
|
|
|
|
|
def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata=None):
|
|
image = image.astype(int)
|
|
|
|
camera_name = parameters["camera_name"]
|
|
|
|
background = parameters.get("background_data")
|
|
background_name = parameters.get("image_background")
|
|
background_mode = parameters.get("image_background_enable")
|
|
|
|
roi_signal = parameters.get("roi_signal")
|
|
roi_background = parameters.get("roi_background")
|
|
project_axis = parameters.get("project_axis", 0)
|
|
threshold = parameters.get("threshold")
|
|
|
|
|
|
# maintain the structure of processing_parameters
|
|
background_shape = None
|
|
|
|
# maintain the structure of res
|
|
projection_signal = projection_background = None
|
|
|
|
|
|
try:
|
|
|
|
if background is not None:
|
|
background_shape = background.shape
|
|
image -= background
|
|
|
|
if threshold is not None:
|
|
image -= threshold
|
|
image[image < 0] = 0
|
|
|
|
if roi_signal is not None:
|
|
projection_signal = get_roi_projection(image, roi_signal, project_axis)
|
|
|
|
if roi_background is not None:
|
|
projection_background = get_roi_projection(image, roi_background, project_axis)
|
|
|
|
except Exception as e:
|
|
lineno = sys.exc_info()[2].tb_lineno
|
|
tn = type(e).__name__
|
|
status = f"Error in line number {lineno}: {tn}: {e}"
|
|
else:
|
|
status = "OK"
|
|
|
|
|
|
processing_parameters = {
|
|
"image_shape": image.shape,
|
|
"background_shape": background_shape,
|
|
|
|
"background_name": background_name,
|
|
"background_mode": background_mode,
|
|
|
|
"roi_signal": roi_signal,
|
|
"roi_background": roi_background,
|
|
"project_axis": project_axis,
|
|
"threshold": threshold,
|
|
|
|
"status": status
|
|
}
|
|
|
|
processing_parameters = json.dumps(processing_parameters)
|
|
|
|
res = {
|
|
camera_name + ".processing_parameters": processing_parameters,
|
|
|
|
camera_name + ".projection_signal": projection_signal,
|
|
camera_name + ".projection_background": projection_background
|
|
}
|
|
|
|
return res
|
|
|
|
|
|
|
|
def get_roi_projection(image, roi, axis):
|
|
x_start, x_stop, y_start, y_stop = roi
|
|
cropped = image[x_start:x_stop, y_start:y_stop]
|
|
project = cropped.mean(axis=axis)
|
|
return project
|
|
|
|
|
|
|