Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkContourUtils.h"
00019 #include "mitkImageCast.h"
00020 #include "ipSegmentation.h"
00021
00022
00023 InstantiateAccessFunctionForFixedDimension_2( mitk::ContourUtils::ItkCopyFilledContourToSlice, 2, const mitk::Image*, int );
00024
00025 mitk::ContourUtils::ContourUtils()
00026 {
00027 }
00028
00029 mitk::ContourUtils::~ContourUtils()
00030 {
00031 }
00032
00033 mitk::Contour::Pointer mitk::ContourUtils::ProjectContourTo2DSlice(Image* slice, Contour* contourIn3D, bool itkNotUsed( correctionForIpSegmentation ), bool constrainToInside)
00034 {
00035 if ( !slice || !contourIn3D ) return NULL;
00036
00037 Contour::Pointer projectedContour = Contour::New();
00038
00039 const Contour::PathType::VertexListType* pointsIn3D = contourIn3D->GetContourPath()->GetVertexList();
00040 const Geometry3D* sliceGeometry = slice->GetGeometry();
00041 for ( Contour::PathType::VertexListType::const_iterator iter = pointsIn3D->begin();
00042 iter != pointsIn3D->end();
00043 ++iter )
00044 {
00045 Contour::PathType::VertexType currentPointIn3DITK = *iter;
00046 Point3D currentPointIn3D;
00047 for (int i = 0; i < 3; ++i) currentPointIn3D[i] = currentPointIn3DITK[i];
00048
00049 Point3D projectedPointIn2D;
00050 projectedPointIn2D.Fill(0.0);
00051 sliceGeometry->WorldToIndex( currentPointIn3D, projectedPointIn2D );
00052 MITK_DEBUG << "world point " << currentPointIn3D << " in index is " << projectedPointIn2D;
00053
00054 if ( !sliceGeometry->IsIndexInside( projectedPointIn2D ) && constrainToInside )
00055 {
00056 MITK_INFO << "**" << currentPointIn3D << " is " << projectedPointIn2D << " --> correct it (TODO)" << std::endl;
00057 }
00058
00059 projectedContour->AddVertex( projectedPointIn2D );
00060 }
00061
00062 return projectedContour;
00063 }
00064
00065 mitk::Contour::Pointer mitk::ContourUtils::BackProjectContourFrom2DSlice(Image* slice, Contour* contourIn2D, bool itkNotUsed( correctionForIpSegmentation ) )
00066 {
00067 if ( !slice || !contourIn2D ) return NULL;
00068
00069 Contour::Pointer worldContour = Contour::New();
00070
00071 const Contour::PathType::VertexListType* pointsIn2D = contourIn2D->GetContourPath()->GetVertexList();
00072 const Geometry3D* sliceGeometry = slice->GetGeometry();
00073 for ( Contour::PathType::VertexListType::const_iterator iter = pointsIn2D->begin();
00074 iter != pointsIn2D->end();
00075 ++iter )
00076 {
00077 Contour::PathType::VertexType currentPointIn3DITK = *iter;
00078 Point3D currentPointIn2D;
00079 for (int i = 0; i < 3; ++i) currentPointIn2D[i] = currentPointIn3DITK[i];
00080
00081 Point3D worldPointIn3D;
00082 worldPointIn3D.Fill(0.0);
00083 sliceGeometry->IndexToWorld( currentPointIn2D, worldPointIn3D );
00084 MITK_DEBUG << "index " << currentPointIn2D << " world " << worldPointIn3D << std::endl;
00085
00086 worldContour->AddVertex( worldPointIn3D );
00087 }
00088
00089 return worldContour;
00090 }
00091
00092
00093
00094 void mitk::ContourUtils::FillContourInSlice( Contour* projectedContour, Image* sliceImage, int paintingPixelValue )
00095 {
00096
00097
00098
00099
00100 mitkIpInt4_t* picContour = new mitkIpInt4_t[2 * projectedContour->GetNumberOfPoints()];
00101 const Contour::PathType::VertexListType* pointsIn2D = projectedContour->GetContourPath()->GetVertexList();
00102 unsigned int index(0);
00103 for ( Contour::PathType::VertexListType::const_iterator iter = pointsIn2D->begin();
00104 iter != pointsIn2D->end();
00105 ++iter, ++index )
00106 {
00107 picContour[ 2 * index + 0 ] = static_cast<mitkIpInt4_t>( (*iter)[0] + 0.5);
00108 picContour[ 2 * index + 1 ] = static_cast<mitkIpInt4_t>( (*iter)[1] + 0.5);
00109 MITK_DEBUG << "mitk 2d [" << (*iter)[0] << ", " << (*iter)[1] << "] pic [" << picContour[ 2*index+0] << ", " << picContour[ 2*index+1] << "]";
00110 }
00111
00112 assert( sliceImage->GetSliceData() );
00113 mitkIpPicDescriptor* originalPicSlice = sliceImage->GetSliceData()->GetPicDescriptor();
00114 mitkIpPicDescriptor* picSlice = ipMITKSegmentationNew( originalPicSlice );
00115 ipMITKSegmentationClear( picSlice );
00116
00117 assert( originalPicSlice && picSlice );
00118
00119
00120 ipMITKSegmentationCombineRegion ( picSlice, picContour, projectedContour->GetNumberOfPoints(), NULL, IPSEGMENTATION_OR, 1);
00121
00122 delete[] picContour;
00123
00124
00125
00126
00127
00128 Image::Pointer ipsegmentationModifiedSlice = Image::New();
00129 ipsegmentationModifiedSlice->Initialize( picSlice );
00130 ipsegmentationModifiedSlice->SetPicSlice( picSlice );
00131
00132 AccessFixedDimensionByItk_2( sliceImage, ItkCopyFilledContourToSlice, 2, ipsegmentationModifiedSlice, paintingPixelValue );
00133
00134 ipsegmentationModifiedSlice = NULL;
00135 ipMITKSegmentationFree( picSlice );
00136 }
00137
00138 template<typename TPixel, unsigned int VImageDimension>
00139 void mitk::ContourUtils::ItkCopyFilledContourToSlice( itk::Image<TPixel,VImageDimension>* originalSlice, const Image* filledContourSlice, int overwritevalue )
00140 {
00141 typedef itk::Image<TPixel,VImageDimension> SliceType;
00142
00143 typename SliceType::Pointer filledContourSliceITK;
00144 CastToItkImage( filledContourSlice, filledContourSliceITK );
00145
00146
00147 typedef itk::ImageRegionIterator< SliceType > OutputIteratorType;
00148 typedef itk::ImageRegionConstIterator< SliceType > InputIteratorType;
00149
00150 InputIteratorType inputIterator( filledContourSliceITK, filledContourSliceITK->GetLargestPossibleRegion() );
00151 OutputIteratorType outputIterator( originalSlice, originalSlice->GetLargestPossibleRegion() );
00152
00153 outputIterator.GoToBegin();
00154 inputIterator.GoToBegin();
00155
00156 while ( !outputIterator.IsAtEnd() )
00157 {
00158 if ( inputIterator.Get() != 0 )
00159 {
00160 outputIterator.Set( overwritevalue );
00161 }
00162
00163 ++outputIterator;
00164 ++inputIterator;
00165 }
00166 }
00167