Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes

mitk::OpenCVVideoSource Class Reference

#include <mitkOpenCVVideoSource.h>

Inheritance diagram for mitk::OpenCVVideoSource:
Inheritance graph
[legend]
Collaboration diagram for mitk::OpenCVVideoSource:
Collaboration graph
[legend]

List of all members.

Public Types

typedef OpenCVVideoSource Self
typedef itk::Object Superclass
typedef itk::SmartPointer< SelfPointer
typedef itk::SmartPointer
< const Self
ConstPointer

Public Member Functions

virtual const char * GetClassName () const
virtual void SetVideoFileInput (const char *filename, bool repeatVideo, bool useCVCAMLib=false)
virtual void SetVideoCameraInput (int cameraindex, bool useCVCAMLib=false)
virtual double GetVideoCaptureProperty (int property_id)
virtual int SetVideoCaptureProperty (int property_id, double value)
virtual void GetCurrentFrameAsOpenCVImage (IplImage *image)
const IplImage * GetImage ()
const IplImage * GetCurrentFrame ()
virtual void GetCurrentFrameAsItkHSVPixelImage (HSVPixelImageType::Pointer &Image)
virtual void FetchFrame ()
virtual unsigned char * GetVideoTexture ()
virtual void StartCapturing ()
virtual void StopCapturing ()
virtual IplImage * RotateImage (IplImage *input)
virtual void EnableOnlineImageUndistortion (mitk::Point3D focal, mitk::Point3D principal, mitk::Point4D distortion)
virtual void DisableOnlineImageUndistortion ()
virtual bool OnlineImageUndistortionEnabled () const
virtual void PauseCapturing ()
virtual bool GetCapturePaused () const
virtual std::string GetVideoFileName () const
virtual short GetGrabbingDeviceNumber () const
virtual bool GetRepeatVideo ()
virtual void SetRepeatVideo (bool _arg)

Static Public Member Functions

static Pointer New ()

Protected Member Functions

 OpenCVVideoSource ()
virtual ~OpenCVVideoSource ()
void Reset ()
void UpdateVideoTexture ()
void sleep (unsigned int ms)
void RGBtoHSV (float r, float g, float b, float &h, float &s, float &v)

Protected Attributes

CvCapture * m_VideoCapture
IplImage * m_CurrentImage
unsigned char * m_CurrentVideoTexture
IplImage * m_PauseImage
bool m_CapturePaused
std::string m_VideoFileName
short m_GrabbingDeviceNumber
bool m_RepeatVideo
bool m_UseCVCAMLib
bool m_UndistortImage
mitk::UndistortCameraImage::Pointer m_UndistortCameraImage

Detailed Description

Interface for acquiring video data using Intel's OPENCV library. Video data may either be provided from a file or a grabbing device. At the moment, OPENCV includes two separated modules for this grabbing, but only HighGui is used here. Initialize via SetVideoFileInput() or SetVideoCameraInput(), start processing with StartCapturing();

Definition at line 44 of file mitkOpenCVVideoSource.h.


Member Typedef Documentation

typedef itk::SmartPointer<const Self> mitk::OpenCVVideoSource::ConstPointer

Reimplemented from mitk::VideoSource.

Definition at line 55 of file mitkOpenCVVideoSource.h.

typedef itk::SmartPointer<Self> mitk::OpenCVVideoSource::Pointer

Reimplemented from mitk::VideoSource.

Definition at line 55 of file mitkOpenCVVideoSource.h.

Reimplemented from mitk::VideoSource.

Definition at line 55 of file mitkOpenCVVideoSource.h.

Reimplemented from mitk::VideoSource.

Definition at line 55 of file mitkOpenCVVideoSource.h.


Constructor & Destructor Documentation

mitk::OpenCVVideoSource::OpenCVVideoSource (  ) [protected]
mitk::OpenCVVideoSource::~OpenCVVideoSource (  ) [protected, virtual]

Definition at line 35 of file mitkOpenCVVideoSource.cpp.

{
  this->Reset();
}

Member Function Documentation

