Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkToFOpenCVImageGrabber.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 ============================================================================*/
13 
14 // mitk includes
15 #include "mitkImageDataItem.h"
17 #include "mitkImageReadAccessor.h"
18 
19 #include "vtkSmartPointer.h"
20 #include "vtkColorTransferFunction.h"
21 #include "vtkFloatArray.h"
22 
23 
24 namespace mitk
25 {
27  {
31  m_ImageType = 0;
32  m_ImageDepth = IPL_DEPTH_32F;
33  m_ImageGrabber = nullptr;
34  }
35 
37  {
38  }
39 
41  {
42  m_ImageGrabber->Update();
43  unsigned int numOfPixel = m_ImageGrabber->GetCaptureWidth()*m_ImageGrabber->GetCaptureHeight();
44  // copy current mitk images
45  unsigned int dimensions[4];
46  dimensions[0] = this->m_ImageGrabber->GetCaptureWidth();
47  dimensions[1] = this->m_ImageGrabber->GetCaptureHeight();
48  dimensions[2] = 1;
49  dimensions[3] = 1;
50 
51  // create single component float pixel type
52  mitk::PixelType FloatType = MakeScalarPixelType<float>();
53 
54  ImageReadAccessor imgGrabAcc0(m_ImageGrabber->GetOutput(0), m_ImageGrabber->GetOutput(0)->GetSliceData());
55  ImageReadAccessor imgGrabAcc1(m_ImageGrabber->GetOutput(1), m_ImageGrabber->GetOutput(1)->GetSliceData());
56  ImageReadAccessor imgGrabAcc2(m_ImageGrabber->GetOutput(2), m_ImageGrabber->GetOutput(2)->GetSliceData());
57 
58  mitk::Image::Pointer currentMITKIntensityImage = mitk::Image::New();
59  currentMITKIntensityImage->Initialize(FloatType, 2, dimensions);
60  currentMITKIntensityImage->SetSlice((float*) imgGrabAcc2.GetData(),0,0,0);
61 
62  mitk::Image::Pointer currentMITKAmplitudeImage = mitk::Image::New();
63  currentMITKAmplitudeImage->Initialize(FloatType, 2, dimensions);
64  currentMITKAmplitudeImage->SetSlice((float*)imgGrabAcc1.GetData(),0,0,0);
65 
66  mitk::Image::Pointer currentMITKDistanceImage = mitk::Image::New();
67  currentMITKDistanceImage->Initialize(FloatType, 2, dimensions);
68  currentMITKDistanceImage->SetSlice((float*)imgGrabAcc0.GetData(),0,0,0);
69  // copy mitk images to OpenCV images
70  if (m_ImageDepth==IPL_DEPTH_32F)
71  {
72  if (m_ImageType==1)
73  {
74  ImageReadAccessor currentAmplAcc(currentMITKAmplitudeImage, currentMITKAmplitudeImage->GetSliceData(0, 0, 0));
75  float* amplitudeFloatData = (float*) currentAmplAcc.GetData();
76  memcpy(m_CurrentOpenCVAmplitudeImage->imageData,(unsigned char*)amplitudeFloatData,numOfPixel*sizeof(float));
77  return cv::cvarrToMat(m_CurrentOpenCVAmplitudeImage, false);
78  }
79  else if (m_ImageType==2)
80  {
81  ImageReadAccessor currentIntenAcc(currentMITKIntensityImage, currentMITKIntensityImage->GetSliceData(0, 0, 0));
82  float* intensityFloatData = (float*) currentIntenAcc.GetData();
83  memcpy(m_CurrentOpenCVIntensityImage->imageData,(unsigned char*)intensityFloatData,numOfPixel*sizeof(float));
84  return cv::cvarrToMat(m_CurrentOpenCVIntensityImage, false);
85  }
86  else
87  {
88  ImageReadAccessor currentDistAcc(currentMITKDistanceImage, currentMITKDistanceImage->GetSliceData(0, 0, 0));
89  float* distanceFloatData = (float*) currentDistAcc.GetData();
90  memcpy(m_CurrentOpenCVDistanceImage->imageData,(unsigned char*)distanceFloatData,numOfPixel*sizeof(float));
91  return cv::cvarrToMat(m_CurrentOpenCVDistanceImage, false);
92  }
93  }
94  else
95  {
96  if (m_ImageType==1)
97  {
98  this->MapScalars(currentMITKAmplitudeImage, m_CurrentOpenCVAmplitudeImage);
99  return cv::cvarrToMat(m_CurrentOpenCVAmplitudeImage, false);
100  }
101  else if (m_ImageType==2)
102  {
103  this->MapScalars(currentMITKIntensityImage, m_CurrentOpenCVIntensityImage);
104  return cv::cvarrToMat(m_CurrentOpenCVIntensityImage, false);
105  }
106  else
107  {
108  this->MapScalars(currentMITKDistanceImage, m_CurrentOpenCVDistanceImage);
109  return cv::cvarrToMat(m_CurrentOpenCVDistanceImage, false);
110  }
111  }
112  }
113 
114  void ToFOpenCVImageGrabber::SetImageType(unsigned int imageType)
115  {
116  m_ImageType = imageType;
117  }
118 
119  void ToFOpenCVImageGrabber::SetImageDepth(unsigned int imageDepth)
120  {
121  m_ImageDepth = imageDepth;
122  }
123 
125  {
126  m_ImageGrabber = imageGrabber;
127  }
128 
130  {
131  return m_ImageGrabber;
132  }
133 
135  {
136  if (m_ImageGrabber.IsNotNull())
137  {
138  m_ImageGrabber->ConnectCamera();
139  //Initialize cv Images after the camera is conneceted and we know the resolution
140  m_CurrentOpenCVIntensityImage = cvCreateImage(cvSize(m_ImageGrabber->GetCaptureWidth(), m_ImageGrabber->GetCaptureHeight()), m_ImageDepth, 1);
141  m_CurrentOpenCVAmplitudeImage = cvCreateImage(cvSize(m_ImageGrabber->GetCaptureWidth(), m_ImageGrabber->GetCaptureHeight()), m_ImageDepth, 1);
142  m_CurrentOpenCVDistanceImage = cvCreateImage(cvSize(m_ImageGrabber->GetCaptureWidth(), m_ImageGrabber->GetCaptureHeight()), m_ImageDepth, 1);
143  m_ImageGrabber->StartCamera();
144  }
145  }
146 
148  {
149  if (m_ImageGrabber.IsNotNull())
150  {
151  m_ImageGrabber->StopCamera();
152  m_ImageGrabber->DisconnectCamera();
153  }
154  }
155 
156  void ToFOpenCVImageGrabber::MapScalars( mitk::Image::Pointer mitkImage, IplImage* openCVImage)
157  {
158  unsigned int numOfPixel = m_ImageGrabber->GetCaptureWidth()*m_ImageGrabber->GetCaptureHeight();
159  ImageReadAccessor imgAcc(mitkImage, mitkImage->GetSliceData(0, 0, 0));
160  float* floatData = (float*)imgAcc.GetData();
161  vtkSmartPointer<vtkColorTransferFunction> colorTransferFunction = vtkSmartPointer<vtkColorTransferFunction>::New();
162  vtkSmartPointer<vtkFloatArray> floatArrayInt = vtkSmartPointer<vtkFloatArray>::New();
163  floatArrayInt->Initialize();
164  floatArrayInt->SetArray(floatData, numOfPixel, 0);
165  mitk::ScalarType min = mitkImage->GetStatistics()->GetScalarValueMin();
166  mitk::ScalarType max = mitkImage->GetStatistics()->GetScalarValueMaxNoRecompute();
167  MITK_INFO<<"Minimum: "<<min;
168  MITK_INFO<<"Maximum: "<<max;
169  colorTransferFunction->RemoveAllPoints();
170  colorTransferFunction->AddRGBPoint(min, 0, 0, 0);
171  colorTransferFunction->AddRGBPoint(max, 1, 1, 1);
172  colorTransferFunction->SetColorSpaceToHSV();
173  colorTransferFunction->MapScalarsThroughTable(floatArrayInt, (unsigned char*)openCVImage->imageData, VTK_LUMINANCE);
174  }
175 
176 } // end namespace mitk
void SetImageDepth(unsigned int imageDepth)
set the depth of the image. Some functions of OpenCV do not support IPL_DEPTH_32F. Warning: changing from default results in a mapping of the pixel value through a lookup table IPL_DEPTH_1U 1 IPL_DEPTH_8U 8 IPL_DEPTH_16U 16 IPL_DEPTH_32F 32 (Default)
#define MITK_INFO
Definition: mitkLogMacros.h:18
double ScalarType
DataCollection - Class to facilitate loading/accessing structured data.
void SetToFImageGrabber(mitk::ToFImageGrabber::Pointer imageGrabber)
set the ImageGrabber used for fetching image data from the camera
IplImage * m_CurrentOpenCVDistanceImage
OpenCV image holding the current distance data.
unsigned int m_ImageDepth
image depth currently used by this image source. Warning: Changing from default (IPL_DEPTH_32F) resul...
mitk::ToFImageGrabber::Pointer GetToFImageGrabber()
get the ImageGrabber used for fetching image data from the camera
IplImage * m_CurrentOpenCVIntensityImage
OpenCV image holding the current intensity data.
static T max(T x, T y)
Definition: svm.cpp:56
static Pointer New()
void MapScalars(mitk::Image::Pointer mitkImage, IplImage *openCVImage)
map scalars through lookup table
static T min(T x, T y)
Definition: svm.cpp:53
mitk::ToFImageGrabber::Pointer m_ImageGrabber
ImageGrabber used for fetching ToF image data from the camera.
void SetImageType(unsigned int imageType)
set type of image you want to grab. 0: Distance image (Default) 1: Amplitude image 2: Intensity image...
cv::Mat GetImage() override
Get current ToF image. Specify image you want to grab with SetImageType()
ImageReadAccessor class to get locked read access for a particular image part.
unsigned int m_ImageType
type of image currently supplied by this image source
IplImage * m_CurrentOpenCVAmplitudeImage
OpenCV image holding the current amplitude data.
const void * GetData() const
Gives const access to the data.
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51