《itk实用demo》-删除canny图像中比较小的部分

本文介绍了一种使用ITK库进行Canny边缘检测后的图像处理方法,通过调整像素强度、二值化转换、区域标记和去除小面积区域等步骤,实现了对Canny检测结果的有效后处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

删除canny图像中比较小的部分

    typedef itk::RescaleIntensityImageFilter<ImageType2D, ImageType2D> RescaleIntensityImageFilterType;
    RescaleIntensityImageFilterType::Pointer rescaleFliter = RescaleIntensityImageFilterType::New();
    rescaleFliter->SetInput(image);
    rescaleFliter->SetOutputMinimum(0);
    rescaleFliter->SetOutputMaximum(5);
    //------------------------------------------------------------
    typedef itk::BinaryImageToLabelMapFilter<ImageType2D> BinaryImageToLabelMapFilterType;
    BinaryImageToLabelMapFilterType::Pointer binaryImageToLabelMapFilter = BinaryImageToLabelMapFilterType::New();
    binaryImageToLabelMapFilter->SetInput(rescaleFliter->GetOutput());
    binaryImageToLabelMapFilter->SetFullyConnected(true);
    binaryImageToLabelMapFilter->SetInputForegroundValue(5);
    binaryImageToLabelMapFilter->Update();

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

    std::vector<unsigned long> labelsToRemove;

    for(unsigned int i = 0; 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() < 20)
        {
            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 = 0; 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(1);
    thresholdfilter->SetUpperThreshold(255);
    thresholdfilter->SetInsideValue(255);
    thresholdfilter->SetOutsideValue(0);
    thresholdfilter->Update();
    //----------------------------------
    ImageType2D::Pointer image_temp =ImageType2D::New();
    image_temp->Graft(thresholdfilter->GetOutput());

    return image_temp;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值