void mitk::OpenCVVideoSource::DisableOnlineImageUndistortion (  ) [virtual]

Definition at line 261 of file mitkOpenCVVideoSource.cpp.

{
  m_UndistortImage = false;
}
void mitk::OpenCVVideoSource::EnableOnlineImageUndistortion ( mitk::Point3D  focal,
mitk::Point3D  principal,
mitk::Point4D  distortion 
) [virtual]

Definition at line 247 of file mitkOpenCVVideoSource.cpp.

References mitk::UndistortCameraImage::New().

{
  // Initialize Undistortion
  m_UndistortImage = true;
  float kc[4];
  kc[0] = distortion[0]; kc[1] = distortion[1];
  kc[2] = distortion[2]; kc[3] = distortion[3];
  if(m_CaptureWidth == 0 || m_CaptureHeight == 0)
    FetchFrame();

  m_UndistortCameraImage = mitk::UndistortCameraImage::New();
  m_UndistortCameraImage->SetUndistortImageFastInfo(focal[0], focal[1], principal[0], principal[1],  kc, (float)m_CaptureWidth, (float)m_CaptureHeight);
}
void mitk::OpenCVVideoSource::FetchFrame (  ) [virtual]

Reimplemented from mitk::VideoSource.

Definition at line 125 of file mitkOpenCVVideoSource.cpp.

References MITK_DEBUG.

{ // main procedure for updating video data
  if(m_CapturingInProcess)
  {
    if(m_VideoCapture) // we use highgui
    {
      if(!m_CapturePaused)
      {
        // release old image here
        m_CurrentImage = cvQueryFrame(m_VideoCapture);

        ++m_FrameCount;
      }

      if(m_CurrentImage == NULL) // do we need to repeat the video if it is from video file?
      {
        double framePos = this->GetVideoCaptureProperty(CV_CAP_PROP_POS_AVI_RATIO);
        MITK_DEBUG << "End of video file found. framePos: " << framePos;
        if(m_RepeatVideo &&  framePos >= 0.99)
        {
          MITK_DEBUG << "Restarting video file playback.";
          this->SetVideoCaptureProperty(CV_CAP_PROP_POS_AVI_RATIO, 0);
          m_CurrentImage = cvQueryFrame(m_VideoCapture);
        }
      }
      if(m_CaptureWidth == 0 || m_CaptureHeight == 0)
      {
        MITK_DEBUG << "Trying to set m_CaptureWidth & m_CaptureHeight.";
        m_CaptureWidth  = m_CurrentImage->width;
        m_CaptureHeight = m_CurrentImage->height;
        std::cout << "frame width: " << m_CaptureWidth << ", height: " << m_CaptureHeight << std::endl;
        m_CurrentImage->origin = 0;
      }
    }
  }
}
virtual bool mitk::OpenCVVideoSource::GetCapturePaused (  ) const [virtual]
virtual const char* mitk::OpenCVVideoSource::GetClassName (  ) const [virtual]

Reimplemented from mitk::VideoSource.

const IplImage * mitk::OpenCVVideoSource::GetCurrentFrame (  )

Definition at line 108 of file mitkOpenCVVideoSource.cpp.

Referenced by QmitkOpenCVVideoControls::NewFrameAvailable().

{
  return m_CurrentImage;
}
void mitk::OpenCVVideoSource::GetCurrentFrameAsItkHSVPixelImage ( HSVPixelImageType::Pointer &  Image ) [virtual]

Definition at line 267 of file mitkOpenCVVideoSource.cpp.

