Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
Medical Imaging Interaction Toolkit
mitkUSImageSource.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 
13 #include "mitkUSImageSource.h"
14 #include "mitkProperties.h"
15 
16 const char* mitk::USImageSource::IMAGE_PROPERTY_IDENTIFIER = "id_nummer";
17 
19  : m_OpenCVToMitkFilter(mitk::OpenCVToMitkImageFilter::New()),
20  m_MitkToOpenCVFilter(nullptr),
21  m_ImageFilter(mitk::BasicCombinationOpenCVImageFilter::New()),
22  m_CurrentImageId(0),
23  m_ImageFilterMutex(itk::FastMutexLock::New())
24 {
25 }
26 
28 {
29 }
30 
31 void mitk::USImageSource::PushFilter(AbstractOpenCVImageFilter::Pointer filter)
32 {
33  m_ImageFilter->PushFilter(filter);
34 }
35 
36 bool mitk::USImageSource::RemoveFilter(AbstractOpenCVImageFilter::Pointer filter)
37 {
38  return m_ImageFilter->RemoveFilter(filter);
39 }
40 
41 bool mitk::USImageSource::GetIsFilterInThePipeline(AbstractOpenCVImageFilter::Pointer filter)
42 {
43  return m_ImageFilter->GetIsFilterOnTheList(filter);
44 }
45 
46 std::vector<mitk::Image::Pointer> mitk::USImageSource::GetNextImage()
47 {
48  std::vector<mitk::Image::Pointer> result;
49 
50  // Apply OpenCV based filters beforehand
51  if (m_ImageFilter.IsNotNull() && !m_ImageFilter->GetIsEmpty())
52  {
53  std::vector<cv::Mat> imageVector;
54  GetNextRawImage(imageVector);
55  if(result.size() != imageVector.size())
56  result.resize(imageVector.size());
57 
58  for (size_t i = 0; i < imageVector.size(); ++i)
59  {
60  if (!imageVector[i].empty())
61  {
62  m_ImageFilterMutex->Lock();
63  m_ImageFilter->FilterImage(imageVector[i], m_CurrentImageId);
64  m_ImageFilterMutex->Unlock();
65 
66  // convert to MITK image
67  this->m_OpenCVToMitkFilter->SetOpenCVMat(imageVector[i]);
68  this->m_OpenCVToMitkFilter->Update();
69 
70  // OpenCVToMitkImageFilter returns a standard mitk::image.
71  result[i] = this->m_OpenCVToMitkFilter->GetOutput();
72  }
73  }
74  }
75  else
76  {
77  this->GetNextRawImage(result);
78  }
79 
80  for (size_t i = 0; i < result.size(); ++i)
81  {
82  if (result[i].IsNotNull())
83  {
84  result[i]->SetProperty(IMAGE_PROPERTY_IDENTIFIER, mitk::IntProperty::New(m_CurrentImageId));
85  }
86  else
87  {
88  //MITK_WARN("mitkUSImageSource") << "Result image " << i << " is not set.";
89  result[i] = mitk::Image::New();
90  }
91  }
92  m_CurrentImageId++;
93 
94  return result;
95 }
96 
97 void mitk::USImageSource::GetNextRawImage(std::vector<cv::Mat>& imageVector)
98 {
99  // create filter object if it does not exist yet
101  {
103  }
104 
105  // get mitk image through virtual method of the subclass
106  std::vector<mitk::Image::Pointer> mitkImg;
107  this->GetNextRawImage(mitkImg);
108 
109  for (unsigned int i = 0; i < mitkImg.size(); ++i)
110  {
111  if (mitkImg[i].IsNull() || !mitkImg[i]->IsInitialized())
112  {
113  imageVector[i] = cv::Mat();
114  }
115  else
116  {
117  // convert mitk::Image to an OpenCV image
118  m_MitkToOpenCVFilter->SetImage(mitkImg[i]);
119  imageVector[i] = m_MitkToOpenCVFilter->GetOpenCVMat();
120  }
121  }
122 }
mitk::OpenCVToMitkImageFilter::Pointer m_OpenCVToMitkFilter
Used to convert from OpenCV Images to MITK Images.
DataCollection - Class to facilitate loading/accessing structured data.
bool RemoveFilter(AbstractOpenCVImageFilter::Pointer filter)
bool GetIsFilterInThePipeline(AbstractOpenCVImageFilter::Pointer filter)
virtual void GetNextRawImage(std::vector< cv::Mat > &)
Set the given OpenCV image matrix to the next image received from the device or file.
Filter for creating MITK RGB Images from an OpenCV image.
static Pointer New()
static Pointer New()
void PushFilter(AbstractOpenCVImageFilter::Pointer filter)
mitk::ImageToOpenCVImageFilter::Pointer m_MitkToOpenCVFilter
Used to convert from MITK Images to OpenCV Images.
static const char * IMAGE_PROPERTY_IDENTIFIER
std::vector< mitk::Image::Pointer > GetNextImage()
Retrieves the next frame. This will typically be the next frame in a file or the last cached file in ...