Medical Imaging Interaction Toolkit  2018.4.99-08619e4f
Medical Imaging Interaction Toolkit
mitkMovieGeneratorOpenCV.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 
14 //#include <GL/gl.h>
15 #include "mitkGL.h"
16 #include <iostream>
17 
18 
20 {
21  m_initialized = false;
22  m_aviWriter = nullptr;
23  m_dwRate = 20;
24 
25  m_FourCCCodec = nullptr;
26  m_RemoveColouredFrame = true;
27 }
28 
29 
30 void mitk::MovieGeneratorOpenCV::SetFileName( const char *fileName )
31 {
32 
33  m_sFile = fileName;
34 }
35 
37 {
38  m_dwRate = static_cast<int>(rate);
39 }
40 
42 {
43  m_RemoveColouredFrame = RemoveColouredFrame;
44 }
45 
47 {
48  m_width = m_renderer->GetRenderWindow()->GetSize()[0]; // changed from glGetIntegerv( GL_VIEWPORT, viewport );
49  m_height = m_renderer->GetRenderWindow()->GetSize()[1]; // due to sometimes strange dimensions
50 
51  if(m_RemoveColouredFrame)
52  {
53  m_width -= 10; //remove colored boarders around renderwindows
54  m_height -= 10;
55  }
56 
57  m_width -= m_width % 4; // some video codecs have prerequisites to the image dimensions
58  m_height -= m_height % 4;
59 
60  m_currentFrame = cvCreateImage(cvSize(m_width,m_height),8,3); // creating image with widget size, 8 bit per pixel and 3 channel r,g,b
61  m_currentFrame->origin = 1; // avoid building a video with bottom up
62 
63  /*
64  m_sFile = Name of the output video file.
65  CV_FOURCC = 4-character code of codec used to compress the frames. For example, CV_FOURCC('P','I','M','1') is MPEG-1 codec,
66  CV_FOURCC('M','J','P','G') is motion-jpeg codec etc.
67  CV_FOURCC('P','I','M','1') = MPEG-1 codec
68  CV_FOURCC('M','J','P','G') = motion-jpeg codec (does not work well)
69  CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
70  CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
71  CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
72  CV_FOURCC('U', '2', '6', '3') = H263 codec
73  CV_FOURCC('I', '2', '6', '3') = H263I codec
74  CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
75 
76  List of FOURCC codes is available at http://msdn2.microsoft.com/en-us/library/ms867195.aspx
77 
78  Under Win32 it is possible to pass -1 in order to choose compression
79  method and additional compression parameters from dialog.
80  m_dwRate = Framerate of the created video stream.
81  frame_size Size of video frames. InitGenerator
82  1 = If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames
83  (the flag is currently supported on Windows only).*/
84 
85  if(m_FourCCCodec != nullptr)
86  {
87  #ifdef WIN32
88  m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),CV_FOURCC(m_FourCCCodec[0],m_FourCCCodec[1],m_FourCCCodec[2],
89  m_FourCCCodec[3]),m_dwRate,cvSize(m_width,m_height),1); //initializing video writer
90  #else
91  m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),CV_FOURCC(m_FourCCCodec[0],m_FourCCCodec[1],m_FourCCCodec[2],
92  m_FourCCCodec[3]),m_dwRate,cvSize(m_width,m_height)); //initializing video writer
93  #endif
94  }
95  else
96  {
97  #ifdef WIN32
98  m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),-1,m_dwRate,cvSize(m_width,m_height),1); //initializing video writer
99  #else
100  m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),CV_FOURCC('X','V','I','D'),m_dwRate,cvSize(m_width,m_height)); //initializing video writer
101  #endif
102  }
103 
104 
105  if(!m_aviWriter)
106  {
107  std::cout << "errors initializing video writer...correct video file path? on linux: ffmpeg must be included in OpenCV.";
108  return false;
109  }
110 
111  return true;
112 }
113 
114 
116 {
117  //cvSetImageData(m_currentFrame,data,m_width*3);
118  memcpy(m_currentFrame->imageData,data,m_width*m_height*3);
119  cvWriteFrame(m_aviWriter,m_currentFrame);
120  return true;
121 }
122 
123 
125 {
126  if (m_aviWriter)
127  {
128  cvReleaseVideoWriter(&m_aviWriter);
129  }
130  return true;
131 }
void SetFrameRate(unsigned int rate) override
bool InitGenerator() override
called directly before the first frame is added
void SetFileName(const char *fileName) override
filename under which movie is saved
std::string m_sFile
name of output file
bool AddFrame(void *data) override
used to add a frame
bool TerminateGenerator() override
called after the last frame is added
vtkRenderWindow * GetRenderWindow() const
Access the RenderWindow into which this renderer renders.
int m_width
InitGenerator() stores movie size in those variables.
BaseRenderer * m_renderer
stores the uses renderer