{
  FetchFrame();

  // Prepare iteration
  HSVConstIteratorType itImage( Image, Image->GetLargestPossibleRegion());
  itImage.Begin();
  HSVPixelType pixel;
  int rowsize = 3 * m_CaptureWidth;

  char* bufferend;
  char* picture;
  picture = this->m_CurrentImage->imageData;
  bufferend = this->m_CurrentImage->imageData + 3*(m_CaptureHeight*m_CaptureWidth);

  float r,g,b,h,s,v;
  try
  {
    // we have to flip the image
    for(char* datapointer = bufferend - rowsize;datapointer >= picture; datapointer -= rowsize)
    {
      for(char* current = datapointer; current < datapointer + rowsize; current++)
      {

          b = *current; current++;
          g = *current; current++;
          r = *current;
          RGBtoHSV(r,g,b,h,s,v);
          pixel[0] = h;
          pixel[1] = s;
          pixel[2] = v;

          itImage.Set(pixel);
          ++itImage;
      }
    }
  }
  catch( ... )
  {
    std::cout << "Exception raised mitkOpenCVVideoSource: get hsv itk image conversion error." << std::endl;
  }

}
void mitk::OpenCVVideoSource::GetCurrentFrameAsOpenCVImage ( IplImage *  image ) [virtual]

Definition at line 113 of file mitkOpenCVVideoSource.cpp.

{ // get last captured frame for processing the image data
  if(m_CurrentImage)
  {
    if(image)
    {
      image->origin = m_CurrentImage->origin;
      memcpy(image->imageData,m_CurrentImage->imageData,m_CurrentImage->width*m_CurrentImage->height*m_CurrentImage->nChannels);
    }
  }
}
virtual short mitk::OpenCVVideoSource::GetGrabbingDeviceNumber (  ) const [virtual]

Returns the GrabbingDeviceNumber (maybe -1 if a video file is used)

Referenced by QmitkOpenCVVideoControls::SetVideoBackground().

const IplImage * mitk::OpenCVVideoSource::GetImage ( void   ) [virtual]

Return the current frame

Implements mitk::OpenCVImageSource.

Definition at line 103 of file mitkOpenCVVideoSource.cpp.

{
  return m_CurrentImage;
}
virtual bool mitk::OpenCVVideoSource::GetRepeatVideo (  ) [virtual]
double mitk::OpenCVVideoSource::GetVideoCaptureProperty ( int  property_id ) [virtual]

Definition at line 66 of file mitkOpenCVVideoSource.cpp.

Referenced by QmitkOpenCVVideoControls::NewFrameAvailable(), and QmitkOpenCVVideoControls::on_VideoProgressSlider_sliderPressed().

{
  return cvGetCaptureProperty(m_VideoCapture, property_id);
}
virtual std::string mitk::OpenCVVideoSource::GetVideoFileName (  ) const [virtual]

Returns the video file name (maybe empty if a grabbing device is used)

Referenced by QmitkOpenCVVideoControls::SetVideoBackground().

unsigned char * mitk::OpenCVVideoSource::GetVideoTexture (  ) [virtual]

Reimplemented from mitk::VideoSource.

Definition at line 77 of file mitkOpenCVVideoSource.cpp.

{ // Fetch Frame and return pointer to opengl texture
  FetchFrame();

  if (m_RotationEnabled)
  {
    //store pointer to release memory for the image after rotation
    IplImage * tmpImage = m_CurrentImage;

    //rotate the image to get a static video
    m_CurrentImage = this->RotateImage(m_CurrentImage);

    // release memory
    cvReleaseImage(&tmpImage); //
  }

  //transfer the image to a texture
  this->UpdateVideoTexture();
  return this->m_CurrentVideoTexture;
  this->Modified();
}
static Pointer mitk::OpenCVVideoSource::New (  ) [static]

Reimplemented from mitk::VideoSource.

Referenced by QmitkVideoPlayer::CreateQtPartControl().

bool mitk::OpenCVVideoSource::OnlineImageUndistortionEnabled (  ) const [virtual]
Returns:
true if image undistorsion is enabled

Definition at line 220 of file mitkOpenCVVideoSource.cpp.

void mitk::OpenCVVideoSource::PauseCapturing (  ) [virtual]
void mitk::OpenCVVideoSource::Reset (  ) [protected]

Resets the whole class for capturing from a new device

Definition at line 383 of file mitkOpenCVVideoSource.cpp.

