Refactor code in a function

Hi,

I’m trying to refactor some code in a function. The function is:

ITKImgLabelType2D::Pointer GetLabelImageFilter(const ITKImgLabelType2D::Pointer& labelMap)
{
	using LabelMapToLabelImageFilterType = itk::LabelMapToLabelImageFilter<BinaryImageToShapeLabelMapFilterType::OutputImageType, ITKImgLabelType2D>;
	auto labelMapToLabelImageFilter = LabelMapToLabelImageFilterType::New();
	labelMapToLabelImageFilter->SetInput(labelMap->GetOutput());
	try {
		labelMapToLabelImageFilter->Update();
	}
	catch (const itk::ExceptionObject& err) {
		file1 << "ExceptionObject caught !" << __func__ << std::endl;
		file1 << err.what() << std::endl;
	}
	WriteImage(labelMapToLabelImageFilter->GetOutput(), "labelMapToLabelImageFilter.jpg", file1);
	return labelMapToLabelImageFilter->GetOutput();
}

where ITKImgLabelType2D = itk::Image<unsigned char,2>

When I call this function from main() I don’t know why but I don’t get back anything, the execution never goes out of the function, it stays forever in the return statement.

Has anyone any idea of which could be the error?

Thank you so much for your time!

Cristina Montserrat

The function is strangely named, but the code looks fine. Try adding a variable:

ITKImgLabelType2D::Pointer result = labelMapToLabelImageFilter->GetOutput();
// result->DisconnectPipeline();
return result;

Also, calling DisconnectPipeline() might help.

1 Like

It’s not clear to me what type ITKImgLabelType2Dis defined as. Is it an itk::LabelMap or some type if LabelMap filter? The name suggests an LabelMap ( a DataObject ), while the usage labelMap->GetOutput suggests that is a ProcessObject.

To help in debugging, I’d suggest making the input a LabelMap, ensuring it is updated and disconnected from the pipeline before executing your function. Other wise when labelMapToLabelImageFilter->Update is called all preceding filters will be included in the process of the pipeline execution triggered by the Update.

1 Like