Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkPhotoacousticFilterService.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 #include "mitkITKImageImport.h"
16 #include <chrono>
17 #include <mitkCropImageFilter.h>
21 #include "../ITKFilter/ITKUltrasound/itkBModeImageFilter.h"
22 #include "../ITKFilter/itkPhotoacousticBModeImageFilter.h"
23 #include <mitkBandpassFilter.h>
24 
25 // itk dependencies
26 #include "itkImage.h"
27 #include "itkResampleImageFilter.h"
28 #include "itkCastImageFilter.h"
29 #include "itkCropImageFilter.h"
30 #include "itkRescaleIntensityImageFilter.h"
31 #include "itkIntensityWindowingImageFilter.h"
32 #include <itkIndex.h>
33 #include "itkBSplineInterpolateImageFunction.h"
34 #include <mitkImageToItk.h>
35 
36 // needed itk image filters
37 #include "mitkImageCast.h"
38 
40 {
41  MITK_INFO << "[PhotoacousticFilterService] created filter service";
42 }
43 
45 {
46  MITK_INFO << "[PhotoacousticFilterService] destructed filter service";
47 }
48 
50  mitk::Image::Pointer inputImage,
51  BModeMethod method, bool UseLogFilter)
52 {
53  // the image needs to be of floating point type for the envelope filter to work; the casting is done automatically by the CastToItkImage
54  typedef itk::Image< float, 3 > itkFloatImageType;
55 
56  auto floatImage = ConvertToFloat(inputImage);
57 
58  if (method == BModeMethod::Abs)
59  {
61  filter->UseLogFilter(UseLogFilter);
62  filter->SetInput(floatImage);
63  filter->Update();
64  return filter->GetOutput();
65  }
66 
68  BModeFilterType::Pointer bModeFilter = BModeFilterType::New(); // LogFilter
69 
71  PhotoacousticBModeImageFilter::Pointer photoacousticBModeFilter = PhotoacousticBModeImageFilter::New(); // No LogFilter
72 
73  typedef itk::ResampleImageFilter < itkFloatImageType, itkFloatImageType > ResampleImageFilter;
74  ResampleImageFilter::Pointer resampleImageFilter = ResampleImageFilter::New();
75 
76  itkFloatImageType::Pointer itkImage;
77 
78  mitk::CastToItkImage(floatImage, itkImage);
79 
80  if (UseLogFilter)
81  {
82  bModeFilter->SetInput(itkImage);
83  bModeFilter->SetDirection(1);
84  itkImage = bModeFilter->GetOutput();
85  }
86  else
87  {
88  photoacousticBModeFilter->SetInput(itkImage);
89  photoacousticBModeFilter->SetDirection(1);
90  itkImage = photoacousticBModeFilter->GetOutput();
91  }
92 
93  return mitk::GrabItkImageMemory(itkImage);
94 }
95 
97  mitk::Image::Pointer inputImage,
98  double *outputSpacing)
99 {
100  typedef itk::Image< float, 3 > itkFloatImageType;
101 
102  auto floatImage = ConvertToFloat(inputImage);
103 
104  typedef itk::ResampleImageFilter < itkFloatImageType, itkFloatImageType > ResampleImageFilter;
105  ResampleImageFilter::Pointer resampleImageFilter = ResampleImageFilter::New();
106 
107  itkFloatImageType::Pointer itkImage;
108 
109  mitk::CastToItkImage(floatImage, itkImage);
110 
111  itkFloatImageType::SpacingType outputSpacingItk;
112  itkFloatImageType::SizeType inputSizeItk = itkImage->GetLargestPossibleRegion().GetSize();
113  itkFloatImageType::SizeType outputSizeItk = inputSizeItk;
114 
115  outputSpacingItk[0] = outputSpacing[0];
116  outputSpacingItk[1] = outputSpacing[1];
117  outputSpacingItk[2] = itkImage->GetSpacing()[2];
118 
119  outputSizeItk[0] = outputSizeItk[0] * (floatImage->GetGeometry()->GetSpacing()[0] / outputSpacing[0]);
120  outputSizeItk[1] = outputSizeItk[1] * (floatImage->GetGeometry()->GetSpacing()[1] / outputSpacing[1]);
121 
122  resampleImageFilter->SetInput(itkImage);
123  resampleImageFilter->SetSize(outputSizeItk);
124  resampleImageFilter->SetOutputSpacing(outputSpacingItk);
125 
126  resampleImageFilter->UpdateLargestPossibleRegion();
127  return mitk::GrabItkImageMemory(resampleImageFilter->GetOutput());
128 }
129 
130 
132  mitk::Image::Pointer inputImage,
133  double *outputDimension)
134 {
135  typedef itk::Image< float, 3 > itkFloatImageType;
136 
137  auto floatImage = ConvertToFloat(inputImage);
138 
139  typedef itk::ResampleImageFilter < itkFloatImageType, itkFloatImageType > ResampleImageFilter;
140  ResampleImageFilter::Pointer resampleImageFilter = ResampleImageFilter::New();
141 
142  itkFloatImageType::Pointer itkImage;
143 
144  mitk::CastToItkImage(floatImage, itkImage);
145 
146  itkFloatImageType::SpacingType outputSpacingItk;
147  itkFloatImageType::SizeType inputSizeItk = itkImage->GetLargestPossibleRegion().GetSize();
148  itkFloatImageType::SizeType outputSizeItk = inputSizeItk;
149 
150  outputSizeItk[0] = outputDimension[0];
151  outputSizeItk[1] = outputDimension[1];
152 
153  MITK_INFO << outputSizeItk[0] << " " << outputSizeItk[1];
154 
155  outputSpacingItk[0] = (double)inputSizeItk[0] / (double)outputSizeItk[0] * floatImage->GetGeometry()->GetSpacing()[0];
156  outputSpacingItk[1] = (double)inputSizeItk[1] / (double)outputSizeItk[1] * floatImage->GetGeometry()->GetSpacing()[1];
157  outputSpacingItk[2] = itkImage->GetSpacing()[2];
158 
159  MITK_INFO << outputSpacingItk[0] << " " << outputSpacingItk[1];
160 
161  resampleImageFilter->SetInput(itkImage);
162  resampleImageFilter->SetSize(outputSizeItk);
163  resampleImageFilter->SetOutputSpacing(outputSpacingItk);
164 
165  resampleImageFilter->UpdateLargestPossibleRegion();
166  return mitk::GrabItkImageMemory(resampleImageFilter->GetOutput());
167 }
168 
170  mitk::Image::Pointer inputImage,
171  int above, int below,
172  int right, int left,
173  int zStart, int zEnd,
174  int* errCode)
175 {
176  *errCode = 0;
177  try
178  {
179  auto floatImage = ConvertToFloat(inputImage);
181  cropImageFilter->SetInput(floatImage);
182  cropImageFilter->SetXPixelsCropStart(left);
183  cropImageFilter->SetXPixelsCropEnd(right);
184  cropImageFilter->SetYPixelsCropStart(above);
185  cropImageFilter->SetYPixelsCropEnd(below);
186  cropImageFilter->SetZPixelsCropStart(zStart);
187  cropImageFilter->SetZPixelsCropEnd(zEnd);
188  cropImageFilter->Update();
189  return cropImageFilter->GetOutput();
190  }
191  catch (mitk::Exception &e)
192  {
193  std::string errorMessage = "Caught unexpected exception ";
194  errorMessage.append(e.what());
195  MITK_ERROR << errorMessage;
196  *errCode = -1;
198  unsigned int dim[3] = { 1,1,1 };
199  ret->Initialize(MakeScalarPixelType<float>(), 3, dim);
200 
201  return ret;
202  }
203 }
204 
205 mitk::Image::Pointer mitk::PhotoacousticFilterService::ExtendImage(mitk::Image::Pointer inputImage, float pixelColor, unsigned int outputDimensionY)
206 {
207  mitk::Image::Pointer outputImage = mitk::Image::New();
208  unsigned int dim[] = {inputImage->GetDimension(0), outputDimensionY, inputImage->GetDimension(2)};
209  outputImage->Initialize(inputImage->GetPixelType(), 3, dim);
210 
211  float *sliceData = new float[dim[0] * dim[1]];
212 
213  for (size_t i = inputImage->GetDimension(1) * dim[0]; i < dim[0] * dim[1]; ++i)
214  {
215  sliceData[i] = pixelColor;
216  }
217 
218  for (unsigned int slice = 0; slice < dim[2]; ++slice)
219  {
220  mitk::ImageReadAccessor cpy(inputImage, inputImage->GetSliceData(slice));
221  cpy.GetData();
222  std::memcpy((void*)sliceData, cpy.GetData(), sizeof(float) * inputImage->GetDimension(1) * dim[0]);
223  outputImage->SetSlice(sliceData, slice);
224  }
225 
226  delete[] sliceData;
227  return outputImage;
228 }
229 
231  mitk::Image::Pointer inputImage,
232  BeamformingSettings::Pointer config,
233  std::function<void(int, std::string)> progressHandle)
234 {
235  Image::Pointer processedImage = mitk::Image::New();
236 
237  if (inputImage->GetDimension() != 3)
238  {
240  dimensionImageFilter->SetInput(inputImage);
241  dimensionImageFilter->Update();
242  processedImage = dimensionImageFilter->GetOutput();
243  }
244  else
245  {
246  processedImage = inputImage;
247  }
248 
250  m_BeamformingFilter->SetInput(ConvertToFloat(processedImage));
251  m_BeamformingFilter->SetProgressHandle(progressHandle);
252  m_BeamformingFilter->UpdateLargestPossibleRegion();
253 
254  processedImage = m_BeamformingFilter->GetOutput();
255 
256  return processedImage;
257 }
258 
261  float BPHighPass, float BPLowPass,
262  float alphaHighPass, float alphaLowPass,
263  float TimeSpacing, float SpeedOfSound, bool IsBFImage)
264 {
265  try
266  {
267  auto floatData = ConvertToFloat(data);
269  bandpassFilter->SetInput(floatData);
270  bandpassFilter->SetHighPass(BPHighPass);
271  bandpassFilter->SetLowPass(BPLowPass);
272  bandpassFilter->SetHighPassAlpha(alphaHighPass);
273  bandpassFilter->SetLowPassAlpha(alphaLowPass);
274  bandpassFilter->SetSpeedOfSound(SpeedOfSound);
275  bandpassFilter->SetTimeSpacing(TimeSpacing);
276  bandpassFilter->SetIsBFImage(IsBFImage);
277  bandpassFilter->Update();
278  return bandpassFilter->GetOutput();
279  }
280  catch (mitk::Exception &e)
281  {
282  std::string errorMessage = "Caught unexpected exception ";
283  errorMessage.append(e.what());
284  MITK_ERROR << errorMessage;
285 
286  return data;
287  }
288 }
289 
291 {
292  if ((inputImage->GetPixelType().GetTypeAsString() == "scalar (float)" ||
293  inputImage->GetPixelType().GetTypeAsString() == " (float)"))
294  {
295  return inputImage;
296  }
297 
299  castToFloatImageFilter->SetInput(inputImage);
300  castToFloatImageFilter->Update();
301  return castToFloatImageFilter->GetOutput();
302 }
static Pointer New()
#define MITK_INFO
Definition: mitkLogMacros.h:18
mitk::Image::Pointer ApplyResampling(mitk::Image::Pointer inputImage, double *outputSpacing)
Resamples the given image.
#define MITK_ERROR
Definition: mitkLogMacros.h:20
mitk::Image::Pointer ApplyBmodeFilter(mitk::Image::Pointer inputImage, BModeMethod method=BModeMethod::Abs, bool UseLogFilter=false)
Applies a B-Mode Filter.
mitk::Image::Pointer ApplyResamplingToDim(mitk::Image::Pointer inputImage, double *outputDimension)
static Pointer New()
mitk::Image::Pointer ExtendImage(mitk::Image::Pointer inputImage, float pixelColor, unsigned int outputDimensionY)
mitk::BeamformingFilter::Pointer m_BeamformingFilter
For performance reasons, an instance of the Beamforming filter is initialized as soon as possible and...
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
mitk::Image::Pointer ApplyCropping(mitk::Image::Pointer inputImage, int above, int below, int right, int left, int minSlice, int maxSlice, int *errCode)
Crops the given image.
static Pointer New()
mitk::Image::Pointer ApplyBandpassFilter(mitk::Image::Pointer data, float BPHighPass, float BPLowPass, float alphaHighPass, float alphaLowPass, float timeSpacing, float SpeedOfSound, bool IsBFImage)
Applies a Bandpass filter to the given image.
static Pointer New()
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
mitk::Image::Pointer ConvertToFloat(mitk::Image::Pointer)
Create an Photoacoustic B-Mode (Brightness-Mode) image from raw "RF" data. The RF&#39;s envelope is calcu...
mitk::Image::Pointer ApplyBeamforming(mitk::Image::Pointer inputImage, BeamformingSettings::Pointer config, std::function< void(int, std::string)> progressHandle=[](int, std::string) {})
Beamforms the given image.
ImageReadAccessor class to get locked read access for a particular image part.
Create an ultrasound B-Mode (Brightness-Mode) image from raw "RF" data. The RF&#39;s envelope is calculat...
BModeMethod
Defines the methods for the B-Mode filter Currently implemented are an Envelope Detection filter and ...
const void * GetData() const
Gives const access to the data.