天天看點

《itk實用demo》-删除canny圖像中比較小的部分

删除canny圖像中比較小的部分
typedef itk::RescaleIntensityImageFilter<ImageType2D, ImageType2D> RescaleIntensityImageFilterType;
    RescaleIntensityImageFilterType::Pointer rescaleFliter = RescaleIntensityImageFilterType::New();
    rescaleFliter->SetInput(image);
    rescaleFliter->SetOutputMinimum();
    rescaleFliter->SetOutputMaximum();
    //------------------------------------------------------------
    typedef itk::BinaryImageToLabelMapFilter<ImageType2D> BinaryImageToLabelMapFilterType;
    BinaryImageToLabelMapFilterType::Pointer binaryImageToLabelMapFilter = BinaryImageToLabelMapFilterType::New();
    binaryImageToLabelMapFilter->SetInput(rescaleFliter->GetOutput());
    binaryImageToLabelMapFilter->SetFullyConnected(true);
    binaryImageToLabelMapFilter->SetInputForegroundValue();
    binaryImageToLabelMapFilter->Update();

    std::cout << "There are " << binaryImageToLabelMapFilter->GetOutput()->GetNumberOfLabelObjects() << " objects." << std::endl;

    std::vector<unsigned long> labelsToRemove;

    for(unsigned int i = ; i < binaryImageToLabelMapFilter->GetOutput()->GetNumberOfLabelObjects(); i++)
    {
        // Get the ith region
        BinaryImageToLabelMapFilterType::OutputImageType::LabelObjectType* labelObject = binaryImageToLabelMapFilter->GetOutput()->GetNthLabelObject(i);
        if (ifDebug)
        {
            std::cout << "Region " << i << " has " << labelObject->Size() << " pixels." << std::endl;
        }        

        // Mark every other label to be removed
        //if(i%2 == 0)
        if(labelObject->Size() < )
        {
            labelsToRemove.push_back(labelObject->GetLabel());
        }
    }
    std::cout << "Removing " << labelsToRemove.size() << " objects." << std::endl;
    // Remove all regions that were marked for removal.
    for(unsigned int i = ; i < labelsToRemove.size(); ++i)
    {
        binaryImageToLabelMapFilter->GetOutput()->RemoveLabel(labelsToRemove[i]);
    }

    std::cout << "There are " << binaryImageToLabelMapFilter->GetOutput()->GetNumberOfLabelObjects() 
        << " objects remaining." << std::endl;

    typedef itk::LabelMapToLabelImageFilter<BinaryImageToLabelMapFilterType::OutputImageType, ImageType2D > L2IType;
    L2IType::Pointer l2i = L2IType::New();
    l2i->SetInput( binaryImageToLabelMapFilter->GetOutput());
    l2i->Update();

    typedef itk::BinaryThresholdImageFilter<ImageType2D, ImageType2D>  ThresholdFilterType;
    ThresholdFilterType::Pointer thresholdfilter = ThresholdFilterType::New();
    thresholdfilter->SetInput( l2i->GetOutput() );
    thresholdfilter->SetLowerThreshold();
    thresholdfilter->SetUpperThreshold();
    thresholdfilter->SetInsideValue();
    thresholdfilter->SetOutsideValue();
    thresholdfilter->Update();
    //----------------------------------
    ImageType2D::Pointer image_temp =ImageType2D::New();
    image_temp->Graft(thresholdfilter->GetOutput());

    return image_temp;