{
  // set capturing to false
  this->StopCapturing();
  if(m_VideoCapture)
    cvReleaseCapture(&m_VideoCapture);
  m_VideoCapture = 0;
  m_CurrentImage = 0;
  m_CaptureWidth = 0;
  m_CaptureHeight = 0;
  delete m_CurrentVideoTexture;
  m_CurrentVideoTexture = 0;
  if(m_PauseImage)
    cvReleaseImage(&m_PauseImage);
  m_PauseImage = 0;
  m_CapturePaused = false;
  m_VideoFileName.clear();
  m_GrabbingDeviceNumber = -1;
  // do not touch repeat video
  //m_RepeatVideo = false;
  m_UseCVCAMLib = false;
  // do not touch undistort settings
  // bool m_UndistortImage;
}
void mitk::OpenCVVideoSource::RGBtoHSV ( float  r,
float  g,
float  b,
float &  h,
float &  s,
float &  v 
) [protected]

Definition at line 311 of file mitkOpenCVVideoSource.cpp.

{
    if(r > 1.0)
      r = r/255;
    if(b > 1.0)
      b = b/255;
    if(g > 1.0)
      g = g/255;

    float mn=r,mx=r;
    int maxVal=0;

    if (g > mx){ mx=g;maxVal=1;}
    if (b > mx){ mx=b;maxVal=2;}
    if (g < mn) mn=g;
    if (b < mn) mn=b;

    float  delta = mx - mn;

    v = mx;
    if( mx != 0 )
      s = delta / mx;
    else
    {
      s = 0;
      h = 0;
      return;
    }
    if (s==0.0f)
    {
      h=-1;
      return;
    }
    else
    {
      switch (maxVal)
      {
      case 0:{h = ( g - b ) / delta;break;}         // yel < h < mag
      case 1:{h = 2 + ( b - r ) / delta;break;}     // cyan < h < yel
      case 2:{h = 4 + ( r - g ) / delta;break;}     // mag < h < cyan
      }
    }

    h *= 60;
    if( h < 0 ) h += 360;
}
IplImage * mitk::OpenCVVideoSource::RotateImage ( IplImage *  input ) [virtual]

Definition at line 362 of file mitkOpenCVVideoSource.cpp.

{
  if(input == NULL)
  { //warn the user and quit
    std::cout<<"openCVVideoSource: Current video image is null! "<< std::endl;
    return input;
  }
  IplImage* dst = cvCloneImage( input );
  double angle =  this->GetRotationAngle(); //degree
  CvPoint2D32f centre;
  CvMat *translate = cvCreateMat(2, 3, CV_32FC1);
  cvSetZero(translate);
  centre.x =   m_CaptureWidth/2;
  centre.y = m_CaptureHeight/2;
  cv2DRotationMatrix(centre, angle, 1.0, translate);
  cvWarpAffine(input, dst, translate,CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS , cvScalarAll(0));
  cvReleaseMat(&translate);

  return dst;
}
virtual void mitk::OpenCVVideoSource::SetRepeatVideo ( bool  _arg ) [virtual]
void mitk::OpenCVVideoSource::SetVideoCameraInput ( int  cameraindex,
bool  useCVCAMLib = false 
) [virtual]

Definition at line 54 of file mitkOpenCVVideoSource.cpp.

References MITK_ERROR.

Referenced by QmitkOpenCVVideoControls::on_PlayButton_clicked().

{
  this->Reset();

  m_GrabbingDeviceNumber = cameraindex;
  m_VideoCapture = cvCaptureFromCAM(m_GrabbingDeviceNumber);
  if(!m_VideoCapture)
    MITK_ERROR << "Error in initializing CVHighGUI video camera!"<< std::endl;

  this->Modified();
}
int mitk::OpenCVVideoSource::SetVideoCaptureProperty ( int  property_id,
double  value 
) [virtual]

Definition at line 71 of file mitkOpenCVVideoSource.cpp.

Referenced by QmitkOpenCVVideoControls::on_VideoProgressSlider_sliderReleased().

{
  return cvSetCaptureProperty(m_VideoCapture, property_id, value);
}
void mitk::OpenCVVideoSource::SetVideoFileInput ( const char *  filename,
bool  repeatVideo,
bool  useCVCAMLib = false 
) [virtual]

