[dmscript] create double arctan function for white line ratio

PHOTO EMBED

Sun Sep 29 2024 07:53:10 GMT+0000 (Coordinated Universal Time)

Saved by @j2hwank

image double_arctan_background(image signal, number E_L3, number E_L2, number A1, number A2, number w1, number w2, number max_intensity)
{
    /// This function calculates the double arc tangent function background for a given signal.
    /// The double arctangent function is computed using two sets of parameters (A1, E_L3, w1) and (A2, E_L2, w2).
    /// The resulting background is normalized to a given maximum intensity.

    // Clone the input signal to use it as a base for the background calculation
    //	signal: The input image for which the background is calculated.
	//	E_L3, E_L2: These are the energy levels for the arctangent functions.
	//	A1, A2: Amplitude scaling factors for the two arctangent components.
	//	w1, w2: Widths or scaling factors that define the sharpness of the arctangent functions.
	//	max_intensity: The maximum intensity to which the background image will be normalized.
    image background := ImageClone(signal);
    
    // Variables to store the minimum and maximum values of the calculated background
    number raw_min, raw_max;

    // Calculate the double arctangent background function.
    // The equation used is a sum of two arctangent functions with different parameters.
    // `icol` represents the column index of each pixel, which is used in the calculation.
    background = A1 * atan((icol - E_L3) / w1) + A2 * atan((icol - E_L2) / w2);

    // Find the minimum and maximum intensity values in the background image
    raw_min = min(background);
    raw_max = max(background);

    // If the image has no intensity variation (raw_min == raw_max), set the entire image to the max_intensity
    if (raw_min == raw_max)
    {
        background = max_intensity;
    }
    else
    {
        // Normalize the background image intensities to the range [0, max_intensity]
        // (background - raw_min) shifts the intensities so the minimum becomes 0
        // Dividing by (raw_max - raw_min) scales the range to [0, 1]
        // Multiplying by max_intensity scales the range to [0, max_intensity]
        background = (background - raw_min) / (raw_max - raw_min) * max_intensity;
    }

    // Return the calculated background image
    return background;
}
content_copyCOPY