I am using ImageToVTKImageFilter to convert ITKImage to VTKImage with the following code:
typedef itk::ImageToVTKImageFilter ConverterType;
typename ConverterType::Pointer converter = ConverterType::New();
converter->SetInput(image);
converter->Update();
converter->UpdateOutputInformation();
The Update() function is a blocking call that takes 2-3 seconds to complete. During this time, my Qt UI freezes because Update() blocks the main thread responsible for UI rendering.
To address this, I tried moving the Update() call to a separate thread using QThread and also attempted to trigger Update() with a timer. Unfortunately, both methods not worked out.
Has anyone encountered a similar issue or have recommendations for avoiding UI thread blocking? I want the image conversion to occur asynchronously without freezing the UI. Any assistance would be greatly appreciated!
Hello,
I imagine you want to animate some kind of progress bar or some other visual feedback to the user the process is going on. In my program I do this during a large data file load, in the client code:
// data load takes place in another thread, so we can show and update a progress bar
//////////////////////////////////
QProgressDialog progressDialog;
progressDialog.show();
progressDialog.setLabelText("Loading and parsing " + _path + "...");
progressDialog.setMinimum(0);
progressDialog.setValue(0);
progressDialog.setMaximum(getFileSize() / 100); // see DataLoader::doLoad(). Dividing
// by 100 allows a max value of ~400GB
// when converting from long to int
QThread *thread = new QThread(); // does it need to set parent (a QObject)?
DataLoader *dl = new DataLoader(file, _data, data_line_count, _dataPageFirstLine,
_dataPageLastLine); // Do not set a parent. The object
// cannot be moved if it has a
// parent.
dl->moveToThread(thread);
dl->connect(thread, SIGNAL(finished()), dl, SLOT(deleteLater()));
dl->connect(thread, SIGNAL(started()), dl, SLOT(doLoad()));
dl->connect(dl, SIGNAL(progress(int)), &progressDialog, SLOT(setValue(int)));
thread->start();
/////////////////////////////////
// wait for the data load to finish
// not very beautiful, but simple and effective
while (!dl->isFinished()) {
thread->wait(200); // reduces cpu usage, refreshes at each 500 milliseconds
QCoreApplication::processEvents(); // let Qt repaint widgets
}
The DataLoader
class is here: gammaray/domain/auxiliary/dataloader.h at master · PauloCarvalhoRJ/gammaray · GitHub and gammaray/domain/auxiliary/dataloader.cpp at master · PauloCarvalhoRJ/gammaray · GitHub . I think you can taylor the code to suit your need.
I hope this helps,
PC
@Paulo_Carvalho Thank you for your response. In older ITK versions, we used the VTK render or geometry change call to update ITK, which was faster than the converter update. However, in the latest ITK version, the image appears blank without the converter update.
I will also explore your data loader option.