How to extract the region

I am a beginner. I use the connected component analysis method to extract the labels of a volume data. How can I use any one of the labels as a mask to extract the corresponding area in the original data? Which header files should I use? I am a newbie, a little confused, I really hope someone with experience can tell me!

You might be interested in LabelStatistics or LabelImageToShapeLabelMap filters. Examples are linked below the detailed description.

1 Like

Thank you! I tried your link and created a new 3D data by iterative method according to a specified label, but it seems that the iterative process is very time-consuming. Does itk have a built-in method to extract the label area directly?

LabelTypeUS label_idx = 1; //Specify the index of the label
ShapeLabelObjectType::Pointer labelObject = ShapeLabelObjectType::New();
labelObject = labelMap->GetNthLabelObject(label_idx);
itk::Index<Dimension> index;
itk::Size<Dimension> size;
index = labelObject->GetBoundingBox().GetIndex();
size = labelObject->GetBoundingBox().GetSize();
std::cout << "label: " << itk::NumericTraits<LabelMapTypeUS::LabelType>::PrintType(labelObject->GetLabel()) << std::endl;
std::cout << "index: " << index << std::endl;
std::cout << "size: " << size << std::endl;
ImageTypeUC::Pointer image = ImageTypeUC::New();
ImageTypeUC::RegionType region;
image->SetRegions(labelObject->GetRegion());
image->Allocate();
itk::ImageRegionIterator<ImageTypeUC> imageIterator(image, image->GetLargestPossibleRegion());
label = labelObject->GetLabel();
while (!imageIterator.IsAtEnd()) {
	if (labelMap->GetPixel(imageIterator.GetIndex()) == label) {
		imageIterator.Set(255);
	}
	else
	{
		imageIterator.Set(0);
	}
	++imageIterator;
}
std::cout << "extract label image succeed" << std::endl;

If you are trying to create a binary image for each label, BinaryThresholdImageFilter could help if you use it in a for loop at the end of the pipeline. For just one label, your code looks good already. Just make sure you are not running it in Debug mode, which is many times slower than Release.

1 Like

Thank you very much for your patience and professional help! I am sure I ran it in Release mode, but still slowly, taking about five minutes in a region, which size is 314208824, Do you think this is reasonable? I think it’s too slow, and there should be a faster way.

1 Like

Try BinaryImageToLabelMapFilter. That will not compute shape parameters.

1 Like