ITK  5.2.0
Insight Toolkit
itkOpenCVVideoIO.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkOpenCVVideoIO_h
19 #define itkOpenCVVideoIO_h
20 
21 #include "itkVideoIOBase.h"
22 
23 #include "opencv2/core/version.hpp"
24 #if !defined(CV_VERSION_EPOCH)
25 // OpenCV 3.x
26 # include "opencv2/videoio/videoio.hpp"
27 # include "opencv2/videoio/videoio_c.h"
28 # include "opencv2/imgproc/imgproc_c.h"
29 #else
30 // OpenCV 2.4.x
31 # include "cv.h"
32 # include "highgui.h"
33 #endif
34 
35 #include "ITKVideoBridgeOpenCVExport.h"
36 
37 
38 namespace itk
39 {
47 class ITKVideoBridgeOpenCV_EXPORT OpenCVVideoIO : public VideoIOBase
48 {
49 public:
50  ITK_DISALLOW_COPY_AND_MOVE(OpenCVVideoIO);
51 
56 
57  using TemporalOffsetType = Superclass::TemporalOffsetType;
58  using FrameOffsetType = Superclass::FrameOffsetType;
59  using TemporalRatioType = Superclass::TemporalRatioType;
60  using CameraIDType = Superclass::CameraIDType;
61 
63  itkNewMacro(Self);
64 
66  itkTypeMacro(OpenCVVideoIO, Superclass);
67 
69  virtual void
70  FinishReadingOrWriting();
71 
72  //
73  // Data reading-related methods
74  //
75 
77  virtual void
78  SetReadFromFile();
79 
81  virtual void
82  SetReadFromCamera();
83 
86  virtual bool
87  CanReadFile(const char *);
88 
90  virtual bool
91  CanReadCamera(CameraIDType cameraID) const;
92 
94  virtual void
95  ReadImageInformation();
96 
98  virtual void
99  Read(void * buffer);
100 
103  virtual bool
104  SetNextFrameToRead(FrameOffsetType frameNumber);
105 
107  virtual TemporalOffsetType
108  GetPositionInMSec() const;
109  virtual TemporalRatioType
110  GetRatio() const;
111  virtual FrameOffsetType
112  GetFrameTotal() const;
113  virtual TemporalRatioType
114  GetFramesPerSecond() const;
115  virtual FrameOffsetType
116  GetCurrentFrame() const;
117  virtual FrameOffsetType
118  GetIFrameInterval() const;
119  virtual FrameOffsetType
120  GetLastIFrame() const;
122 
123  //
124  // Data writing-related methods
125  //
126 
128  virtual void
129  SetCameraIndex(CameraIDType idx);
130  virtual CameraIDType
131  GetCameraIndex() const;
133 
136  virtual double
137  GetSpacing(unsigned int itkNotUsed(i)) const
138  {
139  return 1.0;
140  }
141  virtual double
142  GetOrigin(unsigned int itkNotUsed(i)) const
143  {
144  return 0.0;
145  }
146  virtual std::vector<double>
147  GetDirection(unsigned int i) const
148  {
149  return this->GetDefaultDirection(i);
150  }
152 
155  virtual bool
156  CanWriteFile(const char *);
157 
160  virtual void
161  WriteImageInformation();
162 
165  virtual void
166  Write(const void * buffer);
167 
169  virtual void
170  SetWriterParameters(TemporalRatioType fps,
171  const std::vector<SizeValueType> & dim,
172  const char * fourCC,
173  unsigned int nChannels,
174  IOComponentType componentType);
175 
176 protected:
177  OpenCVVideoIO();
178  ~OpenCVVideoIO();
179 
180  void
181  PrintSelf(std::ostream & os, Indent indent) const override;
182 
184  void
185  UpdateReaderProperties();
186 
188  void
189  ResetMembers();
190 
192  void
193  OpenReader();
194 
196  void
197  OpenWriter();
198 
199 private:
200 private:
201  IplImage * m_CVImage;
202  IplImage * m_TempImage;
203  CvCapture * m_Capture;
204  CvVideoWriter * m_Writer;
205  int m_FourCC;
206 
208 };
209 } // end namespace itk
210 
211 #endif // itkOpenCVVideoIO_h
itk::OpenCVVideoIO::m_CameraIndex
int m_CameraIndex
Definition: itkOpenCVVideoIO.h:207
itk::OpenCVVideoIO
VideoIO object for reading and writing videos using OpenCV.
Definition: itkOpenCVVideoIO.h:47
itk::OpenCVVideoIO::GetOrigin
virtual double GetOrigin(unsigned int) const
Definition: itkOpenCVVideoIO.h:142
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::OpenCVVideoIO::m_CVImage
IplImage * m_CVImage
Definition: itkOpenCVVideoIO.h:201
itkVideoIOBase.h
itk::OpenCVVideoIO::GetSpacing
virtual double GetSpacing(unsigned int) const
Definition: itkOpenCVVideoIO.h:137
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::OpenCVVideoIO::GetDirection
virtual std::vector< double > GetDirection(unsigned int i) const
Definition: itkOpenCVVideoIO.h:147
itk::VideoIOBase::TemporalRatioType
double TemporalRatioType
Definition: itkVideoIOBase.h:84
itk::OpenCVVideoIO::m_Writer
CvVideoWriter * m_Writer
Definition: itkOpenCVVideoIO.h:204
itk::VideoIOBase::TemporalOffsetType
double TemporalOffsetType
Definition: itkVideoIOBase.h:82
itk::OpenCVVideoIO::m_FourCC
int m_FourCC
Definition: itkOpenCVVideoIO.h:205
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::VideoIOBase::CameraIDType
SizeValueType CameraIDType
Definition: itkVideoIOBase.h:87
itk::VideoIOBase::FrameOffsetType
SizeValueType FrameOffsetType
Definition: itkVideoIOBase.h:83
itk::OpenCVVideoIO::m_TempImage
IplImage * m_TempImage
Definition: itkOpenCVVideoIO.h:202
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::VideoIOBase
Abstract superclass defines video IO interface.
Definition: itkVideoIOBase.h:70
itk::OpenCVVideoIO::m_Capture
CvCapture * m_Capture
Definition: itkOpenCVVideoIO.h:203