22 : m_VideoCapture(nullptr),
23 m_CurrentImage(nullptr),
24 m_CurrentVideoTexture(nullptr),
25 m_PauseImage(nullptr),
26 m_GrabbingDeviceNumber(-1),
29 m_UndistortImage(false),
30 m_FlipXAxisEnabled(false),
31 m_FlipYAxisEnabled(false)
45 m_VideoCapture = cvCaptureFromFile(filename);
47 MITK_WARN <<
"Error in initializing video file input!";
49 m_RepeatVideo = repeatVideo;
58 m_GrabbingDeviceNumber = cameraindex;
59 m_VideoCapture = cvCaptureFromCAM(m_GrabbingDeviceNumber);
61 MITK_ERROR <<
"Error in initializing CVHighGUI video camera!"<< std::endl;
68 return cvGetCaptureProperty(m_VideoCapture, property_id);
73 return cvSetCaptureProperty(m_VideoCapture, property_id, value);
81 if (m_FlipXAxisEnabled || m_FlipYAxisEnabled)
84 m_CurrentImage = this->FlipImage(m_CurrentImage);
89 this->UpdateVideoTexture();
90 return this->m_CurrentVideoTexture;
98 cv::Mat copy( m_CurrentImage,
false );
106 return m_CurrentImage;
115 image->origin = m_CurrentImage->origin;
116 memcpy(image->imageData,m_CurrentImage->imageData,m_CurrentImage->width*m_CurrentImage->height*m_CurrentImage->nChannels);
123 if(m_CapturingInProcess)
130 m_CurrentImage = cvQueryFrame(m_VideoCapture);
134 if(m_CurrentImage ==
nullptr)
136 double framePos = this->GetVideoCaptureProperty(CV_CAP_PROP_POS_AVI_RATIO);
137 MITK_DEBUG <<
"End of video file found. framePos: " << framePos;
138 if(m_RepeatVideo && framePos >= 0.99)
140 MITK_DEBUG <<
"Restarting video file playback.";
141 this->SetVideoCaptureProperty(CV_CAP_PROP_POS_AVI_RATIO, 0);
143 m_CurrentImage = cvQueryFrame(m_VideoCapture);
147 std::ostringstream s;
148 s <<
"End of video file " << m_VideoFileName;
149 std::logic_error err( s.str() );
156 if(m_UndistortImage && m_UndistortCameraImage.IsNotNull())
157 m_UndistortCameraImage->UndistortImageFast(m_CurrentImage,
nullptr);
160 if(m_CaptureWidth == 0 || m_CaptureHeight == 0)
162 MITK_DEBUG <<
"Trying to set m_CaptureWidth & m_CaptureHeight.";
163 m_CaptureWidth = m_CurrentImage->width;
164 m_CaptureHeight = m_CurrentImage->height;
165 MITK_INFO <<
"frame width: " << m_CaptureWidth <<
", height: " << m_CaptureHeight;
166 m_CurrentImage->origin = 0;
177 if(m_CurrentVideoTexture ==
nullptr)
178 m_CurrentVideoTexture =
new unsigned char[m_CaptureWidth*m_CaptureHeight*3];
180 int width = m_CurrentImage->width;
181 int height = m_CurrentImage->height;
182 int widthStep = m_CurrentImage->widthStep;
183 int nChannels = m_CurrentImage->nChannels;
184 unsigned char* tex = m_CurrentVideoTexture;
185 char* data = m_CurrentImage->imageData;
186 char* currentData = m_CurrentImage->imageData;
192 for(
int i=0;i<width*height*3;i+=3,++wIndex)
201 iout = -hIndex+height-1;
204 currentData = data + iout*widthStep;
206 tex[i+2] = currentData[jout*nChannels + 0];
207 tex[i+1] = currentData[jout*nChannels + 1];
208 tex[i] = currentData[jout*nChannels + 2];
215 if(m_VideoCapture !=
nullptr)
216 m_CapturingInProcess =
true;
218 m_CapturingInProcess =
false;
223 m_CapturingInProcess =
false;
228 return m_UndistortCameraImage;
233 m_CapturePaused = !m_CapturePaused;
237 m_PauseImage = cvCloneImage(m_CurrentImage);
241 m_UndistortCameraImage->UndistortImageFast(m_PauseImage,
nullptr);
242 m_CurrentImage = m_PauseImage;
246 cvReleaseImage( &m_PauseImage );
247 m_CurrentImage =
nullptr;
248 m_PauseImage =
nullptr;
255 m_UndistortImage =
true;
257 kc[0] = distortion[0]; kc[1] = distortion[1];
258 kc[2] = distortion[2]; kc[3] = distortion[3];
259 if(m_CaptureWidth == 0 || m_CaptureHeight == 0)
263 m_UndistortCameraImage->SetUndistortImageFastInfo(focal[0], focal[1], principal[0], principal[1], kc, (
float)m_CaptureWidth, (
float)m_CaptureHeight);
268 m_UndistortImage =
false;
280 int rowsize = 3 * m_CaptureWidth;
284 picture = this->m_CurrentImage->imageData;
285 bufferend = this->m_CurrentImage->imageData + 3*(m_CaptureHeight*m_CaptureWidth);
291 for(
char* datapointer = bufferend - rowsize;datapointer >= picture; datapointer -= rowsize)
293 for(
char* current = datapointer; current < datapointer + rowsize; current++)
295 b = *current; current++;
296 g = *current; current++;
298 RGBtoHSV(r,g,b,h,s,v);
310 std::cout <<
"Exception raised mitkOpenCVVideoSource: get hsv itk image conversion error." << std::endl;
327 if (g > mx){ mx=g;maxVal=1;}
328 if (b > mx){ mx=b;maxVal=2;}
332 float delta = mx - mn;
352 case 0:{h = ( g - b ) / delta;
break;}
353 case 1:{h = 2 + ( b - r ) / delta;
break;}
354 case 2:{h = 4 + ( r - g ) / delta;
break;}
359 if( h < 0 ) h += 360;
370 std::cout<<
"openCVVideoSource: Current video image is null! "<< std::endl;
374 if(m_FlipXAxisEnabled && !m_FlipYAxisEnabled)
376 cvFlip(input,
nullptr,0);
378 if(!m_FlipXAxisEnabled && m_FlipYAxisEnabled)
380 cvFlip(input,
nullptr,1);
382 if(m_FlipXAxisEnabled && m_FlipYAxisEnabled)
384 cvFlip(input,
nullptr,-1);
393 this->StopCapturing();
395 cvReleaseCapture(&m_VideoCapture);
396 m_VideoCapture =
nullptr;
397 m_CurrentImage =
nullptr;
400 delete m_CurrentVideoTexture;
401 m_CurrentVideoTexture =
nullptr;
403 cvReleaseImage(&m_PauseImage);
404 m_PauseImage =
nullptr;
405 m_CapturePaused =
false;
406 m_VideoFileName.clear();
407 m_GrabbingDeviceNumber = -1;
410 m_UseCVCAMLib =
false;
417 this->m_FlipXAxisEnabled = enable;
423 this->m_FlipXAxisEnabled = enable;
virtual void SetEnableYAxisFlip(bool enable)
virtual int SetVideoCaptureProperty(int property_id, double value)
virtual void StopCapturing() override
itk::SmartPointer< Self > Pointer
virtual void GetCurrentFrameAsItkHSVPixelImage(HSVPixelImageType::Pointer &Image)
itk::ImageRegionIterator< HSVPixelImageType > HSVConstIteratorType
virtual double GetVideoCaptureProperty(int property_id)
virtual void EnableOnlineImageUndistortion(mitk::Point3D focal, mitk::Point3D principal, mitk::Point4D distortion)
virtual void StartCapturing() override
void RGBtoHSV(float r, float g, float b, float &h, float &s, float &v)
virtual void GetCurrentFrameAsOpenCVImage(IplImage *image)
virtual void SetEnableXAxisFlip(bool enable)
virtual void SetVideoFileInput(const char *filename, bool repeatVideo, bool useCVCAMLib=false)
virtual void SetVideoCameraInput(int cameraindex, bool useCVCAMLib=false)
void UpdateVideoTexture()
static const std::string filename
virtual void PauseCapturing() override
virtual void FetchFrame() override
Image class for storing images.
virtual cv::Mat GetImage() override
virtual ~OpenCVVideoSource()
virtual void DisableOnlineImageUndistortion()
virtual unsigned char * GetVideoTexture() override
virtual const IplImage * GetCurrentFrame()
virtual IplImage * FlipImage(IplImage *input)
virtual bool OnlineImageUndistortionEnabled() const
itk::FixedArray< float, 3 > HSVPixelType