Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkUSImageVideoSource.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 ===================================================================*/
16 
17 // MITK HEADER
18 #include "mitkUSImageVideoSource.h"
19 #include "mitkImage.h"
20 
21 //OpenCV HEADER
22 #include <cv.h>
23 #include <highgui.h>
24 
25 //Other
26 #include <stdio.h>
27 
29  : m_VideoCapture(new cv::VideoCapture()),
30  m_IsVideoReady(false),
31  m_IsGreyscale(false),
32  m_IsCropped(false),
33  m_ResolutionOverrideWidth(0),
34  m_ResolutionOverrideHeight(0),
35  m_ResolutionOverride(false),
36  m_GrayscaleFilter(mitk::ConvertGrayscaleOpenCVImageFilter::New()),
37  m_CropFilter(mitk::CropOpenCVImageFilter::New())
38 {
39 }
40 
42 {
43  m_VideoCapture->release();
44  delete m_VideoCapture;
45 }
46 
48 {
49  m_VideoCapture->open(path.c_str());
50 
51  // check if we succeeded
52  if(!m_VideoCapture->isOpened()) { m_IsVideoReady = false; }
53  else { m_IsVideoReady = true; }
54 
55  // if Override is enabled, use it
56  if (m_ResolutionOverride)
57  {
58  m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, this->m_ResolutionOverrideWidth);
59  m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, this->m_ResolutionOverrideHeight);
60  }
61 }
62 
64 {
65  m_VideoCapture->open(deviceID);
66  if(!m_VideoCapture->isOpened()) // check if we succeeded
67  m_IsVideoReady = false;
68  else
69  m_IsVideoReady = true;
70 
71  // if Override is enabled, use it
72  if (m_ResolutionOverride)
73  {
74  m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, this->m_ResolutionOverrideWidth);
75  m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, this->m_ResolutionOverrideHeight);
76  }
77 }
78 
80 {
81  m_VideoCapture->release();
82  delete m_VideoCapture;
83  m_VideoCapture = new cv::VideoCapture();
84 }
85 
87  if ( ! isColor && ! m_IsGreyscale )
88  {
89  this->PushFilter(m_GrayscaleFilter.GetPointer());
90  }
91  else if ( isColor && m_IsGreyscale )
92  {
93  this->RemoveFilter(m_GrayscaleFilter.GetPointer());
94  }
95 
96  m_IsGreyscale = !isColor;
97 }
98 
100 {
101  if (m_VideoCapture) { return m_VideoCapture->get(CV_CAP_PROP_FRAME_HEIGHT); }
102  else { return 0; }
103 }
104 
106 {
107  if (m_VideoCapture) { return m_VideoCapture->get(CV_CAP_PROP_FRAME_WIDTH); }
108  else { return 0; }
109 }
110 
112 {
113  if (!m_VideoCapture) { return false; }
114 
115  return m_VideoCapture->isOpened();
116 }
117 
118 void mitk::USImageVideoSource::SetRegionOfInterest(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY)
119 {
120  m_CropFilter->SetCropRegion(topLeftX, topLeftY, bottomRightX, bottomRightY);
121 
122  if (! m_IsCropped && ! m_CropFilter->GetIsCropRegionEmpty())
123  {
124  this->PushFilter(m_CropFilter.GetPointer());
125  m_IsCropped = true;
126  }
127 }
128 
130 {
131  this->SetRegionOfInterest(roi.topLeftX, roi.topLeftY, roi.bottomRightX, roi.bottomRightY);
132 }
133 
135 {
136  int width = this->GetImageWidth();
137  int height = this->GetImageHeight();
138 
139  this->SetRegionOfInterest(cropping.left, cropping.top, width - cropping.right, height - cropping.bottom);
140 }
141 
143 {
144  cv::Rect cropRect = m_CropFilter->GetCropRegion();
145 
146  USImageCropping cropping;
147  cropping.left = cropRect.x;
148  cropping.top = cropRect.y;
149 
150  if ( cropRect.height == 0 )
151  {
152  cropping.bottom = 0;
153  }
154  else
155  {
156  cropping.bottom = this->GetImageHeight() - (cropRect.y + cropRect.height);
157  }
158 
159  if ( cropRect.width == 0 )
160  {
161  cropping.right = 0;
162  }
163  else
164  {
165  cropping.right = this->GetImageWidth() - (cropRect.x + cropRect.width);
166  }
167 
168  return cropping;
169 }
170 
172 {
173  cv::Rect cropRect = m_CropFilter->GetCropRegion();
174 
175  return USImageRoi(cropRect.x, cropRect.y, cropRect.x + cropRect.width, cropRect.y + cropRect.height);
176 }
177 
179 {
180  this->RemoveFilter(m_CropFilter.GetPointer());
181  m_IsCropped = false;
182 }
183 
185 {
186  // loop video if necessary
187  //Commented out because setting and getting of these properties is not supported. Therefore on Linux
188  //you'll always get some Highgui errors from OpenCV
189  /*if (m_VideoCapture->get(CV_CAP_PROP_POS_FRAMES) == m_VideoCapture->get(CV_CAP_PROP_FRAME_COUNT))
190  {
191  m_VideoCapture->set(CV_CAP_PROP_POS_FRAMES, 0);
192  }*/
193 
194  // retrieve image
195  *m_VideoCapture >> image; // get a new frame from camera
196 }
197 
199 {
200  cv::Mat cv_img;
201 
202  this->GetNextRawImage(cv_img);
203 
204  // convert to MITK-Image
205  IplImage ipl_img = cv_img;
206 
207  this->m_OpenCVToMitkFilter->SetOpenCVImage(&ipl_img);
208  this->m_OpenCVToMitkFilter->Update();
209 
210  // OpenCVToMitkImageFilter returns a standard mitk::image. We then transform it into an USImage
211  image = this->m_OpenCVToMitkFilter->GetOutput();
212 
213  // clean up
214  cv_img.release();
215 }
216 
218 {
219  this->m_ResolutionOverrideHeight = height;
220  this->m_ResolutionOverrideWidth = width;
221 
222  if (m_VideoCapture != nullptr)
223  {
224  m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, width);
225  m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, height);
226  }
227 }
Defines a region of interest by distances to the four image borders.
DataCollection - Class to facilitate loading/accessing structured data.
Defines a region of interest by top left and bottom right corner.
virtual void GetNextRawImage(cv::Mat &image) override
Next image is gathered from the image source.
bool GetIsReady()
Returns true if images can be delivered.
void SetVideoFileInput(std::string path)
Opens a video file for streaming. If nothing goes wrong, the VideoSource is ready to deliver images a...
void OverrideResolution(int width, int height)
This is a workaround for a problem that happens with some video device drivers.
void SetCameraInput(int deviceID)
Opens a video device for streaming. Takes the Device id. Try -1 for "grab the first you can get" whic...
void SetColorOutput(bool isColor)
Sets the output image to rgb or grayscale. Output is color by default and can be set to color by pass...
void SetCropping(USImageCropping cropping)
Defines the cropping area. The rectangle will be justified to the image borders if the given rectangl...
void SetRegionOfInterest(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY)
Defines the cropping area. The rectangle will be justified to the image borders if the given rectangl...
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.