13 #ifndef ITKLINEHISTOGRAMBASEDMASSIMAGEFILTER_CPP 14 #define ITKLINEHISTOGRAMBASEDMASSIMAGEFILTER_CPP 17 #include <itkImageRegionIteratorWithIndex.h> 18 #include <itkBinaryContourImageFilter.h> 23 template<
class TInputImageType,
class TOutputImageType,
class TMaskImageType>
28 if(m_ImageMask.IsNull())
30 itkExceptionMacro(
"No binary mask image provided.")
33 if(m_BinaryContour.IsNull()){
36 typename TMaskImageType::RegionType region = m_ImageMask->GetLargestPossibleRegion();
37 unsigned int xdim = region.GetSize(0);
38 unsigned int ydim = region.GetSize(1);
39 unsigned int zdim = region.GetSize(2);
42 typename TMaskImageType::Pointer mask_copy = TMaskImageType::New();
43 mask_copy->SetSpacing(m_ImageMask->GetSpacing());
44 mask_copy->SetDirection(m_ImageMask->GetDirection());
45 mask_copy->SetOrigin(m_ImageMask->GetOrigin());
46 mask_copy->SetRegions(region);
47 mask_copy->Allocate();
48 mask_copy->FillBuffer(0);
50 itk::ImageRegionIteratorWithIndex<TMaskImageType> oit(mask_copy, mask_copy->GetLargestPossibleRegion());
51 itk::ImageRegionConstIteratorWithIndex<TMaskImageType> mit(m_ImageMask, m_ImageMask->GetLargestPossibleRegion());
55 && mit.GetIndex()[0] != 0
56 && mit.GetIndex()[1] != 0
57 && mit.GetIndex()[2] != 0
58 && mit.GetIndex()[0] != xdim-1
59 && mit.GetIndex()[1] != ydim-1
60 && mit.GetIndex()[2] != zdim-1)
68 typedef itk::BinaryContourImageFilter<TMaskImageType,TMaskImageType> BinaryContourImagefilterType;
69 typename BinaryContourImagefilterType::Pointer filter = BinaryContourImagefilterType::New();
70 filter->SetInput(mask_copy);
71 filter->SetBackgroundValue(0);
72 filter->SetForegroundValue(1);
73 filter->SetFullyConnected(
true);
76 m_BinaryContour = filter->GetOutput();
81 if(m_BinaryContour.IsNull())
83 itkExceptionMacro(
"No binary contour image provided.")
86 m_CenterOfMask = GetCenterOfMass(m_ImageMask);
88 if(m_CenterOfMask.is_zero())
90 itkExceptionMacro(
"Center of mass is corrupt.")
96 template<
class TInputImageType,
class TOutputImageType,
class TMaskImageType>
103 typedef itk::ImageRegionConstIterator< TMaskImageType > IteratorType;
104 IteratorType iter( maskImage, maskImage->GetLargestPossibleRegion() );
107 vnl_vector_fixed<double,3> mean_index;
109 unsigned int count = 0;
110 while ( !iter.IsAtEnd() )
112 if ( iter.Get() != 0 )
115 img->GetGeometry()->IndexToWorld(iter.GetIndex(),current_index_pos);
116 mean_index += current_index_pos.GetVnlVector();
126 template<
class TInputImageType,
class TOutputImageType,
class TMaskImageType>
128 ::ThreadedGenerateData(
const typename Superclass::OutputImageRegionType &outputRegionForThread, ThreadIdType )
131 TOutputImageType * outimage = this->GetOutput();
134 itk::ImageRegionConstIteratorWithIndex<TMaskImageType> cit(m_BinaryContour, outputRegionForThread);
135 itk::ImageRegionConstIteratorWithIndex<TInputImageType> iit(this->GetInput(), outputRegionForThread);
136 itk::ImageRegionIteratorWithIndex<TOutputImageType > oit(outimage,outputRegionForThread);
137 typedef typename itk::ImageRegionIteratorWithIndex<TInputImageType >::IndexType IndexType;
139 std::vector<IndexType> target_world_indices;
141 while(!cit.IsAtEnd())
143 if(cit.Value() != 0 )
144 target_world_indices.push_back(cit.GetIndex());
152 while(!target_world_indices.empty())
154 vnl_vector<double> u_v(3,1), x_v(3,1), p_v(3,1);
155 double line_histo_area = 0;
159 image->GetGeometry()->IndexToWorld(target_world_indices.back(),skull_point);
163 u_v = skull_point.GetVnlVector() - m_CenterOfMask;
166 p_v = m_CenterOfMask;
173 center_point[0] = m_CenterOfMask[0];
174 center_point[1] = m_CenterOfMask[1];
175 center_point[2] = m_CenterOfMask[2];
178 oit.SetIndex(tmp_index);
180 std::vector<IndexType> under_line_indices;
186 under_line_indices.push_back(tmp_index);
189 iit.SetIndex(tmp_index);
190 line_histo_area += iit.Value();
193 if(tmp_index == target_world_indices.back())
199 while(tmp_index == oit.GetIndex())
201 x_v = p_v + s_s * u_v;
202 tmp_point.GetVnlVector().set(x_v.data_block());
207 oit.SetIndex(tmp_index);
211 while (!under_line_indices.empty()) {
212 IndexType current_index = under_line_indices.back();
213 under_line_indices.pop_back();
215 oit.SetIndex(current_index);
216 oit.Set(line_histo_area / (s_s * u_v).magnitude() );
219 target_world_indices.pop_back();
225 template<
class TInputImageType,
class TOutputImageType,
class TMaskImageType>
228 this->m_ImageMask = maskimage;
231 template<
class TInputImageType,
class TOutputImageType,
class TMaskImageType>
234 this->m_BinaryContour = binarycontour;
237 template<
class TInputImageType,
class TOutputImageType,
class TMaskImageType>
240 this->SetNumberOfIndexedOutputs(1);
241 this->SetNumberOfIndexedInputs(1);
243 m_CenterOfMask.fill(0);
246 template<
class TInputImageType,
class TOutputImageType,
class TMaskImageType>
void SetBinaryContour(TMaskImageType *contouriamge)
void SetImageMask(TMaskImageType *maskimage)
mitk::Image::Pointer image
void WorldToIndex(const mitk::Point3D &pt_mm, mitk::Point3D &pt_units) const
Convert world coordinates (in mm) of a point to (continuous!) index coordinates.
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
BaseGeometry Describes the geometry of a data object.