# Refactor code in a function

**URL:** <https://discourse.itk.org/t/refactor-code-in-a-function/5887>\
**Category:** Algorithms\
**Tags:** itk\
**Created:** [April 21, 2023, 9:36am UTC](https://discourse.itk.org/t/refactor-code-in-a-function/5887 "2023-04-21T09:36:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cristinamontserrat](https://discourse.itk.org/user_avatar/discourse.itk.org/cristinamontserrat/32/3410_2.png) [@cristinamontserrat](https://discourse.itk.org/u/cristinamontserrat)\
**Post date:** [April 21, 2023, 9:36am UTC](https://discourse.itk.org/t/refactor-code-in-a-function/5887/1 "2023-04-21T09:36:19Z")

</div>

Hi,

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

```auto
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

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [April 24, 2023, 12:51pm UTC](https://discourse.itk.org/t/refactor-code-in-a-function/5887/2 "2023-04-24T12:51:56Z")

</div>

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

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

```

Also, calling `DisconnectPipeline()` might help.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [April 24, 2023, 1:06pm UTC](https://discourse.itk.org/t/refactor-code-in-a-function/5887/3 "2023-04-24T13:06:47Z")

</div>

It’s not clear to me what type `ITKImgLabelType2D`is 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.

> [@cristinamontserrat](#):
>
> `ITKImgLabelType2D::Pointer GetLabelImageFilter(const ITKImgLabelType2D::Pointer& labelMap)`
