29#include <opencv2/highgui/highgui.hpp>
30#include <opencv2/imgproc/imgproc.hpp>
31#include <opencv2/videoio/videoio.hpp>
65 std::string file = config.
check(
"movie",
Value(
""),
66 "if present, read from specified file rather than camera").asString();
71 m_cap.open(file.c_str());
72 if (!
m_cap.isOpened()) {
73 yCError(OPENCVGRABBER,
"Unable to open file '%s' for capture!", file.c_str());
78 m_loop = config.
check(
"loop",
"if present, loop movie");
84 config.
check(
"camera",
85 Value(cv::VideoCaptureAPIs::CAP_ANY),
86 "if present, read from camera identified by this index").asInt32();
88 m_cap.open(camera_idx);
89 if (!
m_cap.isOpened()) {
90 yCError(OPENCVGRABBER,
"Unable to open camera for capture!");
95 yCInfo(OPENCVGRABBER,
"Capturing from camera: %d",camera_idx);
98 if ( config.
check(
"framerate",
"if present, specifies desired camera device framerate") ) {
99 double m_fps = config.
check(
"framerate",
Value(-1)).asFloat64();
100 m_cap.set(cv::VideoCaptureProperties::CAP_PROP_FPS, m_fps);
104 if (config.
check(
"flip_x",
"if present, flip the image along the x-axis")) {
108 if (config.
check(
"flip_y",
"if present, flip the image along the y-axis")) {
112 if (config.
check(
"transpose",
"if present, rotate the image along of 90 degrees")) {
118 if (config.
check(
"width",
"if present, specifies desired image width")) {
121 m_cap.set(cv::VideoCaptureProperties::CAP_PROP_FRAME_WIDTH,
m_w);
124 m_w =
m_cap.get(cv::VideoCaptureProperties::CAP_PROP_FRAME_WIDTH);
127 if (config.
check(
"height",
"if present, specifies desired image height")) {
130 m_cap.set(cv::VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT,
m_h);
133 m_h =
m_cap.get(cv::VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT);
138 yCInfo(OPENCVGRABBER,
"OpenCVGrabber opened");
177 if (!
m_cap.isOpened()) {
183 if (
static_cast<int>(image.
width()) !=
m_w ||
static_cast<int>(image.
height()) !=
m_h) {
191 if (bgr.empty() &&
m_loop) {
208 cv::cvtColor(bgr, frame, cv::COLOR_BGR2RGB);
212 cv::transpose(frame, transposed);
213 image.
resize(transposed.cols, transposed.rows);
215 transposed.copyTo(frame);
219 cv::flip(frame, frame, -1);
221 cv::flip(frame, frame, 0);
223 cv::flip(frame, frame, 1);
227 yCDebug(OPENCVGRABBER,
"Received image of size %dx%d", frame.cols, frame.rows);
244 yCDebug(OPENCVGRABBER,
"Software scaling from %dx%d to %dx%d", frame.cols, frame.rows,
m_w,
m_h);
249 cv::resize(frame, resized, {
m_w,
m_h});
250 image.
resize(resized.cols, resized.rows);
252 resized.copyTo(frame);
255 yCTrace(OPENCVGRABBER,
"%d by %d image", frame.cols, frame.rows);
bool fromFile
Whether reading from file or camera.
yarp::os::Property m_config
Saved copy of the device configuration.
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
Read an image from the grabber.
bool close() override
Close a grabber.
cv::VideoCapture m_cap
OpenCV image capture object.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::os::Stamp m_laststamp
int m_w
Width of the images a grabber produces.
int m_h
Height of the images a grabber produces.
bool m_loop
Whether to loop or not.
Interface implemented by all device drivers.
A factory for creating driver objects of a particular type.
Global factory for devices.
A container for a device driver.
A class for storing options and configuration information.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
A base class for nested structures that can be searched.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
A single value (typically within a Bottle).
size_t width() const
Gets width of image in pixels.
size_t getRowSize() const
Size of the underlying image buffer rows.
unsigned char * getRawImage() const
Access to the internal image buffer.
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
void zero()
Set all pixels to 0.
size_t height() const
Gets height of image in pixels.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCDebug(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.