19#include <opencv2/videoio/legacy/constants_c.h>
22 : m_VideoCapture(new
cv::VideoCapture()),
23 m_IsVideoReady(false),
26 m_ResolutionOverrideWidth(0),
27 m_ResolutionOverrideHeight(0),
28 m_ResolutionOverride(false),
36 m_VideoCapture->release();
37 delete m_VideoCapture;
42 m_VideoCapture->open(path.c_str());
45 if(!m_VideoCapture->isOpened()) { m_IsVideoReady =
false; }
46 else { m_IsVideoReady =
true; }
49 if (m_ResolutionOverride)
51 m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, this->m_ResolutionOverrideWidth);
52 m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, this->m_ResolutionOverrideHeight);
58 m_VideoCapture->open(deviceID);
59 if(!m_VideoCapture->isOpened())
60 m_IsVideoReady =
false;
62 m_IsVideoReady =
true;
65 if (m_ResolutionOverride)
67 m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, this->m_ResolutionOverrideWidth);
68 m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, this->m_ResolutionOverrideHeight);
74 m_VideoCapture->release();
75 delete m_VideoCapture;
76 m_VideoCapture =
new cv::VideoCapture();
80 if ( ! isColor && ! m_IsGreyscale )
82 this->PushFilter(m_GrayscaleFilter.GetPointer());
84 else if ( isColor && m_IsGreyscale )
86 this->RemoveFilter(m_GrayscaleFilter.GetPointer());
89 m_IsGreyscale = !isColor;
94 if (m_VideoCapture) {
return m_VideoCapture->get(CV_CAP_PROP_FRAME_HEIGHT); }
100 if (m_VideoCapture) {
return m_VideoCapture->get(CV_CAP_PROP_FRAME_WIDTH); }
106 if (!m_VideoCapture) {
return false; }
108 return m_VideoCapture->isOpened();
113 m_CropFilter->SetCropRegion(topLeftX, topLeftY, bottomRightX, bottomRightY);
115 if (! m_IsCropped && ! m_CropFilter->GetIsCropRegionEmpty())
117 this->PushFilter(m_CropFilter.GetPointer());
129 int width = this->GetImageWidth();
130 int height = this->GetImageHeight();
132 this->SetRegionOfInterest(cropping.
left, cropping.
top, width - cropping.
right, height - cropping.
bottom);
137 cv::Rect cropRect = m_CropFilter->GetCropRegion();
140 cropping.
left = cropRect.x;
141 cropping.
top = cropRect.y;
143 if ( cropRect.height == 0 )
149 cropping.
bottom = this->GetImageHeight() - (cropRect.y + cropRect.height);
152 if ( cropRect.width == 0 )
158 cropping.
right = this->GetImageWidth() - (cropRect.x + cropRect.width);
166 cv::Rect cropRect = m_CropFilter->GetCropRegion();
168 return USImageRoi(cropRect.x, cropRect.y, cropRect.x + cropRect.width, cropRect.y + cropRect.height);
173 this->RemoveFilter(m_CropFilter.GetPointer());
187 if (image.size() != 1)
191 *m_VideoCapture >> image[0];
196 if (image.size() != 1)
199 std::vector<cv::Mat> cv_img;
201 this->GetNextRawImage(cv_img);
204 IplImage ipl_img = cvIplImage(cv_img[0]);
206 this->m_OpenCVToMitkFilter->SetOpenCVImage(&ipl_img);
207 this->m_OpenCVToMitkFilter->Update();
210 image[0] = this->m_OpenCVToMitkFilter->GetOutput();
218 this->m_ResolutionOverrideHeight = height;
219 this->m_ResolutionOverrideWidth = width;
221 if (m_VideoCapture !=
nullptr)
223 m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, width);
224 m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, height);
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 SetCropping(USImageCropping cropping)
Defines the cropping area. The rectangle will be justified to the image borders if the given rectangl...
void OverrideResolution(int width, int height)
This is a workaround for a problem that happens with some video device drivers.
void SetVideoFileInput(std::string path)
Opens a video file for streaming. If nothing goes wrong, the VideoSource is ready to deliver images a...
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...
USImageCropping GetCropping()
void GetNextRawImage(std::vector< cv::Mat > &image) override
Next image is gathered from the image source.
bool GetIsReady()
Returns true if images can be delivered.
void RemoveRegionOfInterest()
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...
~USImageVideoSource() override
USImageRoi GetRegionOfInterest()
Defines a region of interest by distances to the four image borders.
Defines a region of interest by top left and bottom right corner.