Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (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 // MITK HEADER
14 #include "mitkUSImageVideoSource.h"
15 #include "mitkImage.h"
16 
17 //Other
18 #include <cstdio>
19 #include <highgui.h>
20 
22  : m_VideoCapture(new cv::VideoCapture()),
23  m_IsVideoReady(false),
24  m_IsGreyscale(false),
25  m_IsCropped(false),
26  m_ResolutionOverrideWidth(0),
27  m_ResolutionOverrideHeight(0),
28  m_ResolutionOverride(false),
29  m_GrayscaleFilter(mitk::ConvertGrayscaleOpenCVImageFilter::New()),
30  m_CropFilter(mitk::CropOpenCVImageFilter::New())
31 {
32 }
33 
35 {
36  m_VideoCapture->release();
37  delete m_VideoCapture;
38 }
39 
41 {
42  m_VideoCapture->open(path.c_str());
43 
44  // check if we succeeded
45  if(!m_VideoCapture->isOpened()) { m_IsVideoReady = false; }
46  else { m_IsVideoReady = true; }
47 
48  // if Override is enabled, use it
50  {
51  m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, this->m_ResolutionOverrideWidth);
52  m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, this->m_ResolutionOverrideHeight);
53  }
54 }
55 
57 {
58  m_VideoCapture->open(deviceID);
59  if(!m_VideoCapture->isOpened()) // check if we succeeded
60  m_IsVideoReady = false;
61  else
62  m_IsVideoReady = true;
63 
64  // if Override is enabled, use it
66  {
67  m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, this->m_ResolutionOverrideWidth);
68  m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, this->m_ResolutionOverrideHeight);
69  }
70 }
71 
73 {
74  m_VideoCapture->release();
75  delete m_VideoCapture;
76  m_VideoCapture = new cv::VideoCapture();
77 }
78 
80  if ( ! isColor && ! m_IsGreyscale )
81  {
82  this->PushFilter(m_GrayscaleFilter.GetPointer());
83  }
84  else if ( isColor && m_IsGreyscale )
85  {
86  this->RemoveFilter(m_GrayscaleFilter.GetPointer());
87  }
88 
89  m_IsGreyscale = !isColor;
90 }
91 
93 {
94  if (m_VideoCapture) { return m_VideoCapture->get(CV_CAP_PROP_FRAME_HEIGHT); }
95  else { return 0; }
96 }
97 
99 {
100  if (m_VideoCapture) { return m_VideoCapture->get(CV_CAP_PROP_FRAME_WIDTH); }
101  else { return 0; }
102 }
103 
105 {
106  if (!m_VideoCapture) { return false; }
107 
108  return m_VideoCapture->isOpened();
109 }
110 
111 void mitk::USImageVideoSource::SetRegionOfInterest(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY)
112 {
113  m_CropFilter->SetCropRegion(topLeftX, topLeftY, bottomRightX, bottomRightY);
114 
115  if (! m_IsCropped && ! m_CropFilter->GetIsCropRegionEmpty())
116  {
117  this->PushFilter(m_CropFilter.GetPointer());
118  m_IsCropped = true;
119  }
120 }
121 
123 {
125 }
126 
128 {
129  int width = this->GetImageWidth();
130  int height = this->GetImageHeight();
131 
132  this->SetRegionOfInterest(cropping.left, cropping.top, width - cropping.right, height - cropping.bottom);
133 }
134 
136 {
137  cv::Rect cropRect = m_CropFilter->GetCropRegion();
138 
139  USImageCropping cropping;
140  cropping.left = cropRect.x;
141  cropping.top = cropRect.y;
142 
143  if ( cropRect.height == 0 )
144  {
145  cropping.bottom = 0;
146  }
147  else
148  {
149  cropping.bottom = this->GetImageHeight() - (cropRect.y + cropRect.height);
150  }
151 
152  if ( cropRect.width == 0 )
153  {
154  cropping.right = 0;
155  }
156  else
157  {
158  cropping.right = this->GetImageWidth() - (cropRect.x + cropRect.width);
159  }
160 
161  return cropping;
162 }
163 
165 {
166  cv::Rect cropRect = m_CropFilter->GetCropRegion();
167 
168  return USImageRoi(cropRect.x, cropRect.y, cropRect.x + cropRect.width, cropRect.y + cropRect.height);
169 }
170 
172 {
173  this->RemoveFilter(m_CropFilter.GetPointer());
174  m_IsCropped = false;
175 }
176 
178 {
179  // loop video if necessary
180  //Commented out because setting and getting of these properties is not supported. Therefore on Linux
181  //you'll always get some Highgui errors from OpenCV
182  /*if (m_VideoCapture->get(CV_CAP_PROP_POS_FRAMES) == m_VideoCapture->get(CV_CAP_PROP_FRAME_COUNT))
183  {
184  m_VideoCapture->set(CV_CAP_PROP_POS_FRAMES, 0);
185  }*/
186 
187  if (image.size() != 1)
188  image.resize(1);
189 
190  // retrieve image
191  *m_VideoCapture >> image[0]; // get a new frame from camera
192 }
193 
194 void mitk::USImageVideoSource::GetNextRawImage(std::vector<mitk::Image::Pointer>& image )
195 {
196  if (image.size() != 1)
197  image.resize(1);
198 
199  std::vector<cv::Mat> cv_img;
200 
201  this->GetNextRawImage(cv_img);
202 
203  // convert to MITK-Image
204  IplImage ipl_img = cv_img[0];
205 
206  this->m_OpenCVToMitkFilter->SetOpenCVImage(&ipl_img);
207  this->m_OpenCVToMitkFilter->Update();
208 
209  // OpenCVToMitkImageFilter returns a standard mitk::image. We then transform it into an USImage
210  image[0] = this->m_OpenCVToMitkFilter->GetOutput();
211 
212  // clean up
213  cv_img[0].release();
214 }
215 
217 {
218  this->m_ResolutionOverrideHeight = height;
219  this->m_ResolutionOverrideWidth = width;
220 
221  if (m_VideoCapture != nullptr)
222  {
223  m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, width);
224  m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, height);
225  }
226 }
Defines a region of interest by distances to the four image borders.
ConvertGrayscaleOpenCVImageFilter::Pointer m_GrayscaleFilter
mitk::OpenCVToMitkImageFilter::Pointer m_OpenCVToMitkFilter
Used to convert from OpenCV Images to MITK Images.
cv::VideoCapture * m_VideoCapture
The source of the video, managed internally.
DataCollection - Class to facilitate loading/accessing structured data.
CropOpenCVImageFilter::Pointer m_CropFilter
Defines a region of interest by top left and bottom right corner.
void GetNextRawImage(std::vector< cv::Mat > &image) override
Next image is gathered from the image source.
bool RemoveFilter(AbstractOpenCVImageFilter::Pointer filter)
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.
mitk::Image::Pointer image
void SetCameraInput(int deviceID)
Opens a video device for streaming. Takes the Device id. Try -1 for "grab the first you can get" whic...
bool m_IsVideoReady
If true, a frame can be grabbed anytime.
void PushFilter(AbstractOpenCVImageFilter::Pointer filter)
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...
bool m_IsGreyscale
If true, image output will be greyscale.
bool m_IsCropped
If true, image will be cropped according to settings of crop filter.