Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
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 // mitk includes
19 #include "mitkImageDataItem.h"
21 #include "mitkImageReadAccessor.h"
22 
23 #include "vtkSmartPointer.h"
24 #include "vtkColorTransferFunction.h"
25 #include "vtkFloatArray.h"
26 
27 
28 namespace mitk
29 {
31  {
35  m_ImageType = 0;
36  m_ImageDepth = IPL_DEPTH_32F;
37  m_ImageGrabber = NULL;
38  }
39 
41  {
42  }
43 
45  {
46  m_ImageGrabber->Update();
47  unsigned int numOfPixel = m_ImageGrabber->GetCaptureWidth()*m_ImageGrabber->GetCaptureHeight();
48  // copy current mitk images
49  unsigned int dimensions[4];
50  dimensions[0] = this->m_ImageGrabber->GetCaptureWidth();
51  dimensions[1] = this->m_ImageGrabber->GetCaptureHeight();
52  dimensions[2] = 1;
53  dimensions[3] = 1;
54 
55  // create single component float pixel type
56  mitk::PixelType FloatType = MakeScalarPixelType<float>();
57 
58  ImageReadAccessor imgGrabAcc0(m_ImageGrabber->GetOutput(0), m_ImageGrabber->GetOutput(0)->GetSliceData());
59  ImageReadAccessor imgGrabAcc1(m_ImageGrabber->GetOutput(1), m_ImageGrabber->GetOutput(1)->GetSliceData());
60  ImageReadAccessor imgGrabAcc2(m_ImageGrabber->GetOutput(2), m_ImageGrabber->GetOutput(2)->GetSliceData());
61 
62  mitk::Image::Pointer currentMITKIntensityImage = mitk::Image::New();
63  currentMITKIntensityImage->Initialize(FloatType, 2, dimensions);
64  currentMITKIntensityImage->SetSlice((float*) imgGrabAcc2.GetData(),0,0,0);
65 
66  mitk::Image::Pointer currentMITKAmplitudeImage = mitk::Image::New();
67  currentMITKAmplitudeImage->Initialize(FloatType, 2, dimensions);
68  currentMITKAmplitudeImage->SetSlice((float*)imgGrabAcc1.GetData(),0,0,0);
69 
70  mitk::Image::Pointer currentMITKDistanceImage = mitk::Image::New();
71  currentMITKDistanceImage->Initialize(FloatType, 2, dimensions);
72  currentMITKDistanceImage->SetSlice((float*)imgGrabAcc0.GetData(),0,0,0);
73  // copy mitk images to OpenCV images
74  if (m_ImageDepth==IPL_DEPTH_32F)
75  {
76  if (m_ImageType==1)
77  {
78  ImageReadAccessor currentAmplAcc(currentMITKAmplitudeImage, currentMITKAmplitudeImage->GetSliceData(0, 0, 0));
79  float* amplitudeFloatData = (float*) currentAmplAcc.GetData();
80  memcpy(m_CurrentOpenCVAmplitudeImage->imageData,(unsigned char*)amplitudeFloatData,numOfPixel*sizeof(float));
81  cv::Mat image(m_CurrentOpenCVAmplitudeImage);
82  return image;
83  }
84  else if (m_ImageType==2)
85  {
86  ImageReadAccessor currentIntenAcc(currentMITKIntensityImage, currentMITKIntensityImage->GetSliceData(0, 0, 0));
87  float* intensityFloatData = (float*) currentIntenAcc.GetData();
88  memcpy(m_CurrentOpenCVIntensityImage->imageData,(unsigned char*)intensityFloatData,numOfPixel*sizeof(float));
89  cv::Mat image(m_CurrentOpenCVIntensityImage);
90  return image;
91  }
92  else
93  {
94  ImageReadAccessor currentDistAcc(currentMITKDistanceImage, currentMITKDistanceImage->GetSliceData(0, 0, 0));
95  float* distanceFloatData = (float*) currentDistAcc.GetData();
96  memcpy(m_CurrentOpenCVDistanceImage->imageData,(unsigned char*)distanceFloatData,numOfPixel*sizeof(float));
97  cv::Mat image(m_CurrentOpenCVDistanceImage);
98  return image;
99  }
100  }
101  else
102  {
103  if (m_ImageType==1)
104  {
105  this->MapScalars(currentMITKAmplitudeImage, m_CurrentOpenCVAmplitudeImage);
106  cv::Mat image(m_CurrentOpenCVAmplitudeImage);
107  return image;
108  }
109  else if (m_ImageType==2)
110  {
111  this->MapScalars(currentMITKIntensityImage, m_CurrentOpenCVIntensityImage);
112  cv::Mat image(m_CurrentOpenCVIntensityImage);
113  return image;
114  }
115  else
116  {
117  this->MapScalars(currentMITKDistanceImage, m_CurrentOpenCVDistanceImage);
118  cv::Mat image(m_CurrentOpenCVDistanceImage);
119  return image;
120  }
121  }
122  }
123 
124  void ToFOpenCVImageGrabber::SetImageType(unsigned int imageType)
125  {
126  m_ImageType = imageType;
127  }
128 
129  void ToFOpenCVImageGrabber::SetImageDepth(unsigned int imageDepth)
130  {
131  m_ImageDepth = imageDepth;
132  }
133 
135  {
136  m_ImageGrabber = imageGrabber;
137  }
138 
140  {
141  return m_ImageGrabber;
142  }
143 
145  {
146  if (m_ImageGrabber.IsNotNull())
147  {
148  m_ImageGrabber->ConnectCamera();
149  //Initialize cv Images after the camera is conneceted and we know the resolution
150  m_CurrentOpenCVIntensityImage = cvCreateImage(cvSize(m_ImageGrabber->GetCaptureWidth(), m_ImageGrabber->GetCaptureHeight()), m_ImageDepth, 1);
151  m_CurrentOpenCVAmplitudeImage = cvCreateImage(cvSize(m_ImageGrabber->GetCaptureWidth(), m_ImageGrabber->GetCaptureHeight()), m_ImageDepth, 1);
152  m_CurrentOpenCVDistanceImage = cvCreateImage(cvSize(m_ImageGrabber->GetCaptureWidth(), m_ImageGrabber->GetCaptureHeight()), m_ImageDepth, 1);
153  m_ImageGrabber->StartCamera();
154  }
155  }
156 
158  {
159  if (m_ImageGrabber.IsNotNull())
160  {
161  m_ImageGrabber->StopCamera();
162  m_ImageGrabber->DisconnectCamera();
163  }
164  }
165 
166  void ToFOpenCVImageGrabber::MapScalars( mitk::Image::Pointer mitkImage, IplImage* openCVImage)
167  {
168  unsigned int numOfPixel = m_ImageGrabber->GetCaptureWidth()*m_ImageGrabber->GetCaptureHeight();
169  ImageReadAccessor imgAcc(mitkImage, mitkImage->GetSliceData(0, 0, 0));
170  float* floatData = (float*)imgAcc.GetData();
171  vtkSmartPointer<vtkColorTransferFunction> colorTransferFunction = vtkSmartPointer<vtkColorTransferFunction>::New();
172  vtkSmartPointer<vtkFloatArray> floatArrayInt = vtkSmartPointer<vtkFloatArray>::New();
173  floatArrayInt->Initialize();
174  floatArrayInt->SetArray(floatData, numOfPixel, 0);
175  mitk::ScalarType min = mitkImage->GetStatistics()->GetScalarValueMin();
176  mitk::ScalarType max = mitkImage->GetStatistics()->GetScalarValueMaxNoRecompute();
177  MITK_INFO<<"Minimum: "<<min;
178  MITK_INFO<<"Maximum: "<<max;
179  colorTransferFunction->RemoveAllPoints();
180  colorTransferFunction->AddRGBPoint(min, 0, 0, 0);
181  colorTransferFunction->AddRGBPoint(max, 1, 1, 1);
182  colorTransferFunction->SetColorSpaceToHSV();
183  colorTransferFunction->MapScalarsThroughTable(floatArrayInt, (unsigned char*)openCVImage->imageData, VTK_LUMINANCE);
184  }
185 
186 } // 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:22
double ScalarType
const void * GetData() const
Gives const access to the data.
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:70
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:67
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.
Class for defining the data type of pixels.
Definition: mitkPixelType.h:55
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.