I want to use the ConnectedComponentImageFilter to delete the false positive area, and pass the result through other filters (LabelImageToShapeLabelMapFilter,LabelMap,LabelMapToLabelImageFilter namely) to convert into image.
I find the background of the final result turn white and that the white background obscured the label.
However, when i use itk-snap to update, the 3d volume is right, and Each vertebra is clearly segmented.
typedef itk::Image<float, 3> ImageTypeFloat;
typedef itk::Image<int, 3> ImageSeg;
using LabelObject = itk::ShapeLabelObject<int, 3>;
using LabelMap = itk::LabelMap<LabelObject>;
using ConnectComment = itk::ConnectedComponentImageFilter<ImageTypeFloat, ImageSeg>;
using LabelImageToLabelMapFilter = itk::LabelImageToShapeLabelMapFilter<ImageSeg, LabelMap>;
using L2IType = itk::LabelMapToLabelImageFilter<LabelMap, ImageTypeFloat>;
using LabelShapeKeepNObject = itk::LabelShapeKeepNObjectsImageFilter<ImageSeg>;
using ImageRegionIterator = itk::ImageRegionConstIterator<ImageTypeFloat>;
using ImageIterator = itk::ImageRegionIterator<ImageTypeFloat>;
ConnectComment::Pointer connectcomment = ConnectComment::New();
WriteType::Pointer writer = WriteType::New();
ImageIOTypeNII::Pointer niftio = ImageIOTypeNII::New();
connectcomment->SetInput(img);
connectcomment->SetBackgroundValue(0);
connectcomment->Update();
LabelImageToLabelMapFilter::Pointer limgtmap = LabelImageToLabelMapFilter::New();
limgtmap->SetInput(connectcomment->GetOutput());
limgtmap->Update();
LabelMap::Pointer labelmap = limgtmap->GetOutput();
for (float label = 0; label < labelmap->GetNumberOfLabelObjects(); label++)
{
const LabelObject* labelobject = labelmap->GetLabelObject(label);
labelobject->Print(std::cout);
std::cout << "-------------------------------------------------------" << std::endl;
}
std::cout << "--------------------------------Process------------------------" << std::endl;
std::vector<LabelObject::Pointer>filter_objects;
for (LabelMap::Iterator in2(labelmap); !in2.IsAtEnd(); ++in2)
{
if (in2.GetLabelObject()->GetNumberOfPixels() < 10000)
filter_objects.emplace_back(in2.GetLabelObject());
}
for (int i = 0; i < filter_objects.size(); i++)
labelmap->RemoveLabelObject(filter_objects[i]);
L2IType::Pointer l2i = L2IType::New();
l2i->SetInput(labelmap);
l2i->Update();
ImageTypeFloat::Pointer res = l2i->GetOutput();
res->SetSpacing(img->GetSpacing());
res->SetDirection(img->GetDirection());
writer->SetImageIO(niftio);
writer->SetFileName("connect_.nii.gz");
writer->SetInput(res);
writer->Update();
the final result i have mentioned is res
The res image:
ITK-Snap seg: