Returns a pixel index from a given energy

PHOTO EMBED

Sun Sep 29 2024 08:53:31 GMT+0000 (Coordinated Universal Time)

Saved by @j2hwank

number energy_to_pixel(image signal, number energy)
{
    // Get the origin and scale for the X dimension (energy axis)
    number origin = signal.ImageGetDimensionOrigin(0);  // X-axis (dimension 0)
    number scale = signal.ImageGetDimensionScale(0);    // X-axis scale (energy per pixel)
    
    // Calculate the corresponding pixel index for the given energy
    number pixel = (energy - origin) / scale;
    
    // Round to the nearest pixel index
    pixel = round(pixel);
    
    // Ensure the pixel index is within the valid range of the image
    number width = signal.ImageGetDimensionSize(0);  // Get image width (number of pixels)
    if (pixel < 0) pixel = 0;
    if (pixel >= width) pixel = width - 1;
    
    return pixel;
}

// Example usage
image signal := GetFrontImage();  // Assume signal is the current front image
number energy = 783;  // Example energy value
number pixelIndex = energy_to_pixel(signal, energy);
Result("\nPixel corresponding to energy " + energy + " is: " + pixelIndex);
content_copyCOPY