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>

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

int
main()
{
    // 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 );

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

    return 0;
}


See also