Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 //#include <GL/gl.h>
19 #include "mitkGL.h"
20 #include <iostream>
21 
22 
24 {
25  m_initialized = false;
26  m_aviWriter = NULL;
27  m_dwRate = 20;
28 
29  m_FourCCCodec = NULL;
30  m_RemoveColouredFrame = true;
31 }
32 
33 
34 void mitk::MovieGeneratorOpenCV::SetFileName( const char *fileName )
35 {
36 
37  m_sFile = fileName;
38 }
39 
41 {
42  m_dwRate = rate;
43 }
44 
46 {
47  m_RemoveColouredFrame = RemoveColouredFrame;
48 }
49 
51 {
52  m_width = m_renderer->GetRenderWindow()->GetSize()[0]; // changed from glGetIntegerv( GL_VIEWPORT, viewport );
53  m_height = m_renderer->GetRenderWindow()->GetSize()[1]; // due to sometimes strange dimensions
54 
55  if(m_RemoveColouredFrame)
56  {
57  m_width -= 10; //remove colored boarders around renderwindows
58  m_height -= 10;
59  }
60 
61  m_width -= m_width % 4; // some video codecs have prerequisites to the image dimensions
62  m_height -= m_height % 4;
63 
64  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
65  m_currentFrame->origin = 1; // avoid building a video with bottom up
66 
67  /*
68  m_sFile = Name of the output video file.
69  CV_FOURCC = 4-character code of codec used to compress the frames. For example, CV_FOURCC('P','I','M','1') is MPEG-1 codec,
70  CV_FOURCC('M','J','P','G') is motion-jpeg codec etc.
71  CV_FOURCC('P','I','M','1') = MPEG-1 codec
72  CV_FOURCC('M','J','P','G') = motion-jpeg codec (does not work well)
73  CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
74  CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
75  CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
76  CV_FOURCC('U', '2', '6', '3') = H263 codec
77  CV_FOURCC('I', '2', '6', '3') = H263I codec
78  CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
79 
80  List of FOURCC codes is available at http://msdn2.microsoft.com/en-us/library/ms867195.aspx
81 
82  Under Win32 it is possible to pass -1 in order to choose compression
83  method and additional compression parameters from dialog.
84  m_dwRate = Framerate of the created video stream.
85  frame_size Size of video frames. InitGenerator
86  1 = If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames
87  (the flag is currently supported on Windows only).*/
88 
89  if(m_FourCCCodec != NULL)
90  {
91  #ifdef WIN32
92  m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),CV_FOURCC(m_FourCCCodec[0],m_FourCCCodec[1],m_FourCCCodec[2],
93  m_FourCCCodec[3]),m_dwRate,cvSize(m_width,m_height),1); //initializing video writer
94  #else
95  m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),CV_FOURCC(m_FourCCCodec[0],m_FourCCCodec[1],m_FourCCCodec[2],
96  m_FourCCCodec[3]),m_dwRate,cvSize(m_width,m_height)); //initializing video writer
97  #endif
98  }
99  else
100  {
101  #ifdef WIN32
102  m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),-1,m_dwRate,cvSize(m_width,m_height),1); //initializing video writer
103  #else
104  m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),CV_FOURCC('X','V','I','D'),m_dwRate,cvSize(m_width,m_height)); //initializing video writer
105  #endif
106  }
107 
108 
109  if(!m_aviWriter)
110  {
111  std::cout << "errors initializing video writer...correct video file path? on linux: ffmpeg must be included in OpenCV.";
112  return false;
113  }
114 
115  return true;
116 }
117 
118 
120 {
121  //cvSetImageData(m_currentFrame,data,m_width*3);
122  memcpy(m_currentFrame->imageData,data,m_width*m_height*3);
123  cvWriteFrame(m_aviWriter,m_currentFrame);
124  return true;
125 }
126 
127 
129 {
130  if (m_aviWriter)
131  {
132  cvReleaseVideoWriter(&m_aviWriter);
133  }
134  return true;
135 }
virtual bool InitGenerator() override
called directly before the first frame is added
virtual void SetFileName(const char *fileName) override
filename under which movie is saved
virtual bool AddFrame(void *data) override
used to add a frame
virtual bool TerminateGenerator() override
called after the last frame is added