YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
OpenCVWriter.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025-2025 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include "OpenCVWriter.h"
7
8#include <string>
9#include <yarp/os/Property.h>
10#include <yarp/os/Searchable.h>
11#include <yarp/os/Value.h>
12#include <yarp/os/Log.h>
14#include <yarp/os/LogStream.h>
15#include <yarp/sig/Image.h>
16
17#include <opencv2/core/mat.hpp>
18#include <opencv2/imgproc.hpp>
19
20
23
26using yarp::os::Value;
27
29using yarp::sig::PixelRgb;
30
31using namespace yarp::os;
32using namespace yarp::sig;
33using namespace yarp::dev;
34
35
36namespace {
37YARP_LOG_COMPONENT(OPENCVWRITER, "yarp.device.openCVWriter")
38}
39
40
42{
43 if (!parseParams(config))
44 {
45 return false;
46 }
47
48 if (m_filename == "")
49 {
50 yCError(OPENCVWRITER) << "Invalid filename null";
51 return false;
52 }
53
54 if (m_framerate == 0)
55 {
56 yCError(OPENCVWRITER) << "Invalid framerate 0";
57 return false;
58 }
59
60 yCInfo(OPENCVWRITER, "OpenCVWriter opened");
61 return true;
62}
63
65{
67 {
69 }
70 yCInfo(OPENCVWRITER, "OpenCVWriter closed");
71 return true;
72}
73
75{
76 if (image.width() == 0 || image.height() == 0) {
77 yCError(OPENCVWRITER) << "Received empty frame";
78 return false;
79 }
80
81 if (m_width == 0) {
82 m_width = image.width();
83 }
84 if (m_height == 0) {
85 m_height = image.height();
86 }
87
88 if (!m_isInitialized)
89 {
90 m_writer.open(m_filename, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), m_framerate, cv::Size(m_width, m_height));
91 if (!m_writer.isOpened()) {
92 yCError(OPENCVWRITER, "Unable to open output file");
93 return false;
94 }
95 m_isInitialized = true;
96 }
97
98 if (m_width != image.width()) {
99 yCError(OPENCVWRITER) << "Received frame has a width different from the current configuration" << m_width << "<<" << image.width();
100 return false;
101 }
102 if (m_height != image.height()) {
103 yCError(OPENCVWRITER) << "Received frame has a height different from the current configuration: " << m_height << "<<" << image.height();
104 return false;
105 }
106
107 //process the frame
108 cv::Mat frame(m_height, m_width, CV_8UC3, (void*)image.getRawImage());
109 cv::cvtColor(frame, frame, cv::COLOR_RGB2BGR);
110 m_writer.write(frame);
111
112 return true;
113}
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool putImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
Write an image to the device.
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool m_isInitialized
cv::VideoWriter m_writer
Interface implemented by all device drivers.
Read a YARP-format image to a device.
A mini-server for performing network communication in the background.
void release(void *handle) override
Return control to YARP of an object previously taken control of with the acquire() method.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
A class for storing options and configuration information.
Definition Property.h:33
A base class for nested structures that can be searched.
Definition Searchable.h:31
A single value (typically within a Bottle).
Definition Value.h:43
Typed image class.
Definition Image.h:605
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.