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.
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>usingnamespace imagedev;usingnamespace ioformat;usingnamespace iolink;int
main(int argc,char* argv[]){int status =0;try{// ImageDev library initializationif( isInitialized()==false)
imagedev::init();// Open a 3D Amira Mesh fileauto imageInput = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER )+"shale.am");// Callback that displays the progression percentageauto progressionCallback =[](constGenericAlgorithm& algorithm,float ratio ){
std::cout <<" - Progression: "<<100.0* ratio <<"%"<< std::endl;};// Callback that displays the algorithm informationauto informationCallback =[](constGenericAlgorithm& algorithm,const std::string& message ){
std::cout <<" - Information: "<< message << std::endl;};// Enable a callback for indicating the algorithm progression
setProgressRatioCallback( progressionCallback );// Apply a closing filterauto 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 programif(!(( 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
{classProgram{// Callback that displays the progression percentagestaticvoid progressionCallback(object algorithm,float ratio){Console.WriteLine(algorithm.ToString()+" - Progression: "+(100.0* ratio).ToString("0.00")+"%");}// Callback that displays the algorithm informationstaticvoid informationCallback(object algorithm,string message){Console.WriteLine(algorithm.ToString()+" - Information: "+ message);}staticvoidMain(string[] args){int status =0;try{// ImageDev library initializationif(Initialization.IsInitialized()==false)Initialization.Init();// Open a 3D Amira Mesh fileImageView imageInput =ViewIO.ReadImage("Data/images/shale.am")asImageView;// Enable a callback for indicating the algorithm progressionProcessing.SetProgressRatioCallback(progressionCallback);// Apply a closing filtervar imageOutput =Processing.Closing3d(imageInput,50);// Disable the progression callbackProcessing.SetProgressRatioCallback(null);// Enable a callback for indicating the algorithm informationProcessing.SetProgressMessageCallback(informationCallback);// Apply a closing by reconstruction filter
imageOutput =Processing.ClosingByReconstruction3d(imageInput,20);// Disable the information callbackProcessing.SetProgressMessageCallback(null);// Enable the verbose mode that indicates the duration of each algorithmProcessing.SetVerbose(true);// Apply a median filter
imageOutput =Processing.MedianFilter3d(imageInput,5);// Disable the verbose modeProcessing.SetVerbose(false);}catch(Exception error){// Print potential exception in the standard outputSystem.Console.WriteLine("HelloImageDev exception: "+ error.ToString());
status =-1;}// ImageDev library finalizationInitialization.Finish();// Check if we must ask for an enter key to close the programif(!((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 percentagedef progression_callback(algorithm, ratio):
progress = ratio
print(" - Progression: "+"{:.2f}".format(100.0* ratio)+"%")# Callback that displays the algorithm informationdef information_callback(algorithm, message):print(" - Information: "+ message )# Initialize the ImageDev library if not doneif(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()