图像目标区域质心计算

    在matlab中,可以通过直接编程的方法来计算图像目标区域的质心[6],也可以通过专用的质心函数( regionprops() )达到同样的目的[7]。

    然而,在VTK中并没有找到专门用于计算图像目标区域质心的类,唯有通过ITK来实现。下面是主要的实现代码。

QList <QPoint3D> calculateRegionCentroid(vtkImageData *data)
{
    QList < QPoint3D > centroids; // QPoint3D是仿照QPoint(不支持浮点数)而定义的一个类,支持浮点数

    if ( !data )
       return centroids;

    double spacing[3];
    data->GetSpacing( spacing );

    itk::VTKImageToImageFilter<UCharImageType>::Pointer	vtk2itkConnector // 将VTK图像转换为ITK图像
            = itk::VTKImageToImageFilter< UCharImageType>::New();

    BinaryImageToLabelMapFilterType::Pointer binaryImageToLabelMapFilter
            = BinaryImageToLabelMapFilterType::New();

    LabelMapToLabelImageFilterType::Pointer  labelMapToLabelImageFilter
            = LabelMapToLabelImageFilterType::New();

    LabelGeometryImageFilterType::Pointer labelGeometryImageFilter
            = LabelGeometryImageFilterType::New();

    vtk2itkConnector->SetInput( data );

    binaryImageToLabelMapFilter->SetInput( vtk2itkConnector->GetOutput() );

    labelMapToLabelImageFilter->SetInput( binaryImageToLabelMapFilter->GetOutput());

    labelGeometryImageFilter->SetInput( labelMapToLabelImageFilter->GetOutput() );

    // These generate optional outputs.
    labelGeometryImageFilter->CalculatePixelIndicesOn();
    labelGeometryImageFilter->CalculateOrientedBoundingBoxOn();
    labelGeometryImageFilter->CalculateOrientedLabelRegionsOn();
    labelGeometryImageFilter->Update();

    LabelGeometryImageFilterType::LabelsType allLabels = labelGeometryImageFilter->GetLabels();
    LabelGeometryImageFilterType::LabelsType::iterator allLabelsIt;

    for( allLabelsIt = allLabels.begin(); allLabelsIt != allLabels.end(); allLabelsIt++ ) {
        LabelGeometryImageFilterType::LabelPixelType labelValue = *allLabelsIt;
        if ( (int)labelValue > 0 ) {
            double x = labelGeometryImageFilter->GetCentroid( labelValue )[0];
            double y = labelGeometryImageFilter->GetCentroid( labelValue )[1];
            centroids.append( QPoint3D( x * spacing[0], y * spacing[1], 0 ) );
        }
    }

    binaryImageToLabelMapFilter->SetInput( NULL );
    labelMapToLabelImageFilter->SetInput( NULL );
    labelGeometryImageFilter->SetInput( NULL );
    vtk2itkConnector->SetInput( NULL );

    return centroids;
}

参考资料

[1]图像处理之计算二值连通区域的质心

[2]图形图像基本处理之——一个非常容易理解的图像求质心代码

[3]Image processing -Algorithm to calculate centroids of white space in binary image

[4]how to find the centroid of a binary image?

[5]图像目标质心快速搜索算法

[6]MATLAB中求图像中某一区域的质心  

[7]regionprops函数中centroid求质心,其质心评定原理?

你可能感兴趣的:(图像目标区域质心计算)