ImageDev

Algorithm Tracking

This example shows how to use the different callback functions provided to interact with ImageDev algorithms.
Several types of information can be sent by an ImageDev algorithm like its progression ratio, a text message, a formatted message defining specific data or a cancelation proposal. This information can be intercepted with callback functions.

First, this example defines a callback that prints the progression percentage and launches a morphological closing on a 3D image.

Progression: 14.13%
Progression: 43.02%
Progression: 63.16%
Progression: 91.72%
Progression: 100.00%


The progression callback is disabled and a callback printing algorithm messages is defined. A closing by reconstruction is applied on the same image. The verbose mode is still enabled.

ImageDev.ClosingByReconstruction3d - Information: [Reconstruction Step]: iteration 1
Information: [Reconstruction Step]: iteration 3 - Number of voxels modified 6348589
Information: [Reconstruction Step]: iteration 5 - Number of voxels modified 3563828
Information: [Reconstruction Step]: iteration 7 - Number of voxels modified 663815
Information: [Reconstruction Step]: iteration 9 - Number of voxels modified 20
Information: [Reconstruction Step]: iteration 11 - Number of voxels modified 2


At last, the message callback is disabled and the ImageDev verbose mode is enabled. This mode prints in a standard output the name of each algorithm that is launched and its computation time when it ends. A median filter is applied.

Executing MedianFilter3d...
Done in 0s 986ms


Note: In Python, the verbose mode also prints a progress bar in the Python console.

#include <ImageDev/ImageDev.h>
#include <ioformat/IOFormat.h>
#include <string.h>

using namespace imagedev;
using namespace ioformat;
using namespace iolink;

int
main( int argc, char* argv[] )
{
    int status = 0;

    try
    {
        // ImageDev library initialization
        if ( isInitialized() == false )
            imagedev::init();

        // Open a 3D Amira Mesh file
        auto imageInput = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "shale.am" );

        // Callback that displays the progression percentage
        auto progressionCallback = []( const GenericAlgorithm& algorithm, float ratio ) {
            std::cout << " - Progression: " << 100.0 * ratio << "%" << std::endl;
        };

        // Callback that displays the algorithm information
        auto informationCallback = []( const GenericAlgorithm& algorithm, const std::string& message ) {
            std::cout << " - Information: " << message << std::endl;
        };

        // Enable a callback for indicating the algorithm progression
        setProgressRatioCallback( progressionCallback );

        // Apply a closing filter
        auto imageOutput = closing3d( imageInput, 50, Closing3d::CONNECTIVITY_26, Closing3d::LIMITED );

        // Disable the progression callback
        setProgressRatioCallback( nullptr );

        // Enable a callback for indicating the algorithm information
        setProgressMessageCallback( informationCallback );

        // Apply a closing by reconstruction filter
        imageOutput = closingByReconstruction3d( imageInput, 20, ClosingByReconstruction3d::CONNECTIVITY_26 );

        // Disable the information callback
        setProgressMessageCallback( nullptr );

        // Enable the verbose mode that indicates the duration of each algorithm
        setVerbose( true );

        // Apply a median filter
        imageOutput = medianFilter3d( imageInput, 5, MedianFilter3d::CUBE, MedianFilter3d::AUTOMATIC );

        // Disable the verbose mode
        setVerbose( false );

        std::cout << "This example ran successfully." << std::endl;
    }
    catch ( const imagedev::Exception& error )
    {
        // Print potential exception in the standard output
        std::cerr << "ImageDev exception: " << error.what() << std::endl;
        status = -1;
    }

    // ImageDev library finalization
    imagedev::finish();

    // Check if we must ask for an enter key to close the program
    if ( !( ( argc == 2 ) && strcmp( argv[1], "--no-stop-at-end" ) == 0 ) )
        std::cout << "Press Enter key to close this window." << std::endl, getchar();

    return status;
}
using System;
using ImageDev;
using IOLink;
using IOFormat;

namespace T05_01_AlgorithmTracking
{
    class Program
    {
        // Callback that displays the progression percentage
        static void progressionCallback(object algorithm, float ratio)
        {
            Console.WriteLine(algorithm.ToString() + " - Progression: " + (100.0 * ratio).ToString("0.00") + "%");
        }

        // Callback that displays the algorithm information
        static void informationCallback(object algorithm, string message)
        {
            Console.WriteLine(algorithm.ToString() + " - Information: " + message);
        }

        static void Main(string[] args)
        {
            int status = 0;

            try
            {
                // ImageDev library initialization
                if (Initialization.IsInitialized() == false)
                    Initialization.Init();

                // Open a 3D Amira Mesh file
                ImageView imageInput = ViewIO.ReadImage("Data/images/shale.am") as ImageView;

                // Enable a callback for indicating the algorithm progression
                Processing.SetProgressRatioCallback(progressionCallback);

                // Apply a closing filter
                var imageOutput = Processing.Closing3d(imageInput, 50);

                // Disable the progression callback
                Processing.SetProgressRatioCallback(null);

                // Enable a callback for indicating the algorithm information
                Processing.SetProgressMessageCallback(informationCallback);

                // Apply a closing by reconstruction filter
                imageOutput = Processing.ClosingByReconstruction3d(imageInput, 20);

                // Disable the information callback
                Processing.SetProgressMessageCallback(null);

                // Enable the verbose mode that indicates the duration of each algorithm
                Processing.SetVerbose(true);

                // Apply a median filter
                imageOutput = Processing.MedianFilter3d(imageInput, 5);

                // Disable the verbose mode
                Processing.SetVerbose(false);

            }
            catch (Exception error)
            {
                // Print potential exception in the standard output
                System.Console.WriteLine("HelloImageDev exception: " + error.ToString());
                status = -1;
            }

            // ImageDev library finalization
            Initialization.Finish();

            // Check if we must ask for an enter key to close the program
            if (!((args.Length >= 1) && (args[0] == "--no-stop-at-end")))
            {
                System.Console.WriteLine("Press Enter key to close this window.");
                System.Console.ReadKey();
            }

            System.Environment.Exit(status);
        }
    }
}
import imagedev
import imagedev_data
import ioformat

#  Callback that displays the progression percentage
def progression_callback(algorithm, ratio):
  progress = ratio
  print( " - Progression: " + "{:.2f}".format( 100.0 * ratio) + "%" )

# Callback that displays the algorithm information
def information_callback(algorithm, message):
  print(" - Information: " + message )

# Initialize the ImageDev library if not done
if (imagedev.is_initialized() == False): imagedev.init()

# Open a 3D Amira Mesh file
image_input = ioformat.read_image(imagedev_data.get_image_path("shale.am"))

# Enable a callback for indicating the algorithm progression
imagedev.set_progress_ratio_callback(progression_callback)

# Apply a closing filter
imageOutput = imagedev.closing_3d(image_input, 50)

# Disable the progression callback
imagedev.set_progress_ratio_callback(None)

# Enable a callback for indicating the algorithm information
imagedev.set_progress_message_callback(information_callback)

# Apply a closing by reconstruction filter
imageOutput = imagedev.closing_by_reconstruction_3d(image_input, 20)

# Disable the information callback
imagedev.set_progress_message_callback(None)

# Enable the verbose mode that indicates the duration of each algorithm
imagedev.set_verbose(True)

# Apply a median filter
imageOutput = imagedev.median_filter_3d(image_input, 5)

# Disable the verbose mode
imagedev.set_verbose(False)

# ImageDev library finalization
imagedev.finish()


See also