src
video_writer.cpp
Go to the documentation of this file.
1
// *****************************************************************************
2
//
3
// Copyright (c) 2017, Southwest Research Institute® (SwRI®)
4
// All rights reserved.
5
//
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions are met:
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above copyright
11
// notice, this list of conditions and the following disclaimer in the
12
// documentation and/or other materials provided with the distribution.
13
// * Neither the name of Southwest Research Institute® (SwRI®) nor the
14
// names of its contributors may be used to endorse or promote products
15
// derived from this software without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
// ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
//
28
// *****************************************************************************
29
30
#include <
mapviz/video_writer.h
>
31
32
#include <
ros/ros.h
>
33
34
#include <opencv2/imgproc/imgproc.hpp>
35
36
namespace
mapviz
37
{
38
bool
VideoWriter::initializeWriter
(
const
std::string& directory,
int
width,
int
height)
39
{
40
QMutexLocker locker(&
video_mutex_
);
41
if
(!
video_writer_
)
42
{
43
width_
= width;
44
height_
= height;
45
46
ROS_INFO
(
"Initializing recording:\nWidth/Height/Filename: %d / %d / %s"
,
width_
,
height_
, directory.c_str() );
47
video_writer_
= boost::make_shared<cv::VideoWriter>(
48
directory,
49
cv::VideoWriter::fourcc(
'M'
,
'J'
,
'P'
,
'G'
),
50
30,
51
cv::Size(
width_
,
height_
));
52
53
if
(!
video_writer_
->isOpened())
54
{
55
ROS_ERROR
(
"Failed to open video file for writing."
);
56
stop
();
57
return
false
;
58
}
59
}
60
61
return
true
;
62
}
63
64
bool
VideoWriter::isRecording
()
65
{
66
return
video_writer_
.get() !=
NULL
;
67
}
68
69
void
VideoWriter::processFrame
(QImage frame)
70
{
71
try
72
{
73
ROS_DEBUG_THROTTLE
(1.0,
"VideoWriter::processFrame()"
);
74
{
75
QMutexLocker locker(&
video_mutex_
);
76
if
(!
video_writer_
)
77
{
78
ROS_WARN_THROTTLE
(1.0,
"Got frame, but video writer wasn't open."
);
79
return
;
80
}
81
}
82
83
cv::Mat image;
84
cv::Mat temp_image;
85
switch
(frame.format())
86
{
87
case
QImage::Format_ARGB32:
88
// The image received should have its format set to ARGB32, but it's
89
// actually BGRA. Need to convert it to BGR and flip it vertically
90
// before giving it to the cv::VideoWriter.
91
image = cv::Mat(frame.height(), frame.width(), CV_8UC4, frame.bits());
92
cv::cvtColor(image, temp_image, cv::COLOR_BGRA2BGR);
93
cv::flip(temp_image, image, 0);
94
break
;
95
default
:
96
ROS_WARN_THROTTLE
(1.0,
"Unexpected image format: %d"
, frame.format());
97
return
;
98
}
99
100
{
101
QMutexLocker locker(&
video_mutex_
);
102
if
(
video_writer_
)
103
{
104
ROS_DEBUG_THROTTLE
(1.0,
"Writing frame."
);
105
video_writer_
->write(image);
106
}
107
}
108
}
109
catch
(
const
std::exception& e)
110
{
111
ROS_ERROR_THROTTLE
(1.0,
"Error when processing video frame: %s"
, e.what());
112
}
113
}
114
115
void
VideoWriter::stop
()
116
{
117
ROS_INFO
(
"Stopping video recording."
);
118
QMutexLocker locker(&
video_mutex_
);
119
video_writer_
.reset();
120
}
121
}
ROS_ERROR_THROTTLE
#define ROS_ERROR_THROTTLE(period,...)
mapviz
Definition:
color_button.h:36
NULL
#define NULL
video_writer.h
mapviz::VideoWriter::stop
void stop()
Definition:
video_writer.cpp:115
ROS_WARN_THROTTLE
#define ROS_WARN_THROTTLE(period,...)
ros.h
mapviz::VideoWriter::isRecording
bool isRecording()
Definition:
video_writer.cpp:64
mapviz::VideoWriter::video_writer_
boost::shared_ptr< cv::VideoWriter > video_writer_
Definition:
video_writer.h:65
mapviz::VideoWriter::height_
int height_
Definition:
video_writer.h:62
mapviz::VideoWriter::video_mutex_
QMutex video_mutex_
Definition:
video_writer.h:64
mapviz::VideoWriter::width_
int width_
Definition:
video_writer.h:63
mapviz::VideoWriter::processFrame
void processFrame(QImage frame)
Definition:
video_writer.cpp:69
mapviz::VideoWriter::initializeWriter
bool initializeWriter(const std::string &directory, int width, int height)
Definition:
video_writer.cpp:38
ROS_ERROR
#define ROS_ERROR(...)
ROS_INFO
#define ROS_INFO(...)
ROS_DEBUG_THROTTLE
#define ROS_DEBUG_THROTTLE(period,...)
mapviz
Author(s): Marc Alban
autogenerated on Sun Sep 8 2024 02:27:09