Definition at line 40 of file mitkOpenCVVideoSource.cpp.

References MITK_WARN.

Referenced by QmitkOpenCVVideoControls::on_PlayButton_clicked().

{
  this->Reset();
  m_VideoFileName = filename;

  m_VideoCapture = cvCaptureFromFile(filename);
  if(!m_VideoCapture)
    MITK_WARN << "Error in initializing video file input!";

  m_RepeatVideo  = repeatVideo;
  //m_CurrentImage        = cvCreateImage(cvSize(m_CaptureWidth,m_CaptureHeight),8,3);
  this->Modified();

}
void mitk::OpenCVVideoSource::sleep ( unsigned int  ms ) [protected]
void mitk::OpenCVVideoSource::StartCapturing (  ) [virtual]

Reimplemented from mitk::VideoSource.

Definition at line 207 of file mitkOpenCVVideoSource.cpp.

Referenced by QmitkOpenCVVideoControls::on_PlayButton_clicked().

{
  if(m_VideoCapture != NULL)
    m_CapturingInProcess = true;
  else
    m_CapturingInProcess = false;
}
void mitk::OpenCVVideoSource::StopCapturing (  ) [virtual]

Reimplemented from mitk::VideoSource.

Definition at line 215 of file mitkOpenCVVideoSource.cpp.

Referenced by QmitkOpenCVVideoControls::Stop().

{
  m_CapturingInProcess = false;
}
void mitk::OpenCVVideoSource::UpdateVideoTexture (  ) [protected]

Definition at line 162 of file mitkOpenCVVideoSource.cpp.

{  //write the grabbed frame into an opengl compatible array, that means flip it and swap channel order
  if(!m_CurrentImage)
    return;

  if(m_CurrentVideoTexture == NULL)
    m_CurrentVideoTexture = new unsigned char[m_CaptureWidth*m_CaptureHeight*3];

  // only undistort if not paused
  if(m_UndistortImage && !m_CapturePaused)
    m_UndistortCameraImage->UndistortImageFast(m_CurrentImage, 0);

  int width = m_CurrentImage->width;
  int height = m_CurrentImage->height;
  int widthStep = m_CurrentImage->widthStep;
  int nChannels = m_CurrentImage->nChannels;
  unsigned char* tex = m_CurrentVideoTexture;
  char* data = m_CurrentImage->imageData;
  char* currentData = m_CurrentImage->imageData;

  int hIndex=0;
  int wIndex=0;
  int iout,jout;

  for(int i=0;i<width*height*3;i+=3,++wIndex)
  {
    if(wIndex >= width)
    {
      wIndex=0;
      hIndex++;
    }

    // vertically flip the image
    iout = -hIndex+height-1;
    jout = wIndex;

    currentData = data + iout*widthStep;

    tex[i+2] = currentData[jout*nChannels + 0]; // B
    tex[i+1] = currentData[jout*nChannels + 1]; // G
    tex[i] = currentData[jout*nChannels + 2]; // R
  }

}

Member Data Documentation

Definition at line 168 of file mitkOpenCVVideoSource.h.

Definition at line 164 of file mitkOpenCVVideoSource.h.

Reimplemented from mitk::VideoSource.

Definition at line 165 of file mitkOpenCVVideoSource.h.

saves the grabbing device number (is -1 if a videofilename is used or if this is not initialized)

Definition at line 176 of file mitkOpenCVVideoSource.h.

Definition at line 167 of file mitkOpenCVVideoSource.h.

Definition at line 179 of file mitkOpenCVVideoSource.h.

Definition at line 186 of file mitkOpenCVVideoSource.h.

Definition at line 185 of file mitkOpenCVVideoSource.h.

Definition at line 182 of file mitkOpenCVVideoSource.h.

Definition at line 161 of file mitkOpenCVVideoSource.h.

saves the video file name (is empty if a grabbing device is used or if this is not initialized)

Definition at line 172 of file mitkOpenCVVideoSource.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines