Water Background Subtraction Project (Main)
This project is a simple implementation of background subtraction.
Within the project, there are three main project files:
h5_background_subtraction_10_2_23.py
h5_stream_background_subtraction_10_2_23.py
overwrite_10_2_23.py.
All of these are found under the src/ directory. The first file contains the main code for this page, while the others were adaptations on this.
GitHub Repositories:
Imports
The following modules are imported for use in the program:
import os
import numpy as np
import h5py as h5
import matplotlib.pyplot as plt
PeakThresholdProcessor Class
The PeakThresholdProcessor class is used for processing the image peak values in the image array, above or at a specified threshold value. This class provides functions for setting and retrieving coordinate values these values.
The following functions are defined in the PeakThresholdProcessor class:
- class PeakThresholdProcessor
Initialize the PeakThresholdProcessor with the image array and coordinate threshold value.
- Parameters
image_array (numpy.ndarray) – The image array to be processed.
threshold_value (float, optional) – The threshold value to be used for processing the image array, defaults to 0.
- Returns
Returns coordinate list of x and y values above the threshold value.
- Return type
list
- set_threshold_value(new_threshold_value)
Set a new threshold value for the image array.
- Parameters
new_threshold_value (float) – The new threshold value to be used for processing the image array.
- get_threshold_value()
Getter method which returns the current threshold value for the image array.
- Returns
The current threshold value for the image array.
- Return type
float
ArrayRegion Class
The ArrayRegion class is used for processing the image array region, using the coordinate values provided. This class provides functions for setting and retrieving coordinate values these values.
The following functions are defined in the ArrayRegion class:
- class ArrayRegion(array)
Initialize the ArrayRegion class with the image array.
- Parameters
array (numpy.ndarray) – The image array to be processed.
- x_center
The x coordinate of the center of the region. First coordinate in tuple.
- Type
int
- y_center
The y coordinate of the center of the region. Second coordinate in tuple.
- Type
int
- region_size
The size of the region radius.
- Type
int
- set_peak_coordinate(x, y)
Set the x and y coordinates of the center of the region using chosen coordinate.
- Parameters
x (int) – The x coordinate of the center of the region.
y (int) – The y coordinate of the center of the region.
- set_region_size(size)
Set the size of the region radius.
- Parameters
size (int) – The size of the region radius.
- get_region()
Get the region from the image array.
- Returns
The region from the image array.
- Return type
numpy.ndarray
Helper Functions
- load_h5(filename)
This method loads an HDF5 file and prints a success message if the file is loaded successfully. If the file is not found within the working directory, it prints an error message.
- Parameters
filename (str) – The path to the HDF5 file.
- extract_region(image_array, region_size, x_center, y_center)
This function calls the ArrayRegion class to extract the region from the image array.
- Parameters
image_array (numpy.ndarray) – The image array to be processed.
region_size (int) – The size of the region radius.
x_center (int) – The x coordinate of the center of the region.
y_center (int) – The y coordinate of the center of the region.
- Returns
The extracted region from the image array.
- Return type
numpy.ndarray
Main Function
The main function processes image data from specified HDF5 file for 3-ring integration analysis. Calling coordinate_menu for increasing radius value.
- main(filename)
Loads and processes image data from HDF5 file.
- param filename
The path to the HDF5 file containing image data.
- type filename
str
The function performs the following steps:
File Loading:
It calls load_h5 to load the specified HDF5 file.
Image Data Extraction:
Extracts the NumPy array from the HDF5 file, which is 2D array of zeros with shape of (4371, 4150).
Threshold Processing:
It calls PeakThresholdProcessor and creates object with the extracted array region and a threshold of 1000. Then retrieving the coordinates above this threshold.
Ring Integration Analysis:
Interactively calls coordinate_menu for a set of radii (1,2,3,4). And for each value in the list, this calculates and prints the peak estimate by subtracting the average value from the intensity peak value.
The function sets a global variable image_array to store the image data and coordinates to store the coordinates above the threshold. The global variable intensity_peak and avg_values are used to calculate the peak estimates.
The script also defines paths for working with image files and calls the main function with different image paths for processing.