Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
itkExtractChannelFromRgbaImageFilter.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
17 
18 // VTK
19 #include <vtkPolyLine.h>
20 #include <vtkCellArray.h>
21 #include <vtkCellData.h>
22 
23 // misc
24 #include <math.h>
25 
26 namespace itk{
27 
28  template< class ReferenceImageType, class OutputImageType >
30  m_Channel(RED)
31  {
32 
33  }
34 
35  template< class ReferenceImageType, class OutputImageType >
37  {
38  }
39 
40  template< class ReferenceImageType, class OutputImageType >
42  {
43 
44  typename InputImageType::Pointer rgbaImage = static_cast< InputImageType * >( this->ProcessObject::GetInput(0) );
45 
46  typename OutputImageType::Pointer outputImage =
47  static_cast< OutputImageType * >(this->ProcessObject::GetPrimaryOutput());
48 
49  typename InputImageType::RegionType region = rgbaImage->GetLargestPossibleRegion();
50  outputImage->SetSpacing( m_ReferenceImage->GetSpacing() ); // Set the image spacing
51  outputImage->SetOrigin( m_ReferenceImage->GetOrigin() ); // Set the image origin
52  outputImage->SetDirection( m_ReferenceImage->GetDirection() ); // Set the image direction
53  outputImage->SetRegions( m_ReferenceImage->GetLargestPossibleRegion());
54  outputImage->Allocate();
55  outputImage->FillBuffer(0);
56  float* outImageBufferPointer = outputImage->GetBufferPointer();
57 
59  counterImage->SetSpacing( m_ReferenceImage->GetSpacing() ); // Set the image spacing
60  counterImage->SetOrigin( m_ReferenceImage->GetOrigin() ); // Set the image origin
61  counterImage->SetDirection( m_ReferenceImage->GetDirection() ); // Set the image direction
62  counterImage->SetRegions( m_ReferenceImage->GetLargestPossibleRegion());
63  counterImage->Allocate();
64  counterImage->FillBuffer(0);
65  short* counterImageBufferPointer = counterImage->GetBufferPointer();
66 
67  int w = m_ReferenceImage->GetLargestPossibleRegion().GetSize().GetElement(0);
68  int h = m_ReferenceImage->GetLargestPossibleRegion().GetSize().GetElement(1);
69  int d = m_ReferenceImage->GetLargestPossibleRegion().GetSize().GetElement(2);
70 
71  typedef ImageRegionConstIterator< InputImageType > InImageIteratorType;
72  InImageIteratorType rgbaIt(rgbaImage, region);
73  rgbaIt.GoToBegin();
74  while(!rgbaIt.IsAtEnd()){
75 
76  InPixelType x = rgbaIt.Get();
77  ++rgbaIt;
78 
79  itk::Point<float, 3> vertex;
80  itk::Index<3> index = rgbaIt.GetIndex();
81 
82  rgbaImage->TransformIndexToPhysicalPoint(index, vertex);
83  outputImage->TransformPhysicalPointToIndex(vertex, index);
84 
85  itk::ContinuousIndex<float, 3> contIndex;
86  outputImage->TransformPhysicalPointToContinuousIndex(vertex, contIndex);
87 
88  float frac_x = contIndex[0] - index[0];
89  float frac_y = contIndex[1] - index[1];
90  float frac_z = contIndex[2] - index[2];
91  int px = index[0];
92  if (frac_x<0)
93  {
94  px -= 1;
95  frac_x += 1;
96  }
97  int py = index[1];
98  if (frac_y<0)
99  {
100  py -= 1;
101  frac_y += 1;
102  }
103  int pz = index[2];
104  if (frac_z<0)
105  {
106  pz -= 1;
107  frac_z += 1;
108  }
109  frac_x = 1-frac_x;
110  frac_y = 1-frac_y;
111  frac_z = 1-frac_z;
112 
113  // int coordinates inside image?
114  if (px < 0 || px >= w-1)
115  continue;
116  if (py < 0 || py >= h-1)
117  continue;
118  if (pz < 0 || pz >= d-1)
119  continue;
120 
121  OutPixelType out;
122  switch (m_Channel)
123  {
124  case RED:
125  out = (float)x.GetRed()/255;
126  break;
127  case GREEN:
128  out = (float)x.GetGreen()/255;
129  break;
130  case BLUE:
131  out = (float)x.GetBlue()/255;
132  break;
133  case ALPHA:
134  out = (float)x.GetAlpha()/255;
135  }
136 
137  outImageBufferPointer[( px + w*(py + h*pz ))] += out*( frac_x)*( frac_y)*( frac_z);
138  outImageBufferPointer[( px + w*(py+1+ h*pz ))] += out*( frac_x)*(1-frac_y)*( frac_z);
139  outImageBufferPointer[( px + w*(py + h*pz+h))] += out*( frac_x)*( frac_y)*(1-frac_z);
140  outImageBufferPointer[( px + w*(py+1+ h*pz+h))] += out*( frac_x)*(1-frac_y)*(1-frac_z);
141  outImageBufferPointer[( px+1 + w*(py + h*pz ))] += out*(1-frac_x)*( frac_y)*( frac_z);
142  outImageBufferPointer[( px+1 + w*(py + h*pz+h))] += out*(1-frac_x)*( frac_y)*(1-frac_z);
143  outImageBufferPointer[( px+1 + w*(py+1+ h*pz ))] += out*(1-frac_x)*(1-frac_y)*( frac_z);
144  outImageBufferPointer[( px+1 + w*(py+1+ h*pz+h))] += out*(1-frac_x)*(1-frac_y)*(1-frac_z);
145 
146  counterImageBufferPointer[( px + w*(py + h*pz ))] += 1;
147  counterImageBufferPointer[( px + w*(py+1+ h*pz ))] += 1;
148  counterImageBufferPointer[( px + w*(py + h*pz+h))] += 1;
149  counterImageBufferPointer[( px + w*(py+1+ h*pz+h))] += 1;
150  counterImageBufferPointer[( px+1 + w*(py + h*pz ))] += 1;
151  counterImageBufferPointer[( px+1 + w*(py + h*pz+h))] += 1;
152  counterImageBufferPointer[( px+1 + w*(py+1+ h*pz ))] += 1;
153  counterImageBufferPointer[( px+1 + w*(py+1+ h*pz+h))] += 1;
154 
155  }
156 
157  typedef ImageRegionIterator< OutputImageType > OutImageIteratorType;
158  OutImageIteratorType outIt(outputImage, outputImage->GetLargestPossibleRegion());
159  outIt.GoToBegin();
160  typedef ImageRegionConstIterator< itk::Image< short, 3 > > CountImageIteratorType;
161  CountImageIteratorType counterIt(counterImage, counterImage->GetLargestPossibleRegion());
162  counterIt.GoToBegin();
163 
164  while(!outIt.IsAtEnd() && !counterIt.IsAtEnd()){
165  if (counterIt.Value()>0)
166  outIt.Set(outIt.Value()/counterIt.Value());
167  ++outIt;
168  ++counterIt;
169  }
170  }
171 }
itk::SmartPointer< Self > Pointer
itk::Image< itk::RGBAPixel< unsigned char >, 3 > InputImageType
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.