MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkMovieGeneratorOpenCV.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14//#include <GL/gl.h>
15#include "mitkGL.h"
16#include <iostream>
17#include <opencv2/videoio/videoio_c.h>
18
19
21{
22 m_initialized = false;
23 m_aviWriter = nullptr;
24 m_dwRate = 20;
25
26 m_FourCCCodec = nullptr;
27 m_RemoveColouredFrame = true;
28}
29
30
31void mitk::MovieGeneratorOpenCV::SetFileName( const char *fileName )
32{
33
34 m_sFile = fileName;
35}
36
38{
39 m_dwRate = static_cast<int>(rate);
40}
41
43{
44 m_RemoveColouredFrame = RemoveColouredFrame;
45}
46
48{
49 m_width = m_renderer->GetRenderWindow()->GetSize()[0]; // changed from glGetIntegerv( GL_VIEWPORT, viewport );
50 m_height = m_renderer->GetRenderWindow()->GetSize()[1]; // due to sometimes strange dimensions
51
52 if(m_RemoveColouredFrame)
53 {
54 m_width -= 10; //remove colored boarders around renderwindows
55 m_height -= 10;
56 }
57
58 m_width -= m_width % 4; // some video codecs have prerequisites to the image dimensions
59 m_height -= m_height % 4;
60
61 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
62 m_currentFrame->origin = 1; // avoid building a video with bottom up
63
64 /*
65 m_sFile = Name of the output video file.
66 CV_FOURCC = 4-character code of codec used to compress the frames. For example, CV_FOURCC('P','I','M','1') is MPEG-1 codec,
67 CV_FOURCC('M','J','P','G') is motion-jpeg codec etc.
68 CV_FOURCC('P','I','M','1') = MPEG-1 codec
69 CV_FOURCC('M','J','P','G') = motion-jpeg codec (does not work well)
70 CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
71 CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
72 CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
73 CV_FOURCC('U', '2', '6', '3') = H263 codec
74 CV_FOURCC('I', '2', '6', '3') = H263I codec
75 CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
76
77 Under Win32 it is possible to pass -1 in order to choose compression
78 method and additional compression parameters from dialog.
79 m_dwRate = Framerate of the created video stream.
80 frame_size Size of video frames. InitGenerator
81 1 = If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames
82 (the flag is currently supported on Windows only).*/
83
84 if(m_FourCCCodec != nullptr)
85 {
86 #ifdef WIN32
87 m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),CV_FOURCC(m_FourCCCodec[0],m_FourCCCodec[1],m_FourCCCodec[2],
88 m_FourCCCodec[3]),m_dwRate,cvSize(m_width,m_height),1); //initializing video writer
89 #else
90 m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),CV_FOURCC(m_FourCCCodec[0],m_FourCCCodec[1],m_FourCCCodec[2],
91 m_FourCCCodec[3]),m_dwRate,cvSize(m_width,m_height)); //initializing video writer
92 #endif
93 }
94 else
95 {
96 #ifdef WIN32
97 m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),-1,m_dwRate,cvSize(m_width,m_height),1); //initializing video writer
98 #else
99 m_aviWriter = cvCreateVideoWriter(m_sFile.c_str(),CV_FOURCC('X','V','I','D'),m_dwRate,cvSize(m_width,m_height)); //initializing video writer
100 #endif
101 }
102
103
104 if(!m_aviWriter)
105 {
106 std::cout << "errors initializing video writer...correct video file path? on linux: ffmpeg must be included in OpenCV.";
107 return false;
108 }
109
110 return true;
111}
112
113
115{
116 //cvSetImageData(m_currentFrame,data,m_width*3);
117 memcpy(m_currentFrame->imageData,data,m_width*m_height*3);
118 cvWriteFrame(m_aviWriter,m_currentFrame);
119 return true;
120}
121
122
124{
125 if (m_aviWriter)
126 {
127 cvReleaseVideoWriter(&m_aviWriter);
128 }
129 return true;
130}
void SetFileName(const char *fileName) override
bool AddFrame(void *data) override
used to add a frame
void SetFrameRate(unsigned int rate) override
bool TerminateGenerator() override
called after the last frame is added
bool InitGenerator() override
called directly before the first frame is added